[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [ooni-probe/master] By default read the datadirectory from a ini file
commit add94741b2cd235de5379a695885ef7654acf8a0
Author: Arturo Filastò <art@xxxxxxxxx>
Date: Thu Nov 13 12:26:24 2014 +0100
By default read the datadirectory from a ini file
---
ooni/settings.ini | 3 +++
ooni/settings.py | 28 ++++++++++++----------------
ooni/utils/__init__.py | 7 ++++++-
3 files changed, 21 insertions(+), 17 deletions(-)
diff --git a/ooni/settings.ini b/ooni/settings.ini
new file mode 100644
index 0000000..52fb901
--- /dev/null
+++ b/ooni/settings.ini
@@ -0,0 +1,3 @@
+[directories]
+data_dir = /var/lib/ooni/
+
diff --git a/ooni/settings.py b/ooni/settings.py
index 51eef07..fbf85c2 100644
--- a/ooni/settings.py
+++ b/ooni/settings.py
@@ -1,7 +1,7 @@
import os
-import sys
import yaml
import getpass
+from ConfigParser import SafeConfigParser
from twisted.internet import defer, reactor
from twisted.internet.endpoints import TCP4ClientEndpoint
@@ -10,7 +10,7 @@ from os.path import abspath, expanduser
from ooni.utils.net import ConnectAndCloseProtocol, connectProtocol
from ooni import geoip
-from ooni.utils import Storage, log
+from ooni.utils import Storage, log, get_ooni_root
from ooni import errors
@@ -34,20 +34,18 @@ class OConfig(object):
@property
def data_directory(self):
- data_directory = abspath(os.path.join(__file__, '..', '..', 'data'))
-
if os.getenv("OONI_DATA_DIR"):
- data_directory = os.getenv("OONI_DATA_DIR")
+ return os.getenv("OONI_DATA_DIR")
elif self.global_options.get('datadir'):
- data_directory = abspath(expanduser(self.global_options['datadir']))
+ return abspath(expanduser(self.global_options['datadir']))
elif self.advanced.get('data_dir'):
- data_directory = self.advanced['data_dir']
- elif hasattr(sys, 'real_prefix'):
- data_directory = os.path.abspath(os.path.join(sys.prefix, 'share', 'ooni'))
- elif not os.path.exists(data_directory):
- data_directory = '/var/lib/ooni/'
-
- return data_directory
+ return self.advanced['data_dir']
+ else:
+ embedded_settings = os.path.join(get_ooni_root(), 'settings.ini')
+ settings = SafeConfigParser()
+ settings.readfp(open(embedded_settings))
+ return os.path.abspath(settings.get("directories", "data_dir"))
+ #return abspath(os.path.join(__file__, '..', '..', 'data'))
def set_paths(self, ooni_home=None):
if ooni_home:
@@ -100,9 +98,7 @@ class OConfig(object):
with open(sample_config_file) as f:
with open(target_config_file, 'w+') as w:
for line in f:
- if line.startswith(' data_dir: '):
- w.write(' data_dir: %s\n' % self.data_directory)
- elif line.startswith(' logfile: '):
+ if line.startswith(' logfile: '):
w.write(' logfile: %s\n' % os.path.join(self.ooni_home, 'ooniprobe.log'))
else:
w.write(line)
diff --git a/ooni/utils/__init__.py b/ooni/utils/__init__.py
index 180b5b3..02904cc 100644
--- a/ooni/utils/__init__.py
+++ b/ooni/utils/__init__.py
@@ -42,7 +42,7 @@ class Storage(dict):
try:
del self[key]
except KeyError, k:
- raise AttributeError, k
+ raise AttributeError(k)
def __repr__(self):
return '<Storage ' + dict.__repr__(self) + '>'
@@ -171,3 +171,8 @@ def gunzip(filename, dst):
gzip_file = gzip.open(filename)
shutil.copyfileobj(gzip_file, fw)
gzip_file.close()
+
+
+def get_ooni_root():
+ script = os.path.join(__file__, '..')
+ return os.path.dirname(os.path.realpath(script))
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits