[tor-commits] [stem/master] Including tor's git commit in Version

atagar at torproject.org atagar at torproject.org
Sun Jul 22 05:48:17 UTC 2012


commit 63519a16c31b6478e912c41cd7d47e1fcb634750
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Jul 21 22:28:41 2012 -0700

    Including tor's git commit in Version
    
    Expanding the Version class to include an 'extra' and 'git_commit' attribute
    that reflected a proposed expansion of the spec...
    https://trac.torproject.org/6445
    
    This is already the de-facto form of tor versions so moving ahead with
    including it in the Version class.
---
 stem/control.py      |    7 +------
 stem/version.py      |   19 ++++++++++++++++---
 test/unit/version.py |   42 +++++++++++++++++++++++++++++-------------
 3 files changed, 46 insertions(+), 22 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index d7d90c9..d687e1a 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -520,12 +520,7 @@ class Controller(BaseController):
       * ValueError if unable to parse the version
     """
     
-    version_str = self.get_info("version")
-    
-    if " " in version_str:
-      version_str = version_str[:version_str.find(' ')]
-    
-    return stem.version.Version(version_str)
+    return stem.version.Version(self.get_info("version"))
   
   def authenticate(self, *args, **kwargs):
     """
diff --git a/stem/version.py b/stem/version.py
index 087efa9..130ca1c 100644
--- a/stem/version.py
+++ b/stem/version.py
@@ -69,7 +69,7 @@ def get_system_tor_version(tor_cmd = "tor"):
       
       if last_line.startswith("Tor version ") and last_line.endswith("."):
         try:
-          version_str = last_line[12:last_line.find(' ', 12)]
+          version_str = last_line[12:-1]
           VERSION_CACHE[tor_cmd] = Version(version_str)
         except ValueError, exc:
           raise IOError(exc)
@@ -92,6 +92,8 @@ class Version(object):
   :var int micro: micro version
   :var int patch: optional patch level (None if undefined)
   :var str status: optional status tag without the preceding dash such as 'alpha', 'beta-dev', etc (None if undefined)
+  :var str extra: optional extra information without the proceeding space nor its parentheses such as 'git-8be6058d8f31e578' (None if undefined)
+  :var str git_commit: optional git commit id (None if it wasn't provided)
   
   :param str version_str: version to be parsed
   
@@ -100,22 +102,33 @@ class Version(object):
   
   def __init__(self, version_str):
     self.version_str = version_str
-    version_parts = re.match(r'^([0-9]+)\.([0-9]+)\.([0-9]+)(\.[0-9]+)?(-\S*)?$', version_str)
+    version_parts = re.match(r'^([0-9]+)\.([0-9]+)\.([0-9]+)(\.[0-9]+)?(-\S*)?( \(\S*\))?$', version_str)
     
     if version_parts:
-      major, minor, micro, patch, status = version_parts.groups()
+      major, minor, micro, patch, status, extra = version_parts.groups()
       
       # The patch and status matches are optional (may be None) and have an extra
       # proceeding period or dash if they exist. Stripping those off.
       
+      # TODO: The 'extra' attribute isn't technically part of the spec yet, but
+      # it's useful and I'm trying to add it...
+      # https://trac.torproject.org/6445
+      
       if patch: patch = int(patch[1:])
       if status: status = status[1:]
+      if extra: extra = extra[2:-1]
       
       self.major = int(major)
       self.minor = int(minor)
       self.micro = int(micro)
       self.patch = patch
       self.status = status
+      self.extra = extra
+      
+      if extra and re.match("^git-[0-9a-f]{16}$", extra):
+        self.git_commit = extra[4:]
+      else:
+        self.git_commit = None
     else: raise ValueError("'%s' isn't a properly formatted tor version" % version_str)
   
   def meets_requirements(self, requirements):
diff --git a/test/unit/version.py b/test/unit/version.py
index cfda02c..f32fb7a 100644
--- a/test/unit/version.py
+++ b/test/unit/version.py
@@ -33,7 +33,8 @@ class TestVersion(unittest.TestCase):
     
     mocking.mock(stem.util.system.call, _mock_call)
     version = stem.version.get_system_tor_version()
-    self.assert_versions_match(version, 0, 2, 2, 35, None)
+    self.assert_versions_match(version, 0, 2, 2, 35, None, "git-73ff13ab3cc9570d")
+    self.assertEqual("73ff13ab3cc9570d", version.git_commit)
     
     stem.version.VERSION_CACHE = {}
   
@@ -45,23 +46,34 @@ class TestVersion(unittest.TestCase):
     # valid versions with various number of compontents to the version
     
     version = Version("0.1.2.3-tag")
-    self.assert_versions_match(version, 0, 1, 2, 3, "tag")
+    self.assert_versions_match(version, 0, 1, 2, 3, "tag", None)
     
     version = Version("0.1.2.3")
-    self.assert_versions_match(version, 0, 1, 2, 3, None)
+    self.assert_versions_match(version, 0, 1, 2, 3, None, None)
     
     version = Version("0.1.2-tag")
-    self.assert_versions_match(version, 0, 1, 2, None, "tag")
+    self.assert_versions_match(version, 0, 1, 2, None, "tag", None)
     
     version = Version("0.1.2")
-    self.assert_versions_match(version, 0, 1, 2, None, None)
+    self.assert_versions_match(version, 0, 1, 2, None, None, None)
     
     # checks an empty tag
     version = Version("0.1.2.3-")
-    self.assert_versions_match(version, 0, 1, 2, 3, "")
+    self.assert_versions_match(version, 0, 1, 2, 3, "", None)
     
     version = Version("0.1.2-")
-    self.assert_versions_match(version, 0, 1, 2, None, "")
+    self.assert_versions_match(version, 0, 1, 2, None, "", None)
+    
+    # check with extra informaton
+    version = Version("0.1.2.3-tag (git-73ff13ab3cc9570d)")
+    self.assert_versions_match(version, 0, 1, 2, 3, "tag", "git-73ff13ab3cc9570d")
+    self.assertEqual("73ff13ab3cc9570d", version.git_commit)
+    
+    version = Version("0.1.2.3-tag ()")
+    self.assert_versions_match(version, 0, 1, 2, 3, "tag", "")
+    
+    version = Version("0.1.2 (git-73ff13ab3cc9570d)")
+    self.assert_versions_match(version, 0, 1, 2, None, None, "git-73ff13ab3cc9570d")
     
     # checks invalid version strings
     self.assertRaises(ValueError, stem.version.Version, "")
@@ -197,17 +209,21 @@ class TestVersion(unittest.TestCase):
     for i in xrange(0, 100):
       self.assertFalse(Version("0.2.2.%i" % i).meets_requirements(requirements))
   
-  def assert_versions_match(self, version, major, minor, micro, patch, status):
+  def assert_versions_match(self, version, major, minor, micro, patch, status, extra):
     """
     Asserts that the values for a types.Version instance match the given
     values.
     """
     
-    self.assertEqual(version.major, major)
-    self.assertEqual(version.minor, minor)
-    self.assertEqual(version.micro, micro)
-    self.assertEqual(version.patch, patch)
-    self.assertEqual(version.status, status)
+    self.assertEqual(major, version.major)
+    self.assertEqual(minor, version.minor)
+    self.assertEqual(micro, version.micro)
+    self.assertEqual(patch, version.patch)
+    self.assertEqual(status, version.status)
+    self.assertEqual(extra, version.extra)
+    
+    if extra is None:
+      self.assertEqual(None, version.git_commit)
   
   def assert_version_is_greater(self, first_version, second_version):
     """



More information about the tor-commits mailing list