httpserve
  • Overview
  • Repository
  • Tickets
  • Statistics

Repository

Load conf from a config file, improve fuse command line options

Parent commits : 4a931d8275f43cb04e66f3630105d3bb941bf6c0,
Children commits : 4c1093f15029d18f15ed03bc7738091b19b53c59,

By Laurent Defert on 2013-06-22 15:26:35
Load conf from a config file, improve fuse command line options

Browse content
Difference with parent commit 4a931d8275f43cb04e66f3630105d3bb941bf6c0
Files modified:
httpget.py
--- 
+++ 
@@ -465,50 +465,46 @@
         print
         return self.hdd_file
 
-if __name__ == '__main__':
-    parser = argparse.ArgumentParser(description='Serve a file over http.')
-    parser.add_argument('--config', '-c', metavar='configuration file', type=str, nargs=1,
-                       help='Configuration file', default=None)
+def add_arguments(parser):
     parser.add_argument('--urls_list', '-H', metavar='http://host1/,http://host2/', type=str, nargs=1,
-                       help='Configuration file', default='127.0.0.1')
+                       help='Urls of host to download from', default='127.0.0.1')
     parser.add_argument('--nconn', '-n', metavar='n', type=int, nargs=1,
                        help='Concurrent connections', default=5)
     parser.add_argument('--bw_limit', '-l', metavar='n', type=int, nargs=1,
                        help='Max bandwidth limitation (kB/s)', default=None)
     parser.add_argument('url', type=str, nargs='+',
                        help='Urls to download')
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Serve a file over http.')
+    default_conf = os.path.join(os.environ['HOME'], DEFAULT_CONF_FILE)
+    parser.add_argument('--config', '-c', metavar='configuration file', type=str, nargs=1,
+                       help='Configuration file', default=default_conf)
+    add_arguments(parser)
     args = parser.parse_args()
 
-    # Load config from configuration file
-    if args.config is None:
-        try:
-            args.config = os.path.join(os.environ['HOME'], DEFAULT_CONF_FILE)
-        except KeyError:
-            pass
-
-    if not args.config is None:
-        if not os.path.exists(args.config):
-            # Write default configuration file
-            print 'Creating default configuration in file %s' % args.config
-            fp = open(args.config, 'w')
-            fp.write(DEFAULT_CONF)
-            fp.close()
-        conf = ConfigParser.ConfigParser()
-        conf.read(args.config)
-
-        for option, value in conf.items(CONFIG_SECTION):
-            if value.lower() in ['false', 'true', '0', '1']:
-                if value.lower() in ['false', '0']:
-                    value = False
-                else:
-                    value = True
-            if hasattr(args, option):  # Command line arguments have priority over config files ones
-                kw = {option: value}
-                parser.set_defaults(**kw)
+    if not os.path.exists(args.config):
+        # Write default configuration file
+        print 'Creating default configuration in file %s' % args.config
+        fp = open(args.config, 'w')
+        fp.write(DEFAULT_CONF)
+        fp.close()
+    conf = ConfigParser.ConfigParser()
+    conf.read(args.config)
+
+    for option, value in conf.items(CONFIG_SECTION):
+        if value.lower() in ['false', 'true', '0', '1']:
+            if value.lower() in ['false', '0']:
+                value = False
             else:
-                print >> sys.stderr, 'Invalid option "%s" in configuration file %s' % (option, args.config)
-                sys.exit(1)
-        args = parser.parse_args()
+                value = True
+        if hasattr(args, option):  # Command line arguments have priority over config files ones
+            kw = {option: value}
+            parser.set_defaults(**kw)
+        else:
+            print >> sys.stderr, 'Invalid option "%s" in configuration file %s' % (option, args.config)
+            sys.exit(1)
+    args = parser.parse_args()
 
     for single_opt in ['config', 'nconn', 'bw_limit', 'urls_list']:
         val = getattr(args, single_opt)

httpmount.py
--- 
+++ 
@@ -16,7 +16,7 @@
 
 import fuse
 
-from httpget import CONFIG_SECTION, DEFAULT_CONF, DEFAULT_CONF_FILE, Download
+from httpget import CONFIG_SECTION as GET_SECTION, DEFAULT_CONF as GET_CONF, Download, add_arguments
 
 fuse.fuse_python_api = (0, 2)
 
@@ -24,6 +24,18 @@
 
 FORMAT='[%(process)d] %(message)s'
 logging.basicConfig(level=logging.DEBUG, format=FORMAT)
+
+DEFAULT_CONF_FILE = '.httpmount'
+DEFAULT_CONF = '''# httpmount default configuration
+[fs]
+# Urls of root directories
+root=http://127.0.0.1:8080/
+
+store
+
+''' + GET_CONF
+
+CONFIG_SECTIONS = [GET_SECTION, 'fs']
 
 def dirFromList(lst):
     lst = ['.', '..'] + lst
@@ -62,7 +74,7 @@
 
     def run(self):
         try:
-            filename = self.conf.localstore + self.filename
+            filename = self.conf.store + self.filename
             print "saving to", filename
             self.download(filename)
         except Exception, e:
@@ -172,7 +184,7 @@
         if not self.downloads[0].is_alive() and not self.downloads[0].is_finished():
             # If the file exists on the hdd, read it otherwitse download it
             if self.st_size != 0:
-                filename = self.httpfs.localstore + self.path
+                filename = self.httpfs.store + self.path
                 if os.path.isfile(filename):
                     if os.stat(filename).st_size == self.st_size:
                         fp = open(filename)
@@ -219,23 +231,23 @@
 class HTTPFS(fuse.Fuse):
     def fsinit(self):
         try:
-            if not hasattr(self, 'localstore'):
-                self.localstore = 'store/'
-            elif not self.localstore.endswith('/'):
-                self.localstore += '/'
-
-            if not hasattr(self, 'url'):
-                self.url = 'http://127.0.0.1:8080/'
-                print "url:", self.url
-
-            for (path, dirs, files) in os.walk(self.localstore):
-                print path
-                print dirs
-                print files
+            if not hasattr(self, 'store'):
+                self.store = 'store/'
+            elif not self.store.endswith('/'):
+                self.store += '/'
+
+            if not hasattr(self, 'root'):
+                print "no attr root", 10*'!'
+                self.root = 'https://127.0.0.1:8080/'
+
+            #for (path, dirs, files) in os.walk(self.store):
+            #    print path
+            #    print dirs
+            #    print files
 
             self.fp_cache = []
             self.conf_tree = {
-                '/': self.url.split(' '),
+                '/': self.root.split(' '),
             }
             self.tree = HTTPTree()
             print "fsinit done"
@@ -412,6 +424,28 @@
     def flush(self, path):
         return 0
 
+    # Helper function to load command line to match the API
+    # argparse.ArgumentParser as it's used in httpget.py
+    def fuse_add_argument(self, *opts, **kw):
+        if not opts[0].startswith('-'):
+            return
+
+        # Find the command line flag that contains more than character
+        for opt in opts:
+            opt = opt.strip('-')
+            if len(opt) > 1:
+                break
+        else:
+            raise Exception('Invalid option %s' % ','.join(opts))
+
+        kw.pop('type', None)
+        kw.pop('nargs', None)
+
+        print "fuse_add_argument:", opt, kw
+        self.parser.add_option(mountopt=opt, **kw)
+        if 'default' in kw:
+            setattr(self, opt, kw['default'])
+
 if __name__ == '__main__':
     usage = """
 httpmount client.
@@ -423,38 +457,36 @@
                  dash_s_do='setsingle')
     fs.flags = 0
     fs.multithreaded = 1
-
-    fs.parser.add_option(mountopt="config", metavar="PATH", default='local/',
-                             help="store files in PATH [default: %default]")
-    fs.parser.add_option(mountopt="url", metavar="URL", default='http://127.0.0.1:8080/',
-                             help="connect to url URL [default: %default]")
-    fs.parser.add_option(mountopt="localstore", metavar="PATH", default='local/',
-                             help="store files in PATH [default: %default]")
-
-    rgs = fs.parse(values=fs, errex=1)
-
-    if not hasattr(fs, 'config'):
-        # Load config from configuration file
-        try:
-            fs.config = os.path.join(os.environ['HOME'], DEFAULT_CONF_FILE)
-        except KeyError:
-            pass
-
-        if not os.path.exists(fs.config):
-            # Write default configuration file
-            print 'Creating default configuration in file %s' % fs.config
-            fp = open(fs.config, 'w')
-            fp.write(DEFAULT_CONF)
-            fp.close()
-        conf = ConfigParser.ConfigParser()
-        conf.read(fs.config)
-
-        for option, value in conf.items(CONFIG_SECTION):
+    fs.parser.add_argument = fs.fuse_add_argument
+    default_conf = os.path.join(os.environ['HOME'], DEFAULT_CONF_FILE)
+    fs.fuse_add_argument('--config', metavar='configuration file', type=str, nargs=1,
+                       help='Configuration file', default=default_conf)
+    fs.fuse_add_argument("--root", metavar="URL", default='http://127.0.0.1:8080/',
+                             help="Urls of root directories [default: %default]")
+    fs.fuse_add_argument("--store", metavar="PATH", default='store/',
+                             help="Store files in PATH [default: %default]")
+    add_arguments(fs.parser)
+
+    # Load config from configuration file
+    if not os.path.exists(fs.config):
+        # Write default configuration file
+        print 'Creating default configuration in file %s' % fs.config
+        fp = open(fs.config, 'w')
+        fp.write(DEFAULT_CONF)
+        fp.close()
+    conf = ConfigParser.ConfigParser()
+    conf.read(fs.config)
+
+    for section in CONFIG_SECTIONS:
+        print "in section", section
+        for option, value in conf.items(section):
             if value.lower() in ['false', 'true', '0', '1']:
                 if value.lower() in ['false', '0']:
                     value = False
                 else:
                     value = True
             setattr(fs, option, value)
-
+            print "option", option, "=", value
+
+    fs.parse(values=fs, errex=1)
     fs.main()