[tor-commits] [ooni-probe/master] Refactor the renaming of old files to a proper util function
art at torproject.org
art at torproject.org
Sat Dec 8 11:26:53 UTC 2012
commit 8169c927896aaf9b5634c775cd48deb6b6553374
Author: Arturo Filastò <art at fuffa.org>
Date: Sat Dec 8 12:16:38 2012 +0100
Refactor the renaming of old files to a proper util function
* Do not require passing of arguments to startSniffing
* Add unittest for pushFilenameStack
---
ooni/oonicli.py | 6 +++++-
ooni/reporter.py | 5 ++---
ooni/runner.py | 16 +++++++---------
ooni/utils/__init__.py | 33 +++++++++++++++++++++++++++++++--
tests/test_utils.py | 20 ++++++++++++++++++++
5 files changed, 65 insertions(+), 15 deletions(-)
diff --git a/ooni/oonicli.py b/ooni/oonicli.py
index 23c4dd5..44334f0 100644
--- a/ooni/oonicli.py
+++ b/ooni/oonicli.py
@@ -126,9 +126,13 @@ def run():
log.start(cmd_line_options['logfile'])
+ config.cmd_line_options = cmd_line_options
+
if config.privacy.includepcap:
log.msg("Starting")
- runner.startSniffing(cmd_line_options)
+ if not config.reports.pcap:
+ config.generatePcapFilename()
+ runner.startSniffing()
resume = cmd_line_options['resume']
diff --git a/ooni/reporter.py b/ooni/reporter.py
index 6a8fd3f..e14f64d 100644
--- a/ooni/reporter.py
+++ b/ooni/reporter.py
@@ -26,7 +26,7 @@ except ImportError:
from ooni import otime
-from ooni.utils import geodata
+from ooni.utils import geodata, pushFilenameStack
from ooni.utils.net import BodyReceiver, StringProducer, userAgents
from ooni import config
@@ -208,8 +208,7 @@ class YAMLReporter(OReporter):
if os.path.exists(reportfile):
log.msg("Report already exists with filename %s" % reportfile)
- log.msg("Renaming it to %s" % reportfile+'.old')
- os.rename(reportfile, reportfile+'.old')
+ pushFilenameStack(reportfile)
log.debug("Creating %s" % reportfile)
self._stream = open(reportfile, 'w+')
diff --git a/ooni/runner.py b/ooni/runner.py
index 4483ce4..b451816 100644
--- a/ooni/runner.py
+++ b/ooni/runner.py
@@ -4,6 +4,7 @@ import time
import inspect
import traceback
import itertools
+
import yaml
from twisted.python import reflect, usage
@@ -21,7 +22,7 @@ from ooni.reporter import OONIBReporter, YAMLReporter, OONIBReportError
from ooni.inputunit import InputUnitFactory
from ooni.nettest import NetTestCase, NoPostProcessor
-from ooni.utils import log, checkForRoot
+from ooni.utils import log, checkForRoot, pushFilenameStack
from ooni.utils import NotRootError, Storage
from ooni.utils.net import randomFreePort
@@ -521,7 +522,7 @@ def startTor():
d.addErrback(setup_failed)
return d
-def startSniffing(cmd_line_options):
+def startSniffing():
""" Start sniffing with Scapy. Exits if required privileges (root) are not
available.
"""
@@ -536,13 +537,10 @@ def startSniffing(cmd_line_options):
print "Starting sniffer"
config.scapyFactory = ScapyFactory(config.advanced.interface)
- if not config.reports.pcap:
- config.cmd_line_options = cmd_line_options
- config.generatePcapFilename()
- if os.path.exists(config.reports.pcap):
- print "Report PCAP already exists with filename %s" % config.reports.pcap
- print "Renaming it to %s" % config.reports.pcap+".old"
- os.rename(config.reports.pcap, config.reports.pcap+".old")
+ if os.path.exists(config.reports.pcap):
+ print "Report PCAP already exists with filename %s" % config.reports.pcap
+ print "Renaming files with such name..."
+ pushFilenameStack(config.reports.pcap)
sniffer = ScapySniffer(config.reports.pcap)
config.scapyFactory.registerProtocol(sniffer)
diff --git a/ooni/utils/__init__.py b/ooni/utils/__init__.py
index abcf370..baf3f39 100644
--- a/ooni/utils/__init__.py
+++ b/ooni/utils/__init__.py
@@ -1,9 +1,10 @@
-import imp
-import os
import logging
import string
import random
+import glob
import yaml
+import imp
+import os
class Storage(dict):
"""
@@ -83,3 +84,31 @@ def randomStr(length, num=True):
return ''.join(random.choice(chars) for x in range(length))
+def pushFilenameStack(filename):
+ """
+ Takes as input a target filename and checks to see if a file by such name
+ already exists. If it does exist then it will attempt to rename it to .1,
+ if .1 exists it will rename .1 to .2 if .2 exists then it will rename it to
+ .3, etc.
+ This is similar to pushing into a LIFO stack.
+
+ XXX: This will not work with stacks bigger than 10 elements because
+ glob.glob(".*") will return them in the wrong order (a.1, a.11, a.2, a.3,
+ etc.)
+ This is probably not an issue since the only thing it causes is that files
+ will be renamed in the wrong order and you shouldn't have the same report
+ filename for more than 10 reports anyways, because you should be making
+ ooniprobe generate the filename for you.
+
+ Args:
+ filename (str): the path to filename that you wish to create.
+ """
+ stack = glob.glob(filename+".*")
+ for f in reversed(stack):
+ c_filename, c_idx = f.split(".")
+ new_idx = int(c_idx) + 1
+ new_filename = "%s.%s" % (c_filename, new_idx)
+ os.rename(f, new_filename)
+ os.rename(filename, filename+".1")
+
+
diff --git a/tests/test_utils.py b/tests/test_utils.py
new file mode 100644
index 0000000..cc648e0
--- /dev/null
+++ b/tests/test_utils.py
@@ -0,0 +1,20 @@
+import unittest
+from ooni.utils import pushFilenameStack
+
+class TestUtils(unittest.TestCase):
+ def test_pushFilenameStack(self):
+ f = open("dummyfile", "w+")
+ f.write("0\n")
+ f.close()
+ for i in xrange(1, 5):
+ f = open("dummyfile.%s" % i, "w+")
+ f.write("%s\n" % i)
+ f.close()
+
+ pushFilenameStack("dummyfile")
+ for i in xrange(1, 5):
+ f = open("dummyfile.%s" % i)
+ c = f.readlines()[0].strip()
+ self.assertEqual(str(i-1), str(c))
+ f.close()
+
More information about the tor-commits
mailing list