-
Notifications
You must be signed in to change notification settings - Fork 7
/
version.py
69 lines (57 loc) · 1.59 KB
/
version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import re
V_SPLIT = re.compile(r'(\D+)')
V_DIGITS = re.compile(r'\d+')
V_DESIGNATOR = re.compile(r'-?([a-z]+)(\d*)$')
def extend_list(l, size, defval):
while len(l) < size:
l.append(defval)
return l
def clean(x):
if x[0] == '-':
return x[1:]
return x
def canonical_designator(des):
m = V_DESIGNATOR.search(des)
return "%-2s%03d" % (m.group(1),
len(m.group(2)) > 0 and int(m.group(2)) or 0)
def split(x):
has_designator = V_DESIGNATOR.search(x)
base = x[0:has_designator.start()] if has_designator else x
designator = has_designator and canonical_designator(has_designator.group())
pieces = [clean(x) for x in V_SPLIT.split(base)
if x != '' and x != '.']
numbers = extend_list([int(x) for x in pieces], 3, 0)
numbers.append(designator or 'z999')
return numbers
def version_less_than(a, b):
"""Return True if a < b.
>>> version_less_than('0.4', '0.4.0')
False
>>> version_less_than('0.3', '0.3.1')
True
>>> version_less_than('0.9', '0.10')
True
>>> version_less_than('0.10', '0.10a')
False
>>> version_less_than('0.10a', '0.10')
True
>>> version_less_than('0.10a', '0.10-b')
True
>>> version_less_than('0.10a', '0.10-a1')
True
>>> version_less_than('0.9-rc2', '0.9')
True
"""
return split(a) < split(b)
def version_match(a, b):
"""Return True if the two versions match for the major and minor version
numbers.
>>> version_match('0.9-a', '0.9.5')
True
>>> version_match('0.8', '0.9')
False
"""
return split(a)[0:2] == split(b)[0:2]
if __name__ == '__main__':
import doctest
doctest.testmod()