forked from gardener/cc-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.py
332 lines (267 loc) · 10.7 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# Copyright (c) 2019-2020 SAP SE or an SAP affiliate company. All rights reserved. This file is
# licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import collections
import dataclasses
import semver
from typing import (
Iterable,
Set,
Union,
)
import ci.util
NOOP = 'noop'
SET_PRERELEASE = 'set_prerelease'
APPEND_PRERELEASE = 'append_prerelease'
SET_BUILD_METADATA = 'set_build_metadata'
SET_PRERELEASE_AND_BUILD = 'set_prerelease_and_build'
SET_VERBATIM = 'set_verbatim'
@dataclasses.dataclass
class _VersionMetadata:
prefix: str = None
def parse_to_semver(
version,
):
'''
parses the given version into a semver.VersionInfo object.
Different from strict semver, the given version is preprocessed, if required, to
convert the version into a valid semver version, if possible.
The following preprocessings are done:
- strip away `v` prefix
- append patch-level `.0` for two-digit versions
@param version: either a str, or an object with a `version` attr
'''
if isinstance(version, str):
version_str = version
else:
if hasattr(version, 'version'):
if callable(version.version):
version_str = version.version()
else:
version_str = str(version.version)
elif version is None:
raise ValueError('version must not be None')
else:
ci.util.warning(f'unexpected type for version: {type(version)}')
version_str = str(version) # fallback
semver_version_info, _ = _parse_to_semver_and_metadata(version_str)
return semver_version_info
def _parse_to_semver_and_metadata(version: str):
def raise_invalid():
raise ValueError(f'not a valid (semver) version: `{version}`')
if not version:
raise_invalid()
semver_version = version
metadata = _VersionMetadata()
# strip leading `v`
if version[0] == 'v':
semver_version = version[1:]
metadata.prefix = 'v'
# in most cases, we should be fine now
try:
return semver.VersionInfo.parse(semver_version), metadata
except ValueError:
pass # try extending `.0` as patch-level
# blindly append patch-level
if '-' in version:
sep = '-'
else:
sep = '+'
numeric, sep, suffix = semver_version.partition(sep)
numeric += '.0'
try:
return semver.VersionInfo.parse(numeric + sep + suffix), metadata
except ValueError:
# re-raise with original version str
raise_invalid()
def _sort_versions(
versions
):
'''
sorts the given versions (which may be a sequence containing any combination of
str, semver.VersionInfo, or model element bearing a `version` attr) on a best-effort
base.
Firstly, it is checked whether all versions are semver-parsable, using this module's
`parse_to_semver` (which allows some deviations from strict semver-v2). If all versions
are parsable, str representations of the originally given versions are returned, ordered
according to semver artithmetics.
Otherwise, sorting falls back to alphabetical sorting as implemented by python's str.
Note that there is _no_ validation of any kind w.r.t. to the sanity of the passed values.
This function is not intended to be used by external users, and is planned to be removed
again.
'''
if not versions:
return
def to_ver(version_obj):
if hasattr(version_obj, 'version'):
if callable(version_obj.version):
return version_obj.version()
else:
return version_obj.version
else:
return str(version_obj)
try:
# try if all versions are semver-compatible
for version_str in map(to_ver, versions):
parse_to_semver(version_str)
return sorted(
versions,
key=lambda vo: parse_to_semver(to_ver(vo)),
)
except ValueError:
pass # ignore and fall-back to str-sorting
return sorted(
versions,
key=to_ver,
)
def process_version(
version_str: str,
operation: str,
prerelease: str=None,
build_metadata: str=None,
# Limit the length of the build-metadata suffix.
# By default we use 12 chars, following the advice given in
# https://blog.cuviper.com/2013/11/10/how-short-can-git-abbreviate/
# as we usually use git commit hashes
build_metadata_length: int=12,
verbatim_version: str=None,
):
if operation in [SET_PRERELEASE,SET_PRERELEASE_AND_BUILD,APPEND_PRERELEASE] and not prerelease:
raise ValueError('Prerelease must be given when replacing or appending.')
if operation in [SET_BUILD_METADATA,SET_PRERELEASE_AND_BUILD]:
if not build_metadata:
raise ValueError('Build metadata must be given when replacing.')
if build_metadata_length < 0:
raise ValueError('Build metadata must not be empty')
if operation == SET_VERBATIM and (not verbatim_version or prerelease or build_metadata):
raise ValueError('Exactly verbatim-version must be given when using operation set_verbatim')
parsed_version, metadata = _parse_to_semver_and_metadata(version_str)
version_str = str(parsed_version)
if operation == APPEND_PRERELEASE and not parsed_version.prerelease:
raise ValueError('Given SemVer must have prerelease-version to append to.')
if hasattr(semver, operation):
function = getattr(parsed_version, operation)
processed_version = str(function())
elif operation == NOOP:
processed_version = version_str
elif operation == SET_VERBATIM:
processed_version = str(verbatim_version)
elif operation == APPEND_PRERELEASE:
parsed_version = parsed_version.replace(
prerelease='-'.join((parsed_version.prerelease, prerelease))
)
processed_version = str(parsed_version)
else:
parsed_version = parsed_version.replace(prerelease=None, build=None)
if operation in [SET_PRERELEASE, SET_PRERELEASE_AND_BUILD]:
parsed_version = parsed_version.replace(prerelease=prerelease)
if operation in [SET_BUILD_METADATA, SET_PRERELEASE_AND_BUILD]:
parsed_version = parsed_version.replace(build=build_metadata[:build_metadata_length])
processed_version = str(parsed_version)
if metadata.prefix:
return metadata.prefix + processed_version
return processed_version
def find_latest_version(
versions: Iterable[Union[semver.VersionInfo, str]],
ignore_prerelease_versions: bool=False,
) -> str:
latest_candidate = None
latest_candidate_str = None
for candidate in versions:
if isinstance(candidate, str):
candidate_semver = parse_to_semver(candidate)
else:
candidate_semver = candidate
if ignore_prerelease_versions and candidate_semver.prerelease:
continue
if not latest_candidate:
latest_candidate = candidate_semver
latest_candidate_str = candidate
continue
if candidate_semver > latest_candidate:
latest_candidate = candidate_semver
latest_candidate_str = candidate
return latest_candidate_str
# alias with a more expressive (and correct..) name
greatest_version = find_latest_version
def find_latest_version_with_matching_major(
reference_version: Union[semver.VersionInfo, str],
versions: Iterable[Union[semver.VersionInfo, str]],
ignore_prerelease_versions: bool=False,
) -> str:
latest_candidate_semver = None
latest_candidate_str = None
if isinstance(reference_version, str):
reference_version = parse_to_semver(reference_version)
for candidate in versions:
if isinstance(candidate, str):
candidate_semver = parse_to_semver(candidate)
else:
candidate_semver = candidate
# skip if major version does not match
if candidate_semver.major != reference_version.major:
continue
if ignore_prerelease_versions and candidate_semver.prerelease:
continue
if candidate_semver > reference_version:
if not latest_candidate_semver or latest_candidate_semver < candidate_semver:
latest_candidate_semver = candidate_semver
latest_candidate_str = candidate
return latest_candidate_str
def find_latest_version_with_matching_minor(
reference_version: Union[semver.VersionInfo, str],
versions: Iterable[Union[semver.VersionInfo, str]],
ignore_prerelease_versions: bool=False,
) -> str:
latest_candidate_semver = None
latest_candidate_str = None
if isinstance(reference_version, str):
reference_version = parse_to_semver(reference_version)
for candidate in versions:
if isinstance(candidate, str):
candidate_semver = parse_to_semver(candidate)
else:
candidate_semver = candidate
# skip if major version does not match
if candidate_semver.major != reference_version.major:
continue
# skip if minor version does not match
if candidate_semver.minor != reference_version.minor:
continue
if ignore_prerelease_versions and candidate_semver.prerelease:
continue
if candidate_semver >= reference_version:
if not latest_candidate_semver or latest_candidate_semver < candidate_semver:
latest_candidate_semver = candidate_semver
latest_candidate_str = candidate
return latest_candidate_str
def partition_by_major_and_minor(
versions: Iterable[semver.VersionInfo],
) -> Iterable[Set[semver.VersionInfo]]:
'''partition an iterable of semver VersionInfos by their joined major and minor version
'''
partitions = collections.defaultdict(set)
for version_info in versions:
partitions[(version_info.major,version_info.minor)].add(version_info)
yield from [
sorted(partition, reverse=True)
for partition in partitions.values()
]
def is_semver_parseable(version_string: str):
try:
parse_to_semver(version_string)
except ValueError:
ci.util.verbose(f"Could not parse '{version_string}' as semver version")
return False
return True