-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathversion.py
138 lines (123 loc) · 3.94 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
This module contains comparison functions for semantic version strings.
"""
def _parse_version(version_string):
"""
Parses version string and returns a list of integers.
"""
version_split = version_string.split('.')
return [int(x) for x in version_split]
def equals(first_version, second_version):
"""
Checks if two version strings are equal.
Returns boolean True or False.
"""
if first_version == second_version:
return True
else:
return False
def greaterthan(version_one, version_two):
"""
Checks if version_one is strictly greater than version_two.
Returns boolean True or False.
"""
# Parse strings for integer comparison
a_version = _parse_version(version_one)
b_version = _parse_version(version_two)
# Additional variables to enhance code readability
# and append a zero if only major and minor are defined.
if len(a_version) == 3 and len(b_version) == 3:
a_major, a_minor, a_patch = a_version
b_major, b_minor, b_patch = b_version
elif len(a_version) == 2 and len(b_version) == 3:
a_major, a_minor = a_version
b_major, b_minor, b_patch = b_version
a_patch = 0
elif len(a_version) == 3 and len(b_version) == 2:
a_major, a_minor, a_patch = a_version
b_major, b_minor = b_version
b_patch = 0
else:
a_major, a_minor = a_version
b_major, b_minor = b_version
a_patch, b_patch = 0, 0
# Compare versions
if a_major > b_major:
return True
elif (a_major == b_major or
a_major > b_major) and \
a_minor > b_minor:
return True
elif ((a_major == b_major or
a_major > b_major) and \
(a_minor == b_minor or \
a_minor > b_minor)) and \
a_patch > b_patch:
return True
else:
return False
def greaterthan_or_equal(version_one, version_two):
"""
Checks if version_one is greater than or equal to version_two.
Returns boolean True or False.
"""
if greaterthan(version_one, version_two) or \
equals(version_one, version_two):
return True
else:
return False
def lessthan(version_one, version_two):
"""
Checks if version_one is strictly less than version_two.
Returns boolean True or False.
"""
if not greaterthan_or_equal(version_one, version_two):
return True
else:
return False
def lessthan_or_equal(version_one, version_two):
"""
Checks if version_one is less than or equal to version_two.
Returns boolean True or False.
"""
if not greaterthan(version_one, version_two):
return True
else:
return False
def pessimistic(version_one, version_two):
"""
Checks if version_one is within pessimistic versioning range
of version_two. Two or three digit versions are acceptable but
must be consistent.
e.g., pessimistic("2.1.23", "2.1.2") returns True
pessimistic("2.2.23", "2.1.2") returns False
pessimistic("2.24", "2.1") returns True
pessimistic("3.0", "2.1") returns False
"""
# Parse strings for integer comparison
a_version = _parse_version(version_one)
b_version = _parse_version(version_two)
# Additional variables to enhance code readability
if len(a_version) == 3:
a_major, a_minor, a_patch = a_version
b_major, b_minor, b_patch = b_version
else:
a_major, a_minor = a_version
b_major, b_minor = b_version
# Check 3-digit formats
if len(a_version) == 3:
if (a_major == b_major) and \
(a_minor == b_minor):
if a_patch > b_patch:
return True
else:
return False
else:
return False
# Check 2-digit formats
else:
if (a_major == b_major) and \
(a_minor > b_minor):
return True
else:
return False