commit 5360df52c1f4ade6be6e8b09f2dde282b1fe0e76 Author: Arturo Filastò hellais@torproject.org Date: Thu May 31 15:10:19 2012 +0200
Refactor code and log to stdout --- ooni/log.py | 9 +++- ooni/plugoo/interface.py | 21 ++++++++ ooni/plugoo/tests.py | 126 +-------------------------------------------- ooni/tests/worker_test.py | 27 ---------- 4 files changed, 29 insertions(+), 154 deletions(-)
diff --git a/ooni/log.py b/ooni/log.py index bf6ea69..f1b7839 100644 --- a/ooni/log.py +++ b/ooni/log.py @@ -22,10 +22,15 @@ def _get_log_level(level):
def start(logfile=None, loglevel=None, logstdout=True): if log.defaultObserver: + print "%s" % logstdout loglevel = _get_log_level(loglevel) - logfile = logfile file = open(logfile, 'a') if logfile else sys.stderr - log.startLogging(file, setStdout=logstdout) + observer = log.FileLogObserver(file) + if logstdout: + log.startLogging(sys.stdout) + else: + log.startLogging() + log.addObserver(observer.emit) msg("Started OONI")
def msg(message, level=INFO, **kw): diff --git a/ooni/plugoo/interface.py b/ooni/plugoo/interface.py index 72f58d7..68cff1a 100644 --- a/ooni/plugoo/interface.py +++ b/ooni/plugoo/interface.py @@ -1,2 +1,23 @@ from zope.interface import implements, Interface, Attribute
+class ITest(Interface): + """ + This interface represents an OONI test. It fires a deferred on completion. + """ + + shortName = Attribute("""A short user facing description for this test""") + description = Attribute("""A string containing a longer description for the test""") + + requirements = Attribute("""What is required to run this this test, for example raw socket access or UDP or TCP""") + + #deferred = Attribute("""This will be fired on test completion""") + #node = Attribute("""This represents the node that will run the test""") + options = Attribute("""These are the arguments to be passed to the test for it's execution""") + + blocking = Attribute("""True or False, stating if the test should be run in a thread or not.""") + + def startTest(asset): + """ + Launches the Test with the specified arguments on a node. + """ + diff --git a/ooni/plugoo/tests.py b/ooni/plugoo/tests.py index fd19479..3f63476 100644 --- a/ooni/plugoo/tests.py +++ b/ooni/plugoo/tests.py @@ -13,133 +13,9 @@ from twisted.python import failure from ooni import log from ooni.plugoo import assets, work from ooni.plugoo.reports import Report +from ooni.plugoo.interface import ITest
-class Test: - """ - This is a ooni probe Test. - Also known as a Plugoo! - """ - def __init__(self, ooni, name="test"): - self.config = ooni.config - self.logger = ooni.logger - self.name = name - self.report = Report(ooni, - scp=ooni.config.report.ssh, - file=ooni.config.report.file, - tcp=ooni.config.report.tcp) - - - def control(self, *a, **b): - pass - - def experiment(self, *a, **b): - """ - Override this method to write your own - Plugoo. - """ - pass - - def load_assets(self, assets, index=None): - """ - Takes as input an array of Asset objects and - outputs an iterator for the loaded assets. - - example: - assets = [hostlist, portlist, requestlist] - """ - asset_count = len(assets) - bigsize = 0 - bigidx = 0 - - if asset_count > 1: - # If we have more than on asset we try to do some - # optimizations as how to iterate through them by - # picking the largest asset set as the main iterator - # and do a cartesian product on the smaller sets - for i, v in enumerate(assets): - size = v.len() - if size > bigsize: - bigidx, bigsize = (i, size) - - smallassets = list(assets) - smallassets.pop(bigidx) - - i = 0 - for x in assets[bigidx]: - if asset_count > 1: - # XXX this will only work in python 2.6, maybe refactor? - for comb in itertools.product(*smallassets): - if index and i < index: - i += 1 - else: - yield (x,) + comb - else: - if index and i < index: - i += 1 - else: - yield (x) - - def run(self, assets=None, extradata=None, buffer=10, timeout=100000): - self.logger.info("Starting %s", self.name) - jobs = [] - if assets: - self.logger.debug("Running through tests") - - if extradata and 'index' in extradata: - index = extradata['index'] - else: - index = None - - for i, data in enumerate(self.load_assets(assets, index)): - args = {'data': data} - if extradata: - args = dict(args.items()+extradata.items()) - # Append to the job queue - jobs.append(gevent.spawn(self.experiment, **args)) - # If the buffer is full run the jobs - if i % buffer == (buffer - 1): - # Run the jobs with the selected timeout - gevent.joinall(jobs, timeout=timeout) - for job in jobs: - #print "JOB VAL: %s" % job.value - self.logger.info("Writing report(s)") - self.report(job.value) - job.kill() - jobs = [] - - if len(jobs) > 0: - gevent.joinall(jobs, timeout=timeout) - for job in jobs: - #print "JOB VAL: %s" % job.value - self.logger.info("Writing report(s)") - self.report(job.value) - job.kill() - jobs = [] - else: - self.logger.error("No Assets! Dying!") - -class ITest(Interface): - """ - This interface represents an OONI test. It fires a deferred on completion. - """ - - shortName = Attribute("""A short user facing description for this test""") - description = Attribute("""A string containing a longer description for the test""") - - requirements = Attribute("""What is required to run this this test, for example raw socket access or UDP or TCP""") - - #deferred = Attribute("""This will be fired on test completion""") - #node = Attribute("""This represents the node that will run the test""") - options = Attribute("""These are the arguments to be passed to the test for it's execution""") - - blocking = Attribute("""True or False, stating if the test should be run in a thread or not.""") - - def startTest(asset): - """ - Launches the Test with the specified arguments on a node. - """ - class TwistedTest(object): blocking = False
diff --git a/ooni/tests/worker_test.py b/ooni/tests/worker_test.py deleted file mode 100644 index 10887b2..0000000 --- a/ooni/tests/worker_test.py +++ /dev/null @@ -1,27 +0,0 @@ -from twisted.internet import reactor -from plugoo import work, tests - -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 - -wgen = work.WorkGenerator(StupidAsset, tests.StupidTest(None, None, None, None), {'bla': 'aaa'}, start=0) -worker = work.Worker() -for x in wgen: - print "------" - print "Work unit" - print "------" - worker.push(x) - print "------" - -reactor.run() -