Move Bzr.version_cmp to VCS.version_cmp.
authorW. Trevor King <wking@drexel.edu>
Thu, 8 Sep 2011 02:47:14 +0000 (22:47 -0400)
committerW. Trevor King <wking@drexel.edu>
Thu, 8 Sep 2011 02:53:21 +0000 (22:53 -0400)
The version comparison code will be useful for all VCSs.

libbe/storage/vcs/base.py
libbe/storage/vcs/bzr.py

index d37739863702d1dbd5089e2bb8ed08e6e1718e60..22874a5a12f24d990fb218091d112e5ec458b0d7 100644 (file)
@@ -521,6 +521,97 @@ class VCS (libbe.storage.base.VersionedStorage):
             self._version = self._vcs_version()
         return self._version
 
+    def version_cmp(self, *args):
+        """Compare the installed VCS version `V_i` with another version
+        `V_o` (given in `*args`).  Returns
+
+           === ===============
+            1  if `V_i > V_o`
+            0  if `V_i == V_o`
+           -1  if `V_i < V_o`
+           === ===============
+
+        Examples
+        --------
+
+        >>> v = VCS(repo='.')
+        >>> v._version = '2.3.1 (release)'
+        >>> v.version_cmp(2,3,1)
+        0
+        >>> v.version_cmp(2,3,2)
+        -1
+        >>> v.version_cmp(2,3,'a',5)
+        1
+        >>> v.version_cmp(2,3,0)
+        1
+        >>> v.version_cmp(2,3,1,'a',5)
+        1
+        >>> v.version_cmp(2,3,1,1)
+        -1
+        >>> v.version_cmp(3)
+        -1
+        >>> v._version = '2.0.0pre2'
+        >>> v._parsed_version = None
+        >>> v.version_cmp(3)
+        -1
+        >>> v.version_cmp(2,0,1)
+        -1
+        >>> v.version_cmp(2,0,0,'pre',1)
+        1
+        >>> v.version_cmp(2,0,0,'pre',2)
+        0
+        >>> v.version_cmp(2,0,0,'pre',3)
+        -1
+        >>> v.version_cmp(2,0,0,'a',3)
+        1
+        >>> v.version_cmp(2,0,0,'rc',1)
+        -1
+        """
+        if not hasattr(self, '_parsed_version') \
+                or self._parsed_version == None:
+            num_part = self.version().split(' ')[0]
+            self._parsed_version = []
+            for num in num_part.split('.'):
+                try:
+                    self._parsed_version.append(int(num))
+                except ValueError, e:
+                    # bzr version number might contain non-numerical tags
+                    splitter = re.compile(r'[\D]') # Match non-digits
+                    splits = splitter.split(num)
+                    # if len(tag) > 1 some splits will be empty; remove
+                    splits = filter(lambda s: s != '', splits)
+                    tag_starti = len(splits[0])
+                    num_starti = num.find(splits[1], tag_starti)
+                    tag = num[tag_starti:num_starti]
+                    self._parsed_version.append(int(splits[0]))
+                    self._parsed_version.append(tag)
+                    self._parsed_version.append(int(splits[1]))
+        for current,other in zip(self._parsed_version, args):
+            if type(current) != type (other):
+                # one of them is a pre-release string
+                if type(current) != types.IntType:
+                    return -1
+                else:
+                    return 1
+            c = cmp(current,other)
+            if c != 0:
+                return c
+        # see if one is longer than the other
+        verlen = len(self._parsed_version)
+        arglen = len(args)
+        if verlen == arglen:
+            return 0
+        elif verlen > arglen:
+            if type(self._parsed_version[arglen]) != types.IntType:
+                return -1 # self is a prerelease
+            else:
+                return 1
+        else:
+            if type(args[verlen]) != types.IntType:
+                return 1 # args is a prerelease
+            else:
+                return -1
+
     def installed(self):
         if self.version() != None:
             return True
index 6b625c20573ca66e4bf829081bf765cdd4fa7311..c01b6c7f0f97769f85a9e75bbdc184b3518a081d 100644 (file)
@@ -40,7 +40,6 @@ import re
 import shutil
 import StringIO
 import sys
-import types
 
 import libbe
 import base
@@ -68,97 +67,6 @@ class Bzr(base.VCS):
             return None
         return bzrlib.__version__
 
-    def version_cmp(self, *args):
-        """Compare the installed Bazaar version `V_i` with another version
-        `V_o` (given in `*args`).  Returns
-
-           === ===============
-            1  if `V_i > V_o`
-            0  if `V_i == V_o`
-           -1  if `V_i < V_o`
-           === ===============
-
-        Examples
-        --------
-
-        >>> b = Bzr(repo='.')
-        >>> b._version = '2.3.1 (release)'
-        >>> b.version_cmp(2,3,1)
-        0
-        >>> b.version_cmp(2,3,2)
-        -1
-        >>> b.version_cmp(2,3,'a',5)
-        1
-        >>> b.version_cmp(2,3,0)
-        1
-        >>> b.version_cmp(2,3,1,'a',5)
-        1
-        >>> b.version_cmp(2,3,1,1)
-        -1
-        >>> b.version_cmp(3)
-        -1
-        >>> b._version = '2.0.0pre2'
-        >>> b._parsed_version = None
-        >>> b.version_cmp(3)
-        -1
-        >>> b.version_cmp(2,0,1)
-        -1
-        >>> b.version_cmp(2,0,0,'pre',1)
-        1
-        >>> b.version_cmp(2,0,0,'pre',2)
-        0
-        >>> b.version_cmp(2,0,0,'pre',3)
-        -1
-        >>> b.version_cmp(2,0,0,'a',3)
-        1
-        >>> b.version_cmp(2,0,0,'rc',1)
-        -1
-        """
-        if not hasattr(self, '_parsed_version') \
-                or self._parsed_version == None:
-            num_part = self.version().split(' ')[0]
-            self._parsed_version = []
-            for num in num_part.split('.'):
-                try:
-                    self._parsed_version.append(int(num))
-                except ValueError, e:
-                    # bzr version number might contain non-numerical tags
-                    splitter = re.compile(r'[\D]') # Match non-digits
-                    splits = splitter.split(num)
-                    # if len(tag) > 1 some splits will be empty; remove
-                    splits = filter(lambda s: s != '', splits)
-                    tag_starti = len(splits[0])
-                    num_starti = num.find(splits[1], tag_starti)
-                    tag = num[tag_starti:num_starti]
-                    self._parsed_version.append(int(splits[0]))
-                    self._parsed_version.append(tag)
-                    self._parsed_version.append(int(splits[1]))
-        for current,other in zip(self._parsed_version, args):
-            if type(current) != type (other):
-                # one of them is a pre-release string
-                if type(current) != types.IntType:
-                    return -1
-                else:
-                    return 1
-            c = cmp(current,other)
-            if c != 0:
-                return c
-        # see if one is longer than the other
-        verlen = len(self._parsed_version)
-        arglen = len(args)
-        if verlen == arglen:
-            return 0
-        elif verlen > arglen:
-            if type(self._parsed_version[arglen]) != types.IntType:
-                return -1 # self is a prerelease
-            else:
-                return 1
-        else:
-            if type(args[verlen]) != types.IntType:
-                return 1 # args is a prerelease
-            else:
-                return -1
-
     def _vcs_get_user_id(self):
         # excerpted from bzrlib.builtins.cmd_whoami.run()
         try: