[tor-commits] [ooni-probe/master] Write the deck file to always the same filename deck/

art at torproject.org art at torproject.org
Mon Sep 19 12:14:24 UTC 2016


commit 4239679d2d8d28828f6215b70678ab9c99887bd6
Author: Arturo Filastò <arturo at filasto.net>
Date:   Wed Jun 29 20:14:01 2016 +0200

    Write the deck file to always the same filename deck/
    
    * Add some basic unittests for oonideckgen
---
 ooni/deckgen/cli.py            | 39 +++++++++++++++++++++++----------------
 ooni/tests/bases.py            |  1 +
 ooni/tests/test_oonideckgen.py | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 16 deletions(-)

diff --git a/ooni/deckgen/cli.py b/ooni/deckgen/cli.py
index a6046f3..9f2cc4c 100644
--- a/ooni/deckgen/cli.py
+++ b/ooni/deckgen/cli.py
@@ -1,7 +1,10 @@
+from __future__ import print_function
+
 import os
 import sys
 import copy
 import errno
+import shutil
 
 import yaml
 
@@ -14,7 +17,6 @@ from ooni.settings import config
 
 from ooni.deckgen import __version__
 from ooni.deckgen.processors import citizenlab_test_lists
-from ooni.deckgen.processors import namebench_dns_servers
 from ooni.resources.update import download_resources
 
 class Options(usage.Options):
@@ -70,7 +72,7 @@ class Deck(object):
         self.deck_entries.append(deck_entry)
 
     def pprint(self):
-        print yaml.safe_dump(self.deck_entries)
+        print(yaml.safe_dump(self.deck_entries))
 
     def write_to_file(self, filename):
         with open(filename, "w+") as f:
@@ -85,8 +87,8 @@ def generate_deck(options):
             options['output']
         )
     except Exception:
-        print "Could not generate country specific url list"
-        print "We will just use the global one."
+        print("Could not generate country specific url list")
+        print("We will just use the global one.")
 
     url_list_global = citizenlab_test_lists.generate_global_input(
         options['output']
@@ -105,9 +107,9 @@ def generate_deck(options):
     deck_filename = os.path.join(options['output'],
                                  "default-user.deck")
     deck.write_to_file(deck_filename)
-    print "Deck written to %s" % deck_filename
-    print "Run ooniprobe like so:"
-    print "ooniprobe -i %s" % deck_filename
+    print("Deck written to %s" % deck_filename)
+    print("Run ooniprobe like so:")
+    print("ooniprobe -i %s" % deck_filename)
 
 
 @defer.inlineCallbacks
@@ -138,8 +140,8 @@ def run():
     try:
         options.parseOptions()
     except usage.UsageError as error_message:
-        print "%s: %s" % (sys.argv[0], error_message)
-        print options
+        print("%s: %s" % (sys.argv[0], error_message))
+        print(options)
         sys.exit(1)
 
     if not resources_up_to_date():
@@ -154,24 +156,29 @@ def run():
         try:
             options['country-code'] = yield get_user_country_code()
         except errors.ProbeIPUnknown:
-            print "Could not determine your IP address."
-            print "Check your internet connection or specify a country code with -c."
+            print("Could not determine your IP address.")
+            print("Check your internet connection or specify a country code "
+                  "with -c.")
             sys.exit(4)
 
     if len(options['country-code']) != 2:
-        print "%s: --country-code must be 2 characters" % sys.argv[0]
+        print("%s: --country-code must be 2 characters" % sys.argv[0])
         sys.exit(2)
 
     if not os.path.isdir(options['output']):
-        print "%s: %s is not a directory" % (sys.argv[0],
-                                             options['output'])
+        print("%s: %s is not a directory" % (sys.argv[0],
+                                             options['output']))
         sys.exit(3)
 
     options['country-code'] = options['country-code'].lower()
 
     output_dir = os.path.abspath(options['output'])
-    output_dir = os.path.join(output_dir,
-                              "deck-%s" % options['country-code'])
+    output_dir = os.path.join(output_dir, "deck")
+
+    if os.path.isdir(output_dir):
+        print("Found previous deck deleting content of it")
+        shutil.rmtree(output_dir)
+
     options['output'] = output_dir
 
     try:
diff --git a/ooni/tests/bases.py b/ooni/tests/bases.py
index 31cbf94..904aaba 100644
--- a/ooni/tests/bases.py
+++ b/ooni/tests/bases.py
@@ -1,5 +1,6 @@
 import os
 import shutil
+
 from twisted.trial import unittest
 
 from ooni.settings import config
diff --git a/ooni/tests/test_oonideckgen.py b/ooni/tests/test_oonideckgen.py
new file mode 100644
index 0000000..4a52377
--- /dev/null
+++ b/ooni/tests/test_oonideckgen.py
@@ -0,0 +1,35 @@
+import os
+import yaml
+import tempfile
+
+from .bases import ConfigTestCase
+
+from ooni.deckgen import cli
+
+class TestOONIDeckgen(ConfigTestCase):
+    def setUp(self):
+        super(TestOONIDeckgen, self).setUp()
+        test_lists_dir = os.path.join(self.config.ooni_home,
+                                      "resources",
+                                      "citizenlab-test-lists")
+        try:
+            os.makedirs(test_lists_dir)
+        except Exception as exc:
+            pass
+        global_list = os.path.join(test_lists_dir, "global.csv")
+
+        with open(global_list, 'w') as f:
+            f.write("url,category_code,category_description,date_added,"
+                    "source,notes\n")
+            f.write("http://example.com,FOO,Foo,2016-04-15,OONI,\n")
+
+    def test_generate_deck(self):
+        temp_dir = tempfile.mkdtemp()
+        cli.generate_deck({
+            "country-code": "it",
+            "output": temp_dir,
+            "collector": None,
+            "bouncer": None
+        })
+        with open(os.path.join(temp_dir, "default-user.deck")) as f:
+            self.assertEqual(len(yaml.safe_load(f)), 3)





More information about the tor-commits mailing list