[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [bridgedb/master] Refactor bridgedb.Main.run() and bridgedb.Main._reloadFn().



commit afa265eb06a97831200389b18225f410b0927e55
Author: Isis Lovecruft <isis@xxxxxxxxxxxxxx>
Date:   Fri Oct 18 22:35:29 2013 +0000

    Refactor bridgedb.Main.run() and bridgedb.Main._reloadFn().
    
    The main entry point and server reload function are refactored to recieve the
    parsed options from the commandline script, scripts/bridgedb.
    
    It is necessary to pass *args to `_reloadFn()` (as well as its original
    stubbed lambda function), because the arguments are technically unknown to the
    scope of the `reload()` function embedded in `bridgedb.Main.startup()` when it
    is evaluated at compile time (they are not known until later when
    `bridgedb.Main.run()` is called via scripts/bridgedb).
---
 lib/bridgedb/Main.py |   60 ++++++++++++++++++++++++++------------------------
 1 file changed, 31 insertions(+), 29 deletions(-)

diff --git a/lib/bridgedb/Main.py b/lib/bridgedb/Main.py
index fd9fbda..76f0832 100644
--- a/lib/bridgedb/Main.py
+++ b/lib/bridgedb/Main.py
@@ -16,14 +16,16 @@ import gettext
 
 from twisted.internet import reactor
 
+from bridgedb import options
+
 import bridgedb.Bridges as Bridges
 import bridgedb.Dist as Dist
 import bridgedb.Time as Time
 import bridgedb.Storage
-import bridgedb.Opt as Opt
 import bridgedb.Bucket as Bucket
 import bridgedb.Util as Util
 
+
 class Conf:
     """A configuration object.  Holds unvalidated attributes.
     """
@@ -278,10 +280,11 @@ def loadProxyList(cfg):
         f.close()
     return ipset
 
-_reloadFn = lambda: True
+_reloadFn = lambda x: True
 def _handleSIGHUP(*args):
     """Called when we receive a SIGHUP; invokes _reloadFn."""
-    reactor.callLater(0, _reloadFn)
+    reactor.callLater(0, _reloadFn, *args)
+
 
 class ProxyCategory:
     def __init__(self):
@@ -291,7 +294,8 @@ class ProxyCategory:
     def replaceProxyList(self, ipset):
         self.ipset = ipset
 
-def startup(cfg):
+
+def startup(cfg, options):
     """Parse bridges,
     """
     # Expand any ~ characters in paths in the configuration.
@@ -388,14 +392,13 @@ def startup(cfg):
         splitter.addPseudoRing(p)
 
     # Make the parse-bridges function get re-called on SIGHUP.
-    def reload():
+    def reload(*args):
         logging.info("Caught SIGHUP")
 
-        # re open config file
-        options, arguments = Opt.parseOpts()
+        # reparse the config file
         configuration = {}
-        if options.configfile:
-            execfile(options.configfile, configuration)
+        if options['config']:
+            execfile(options['config'], configuration)
             cfg = Conf(**configuration)
             # update loglevel on (re)load
             level = getattr(cfg, 'LOGLEVEL', 'WARNING')
@@ -438,7 +441,7 @@ def startup(cfg):
     signal.signal(signal.SIGHUP, _handleSIGHUP)
 
     # And actually load it to start.
-    reload()
+    reload(options)
 
     # Configure HTTP and/or HTTPS servers.
     if cfg.HTTPS_DIST and cfg.HTTPS_SHARE:
@@ -457,34 +460,33 @@ def startup(cfg):
         if cfg.PIDFILE:
             os.unlink(cfg.PIDFILE)
 
-def run():
-    """Parse the command line to determine where the configuration is.
-       Parse the configuration, and start the servers.
+def run(options):
+    """This is the main entry point into BridgeDB.
+
+    Given the parsed commandline options, this function handles locating the
+    configuration file, loading and parsing it, and then either
+    starting/reloading the servers or dumping bridge assignments to files.
+
+    :type options: :class:`bridgedb.opt.MainOptions`
+    :param options: A pre-parsed options class.
     """
-    options, arguments = Opt.parseOpts()
     configuration = {}
 
-    if options.testing:
-        configuration = CONFIG
-    elif not options.configfile:
-        print "Syntax: %s -c CONFIGFILE" % sys.argv[0]
+    if not options['config']:
+        options.getUsage()
         sys.exit(1)
-    else:
-        configFile = options.configfile
-        execfile(configFile, configuration)
-        C = Conf(**configuration)
-        configuration = C
+
+    configFile = options['config']
+    execfile(configFile, configuration)
+    C = Conf(**configuration)
+    configuration = C
 
     # Change to the directory where we're supposed to run.
     if configuration.RUN_IN_DIR:
         os.chdir(os.path.expanduser(configuration.RUN_IN_DIR))
-
-    if options.dumpbridges:
+    if options['dump-bridges']:
         bucketManager = Bucket.BucketManager(configuration)
         bucketManager.assignBridgesToBuckets()
         bucketManager.dumpBridges()
     else:
-        startup(configuration)
-
-if __name__ == '__main__':
-    run()
+        startup(configuration, options)



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits