[tor-commits] [ooni-probe/master] Set pcap name for each NetTest, and use consistent timestamps.

art at torproject.org art at torproject.org
Tue May 14 15:49:22 UTC 2013


commit 9fc621ad23abf7ec9c8344741d2f2ba401d78b5c
Author: aagbsn <aagbsn at extc.org>
Date:   Fri May 3 13:38:02 2013 +0800

    Set pcap name for each NetTest, and use consistent timestamps.
---
 ooni/config.py   |   23 ++++-------------------
 ooni/director.py |   20 ++++++++++++--------
 ooni/nettest.py  |    5 ++---
 ooni/otime.py    |    2 ++
 4 files changed, 20 insertions(+), 30 deletions(-)

diff --git a/ooni/config.py b/ooni/config.py
index 671f80f..103ace7 100644
--- a/ooni/config.py
+++ b/ooni/config.py
@@ -122,22 +122,7 @@ if not resume_filename:
         with open(resume_filename, 'w+') as f: pass
 
 
-def generatePcapFilename(cmd_line_options=None):
-    if not cmd_line_options:
-        cmd_line_options = {}
-
-    if 'pcapfile' in cmd_line_options:
-        pcap_filename = cmd_line_options['pcapfile']
-    else:
-        if 'test' in cmd_line_options:
-            test_filename = os.path.basename(cmd_line_options['test'])
-        elif 'testdeck' in cmd_line_options:
-            test_filename = os.path.basename(cmd_line_options['testdeck'])
-        else:
-            test_filename = ''
-
-        test_name = '.'.join(test_filename.split(".")[:-1])
-        frm_str = "report_%s_" + otime.timestamp() + ".%s"
-        pcap_filename = frm_str % (test_name, "pcap")
-
-    return pcap_filename
+def generatePcapFilename(testDetails):
+    test_name, start_time = testDetails['test_name'], testDetails['start_time']
+    start_time = otime.epochToTimestamp(start_time)
+    return "report-%s-%s.%s" % (test_name, start_time, "pcap")
diff --git a/ooni/director.py b/ooni/director.py
index 7ad99a9..93134af 100644
--- a/ooni/director.py
+++ b/ooni/director.py
@@ -86,14 +86,10 @@ class Director(object):
         # This deferred is fired once all the measurements and their reporting
         # tasks are completed.
         self.allTestsDone = defer.Deferred()
+        self.sniffer = None
 
     @defer.inlineCallbacks
     def start(self):
-        if config.privacy.includepcap:
-            log.msg("Starting")
-            if not config.reports.pcap:
-                config.reports.pcap = config.generatePcapFilename()
-            self.startSniffing()
 
         if config.advanced.start_tor:
             log.msg("Starting Tor...")
@@ -192,6 +188,13 @@ class Director(object):
 
             _: #XXX very dirty hack
         """
+
+        if config.privacy.includepcap:
+            log.msg("Starting")
+            if not config.reports.pcap:
+                config.reports.pcap = config.generatePcapFilename(net_test_loader.testDetails)
+            self.startSniffing()
+
         report = Report(reporters, self.reportEntryManager)
 
         net_test = NetTest(net_test_loader, report)
@@ -228,9 +231,10 @@ class Director(object):
             print "Renaming files with such name..."
             pushFilenameStack(config.reports.pcap)
 
-        sniffer = ScapySniffer(config.reports.pcap)
-        config.scapyFactory.registerProtocol(sniffer)
-
+        if self.sniffer:
+            config.scapyFactory.unRegisterProtocol(self.sniffer)
+        self.sniffer = ScapySniffer(config.reports.pcap)
+        config.scapyFactory.registerProtocol(self.sniffer)
 
     def startTor(self):
         """ Starts Tor
diff --git a/ooni/nettest.py b/ooni/nettest.py
index 50c0cf0..cd8ab05 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -9,8 +9,7 @@ from ooni import geoip
 from ooni.tasks import Measurement
 from ooni.utils import log, checkForRoot, geodata
 from ooni import config
-from ooni import otime
-
+import time
 from ooni import errors as e
 
 from inspect import getmembers
@@ -69,7 +68,7 @@ class NetTestLoader(object):
                 or ('countrycode' not in client_geodata):
             client_geodata['countrycode'] = None
 
-        test_details = {'start_time': otime.utcTimeNow(),
+        test_details = {'start_time': time.time(),
             'probe_asn': client_geodata['asn'],
             'probe_cc': client_geodata['countrycode'],
             'probe_ip': client_geodata['ip'],
diff --git a/ooni/otime.py b/ooni/otime.py
index 84758eb..0446d4d 100644
--- a/ooni/otime.py
+++ b/ooni/otime.py
@@ -87,3 +87,5 @@ def timestamp(t=None):
     ISO8601 = "%Y-%m-%dT%H%M%SZ"
     return t.strftime(ISO8601)
 
+def epochToTimestamp(seconds):
+    return timestamp(datetime.utcfromtimestamp(seconds))





More information about the tor-commits mailing list