remove TODO, empathy has been added
[check-version.git] / version.py
blob9b9040baf43a3ba75f6db60848df71c5578c5e10
1 class Version(object):
2 def __init__(self, ver, debian=False):
3 if debian and ver:
4 # remove epoch and debian part
5 if ':' in ver:
6 ver = ver.split(':', 1)[1]
7 if '-' in ver:
8 ver = ver.split('-', 1)[0]
9 self.ver = ver
11 def __cmp__(self, other):
12 if other is None or other.ver is None:
13 return 1
14 if self.ver is None:
15 return -1
16 if other.ver == self.ver:
17 return 0
19 nb1 = map(int, self.ver.split('.'))
20 nb2 = map(int, other.ver.split('.'))
22 for i, j in zip(nb1, nb2):
23 if i < j:
24 return -1
25 elif i > j:
26 return 1
28 if len(nb1) < len(nb2):
29 return -1
30 elif len(nb1) > len(nb2):
31 return 1
33 return 0
35 def __str__(self):
36 return self.ver
38 if __name__ == '__main__':
39 assert Version('1.0') < Version('2.0')
40 assert Version('1.0') < Version('2.0')
41 assert Version('1.0') != Version('2.0')
42 assert Version('1.0') == Version('1.0')
43 assert Version('1.1.0') < Version('2.0')
44 assert Version('1.1.2') < Version('1.1.3')
45 assert Version('1.3') < Version('1.3.1')
46 assert max(Version('1.0'), Version('1.5'), Version('1.5.2')) == Version('1.5.2')
47 assert max(Version('0.7.9'), Version('0.7.18')) == Version('0.7.18')
48 assert Version(None) < Version('1.3.1')
49 assert Version('1.4.1-3', debian=True) == Version('1.4.1')