[tor-commits] [ooni-probe/master] Add support for specifying nettests via their shorthand name.

art at torproject.org art at torproject.org
Sat Sep 28 20:14:36 UTC 2013


commit cd8084ecdd5742bda39fc3294522464ddef852f0
Author: Arturo Filastò <art at fuffa.org>
Date:   Wed Sep 11 18:00:31 2013 +0200

    Add support for specifying nettests via their shorthand name.
---
 ooni/deck.py     |   24 +++++++++++++++++++++++-
 ooni/errors.py   |    6 ++++++
 ooni/oonicli.py  |   10 +++++++---
 ooni/settings.py |    2 +-
 4 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/ooni/deck.py b/ooni/deck.py
index c284452..5cd8773 100644
--- a/ooni/deck.py
+++ b/ooni/deck.py
@@ -67,6 +67,22 @@ class InputFile(object):
             file_hash = sha256(f.read())
             assert file_hash.hexdigest() == digest
 
+def nettest_to_path(path):
+    """
+    Takes as input either a path or a nettest name.
+    
+    Returns:
+
+        full path to the nettest file.
+    """
+    path_via_name = os.path.join(config.nettest_directory, path + '.py')
+    if os.path.exists(path):
+        return path
+    elif os.path.exists(path_via_name):
+        return path_via_name
+    else:
+        raise e.NetTestNotFound(path)
+
 class Deck(InputFile):
     def __init__(self, deck_hash=None, deckFile=None):
         self.id = deck_hash
@@ -95,8 +111,14 @@ class Deck(InputFile):
             test_deck = yaml.safe_load(f)
 
         for test in test_deck:
+            try:
+                nettest_path = nettest_to_path(test['options']['test_file'])
+            except e.NetTestNotFound:
+                log.err("Could not find %s" % test['options']['test_file'])
+                log.msg("Skipping...")
+                continue
             net_test_loader = NetTestLoader(test['options']['subargs'],
-                    test_file=test['options']['test_file'])
+                    test_file=nettest_path)
             #XXX: If the deck specifies the collector, we use the specified collector
             # And it should also specify the test helper address to use
             # net_test_loader.collector = test['options']['collector']
diff --git a/ooni/errors.py b/ooni/errors.py
index b29b778..a124487 100644
--- a/ooni/errors.py
+++ b/ooni/errors.py
@@ -189,6 +189,12 @@ class CouldNotFindTestHelper(Exception):
 class CouldNotFindTestCollector(Exception):
     pass
 
+class NetTestNotFound(Exception):
+    pass
+
+class MissingRequiredOption(Exception):
+    pass
+
 def get_error(error_key):
     if error_key == 'test-helpers-key-missing':
         return CouldNotFindTestHelper
diff --git a/ooni/oonicli.py b/ooni/oonicli.py
index c511aa0..8622e09 100644
--- a/ooni/oonicli.py
+++ b/ooni/oonicli.py
@@ -14,7 +14,7 @@ from ooni import errors
 
 from ooni.settings import config
 from ooni.director import Director
-from ooni.deck import Deck
+from ooni.deck import Deck, nettest_to_path
 from ooni.reporter import YAMLReporter, OONIBReporter
 from ooni.nettest import NetTestLoader, MissingRequiredOption
 
@@ -141,13 +141,17 @@ def runWithDirector():
             deck.loadDeck(global_options['testdeck'])
         else:
             log.debug("No test deck detected")
+            test_file = nettest_to_path(global_options['test_file'])
             net_test_loader = NetTestLoader(global_options['subargs'],
-                    test_file=global_options['test_file'])
+                    test_file=test_file)
             deck.insert(net_test_loader)
-    except MissingRequiredOption, option_name:
+    except errors.MissingRequiredOption, option_name:
         log.err('Missing required option: "%s"' % option_name)
         print net_test_loader.usageOptions().getUsage()
         sys.exit(2)
+    except errors.NetTestNotFound, path:
+        log.err('Requested NetTest file not found (%s)' % path)
+        sys.exit(3)
     except usage.UsageError, e:
         log.err(e)
         print net_test_loader.usageOptions().getUsage()
diff --git a/ooni/settings.py b/ooni/settings.py
index 227eb34..7d8a7bd 100644
--- a/ooni/settings.py
+++ b/ooni/settings.py
@@ -33,7 +33,7 @@ class OConfig(object):
             self.data_directory = self.advanced['data_dir']
         else:
             self.data_directory = '/usr/share/ooni/'
-        self.nettest_directory = os.path.join(self.data_directory, 'nettests')
+        self.nettest_directory = abspath(os.path.join(__file__, '..', 'nettests'))
 
         self.ooni_home = os.path.join(expanduser('~'), '.ooni')
         self.inputs_directory = os.path.join(self.ooni_home, 'inputs')





More information about the tor-commits mailing list