Add compare_versions method to common_lib/utils

Signed-off-by: John Huang <[email protected]>

--- autotest/client/common_lib/utils.py 2010-04-12 09:44:38.000000000 -0700
+++ autotest/client/common_lib/utils.py 2010-04-12 09:44:38.000000000 -0700
@@ -1246,3 +1246,22 @@
     command = command.replace('"', r'\"')
     command = command.replace('`', r'\`')
     return command
+
+
+def compare_versions(ver1, ver2):
+    """Compares two dot-delimited version strings.
+
+    Version number comparison between ver1 and ver2 of the form 'x.y.z'
+
+    Args:
+        ver1: dot-delimited string in 'x.y.z' form
+        ver2: dot-delimited string in 'x.y.z' form
+
+    Returns:
+        int:  1 if ver1 >  ver2
+              0 if ver1 == ver2
+             -1 if ver1 <  ver2
+    """
+    v1 = [int(x) for x in ver1.split('.')]
+    v2 = [int(x) for x in ver2.split('.')]
+    return cmp(v1, v2)
--- autotest/client/common_lib/utils_unittest.py        2010-04-12 
09:44:38.000000000 -0700
+++ autotest/client/common_lib/utils_unittest.py        2010-04-12 
09:44:38.000000000 -0700
@@ -710,5 +710,18 @@
         self.assertRaises(TypeError, utils.run, 'echo', args='hello')
 
 
+class test_compare_versions(unittest.TestCase):
+    def test_less_than(self):
+        self.assertEqual(utils.compare_versions('1.2.3', '1.7.5'), -1)
+
+
+    def test_equal(self):
+        self.assertEqual(utils.compare_versions('1.2.3', '1.2.3'), 0)
+
+
+    def test_greater_than(self):
+        self.assertEqual(utils.compare_versions('1.3.3', '1.2.3'), 1)
+
+
 if __name__ == "__main__":
     unittest.main()
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to