[tor-commits] [ooni-probe/master] Refactor the pattern for directory creation into a function

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


commit ba8558514aabc963782688ac694daa196c53dc8d
Author: Arturo Filastò <arturo at filasto.net>
Date:   Fri Sep 16 12:10:20 2016 +0200

    Refactor the pattern for directory creation into a function
---
 ooni/deck/store.py          | 14 ++++----------
 ooni/resources.py           | 16 +++++-----------
 ooni/scripts/oonideckgen.py |  8 ++------
 ooni/utils/__init__.py      | 13 +++++++++++++
 ooni/utils/log.py           |  9 ++-------
 ooni/utils/onion.py         |  8 +++-----
 6 files changed, 29 insertions(+), 39 deletions(-)

diff --git a/ooni/deck/store.py b/ooni/deck/store.py
index bd852c7..bf1eb7f 100644
--- a/ooni/deck/store.py
+++ b/ooni/deck/store.py
@@ -6,6 +6,7 @@ from copy import deepcopy
 from twisted.internet import defer
 from twisted.python.filepath import FilePath
 
+from ooni.utils import mkdir_p
 from ooni.deck.deck import NGDeck
 from ooni.otime import timestampNowISO8601UTC
 from ooni.resources import check_for_update
@@ -80,16 +81,9 @@ class InputStore(object):
         self.path = FilePath(config.inputs_directory)
         self.resources = FilePath(config.resources_directory)
 
-        try:
-            self.path.child("descriptors").makedirs()
-        except OSError as e:
-            if not e.errno == errno.EEXIST:
-                raise
-        try:
-            self.path.child("data").makedirs()
-        except OSError as e:
-            if not e.errno == errno.EEXIST:
-                raise
+        mkdir_p(self.path.child("descriptors").path)
+        mkdir_p(self.path.child("data").path)
+
         yield self.update_url_lists(country_code)
 
     @defer.inlineCallbacks
diff --git a/ooni/resources.py b/ooni/resources.py
index edb7781..1830d42 100644
--- a/ooni/resources.py
+++ b/ooni/resources.py
@@ -5,7 +5,7 @@ from twisted.python.filepath import FilePath
 from twisted.internet import defer
 from twisted.web.client import downloadPage, getPage, HTTPClientFactory
 
-from ooni.utils import log, gunzip, rename
+from ooni.utils import log, gunzip, rename, mkdir_p
 from ooni.settings import config
 
 # Disable logs of HTTPClientFactory
@@ -107,11 +107,7 @@ def check_for_update(country_code=None):
     latest_version = yield get_latest_version()
 
     resources_dir = FilePath(config.resources_directory)
-    try:
-        resources_dir.makedirs()
-    except OSError as e:
-        if not e.errno == errno.EEXIST:
-            raise
+    mkdir_p(resources_dir.path)
     current_manifest = resources_dir.child("manifest.json")
 
     if current_manifest.exists():
@@ -153,11 +149,9 @@ def check_for_update(country_code=None):
                 filename = filename[:-3]
                 gzipped = True
             dst_file = resources_dir.child(pre_path).child(filename)
-            try:
-                dst_file.parent().makedirs()
-            except OSError as e:
-                if not e.errno == errno.EEXIST:
-                    raise
+
+            mkdir_p(dst_file.parent().path)
+
             src_file = dst_file.temporarySibling()
             src_file.alwaysCreate = 0
 
diff --git a/ooni/scripts/oonideckgen.py b/ooni/scripts/oonideckgen.py
index ba27ffe..89541d7 100644
--- a/ooni/scripts/oonideckgen.py
+++ b/ooni/scripts/oonideckgen.py
@@ -1,12 +1,12 @@
 from __future__ import print_function
 
-import errno
 import os
 import sys
 
 from twisted.internet import defer, task
 from twisted.python import usage
 
+from ooni.utils import mkdir_p
 from ooni.otime import prettyDateNowUTC
 from ooni import errors
 from ooni.geoip import probe_ip
@@ -120,11 +120,7 @@ def oonideckgen(reactor):
 
     options['country-code'] = options['country-code'].lower()
 
-    try:
-        os.makedirs(os.path.dirname(options['output']))
-    except OSError as exception:
-        if exception.errno != errno.EEXIST:
-            raise
+    mkdir_p(os.path.dirname(options['output']))
 
     generate_deck(options)
 
diff --git a/ooni/utils/__init__.py b/ooni/utils/__init__.py
index a894daf..99f1985 100644
--- a/ooni/utils/__init__.py
+++ b/ooni/utils/__init__.py
@@ -175,3 +175,16 @@ def is_process_running(pid):
         else:
             raise
     return running
+
+def mkdir_p(path):
+    """
+    Like makedirs, but it also ignores EEXIST errors, unless it exists but
+    isn't a directory.
+    """
+    try:
+        os.makedirs(path)
+    except OSError as ose:
+        if ose.errno != errno.EEXIST:
+            raise
+        if not os.path.isdir(path):
+            raise
diff --git a/ooni/utils/log.py b/ooni/utils/log.py
index 93e6b31..f20fdce 100644
--- a/ooni/utils/log.py
+++ b/ooni/utils/log.py
@@ -7,6 +7,7 @@ import logging
 from twisted.python import log as tw_log
 from twisted.python.logfile import DailyLogFile
 
+from ooni.utils import mkdir_p
 from ooni import otime
 
 # Get rid of the annoying "No route found for
@@ -140,13 +141,7 @@ class OONILogger(object):
             log_folder = config.running_path
             logfile = os.path.join(log_folder, "ooniprobe.log")
 
-        try:
-            os.makedirs(log_folder)
-        except OSError as ose:
-            if ose.errno == errno.EEXIST and os.path.isdir(log_folder):
-                pass
-            else:
-                raise
+        mkdir_p(log_folder)
 
         log_filename = os.path.basename(logfile)
         file_log_level = levels.get(config.basic.loglevel,
diff --git a/ooni/utils/onion.py b/ooni/utils/onion.py
index 10f80a3..7414e2f 100644
--- a/ooni/utils/onion.py
+++ b/ooni/utils/onion.py
@@ -15,6 +15,7 @@ from twisted.internet.endpoints import TCP4ClientEndpoint
 from txtorcon import TorConfig, TorState, launch_tor, build_tor_connection
 from txtorcon.util import find_tor_binary as tx_find_tor_binary
 
+from ooni.utils import mkdir_p
 from ooni.utils.net import randomFreePort
 from ooni import constants
 from ooni import errors
@@ -253,12 +254,9 @@ def get_tor_config():
         # 2. We have write permissions to it
         data_dir_usable = is_tor_data_dir_usable(data_dir)
         try:
-            os.makedirs(data_dir)
-            log.debug("%s does not exist. Creating it." % data_dir)
+            mkdir_p(data_dir)
         except OSError as ose:
-            if ose.errno == errno.EEXIST:
-                pass
-            elif ose.errno == errno.EACCESS:
+            if ose.errno == errno.EACCESS:
                 data_dir_usable = False
             else:
                 raise





More information about the tor-commits mailing list