This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 271
/
maven.py
286 lines (227 loc) · 8.71 KB
/
maven.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
Maven utilities for handling versions and such
"""
class IntegerVersionItem(object):
_big_integer_zero_ = 0
def __init__(self, value=None):
self.value = int(value) if value else self._big_integer_zero_
def __repr__(self):
return str(self.value)
def __str__(self):
return str(self.value)
def is_null(self):
return self.value == self._big_integer_zero_
def compare_to(self, other):
if not other:
return 0 if self.value == self._big_integer_zero_ else 1
else:
if type(other) == IntegerVersionItem:
if self.value == other.value:
return 0
elif self.value > other.value:
return 1
else:
return -1
elif type(other) in [StringVersionItem, ListVersionItem]:
return 1 # 1.1 > 1-sp 1.1 > 1-1
else:
return NotImplemented
class StringVersionItem(object):
_qualifiers_ = ["alpha", "beta", "milestone", "rc", "snapshot", "", "sp"]
_aliases_ = {"ga": "", "final": "", "cr": "rc"}
_acronyms_ = {"a": "alpha", "b": "beta", "m": "milestone"}
_release_version_index_ = str(_qualifiers_.index(""))
def __init__(self, value, followed_by_digit):
if followed_by_digit and value in self._acronyms_:
value = self._acronyms_[value]
self.value = self._aliases_.get(value, value)
self.cq = self.comparable_qualifier(self.value)
def __repr__(self):
return self.value
def __str__(self):
return self.value
def is_null(self):
return self.cq == self._release_version_index_
def comparable_qualifier(self, qualifier):
return (
str(self._qualifiers_.index(qualifier))
if qualifier in self._qualifiers_
else "{}-{}".format(len(self._qualifiers_), qualifier)
)
def compare_to(self, other):
if not other:
return (
0
if self.cq == self._release_version_index_
else (1 if self.cq > self._release_version_index_ else -1)
)
else:
if type(other) == StringVersionItem:
if self.cq == other.cq:
return 0
elif self.cq > other.cq:
return 1
else:
return -1
elif type(other) in [IntegerVersionItem, ListVersionItem]:
return -1 # 1.any < 1.1 1.any < 1-1
else:
return NotImplemented
class ListVersionItem(list):
def __init__(self):
super(ListVersionItem, self).__init__()
def __repr__(self):
ret = ""
for version_item in self:
if ret:
ret = (
ret
+ ("-" if type(version_item) == ListVersionItem else ".")
+ version_item.__repr__()
)
else:
ret = version_item.__repr__()
return ret
def __str__(self):
return self.__repr__()
def is_null(self):
return len(self) == 0
def normalize(self):
for index in reversed(range(len(self))):
if self[index].is_null():
self.__delitem__(index)
elif type(self[index]) != ListVersionItem:
break
def compare_to(self, other):
if not other:
return 0 if len(self) == 0 else self[0].compare_to(None)
else:
if type(other) == ListVersionItem:
l_iter = self.__iter__()
r_iter = other.__iter__()
left = next(l_iter, None)
right = next(r_iter, None)
while left or right:
if left and type(left) in [
IntegerVersionItem,
StringVersionItem,
ListVersionItem,
]:
result = left.compare_to(right)
elif right and type(right) in [
IntegerVersionItem,
StringVersionItem,
ListVersionItem,
]:
result = -1 * right.compare_to(left)
else:
raise NotImplemented
if result != 0:
return result
left = next(l_iter, None)
right = next(r_iter, None)
return 0
elif type(other) == IntegerVersionItem:
return -1 # 1-1 < 1.0.x
elif type(other) == StringVersionItem:
return 1 # 1-1 > 1-sp
class MavenVersion(object):
"""
Comparable object that takes a string as input and constructs the canonical Maven version. Two MavenVersion objects
a and b can be compared using:
1. instance method compare_to() produces integer result:
a.compare_to(b) = 0, 1 or -1 if a and b are equal, a is greater than b or a is less than b respectively
2. rich comparison operators produce boolean result:
a == b is True if a and are equal
a >= b is True if a is greater than or equal to b
a <= b is True if a is less than or equal to b
Translated into Python from
https://github.com/apache/maven/blob/d92508179410897404bcc7e826bb0877be2d08b8/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java
"""
def __init__(self, version):
self.value = version
self.items = self._parse_version_(version)
self.canonical = self.items.__repr__()
def __repr__(self):
return self.canonical
def __str__(self):
return self.canonical
def __hash__(self):
return hash(self.canonical)
def __eq__(self, other):
return True if self.compare_to(other) == 0 else False
def __ne__(self, other):
return True if self.compare_to(other) != 0 else False
def __gt__(self, other):
return True if self.compare_to(other) > 0 else False
def __lt__(self, other):
return True if self.compare_to(other) < 0 else False
def __ge__(self, other):
return True if self.compare_to(other) >= 0 else False
def __le__(self, other):
return True if self.compare_to(other) <= 0 else False
@staticmethod
def _get_version_item_(is_digit, version_ss):
return (
IntegerVersionItem(version_ss)
if is_digit
else StringVersionItem(version_ss, False)
)
@staticmethod
def _parse_version_(version):
ver = str(version).strip().lower()
items = ListVersionItem()
wlist = items
stack = list()
stack.append(wlist)
is_digit = False
start_index = 0
for c, i in zip(ver, range(len(ver))):
if c == ".":
if i == start_index:
wlist.append(IntegerVersionItem())
else:
wlist.append(
MavenVersion._get_version_item_(is_digit, ver[start_index:i])
)
start_index = i + 1
elif c == "-":
if i == start_index:
wlist.append(IntegerVersionItem())
else:
wlist.append(
MavenVersion._get_version_item_(is_digit, ver[start_index:i])
)
start_index = i + 1
wlist = ListVersionItem()
items.append(wlist)
stack.append(wlist)
elif str.isdigit(c):
if not is_digit and i > start_index:
wlist.append(StringVersionItem(ver[start_index:i], True))
start_index = i
wlist = ListVersionItem()
items.append(wlist)
stack.append(wlist)
is_digit = True
else:
if is_digit and i > start_index:
wlist.append(
MavenVersion._get_version_item_(True, ver[start_index:i])
)
start_index = i
wlist = ListVersionItem()
items.append(wlist)
stack.append(wlist)
is_digit = False
if len(ver) > start_index:
wlist.append(MavenVersion._get_version_item_(is_digit, ver[start_index:]))
while len(stack) > 0:
wlist = stack.pop()
wlist.normalize()
return items
def compare_to(self, other):
if type(other) == MavenVersion:
return self.items.compare_to(other.items)
else:
return NotImplemented