forked from brownbat/autoEaseFactor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semver.py
175 lines (153 loc) · 6.38 KB
/
semver.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import re
import string
# This is an implementation of semver, detailed here:
# https://semver.org/
# The Semantic Versioning specification was originally authored by Tom
# Preston-Werner, inventor of Gravatar and cofounder of GitHub.
# Creative Commons ― CC BY 3.0
# https://creativecommons.org/licenses/by/3.0/
valid_semver_regex = (r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-"
r"9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*"
r"[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0"
r"-9a-zA-Z-]+)*))?$")
class Version:
def __init__(self, version="0.0.0"):
version = str(version)
assert re.match(valid_semver_regex, version)
self.version = str(version)
self.version_string = str(version)
self.version_list = version.split(".", 2)
self.major, self.minor, self.full_patch = self.version_list[:3]
self.patch = self.full_patch
self.build = ""
self.prerelease = ""
self.prerelease_parts = []
if '+' in self.patch:
self.patch, self.build = self.patch.split('+')
if '-' in self.patch:
self.prerelease = self.patch[self.patch.find('-')+1:]
self.patch = self.patch[:self.patch.find('-')]
if '.' in self.prerelease:
self.prerelease_parts = self.prerelease.split('.')
self.version_list = [self.major, self.minor, self.patch,
self.prerelease]
def __str__(self):
return self.version_string
@staticmethod
def valid(v):
return re.match(valid_semver_regex, str(v))
@staticmethod
def is_numeric(s):
retval = True
for l in s:
if l not in string.digits:
retval = False
return retval
def __lt__(self, other):
if type(other) is str:
other = Version(other)
assert self.valid(other)
retval = False
retval = int(self.major) < int(other.major)
if int(self.major) == int(other.major):
retval = int(self.minor) < int(other.minor)
if int(self.minor) == int(other.minor):
retval = int(self.patch) < int(other.patch)
if int(self.patch) == int(other.patch):
final = False
# no prerelease fields
if (len(self.prerelease_parts) == 0 and
len(other.prerelease_parts) == 0):
final = True
# larger set of prerelease fields takes higher precedence
if (len(self.prerelease_parts) < len(other.prerelease_parts)
and other.prerelease.find(self.prerelease) == 0):
retval = True
final = True
# unless one has no prerelease fields
if len(self.prerelease_parts) == 0:
retval = False
elif (len(self.prerelease) > len(other.prerelease)
and self.prerelease.find(other.prerelease) == 0):
retval = False
final = True
if len(other.prerelease_parts) == 0:
retval = True
else:
# iterate through dotted pr fields
for p, q in zip(self.prerelease.split('.'),
other.prerelease.split('.')):
# ignore if comparison already resolved
if not final:
# compare purely numeric fields
if self.is_numeric(p) and self.is_numeric(q):
if int(p) < int(q):
retval = True
final = True
elif int(p) > int(q):
retval = False
final = True
# compare alphanum fields
elif (not self.is_numeric(p)
and not self.is_numeric(q)):
if p < q:
retval = True
final = True
elif p > q:
retval = False
final = True
# numeric lower than alphanum
else:
if self.is_numeric(p):
retval = True
final = True
else:
retval = False
final = True
return retval
def __eq__(self, other):
return str(self).split('+')[0] == str(other).split('+')[0]
def __le__(self, other):
return self < other or self == other
def __ne__(self, other):
return not self == other
def __gt__(self, other):
return not self <= other
def __ge__(self, other):
return not self < other
examples = []
examples.append(Version())
examples.append(Version("1.0.0-alpha"))
examples.append(Version("1.0.0-alpha.1"))
examples.append(Version("1.0.0-alpha.beta"))
examples.append(Version("1.0.0-beta"))
examples.append(Version("1.0.0-beta.2"))
examples.append(Version("1.0.0-beta.11"))
examples.append(Version("1.0.0-rc.1"))
examples.append(Version("1.0.0"))
examples.append(Version("2.0.0"))
examples.append(Version("2.1.0"))
examples.append(Version("2.1.1"))
examples.append(Version("2.1.26"))
examples.append(Version("2.1.26"))
# examples.append(Version("4.1")) # invalid according to semver guidelines
def verbose(a):
for i in a.__dict__:
print(i, a.__dict__[i])
def test(a, b):
if a < b:
print(f"{a} < {b}")
if a <= b:
print(f"{a} <= {b}")
if a == b:
print(f"{a} == {b}")
if a != b:
print(f"{a} != {b}")
if a > b:
print(f"{a} > {b}")
if a >= b:
print(f"{a} >= {b}")
print()
if __name__ == "__main__":
for pair in zip(examples[:-1], examples[1:]):
test(*pair)