commit a6bc4d53d3288b85b9d4a2712f94f4a4c3e4862d Author: Isis Lovecruft isis@torproject.org Date: Thu Dec 13 02:20:01 2012 +0000
Started fixing a bug in oonicli.updateStatusBar() and runner.runTestList() where the status bar hangs at 100%. It needs to be changed to use twisted.internet.task.Cooperator and t.i.t.CooperativeTask, which means that oonicli.updateStatusBar() still needs to be changed to be an iterator. --- ooni/oonicli.py | 54 ++++++++++++++++++++++++++++++++++++++++-------------- ooni/runner.py | 21 +++++++++------------ 2 files changed, 49 insertions(+), 26 deletions(-)
diff --git a/ooni/oonicli.py b/ooni/oonicli.py index 9d4f783..0bf4d55 100644 --- a/ooni/oonicli.py +++ b/ooni/oonicli.py @@ -42,13 +42,14 @@ class Options(usage.Options): optFlags = [["help", "h"], ["resume", "r"]]
- optParameters = [["reportfile", "o", None, "report file name"], - ["testdeck", "i", None, - "Specify as input a test deck: a yaml file containig the tests to run an their arguments"], - ["collector", "c", None, - "Address of the collector of test results. (example: http://127.0.0.1:8888)"], - ["logfile", "l", None, "log file name"], - ["pcapfile", "p", None, "pcap file name"]] + optParameters = [ + ["reportfile", "o", None, "report file name"], + ["testdeck", "i", None, + "Specify a test deck: a yaml file containing tests and their arguments"], + ["collector", "c", None, + "Address of the collector of test results. (e.g.: http://127.0.0.1:8888)"], + ["logfile", "l", None, "log file name"], + ["pcapfile", "p", None, "pcap file name"]]
compData = usage.Completions( extraActions=[usage.CompleteFiles( @@ -81,14 +82,34 @@ class Options(usage.Options): except: raise usage.UsageError("No test filename specified!")
-def updateStatusBar(): +class CooperativeTimer(object): + """ + A simple timer for the callback to functions on + :class:`twisted.internet.task.Cooperator <t.i.t.Cooperator>`. see + :meth:`oonicli.runTestList <runTestList>`. + + @param seconds: + An integer specifying the second to wait in between updating the + status and ETA bars. + """ + def __init__(self, seconds=5): + self.max_timer_interval = float(seconds) + self.end = time.time() + self.max_timer_interval + + def __call__(self): + return time.time() >= self.end + +def updateStatusBar(stop_func): for test_filename in config.state.keys(): # The ETA is not updated so we we will not print it out for the # moment. eta = config.state[test_filename].eta() progress = config.state[test_filename].progress() - progress_bar_frmt = "[%s] %s%%" % (test_filename, progress) - print progress_bar_frmt + while progress is not None: + print "[%s] %s%%" % (test_filename, progress) + else: + print "[%s] All tests in file completed." % test_filename + stop_func()
def testsEnded(*arg, **kw): """You can place here all the post shutdown tasks.""" @@ -102,7 +123,7 @@ def startSniffing(): from ooni.utils.txscapy import ScapyFactory, ScapySniffer try: checkForRoot() - except NotRootError: + except PermissionsError: print "[!] Includepcap options requires root priviledges to run" print " you should run ooniprobe as root or disable the options in ooniprobe.conf" sys.exit(1) @@ -130,9 +151,14 @@ def runTestList(none, test_list): d2 = defer.DeferredList(deck_dl) d2.addBoth(testsEnded)
- # Print every 5 second the list of current tests running - l = task.LoopingCall(updateStatusBar) - l.start(5.0) + try: + # Print every 5 second the list of current tests running + coop = task.Cooperator(started=False) + coop.cooperate(updateStatusBar) #this will need a .next() method + coop.start() + except StopIteration: + return d2 + return d2
def errorRunningTests(failure): diff --git a/ooni/runner.py b/ooni/runner.py index 6f03e56..19dc171 100644 --- a/ooni/runner.py +++ b/ooni/runner.py @@ -328,9 +328,9 @@ def runTestCasesWithInput(test_cases, test_input, yaml_reporter, reason = getattr(test_instance.__class__, 'skip') else: reason = txutil.acquireAttribute(test_instance._parents, 'skip', None) + if reason is not None: log.warn("%s marked some tests to be skipped. Reason: %s" % (test_instance.name, reason)) - if reason is not None: call_skip = reactor.callLater(0, test_skip_class, reason) d.addBoth(lambda x: call_skip.active() and call_skip.cancel() or x)
@@ -456,14 +456,14 @@ def increaseInputUnitIdx(test_filename): config.stateDict[test_filename] += 1 yield updateResumeFile(test_filename)
-def updateProgressMeters(test_filename, input_unit_factory, - test_case_number): +def updateProgressMeters(test_filename, input_unit_factory, test_case_number): """Update the progress meters for keeping track of test state.""" log.msg("Setting up progress meters") if not config.state.test_filename: config.state[test_filename] = Storage()
- config.state[test_filename].per_item_average = 2.0 + per_item_avg = float(2) + config.state[test_filename].per_item_average = per_item_avg
input_unit_idx = float(config.stateDict[test_filename]) input_unit_items = float(len(input_unit_factory) + 1) @@ -471,27 +471,24 @@ def updateProgressMeters(test_filename, input_unit_factory, total_iterations = input_unit_items * test_case_number current_iteration = input_unit_idx * test_case_number
- log.debug("input_unit_items: %s" % input_unit_items) - log.debug("test_case_number: %s" % test_case_number) - + log.debug("Total InputUnits: %s" % input_unit_items) log.debug("Test case number: %s" % test_case_number) log.debug("Total iterations: %s" % total_iterations) log.debug("Current iteration: %s" % current_iteration)
def progress(): - return (current_iteration / total_iterations) * 100.0 - + current_progress = (current_iteration / total_iterations) * 100.0 + while float(current_progress) < float(100): + return current_progress config.state[test_filename].progress = progress
def eta(): - return (total_iterations - current_iteration) \ - * config.state[test_filename].per_item_average + return (total_iterations - current_iteration) * per_item_avg config.state[test_filename].eta = eta
config.state[test_filename].input_unit_idx = input_unit_idx config.state[test_filename].input_unit_items = input_unit_items
- @defer.inlineCallbacks def runTestCases(test_cases, options, cmd_line_options): """