[tor-commits] [stem/master] Performing pid resolion via getinfo when able

atagar at torproject.org atagar at torproject.org
Tue May 28 04:30:11 UTC 2013


commit 292ef5c36e38dd2820ed00e34ab270f2bb28ecb6
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon May 27 21:28:55 2013 -0700

    Performing pid resolion via getinfo when able
    
    Oops, forgot that I had added a 'GETINFO process/pid' option to tor for just
    this case. Using it when it's available.
---
 stem/control.py                 |   18 ++++++++++++------
 test/unit/control/controller.py |   13 +++++++++++--
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index a1a7ae4..4d416e8 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -1017,14 +1017,20 @@ class Controller(BaseController):
     pid = self._get_cache("pid")
 
     if not pid:
-      pid_file_path = self.get_conf("PidFile", None)
+      getinfo_pid = self.get_info("process/pid", None)
 
-      if pid_file_path is not None:
-        with open(pid_file_path) as pid_file:
-          pid_file_contents = pid_file.read().strip()
+      if getinfo_pid and getinfo_pid.isdigit():
+        pid = int(getinfo_pid)
 
-          if pid_file_contents.isdigit():
-            pid = int(pid_file_contents)
+      if not pid:
+        pid_file_path = self.get_conf("PidFile", None)
+
+        if pid_file_path is not None:
+          with open(pid_file_path) as pid_file:
+            pid_file_contents = pid_file.read().strip()
+
+            if pid_file_contents.isdigit():
+              pid = int(pid_file_contents)
 
       if not pid:
         pid = stem.util.system.get_pid_by_name('tor')
diff --git a/test/unit/control/controller.py b/test/unit/control/controller.py
index ab89ac0..449bc6d 100644
--- a/test/unit/control/controller.py
+++ b/test/unit/control/controller.py
@@ -258,6 +258,15 @@ class TestControl(unittest.TestCase):
     self.assertRaises(ValueError, self.controller.get_pid)
     self.assertEqual(123, self.controller.get_pid(123))
 
+  def test_get_pid_by_getinfo(self):
+    """
+    Exercise the get_pid() resolution via its getinfo option.
+    """
+
+    mocking.mock_method(ControlSocket, "is_localhost", mocking.return_true())
+    mocking.mock_method(Controller, "get_info", mocking.return_value('321'))
+    self.assertEqual(321, self.controller.get_pid())
+
   def test_get_pid_by_pid_file(self):
     """
     Exercise the get_pid() resolution via a PidFile.
@@ -272,10 +281,10 @@ class TestControl(unittest.TestCase):
 
     try:
       with open(pid_file_path, 'w') as pid_file:
-        pid_file.write('321')
+        pid_file.write('432')
 
       mocking.mock_method(Controller, "get_conf", mocking.return_value(pid_file_path))
-      self.assertEqual(321, self.controller.get_pid())
+      self.assertEqual(432, self.controller.get_pid())
     finally:
       os.remove(pid_file_path)
 



More information about the tor-commits mailing list