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

[tor-commits] [ooni-probe/master] Refactor NetTestLoader and add checkOptions, and add Tor support



commit 6254642808851599890c7219c1025a096b486b5c
Author: aagbsn <aagbsn@xxxxxxxx>
Date:   Wed Jan 16 22:36:58 2013 +0000

    Refactor NetTestLoader and add checkOptions, and add Tor support
---
 ooni/director.py |   25 ++++++++++++++++-----
 ooni/nettest.py  |   62 +++++++++++++++++++++++++++---------------------------
 ooni/oonicli.py  |   28 ++++++++++++++----------
 3 files changed, 66 insertions(+), 49 deletions(-)

diff --git a/ooni/director.py b/ooni/director.py
index 9a20491..2dc4e36 100644
--- a/ooni/director.py
+++ b/ooni/director.py
@@ -78,6 +78,22 @@ class Director(object):
 
         self.failures = []
 
+        self.torControlProtocol = None
+
+    def start(self):
+        if config.privacy.includepcap:
+            log.msg("Starting")
+            if not config.reports.pcap:
+                config.generatePcapFilename()
+            self.startSniffing()
+
+        if config.advanced.start_tor:
+            log.msg("Starting Tor...")
+            d = self.startTor()
+        else:
+            d = defer.succeed(None)
+        return d
+
     @property
     def measurementSuccessRatio(self):
         if self.totalMeasurements == 0:
@@ -154,7 +170,7 @@ class Director(object):
     def netTestDone(self, result, net_test):
         self.activeNetTests.remove(net_test)
 
-    def startNetTest(self, net_test_loader, options):
+    def startNetTest(self, _, net_test_loader):
         """
         Create the Report for the NetTest and start the report NetTest.
 
@@ -162,14 +178,11 @@ class Director(object):
             net_test_loader:
                 an instance of :class:ooni.nettest.NetTestLoader
 
-            options:
-                is a dict containing the options to be passed to the chosen net
-                test.
+            _: #XXX very dirty hack
         """
         report = Report(self.reporters, self.reportEntryManager)
 
-        net_test = NetTest(net_test_loader, options, report)
-        net_test.setUpNetTestCases()
+        net_test = NetTest(net_test_loader, report)
         net_test.director = self
 
         self.measurementManager.schedule(net_test.generateMeasurements())
diff --git a/ooni/nettest.py b/ooni/nettest.py
index 4aa013c..464d91a 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -20,7 +20,8 @@ class NoTestCasesFound(Exception):
 class NetTestLoader(object):
     method_prefix = 'test'
 
-    def __init__(self, net_test_file):
+    def __init__(self, net_test_file, net_test_args):
+        self.netTestArgs = net_test_args
         self.testCases = self.loadNetTest(net_test_file)
 
     @property
@@ -145,9 +146,33 @@ class NetTestLoader(object):
         test_class, _ = test_cases[0]
         self.testVersion = test_class.version
         self.testName = os.path.basename(net_test_file).strip('.py')
-
         return test_cases
 
+    def checkOptions(self):
+        """
+        Call processTest and processOptions methods of each NetTestCase
+        """
+        test_classes = set([])
+        for test_class, test_method in self.testCases:
+            test_classes.add(test_class)
+
+        for klass in test_classes:
+            options = self.usageOptions()
+            options.parseOptions(self.netTestArgs)
+            if options:
+                klass.localOptions = options
+
+            test_instance = klass()
+            if test_instance.requiresRoot:
+                checkForRoot()
+            test_instance._checkRequiredOptions()
+            test_instance._checkValidOptions()
+
+            inputs = test_instance.getInputProcessor()
+            if not inputs:
+                inputs = [None]
+            klass.inputs = inputs
+
     def _loadNetTestFromFileObject(self, net_test_string):
         """
         Load NetTest from a string
@@ -231,15 +256,12 @@ class NetTestState(object):
 class NetTest(object):
     director = None
 
-    def __init__(self, net_test_loader, options, report):
+    def __init__(self, net_test_loader, report):
         """
-        net_test_file:
-            is a file object containing the test to be run.
-
-        options:
-            is a dict containing the options to be passed to the net test.
+        net_test_loader:
+             an instance of :class:ooni.nettest.NetTestLoader containing
+             the test to be run.
         """
-        self.options = options
         self.report = report
         self.testCases = net_test_loader.testCases
 
@@ -322,28 +344,6 @@ class NetTest(object):
 
         self.state.allTasksScheduled()
 
-    def setUpNetTestCases(self):
-        """
-        Call processTest and processOptions methods of each NetTestCase
-        """
-        test_classes = set([])
-        for test_class, test_method in self.testCases:
-            test_classes.add(test_class)
-
-        for klass in test_classes:
-            klass.localOptions = self.options
-
-            test_instance = klass()
-            if test_instance.requiresRoot:
-                checkForRoot()
-            test_instance._checkRequiredOptions()
-            test_instance._checkValidOptions()
-
-            inputs = test_instance.getInputProcessor()
-            if not inputs:
-                inputs = [None]
-            klass.inputs = inputs
-
 class NetTestCase(object):
     """
     This is the base of the OONI nettest universe. When you write a nettest
diff --git a/ooni/oonicli.py b/ooni/oonicli.py
index e307d35..78389f4 100644
--- a/ooni/oonicli.py
+++ b/ooni/oonicli.py
@@ -174,12 +174,18 @@ def runWithDirector():
 
     net_test_args = global_options.pop('subargs')
     net_test_file = global_options['test']
+    net_test_loader = NetTestLoader(net_test_file, net_test_args)
 
-    net_test_loader = NetTestLoader(net_test_file)
-    options = net_test_loader.usageOptions()
-    options.parseOptions(net_test_args)
-
-    net_test_options = dict(options)
+    try:
+        net_test_loader.checkOptions()
+    except MissingRequiredOption, option_name:
+        log.err('Missing required option: "%s"' % option_name)
+        print net_test_loader.usageOptions().getUsage()
+        sys.exit(2)
+    except usage.UsageError, e:
+        log.err(e)
+        print net_test_loader.usageOptions().getUsage()
+        sys.exit(2)
 
     # reporters = [YAMLReporter, OONIBReporter]
 
@@ -198,13 +204,11 @@ def runWithDirector():
             sys.exit(1)
 
     director = Director(reporters)
-    try:
-        d = director.startNetTest(net_test_loader, net_test_options)
-        d.addBoth(shutdown)
-        reactor.run()
-    except MissingRequiredOption, option_name:
-        log.err('Missing required option: "%s"' % option_name)
-        print options.getUsage()
+    d = director.start()
+
+    d.addCallback(director.startNetTest, net_test_loader)
+    d.addBoth(shutdown)
+    reactor.run()
 
 def run():
     """



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