[asterisk-commits] russell: testsuite/asterisk/trunk r90 - /asterisk/trunk/asteriskversion.py

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Mar 17 17:28:20 CDT 2010


Author: russell
Date: Wed Mar 17 17:28:16 2010
New Revision: 90

URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=90
Log:
Start adding ability to compare between Asterisk versions

Modified:
    asterisk/trunk/asteriskversion.py

Modified: asterisk/trunk/asteriskversion.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/asteriskversion.py?view=diff&rev=90&r1=89&r2=90
==============================================================================
--- asterisk/trunk/asteriskversion.py (original)
+++ asterisk/trunk/asteriskversion.py Wed Mar 17 17:28:16 2010
@@ -29,6 +29,35 @@
 
     def __str__(self):
         return self.version_str
+
+    def __cmp__(self, other):
+        if self.svn or other.svn:
+            print "ERROR: SVN version comparisons not yet implemented."
+            return 0
+        else:
+            res = self.__cmp_version_part(self.concept, other.concept)
+            if res != 0:
+                return res
+
+            res = self.__cmp_version_part(self.major, other.major)
+            if res != 0:
+                return res
+
+            res = self.__cmp_version_part(self.minor, other.minor)
+            if res != 0:
+                return res
+
+            return self.__cmp_version_part(self.patch, other.patch)
+
+    @staticmethod
+    def __cmp_version_part(selfpart, otherpart):
+        if selfpart is None and otherpart is None:
+            return 0
+        if selfpart is None:
+            return -1
+        if otherpart is None:
+            return 1
+        return cmp(selfpart, otherpart)
 
     def __parse_version(self):
         self.svn = False
@@ -37,9 +66,14 @@
         self.major = parts[1]
         try:
             self.minor = parts[2]
-            self.patch = parts[3]
         except:
-            pass
+            self.minor = None
+            self.patch = None
+        if self.minor is not None:
+            try:
+                self.patch = parts[3]
+            except:
+                self.patch = None
 
     def __parse_svn_version(self):
         self.svn = True
@@ -126,6 +160,41 @@
         self.assertEqual(v.branch, "russell-rest")
         self.assertEqual(v.revision, "12345")
 
+    def test_cmp(self):
+        v1 = AsteriskVersion("1.4")
+        v2 = AsteriskVersion("1.6.0")
+        self.assertTrue(v1 < v2)
+
+    def test_cmp2(self):
+        v1 = AsteriskVersion("1.4")
+        v2 = AsteriskVersion("1.4.15")
+        self.assertTrue(v1 < v2)
+
+    def test_cmp3(self):
+        v1 = AsteriskVersion("1.4.30")
+        v2 = AsteriskVersion("1.4.30.1")
+        self.assertTrue(v1 < v2)
+
+    def test_cmp4(self):
+        v1 = AsteriskVersion("1.4")
+        v2 = AsteriskVersion("1.4")
+        self.assertTrue(v1 == v2)
+
+    def test_cmp5(self):
+        v1 = AsteriskVersion("1.6.0")
+        v2 = AsteriskVersion("1.6.0")
+        self.assertTrue(v1 == v2)
+
+    def test_cmp6(self):
+        v1 = AsteriskVersion("1.6.0.10")
+        v2 = AsteriskVersion("1.6.0.10")
+        self.assertTrue(v1 == v2)
+
+    def test_cmp7(self):
+        v1 = AsteriskVersion("1.8")
+        v2 = AsteriskVersion("2.0")
+        self.assertTrue(v1 < v2)
+
 
 def main():
     unittest.main()




More information about the asterisk-commits mailing list