[tor-commits] [bridgedb/master] Move subCommand handling code to separate function in bridgedb.Main.

isis at torproject.org isis at torproject.org
Sun Jan 12 06:06:31 UTC 2014


commit 95988603be4eb7202235382371071710760587a2
Author: Isis Lovecruft <isis at torproject.org>
Date:   Tue Nov 5 08:22:26 2013 +0000

    Move subCommand handling code to separate function in bridgedb.Main.
    
     * MOVE code for running options.subCommands and dumping bridge assignments to
       file to a new function, bridgedb.Main.runSubcommand().
---
 lib/bridgedb/Main.py |   49 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 42 insertions(+), 7 deletions(-)

diff --git a/lib/bridgedb/Main.py b/lib/bridgedb/Main.py
index 750ad1e..924b10c 100644
--- a/lib/bridgedb/Main.py
+++ b/lib/bridgedb/Main.py
@@ -265,7 +265,13 @@ def startup(cfg, options):
     # Set up logging.
     configureLogging(cfg)
 
-    # Write the pidfile.
+    if options['dump-bridges'] or (options.subCommand is not None):
+        runSubcommand(options, cfg)
+
+    # Write the pidfile only after any options.subCommands are run (because
+    # these exit when they are finished). Otherwise, if there is a subcommand,
+    # the real PIDFILE would get overwritten with the PID of the temporary
+    # bridgedb process running the subcommand.
     if cfg.PIDFILE:
         f = open(cfg.PIDFILE, 'w')
         f.write("%s\n" % os.getpid())
@@ -450,6 +456,40 @@ def startup(cfg, options):
             os.unlink(cfg.PIDFILE)
         sys.exit()
 
+def runSubcommand(options, config):
+    """Run a subcommand from the 'Commands' section of the bridgedb help menu.
+
+    :type options: :class:`bridgedb.opt.MainOptions`
+    :param options: A pre-parsed options class containing any arguments and
+        options given in the commandline we were called with.
+    :type config: :class:`bridgedb.Main.Conf`
+    :param config: The current configuration.
+    :raises: :exc:`SystemExit` when all subCommands and subOptions have
+        finished running.
+    """
+    # Make sure that the runner module is only imported after logging is set
+    # up, otherwise we run into the same logging configuration problem as
+    # mentioned above with the EmailServer and HTTPServer.
+    from bridgedb import runner
+
+    if options('dump-bridges'):
+        bucketManager = Bucket.BucketManager(config)
+        bucketManager.assignBridgesToBuckets()
+        bucketManager.dumpBridges()
+
+    if options.subCommand is not None:
+        logging.debug("Running BridgeDB command: '%s'" % options.subCommand)
+
+        if 'descriptors' in options.subOptions:
+            runner.generateDescriptors(options)
+
+        if options.subCommand == 'test':
+            if options.subOptions['trial']:
+                runner.runTrial(options.subOptions)
+            if options.subOptions['unittests']:
+                runner.runTests(options.subOptions)
+        raise SystemExit("Subcommand '%s' finished." % options.subCommand)
+
 def run(options):
     """This is the main entry point into BridgeDB.
 
@@ -484,9 +524,4 @@ def run(options):
     # Store the rundir in case it needs to be used again later:
     cfg.RUNDIR = rundir
 
-    if options['dump-bridges']:
-        bucketManager = Bucket.BucketManager(config)
-        bucketManager.assignBridgesToBuckets()
-        bucketManager.dumpBridges()
-    else:
-        startup(config, options)
+    startup(cfg, options)





More information about the tor-commits mailing list