[tor-commits] [stem/master] Adding function to query tor's version

atagar at torproject.org atagar at torproject.org
Thu Oct 27 03:30:03 UTC 2011


commit 51d2ded44fe48f54c63cf4e5937a2ff91fa99e95
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Oct 21 08:10:57 2011 -0700

    Adding function to query tor's version
    
    Command to issue a 'tor --version' query with a stem.types.Version response.
---
 stem/connection.py |    6 +++---
 stem/process.py    |   42 ++++++++++++++++++++++++++++++++++++++++++
 stem/types.py      |    4 ++--
 3 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/stem/connection.py b/stem/connection.py
index 4853864..4076400 100644
--- a/stem/connection.py
+++ b/stem/connection.py
@@ -59,8 +59,8 @@ class ControlConnection:
     whenever we receive an event from the control socket.
     
     Arguments:
-      event_message (types.ControlMessage) - message received from the control
-                                             socket
+      event_message (stem.types.ControlMessage) -
+          message received from the control socket
     """
     
     pass
@@ -73,7 +73,7 @@ class ControlConnection:
       message (str) - message to be sent to the control socket
     
     Returns:
-      types.ControlMessage with the response from the control socket
+      stem.types.ControlMessage with the response from the control socket
     """
     
     # makes sure that the message ends with a CRLF
diff --git a/stem/process.py b/stem/process.py
index 75e8f6f..b2034c2 100644
--- a/stem/process.py
+++ b/stem/process.py
@@ -7,9 +7,51 @@ import os
 import signal
 import subprocess
 
+import stem.types
+
+from stem.util import system
+
 # number of seconds before we time out our attempt to start a tor instance
 DEFAULT_INIT_TIMEOUT = 90
 
+def get_version(tor_cmd = "tor"):
+  """
+  Provides the version of tor.
+  
+  Arguments:
+    tor_cmd (str) - command used to run tor
+  
+  Returns:
+    stem.types.Version provided by the tor command
+  
+  Raises:
+    IOError if unable to query or parse the version
+  """
+  
+  try:
+    version_cmd = "%s --version" % tor_cmd
+    version_output = system.call(version_cmd)
+  except OSError, exc:
+    raise IOError(exc)
+  
+  if version_output:
+    # output example:
+    # Oct 21 07:19:27.438 [notice] Tor v0.2.1.30. This is experimental software. Do not rely on it for strong anonymity. (Running on Linux i686)
+    # Tor version 0.2.1.30.
+    
+    last_line = version_output[-1]
+    
+    if last_line.startswith("Tor version ") and last_line.endswith("."):
+      try:
+        version_str = last_line[12:-1]
+        return stem.types.get_version(version_str)
+      except ValueError, exc:
+        raise IOError(exc)
+    else:
+      raise IOError("Unexpected response from '%s': %s" % (version_cmd, last_line))
+  else:
+    raise IOError("'%s' didn't have any output" % version_cmd)
+
 def launch_tor(torrc_path, init_msg_handler = None, timeout = DEFAULT_INIT_TIMEOUT):
   """
   Initializes a tor process. This blocks until initialization completes or we
diff --git a/stem/types.py b/stem/types.py
index 818b6d3..a83a7c2 100644
--- a/stem/types.py
+++ b/stem/types.py
@@ -41,7 +41,7 @@ def read_message(control_file):
                    makefile() method for more information)
   
   Returns:
-    ControlMessage read from the socket
+    stem.types.ControlMessage read from the socket
   
   Raises:
     ProtocolError the content from the socket is malformed
@@ -210,7 +210,7 @@ def get_version(version_str):
     version_str (str) - representation of a tor version (ex. "0.2.2.23-alpha")
   
   Returns:
-    types.Version instance
+    stem.types.Version instance
   
   Raises:
     ValueError if input isn't a valid tor version





More information about the tor-commits mailing list