[tor-commits] [arm/master] Replacing tor version handling with stem's

atagar at torproject.org atagar at torproject.org
Mon Dec 17 04:25:17 UTC 2012


commit a40adf7aabbcd063d5def876ee7c9ea46fb5ae67
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Dec 16 13:49:16 2012 -0800

    Replacing tor version handling with stem's
    
    Stem has really good support for parsing and comparing tor versions. Using that
    instead of our home brewed counterpart.
---
 src/cli/wizard.py     |   18 ++++-----
 src/util/torConfig.py |   11 ++---
 src/util/torTools.py  |   98 ++++++-------------------------------------------
 3 files changed, 24 insertions(+), 103 deletions(-)

diff --git a/src/cli/wizard.py b/src/cli/wizard.py
index 86522d1..02a1f17 100644
--- a/src/cli/wizard.py
+++ b/src/cli/wizard.py
@@ -16,6 +16,8 @@ import curses
 import cli.popups
 import cli.controller
 
+import stem.version
+
 from util import connections, enum, log, sysTools, torConfig, torTools, uiTools
 
 # template used to generate the torrc
@@ -94,7 +96,9 @@ BRACKETS = ((' ', ' '),
             ('|', '|'))
 
 # version requirements for options
-VERSION_REQUIREMENTS = {Options.PORTFORWARD: "0.2.3.1-alpha"}
+VERSION_REQUIREMENTS = {
+  Options.PORTFORWARD: stem.version.Requirement.TORRC_PORT_FORWARDING,
+}
 
 # tor's defaults for config options, used to filter unneeded options
 TOR_DEFAULTS = {Options.BANDWIDTH: "5 MB",
@@ -245,16 +249,10 @@ def showWizard():
     log.log(log.WARN, msg)
     return
   
-  # gets tor's version
-  torVersion = None
   try:
-    versionQuery = sysTools.call("tor --version")
-    
-    for line in versionQuery:
-      if line.startswith("Tor version "):
-        torVersion = torTools.parseVersion(line.split(" ")[2])
-        break
+    torVersion = stem.version.get_system_tor_version()
   except IOError, exc:
+    torVersion = None
     log.log(log.INFO, "'tor --version' query failed: %s" % exc)
   
   relayType, config = None, {}
@@ -316,7 +314,7 @@ def showWizard():
   disabledOpt = list(CONFIG["wizard.disabled"])
   
   for opt, optVersion in VERSION_REQUIREMENTS.items():
-    if not torVersion or not torTools.isVersion(torVersion, torTools.parseVersion(optVersion)):
+    if torVersion is None or not torVersion.meets_requirements(optVersion):
       disabledOpt.append(opt)
   
   # the port forwarding option would only work if tor-fw-helper is in the path
diff --git a/src/util/torConfig.py b/src/util/torConfig.py
index 783fa84..fc08393 100644
--- a/src/util/torConfig.py
+++ b/src/util/torConfig.py
@@ -7,6 +7,8 @@ import time
 import socket
 import threading
 
+import stem.version
+
 from util import enum, log, sysTools, torTools, uiTools
 
 CONFIG = {"features.torrc.validate": True,
@@ -791,16 +793,11 @@ class Torrc():
     
     self.valsLock.acquire()
     
-    # The torrc validation relies on 'GETINFO config-text' which was
-    # introduced in tor 0.2.2.7-alpha so if we're using an earlier version
-    # (or configured to skip torrc validation) then this is a no-op. For more
-    # information see:
-    # https://trac.torproject.org/projects/tor/ticket/2501
-    
     if not self.isLoaded(): returnVal = None
     else:
+      torVersion = torTools.getConn().getVersion()
       skipValidation = not CONFIG["features.torrc.validate"]
-      skipValidation |= not torTools.getConn().isVersion("0.2.2.7-alpha")
+      skipValidation |= (torVersion is None or not torVersion.meets_requirements(stem.version.Requirement.GETINFO_CONFIG_TEXT))
       
       if skipValidation:
         log.log(log.INFO, "Skipping torrc validation (requires tor 0.2.2.7-alpha)")
diff --git a/src/util/torTools.py b/src/util/torTools.py
index 7657dcb..5eb04ad 100644
--- a/src/util/torTools.py
+++ b/src/util/torTools.py
@@ -256,64 +256,6 @@ def getBsdJailId():
   log.log(CONFIG["log.unknownBsdJailId"], "Failed to figure out the FreeBSD jail id. Assuming 0.")
   return 0
 
-def parseVersion(versionStr):
-  """
-  Parses the given version string into its expected components, for instance...
-  '0.2.2.13-alpha (git-feb8c1b5f67f2c6f)'
-  
-  would provide:
-  (0, 2, 2, 13, 'alpha')
-  
-  If the input isn't recognized then this returns None.
-  
-  Arguments:
-    versionStr - version string to be parsed
-  """
-  
-  # crops off extra arguments, for instance:
-  # '0.2.2.13-alpha (git-feb8c1b5f67f2c6f)' -> '0.2.2.13-alpha'
-  versionStr = versionStr.split()[0]
-  
-  result = None
-  if versionStr.count(".") in (2, 3):
-    # parses the optional suffix ('alpha', 'release', etc)
-    if versionStr.count("-") == 1:
-      versionStr, versionSuffix = versionStr.split("-")
-    else: versionSuffix = ""
-    
-    # Parses the numeric portion of the version. This can have three or four
-    # entries depending on if an optional patch level was provided.
-    try:
-      versionComp = [int(entry) for entry in versionStr.split(".")]
-      if len(versionComp) == 3: versionComp += [0]
-      result = tuple(versionComp + [versionSuffix])
-    except ValueError: pass
-  
-  return result
-
-def isVersion(myVersion, minVersion):
-  """
-  Checks if the given version meets a given minimum. Both arguments are
-  expected to be version tuples. To get this from a version string use the
-  parseVersion function.
-  
-  Arguments:
-    myVersion  - tor version tuple
-    minVersion - version tuple to be checked against
-  """
-  
-  if myVersion[:4] == minVersion[:4]:
-    return True # versions match
-  else:
-    # compares each of the numeric portions of the version
-    for i in range(4):
-      myVal, minVal = myVersion[i], minVersion[i]
-      
-      if myVal > minVal: return True
-      elif myVal < minVal: return False
-    
-    return True # versions match (should have been caught above...)
-
 def isTorRunning():
   """
   Simple check for if a tor process is running. If this can't be determined
@@ -959,39 +901,23 @@ class Controller(TorCtl.PostEventListener):
     
     return self._getRelayAttr("flags", default)
   
-  def isVersion(self, minVersionStr):
+  def getVersion(self):
     """
-    Checks if we meet the given version. Recognized versions are of the form:
-    <major>.<minor>.<micro>[.<patch>][-<status_tag>]
-    
-    for instance, "0.2.2.13-alpha" or "0.2.1.5". This raises a ValueError if
-    the input isn't recognized, and returns False if unable to fetch our
-    instance's version.
-    
-    According to the spec the status_tag is purely informal, so it's ignored
-    in comparisons.
-    
-    Arguments:
-      minVersionStr - version to be compared against
+    Provides the version of our tor instance, this is None if we don't have a
+    connection.
     """
     
-    minVersion = parseVersion(minVersionStr)
-    
-    if minVersion == None:
-      raise ValueError("unrecognized version: %s" % minVersionStr)
-    
     self.connLock.acquire()
     
-    result = False
-    if self.isAlive():
-      myVersion = parseVersion(self.getInfo("version", ""))
-      
-      if not myVersion: result = False
-      else: result = isVersion(myVersion, minVersion)
-    
-    self.connLock.release()
-    
-    return result
+    try:
+      return self.controller.get_version()
+    except stem.SocketClosed, exc:
+      self.close()
+      return None
+    except:
+      return None
+    finally:
+      self.connLock.release()
   
   def isGeoipUnavailable(self):
     """





More information about the tor-commits mailing list