[tor-commits] [nyx/master] expand_path() helper function

atagar at torproject.org atagar at torproject.org
Sun Feb 14 02:55:49 UTC 2016


commit bdbe7c3e1ba234ace53aee89ecd90bdd134161f8
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jan 17 15:09:33 2016 -0800

    expand_path() helper function
    
    Adding a small util method for expanding relative paths and taking into account
    chroots.
---
 nyx/graph_panel.py       |  1 -
 nyx/torrc_panel.py       |  7 ++++---
 nyx/util/__init__.py     | 23 +++++++++++++++++++++++
 test/util/expand_path.py | 21 +++++++++++++++++++++
 4 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/nyx/graph_panel.py b/nyx/graph_panel.py
index f988125..086d04d 100644
--- a/nyx/graph_panel.py
+++ b/nyx/graph_panel.py
@@ -81,7 +81,6 @@ CONFIG = conf.config_dict('nyx', {
   'features.panels.show.connection': True,
   'features.graph.bw.transferInBytes': False,
   'features.graph.bw.accounting.show': True,
-  'tor.chroot': '',
 }, conf_handler)
 
 
diff --git a/nyx/torrc_panel.py b/nyx/torrc_panel.py
index 65dca26..0c70f37 100644
--- a/nyx/torrc_panel.py
+++ b/nyx/torrc_panel.py
@@ -8,7 +8,7 @@ import threading
 
 import nyx.popups
 
-from nyx.util import panel, tor_config, tor_controller, ui_tools
+from nyx.util import expand_path, panel, tor_config, tor_controller, ui_tools
 
 from stem.control import State
 from stem.util import conf, str_tools
@@ -170,10 +170,11 @@ class TorrcPanel(panel.Panel):
       rendered_contents, corrections, conf_location = None, {}, None
 
       loaded_torrc = tor_config.get_torrc()
+      controller = tor_controller()
 
-      with loaded_torrc.get_lock():
-        conf_location = loaded_torrc.get_config_location()
+      conf_location = expand_path(controller.get_info('config-file', None))
 
+      with loaded_torrc.get_lock():
         if not loaded_torrc.is_loaded():
           rendered_contents = ['### Unable to load the torrc ###']
         else:
diff --git a/nyx/util/__init__.py b/nyx/util/__init__.py
index a7b2e8d..350e96e 100644
--- a/nyx/util/__init__.py
+++ b/nyx/util/__init__.py
@@ -10,6 +10,7 @@ import stem.connection
 import stem.control
 import stem.util.conf
 import stem.util.log
+import stem.util.system
 
 from nyx.util import log
 
@@ -65,6 +66,28 @@ def init_controller(*args, **kwargs):
   return TOR_CONTROLLER
 
 
+ at uses_settings
+def expand_path(path, config):
+  """
+  Expands relative paths and include our chroot if one was set.
+
+  :param str path: path to be expanded
+
+  :returns: **str** with the expanded path
+  """
+
+  if path is None:
+    return None
+
+  try:
+    chroot = config.get('tor.chroot', '')
+    tor_cwd = stem.util.system.cwd(tor_controller().get_pid(None))
+    return chroot + stem.util.system.expand_path(path, tor_cwd)
+  except IOError as exc:
+    stem.util.log.info('Unable to expand a relative path (%s): %s' % (path, exc))
+    return path
+
+
 def join(entries, joiner = ' ', size = None):
   """
   Joins a series of strings similar to str.join(), but only up to a given size.
diff --git a/test/util/expand_path.py b/test/util/expand_path.py
new file mode 100644
index 0000000..3f3d5ff
--- /dev/null
+++ b/test/util/expand_path.py
@@ -0,0 +1,21 @@
+import unittest
+
+from nyx.util import expand_path, uses_settings
+
+from mock import patch, Mock
+
+
+class TestExpandPath(unittest.TestCase):
+  @patch('nyx.util.tor_controller')
+  @patch('stem.util.system.cwd', Mock(return_value = '/your_cwd'))
+  @uses_settings
+  def test_expand_path(self, tor_controller_mock, config):
+    tor_controller_mock().get_pid.return_value = 12345
+    self.assertEqual('/absolute/path/to/torrc', expand_path('/absolute/path/to/torrc'))
+    self.assertEqual('/your_cwd/torrc', expand_path('torrc'))
+
+    config.set('tor.chroot', '/chroot')
+    self.assertEqual('/chroot/absolute/path/to/torrc', expand_path('/absolute/path/to/torrc'))
+    self.assertEqual('/chroot/your_cwd/torrc', expand_path('torrc'))
+
+    config.set('tor.chroot', None)





More information about the tor-commits mailing list