[tor-commits] [ooni-probe/master] Finish a working ooniclient implementation

art at torproject.org art at torproject.org
Thu May 31 03:01:42 UTC 2012


commit 3a31b613c62706d8eb9617c143981a5b1b95df0c
Author: Arturo Filastò <hellais at torproject.org>
Date:   Wed May 2 21:32:37 2012 +0200

    Finish a working ooniclient implementation
---
 oonicli.py        |   95 ++++++++++++++++++++++++++++++++++++++++++++++++----
 plugins/skel.py   |   16 ++++-----
 plugoo/tests.py   |   10 +----
 plugoo/work.py    |   16 ++++++++-
 unittest/tests.py |    4 +-
 5 files changed, 112 insertions(+), 29 deletions(-)

diff --git a/oonicli.py b/oonicli.py
index d77ad0f..0a9c1cc 100755
--- a/oonicli.py
+++ b/oonicli.py
@@ -8,18 +8,27 @@
 #    :license: see LICENSE for more details.
 #
 
-from plugoo import tests
+import sys
+from plugoo import tests, work
 
 from twisted.python import usage
-
 from twisted.plugin import getPlugins
+from twisted.internet import reactor
 
 from zope.interface.exceptions import BrokenImplementation
 from zope.interface.exceptions import BrokenMethodImplementation
 from zope.interface.verify import verifyObject
+from pprint import pprint
+from logo import getlogo
 import plugins
 
+__version__ = "0.0.1-prealpha"
+
 def retrieve_plugoo():
+    """
+    Get all the plugins that implement the ITest interface and get the data
+    associated to them into a dict.
+    """
     interface = tests.ITest
     d = {}
     error = False
@@ -28,11 +37,11 @@ def retrieve_plugoo():
             verifyObject(interface, p)
             d[p.shortName] = p
         except BrokenImplementation, bi:
-            print "Plugin Broke"
+            print "Plugin Broken"
             print bi
             error = True
         except BrokenMethodImplementation, bmi:
-            print "Plugin Broke"
+            print "Plugin Broken"
             error = True
     if error != False:
         print "Plugin Loaded!"
@@ -40,18 +49,88 @@ def retrieve_plugoo():
 
 plugoo = retrieve_plugoo()
 
+class StupidAsset(object):
+    def __init__(self):
+        self.idx = 0
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        if self.idx > 30:
+            raise StopIteration
+        self.idx += 1
+        return self.idx
+
+def runTest(test, options):
+    pprint(test)
+    wgen = work.WorkGenerator(StupidAsset, plugoo[test].__class__,
+            dict(options),
+            start=options['resume'])
+
+    worker = work.Worker()
+    for x in wgen:
+        print "------"
+        print "Work unit"
+        print "------"
+        print x.serialize()
+        worker.push(x)
+        print "------"
+
+    reactor.run()
+
 class Options(usage.Options):
     tests = plugoo.keys()
     subCommands = []
     for test in tests:
-        subCommands.append([test, None, plugoo[test].arguments, "Run the %s test" % test])
+        subCommands.append([test, None, plugoo[test].options, "Run the %s test" % test])
+
+    optFlags = [
+        ['local', 'l', "If the test should be run locally (without having oonid running)"],
+        ['status', 'x', 'Show current state'],
+        ['restart', 'r', 'Restart OONI']
+    ]
 
     optParameters = [
-        ['status', 's', 0, 'Show current state'],
-        ['restart', 'r', None, 'Restart OONI'],
-        ['node', 'n', 'localhost:31415', 'Select target node']
+        ['node', 'n', 'localhost:31415', 'Select target node'],
+        ['ooninet', 'o', 'localhost:4242', "Select OONI-net address for reporting"],
+        ['password', 'p', 'opennetwork', "Specify the password for authentication"],
     ]
 
+    def opt_version(self):
+        """
+        Display OONI version and exit.
+        """
+        print "OONI version:", __version__
+        sys.exit(0)
+
+    def __str__(self):
+        """
+        Hack to get the sweet ascii art into the help output and replace the
+        strings "Commands" with "Tests".
+        """
+        return getlogo() + '\n' + self.getSynopsis() + '\n' + \
+               self.getUsage(width=None).replace("Commands:", "Tests:")
+
 config = Options()
 config.parseOptions()
 
+if config['status']:
+    print "oonid is not running."
+    sys.exit(0)
+
+if config['restart']:
+    print "Restarting oonid."
+    sys.exit(0)
+
+if not config.subCommand:
+    print "Error! No Test Specified."
+    config.opt_help()
+    sys.exit(1)
+
+if config['local']:
+    runTest(config.subCommand, config.subOptions)
+
+else:
+    print "The test will be run on the node %s" % config['node']
+
diff --git a/plugins/skel.py b/plugins/skel.py
index d27932a..41420b1 100644
--- a/plugins/skel.py
+++ b/plugins/skel.py
@@ -1,22 +1,20 @@
 from zope.interface import implements
 from twisted.python import usage
 from twisted.plugin import IPlugin
-from plugoo.tests import ITest
+from plugoo.tests import ITest, TwistedTest
 
 class SkelArgs(usage.Options):
     optParameters = [['assets', 'a', None, 'Asset file'],
-                     ['resume', 'r', None, 'Resume at this index']]
+                     ['resume', 'r', 0, 'Resume at this index']]
 
-class SkelTest(object):
+class SkelTest(TwistedTest):
     implements(IPlugin, ITest)
 
     shortName = "skeleton"
     description = "Skeleton plugin"
     requirements = None
-    arguments = SkelArgs
-
-    def startTest():
-        pass
-
-skel = SkelTest()
+    options = SkelArgs
 
+# We need to instantiate it otherwise getPlugins does not detect it
+# XXX Find a way to load plugins without instantiating them.
+skel = SkelTest(None, None)
diff --git a/plugoo/tests.py b/plugoo/tests.py
index 4c3e1a1..41363ee 100644
--- a/plugoo/tests.py
+++ b/plugoo/tests.py
@@ -126,7 +126,7 @@ class ITest(Interface):
 
     #deferred = Attribute("""This will be fired on test completion""")
     #node = Attribute("""This represents the node that will run the test""")
-    arguments = Attribute("""These are the arguments to be passed to the test for it's execution""")
+    options = Attribute("""These are the arguments to be passed to the test for it's execution""")
 
     def startTest():
         """
@@ -164,9 +164,8 @@ class HTTPRequestTest(object):
         pass
 
 class TwistedTest(object):
-    def __init__(self, asset, node, arguments, ooninet=None):
+    def __init__(self, asset, arguments, ooninet=None):
         self.asset = asset
-        self.node = node
         self.arguments = arguments
         self.start_time = datetime.now()
         #self.ooninet = ooninet
@@ -188,11 +187,6 @@ class TwistedTest(object):
         reactor.callLater(2.0, self.finished, result)
         return self.d
 
-
-class StupidTest(TwistedTest):
-    def __repr__(self):
-        return "<StupidTest %s %s %s>" % (self.arguments, self.asset, self.node)
-
 class TwistedTestFactory(object):
 
     test = StupidTest
diff --git a/plugoo/work.py b/plugoo/work.py
index 53cb18c..809e9bd 100644
--- a/plugoo/work.py
+++ b/plugoo/work.py
@@ -31,10 +31,13 @@ class Worker(object):
         self._queued = []
 
     def _run(self, r):
+        print "RUNNING"
         self._running -= 1
         if self._running < self.maxconcurrent and self._queued:
             workunit, d = self._queued.pop(0)
             for work in workunit:
+                print "Going over workunits bis"
+                print work
                 self._running += 1
                 actuald = work.startTest().addBoth(self._run)
         if isinstance(r, failure.Failure):
@@ -48,8 +51,11 @@ class Worker(object):
         return r
 
     def push(self, workunit):
+        print "PUSHING"
         if self._running < self.maxconcurrent:
             for work in workunit:
+                print "Going over work units"
+                print dir(work)
                 self._running += 1
                 work.startTest().addBoth(self._run)
             return
@@ -102,9 +108,15 @@ class WorkUnit(object):
         Launches the Unit of Work with the specified assets on the node.
         """
         try:
-           asset = self.assetGenerator.next()
-           return self.Test(asset, self.node, self.arguments)
+            asset = self.assetGenerator.next()
+            print "Next shit.."
+            print asset
+            ret = self.Test(asset, self.arguments)
+            print type(ret)
+            print repr(ret)
+            return ret
         except StopIteration:
+            print "Stopped iteration!"
             raise StopIteration
 
 
diff --git a/unittest/tests.py b/unittest/tests.py
index da7c7d0..10887b2 100644
--- a/unittest/tests.py
+++ b/unittest/tests.py
@@ -1,5 +1,5 @@
 from twisted.internet import reactor
-from plugoo import *
+from plugoo import work, tests
 
 class StupidAsset(object):
     def __init__(self):
@@ -14,7 +14,7 @@ class StupidAsset(object):
         self.idx += 1
         return self.idx
 
-wgen = work.WorkGenerator(StupidAsset, tests.StupidTest, {'bla': 'aaa'}, start=0)
+wgen = work.WorkGenerator(StupidAsset, tests.StupidTest(None, None, None, None), {'bla': 'aaa'}, start=0)
 worker = work.Worker()
 for x in wgen:
     print "------"





More information about the tor-commits mailing list