Merge pull request #127 from jumanjiman/python3_cmp

make version comparison compat with python2 and python3
This commit is contained in:
Devan Goodwin 2014-05-12 10:59:30 -03:00
commit cb6985b205
3 changed files with 22 additions and 19 deletions

View file

@ -10,6 +10,7 @@ Python versions
Tito supports Python versions 2.4 (RHEL 5) and up.
See http://docs.python.org/dev/howto/pyporting.html
and http://python3porting.com/differences.html
and https://docs.python.org/3.0/whatsnew/3.0.html
for tips on writing portable Python code.
In particular, you must capture exceptions in a way that's

View file

@ -686,4 +686,6 @@ def compare_version(version1, version2):
"""
def normalize(v):
return [int(x) for x in re.sub(r'(\.0+)*$', '', v).split(".")]
return cmp(normalize(version1), normalize(version2))
a = normalize(version1)
b = normalize(version2)
return (a > b) - (a < b)

View file

@ -84,6 +84,24 @@ class CommonTests(unittest.TestCase):
self.assertEquals("fe87e2b75ed1850718d99c797cc171b88bfad5ca",
extract_sha1(ls_remote_output))
def test_compare_version(self):
self.assertEquals(0, compare_version("1", "1"))
self.assertTrue(compare_version("2.1", "2.2") < 0)
self.assertTrue(compare_version("3.0.4.10", "3.0.4.2") > 0)
self.assertTrue(compare_version("4.08", "4.08.01") < 0)
self.assertTrue(compare_version("3.2.1.9.8144", "3.2") > 0)
self.assertTrue(compare_version("3.2", "3.2.1.9.8144") < 0)
self.assertTrue(compare_version("1.2", "2.1") < 0)
self.assertTrue(compare_version("2.1", "1.2") > 0)
self.assertTrue(compare_version("1.0", "1.0.1") < 0)
self.assertTrue(compare_version("1.0.1", "1.0") > 0)
self.assertEquals(0, compare_version("5.6.7", "5.6.7"))
self.assertEquals(0, compare_version("1.01.1", "1.1.1"))
self.assertEquals(0, compare_version("1.1.1", "1.01.1"))
self.assertEquals(0, compare_version("1", "1.0"))
self.assertEquals(0, compare_version("1.0", "1"))
self.assertEquals(0, compare_version("1.0.2.0", "1.0.2"))
class VersionMathTest(unittest.TestCase):
def test_increase_version_minor(self):
@ -219,21 +237,3 @@ class ExtractBugzillasTest(unittest.TestCase):
find_wrote_in_rpmbuild_output(output)
common.error_out.assert_called_once_with("Unable to locate 'Wrote: ' lines in rpmbuild output: '%s'" % output)
def test_compare_version(self):
self.assertEquals(0, compare_version("1", "1"))
self.assertTrue(compare_version("2.1", "2.2") < 0)
self.assertTrue(compare_version("3.0.4.10", "3.0.4.2") > 0)
self.assertTrue(compare_version("4.08", "4.08.01") < 0)
self.assertTrue(compare_version("3.2.1.9.8144", "3.2") > 0)
self.assertTrue(compare_version("3.2", "3.2.1.9.8144") < 0)
self.assertTrue(compare_version("1.2", "2.1") < 0)
self.assertTrue(compare_version("2.1", "1.2") > 0)
self.assertTrue(compare_version("1.0", "1.0.1") < 0)
self.assertTrue(compare_version("1.0.1", "1.0") > 0)
self.assertEquals(0, compare_version("5.6.7", "5.6.7"))
self.assertEquals(0, compare_version("1.01.1", "1.1.1"))
self.assertEquals(0, compare_version("1.1.1", "1.01.1"))
self.assertEquals(0, compare_version("1", "1.0"))
self.assertEquals(0, compare_version("1.0", "1"))
self.assertEquals(0, compare_version("1.0.2.0", "1.0.2"))