make version comparison compat with python2 and python3

Alas, python3 doesn't have the cmp() function.

Support both python2 and python3 for comparisons using tip from
https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons

Avoids TB on python3:

    ======================================================================
    ERROR: test_compare_version (unit.common-tests.ExtractBugzillasTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/sandbox/test/unit/common-tests.py", line 224, in test_compare_version
        self.assertEquals(0, compare_version("1", "1"))
      File "/home/sandbox/src/tito/common.py", line 689, in compare_version
        return cmp(normalize(version1), normalize(version2))
    NameError: global name 'cmp' is not defined
This commit is contained in:
Paul Morgan 2014-05-12 12:19:07 +00:00
parent b4c103ea4f
commit 576d46345f
2 changed files with 4 additions and 1 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)