-
Notifications
You must be signed in to change notification settings - Fork 29
/
version.py
580 lines (456 loc) · 17.5 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
#
# SPDX-License-Identifier: Apache-2.0
import collections
import dataclasses
import enum
import logging
import semver
from typing import (
Iterable,
Set,
Union,
)
import typing
import ci.util
logger = logging.getLogger(__name__)
Version = semver.VersionInfo | str
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'
class VersionRestriction(enum.Enum):
SAME_MINOR = 'same-minor'
NONE = 'none'
class VersionType(enum.Enum):
SNAPSHOT = 'snapshots'
RELEASE = 'releases'
ANY = 'any'
@dataclasses.dataclass(frozen=True)
class VersionRetentionPolicy:
name: str = None
keep: typing.Union[str, int] = 'all'
match: VersionType = VersionType.ANY
restrict: VersionRestriction = VersionRestriction.NONE
recursive: bool = False
def matches_version_restriction(self, version, ref_version) -> bool:
version = parse_to_semver(version)
final = is_final(version)
if self.match is VersionType.ANY:
pass
if self.match is VersionType.SNAPSHOT and final:
return False
if self.match is VersionType.RELEASE and not final:
return False
# if this line is reached, version-type matches
if self.restrict is VersionRestriction.NONE:
return True
elif self.restrict is VersionRestriction.SAME_MINOR:
ref_version = version.parse_to_semver(ref_version)
return ref_version.minor == version.minor
else:
raise RuntimeError(f'not implemented: {self.restrict}')
@property
def keep_all(self) -> bool:
return self.keep == 'all'
@dataclasses.dataclass
class VersionRetentionPolicies:
name: str
rules: list[VersionRetentionPolicy]
dry_run: bool = True
T = typing.TypeVar('T')
def is_final(
version: Version,
converter: typing.Callable[[T], Version]=None,
) -> bool:
if converter:
version = converter(version)
version = parse_to_semver(version=version)
return not version.build and not version.prerelease
def versions_to_purge(
versions: typing.Iterable[T],
reference_version: Version,
policy: VersionRetentionPolicies,
converter: typing.Callable[[T], Version]=None,
) -> typing.Generator[T, None, None]:
versions_by_policy = collections.defaultdict(list)
def to_version(v: T):
if converter:
v = converter(v)
return v
for v in versions:
converted_version = to_version(v)
for rule in policy.rules:
rule: VersionRetentionPolicy
if rule.matches_version_restriction(
version=converted_version,
ref_version=reference_version,
):
versions_by_policy[rule].append(v)
break # first rule matches
else:
continue
else:
logger.info(f'no rule matched {converted_version}')
for policy, versions in versions_by_policy.items():
policy: VersionRetentionPolicy
if policy.keep_all:
continue
yield from smallest_versions(
versions=versions,
keep=policy.keep,
converter=converter,
)
def parse_to_semver(
version,
invalid_semver_ok: bool=False,
) -> semver.VersionInfo:
'''
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
- rm leading zeroes
@param version: either a str, or an object with a `version` attr
'''
if isinstance(version, str):
version_str = version
elif isinstance(version, semver.VersionInfo):
return 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
try:
semver_version_info, _ = _parse_to_semver_and_prefix(version_str)
except ValueError:
if invalid_semver_ok:
return None
raise
return semver_version_info
def _parse_to_semver_and_prefix(version: str) -> semver.VersionInfo:
def raise_invalid():
raise ValueError(f'not a valid (semver) version: `{version}`')
if not version:
raise_invalid()
semver_version = version
prefix = None
# strip leading `v`
if version[0] == 'v':
semver_version = version[1:]
prefix = 'v'
# in most cases, we should be fine now
try:
return semver.VersionInfo.parse(semver_version), prefix
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)
if len(tuple(c for c in version if c == '.')) == 1:
numeric += '.0'
try:
return semver.VersionInfo.parse(numeric + sep + suffix), prefix
except ValueError:
pass # last try: strip leading zeroes
try:
major, minor, patch = numeric.split('.')
except ValueError:
raise_invalid()
numeric = '.'.join((
str(int(major)),
str(int(minor)),
str(int(patch)),
))
try:
return semver.VersionInfo.parse(numeric + sep + suffix), prefix
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,
skip_patchlevel_zero=False,
):
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, prefix = _parse_to_semver_and_prefix(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 skip_patchlevel_zero:
parsed_version = parse_to_semver(processed_version)
if parsed_version.patch == 0:
processed_version = str(parsed_version.replace(patch=1))
if prefix:
return prefix + processed_version
return processed_version
T = typing.TypeVar('T', semver.VersionInfo, str)
def find_latest_version(
versions: Iterable[T],
ignore_prerelease_versions: bool=False,
invalid_semver_ok: bool=False,
) -> T | None:
latest_candidate = None
latest_candidate_semver = None
for candidate in versions:
if isinstance(candidate, str):
candidate_semver = parse_to_semver(
version=candidate,
invalid_semver_ok=invalid_semver_ok,
)
if not candidate_semver:
continue
else:
candidate_semver = candidate
if ignore_prerelease_versions and candidate_semver.prerelease:
continue
if not latest_candidate_semver:
latest_candidate_semver = candidate_semver
latest_candidate = candidate
continue
if candidate_semver > latest_candidate_semver:
latest_candidate_semver = candidate_semver
latest_candidate = candidate
return latest_candidate
# alias with a more expressive (and correct..) name
greatest_version = find_latest_version
def greatest_version_with_matching_major(
reference_version: Union[semver.VersionInfo, str],
versions: Iterable[T],
ignore_prerelease_versions: bool=False,
) -> T | None:
'''
returns the greatest version with matching major version compared to `reference_version`
if no matching version is found, returns `None`
the returned object (if matching version is found) is guaranteed to be identical to passed-in
from `versions`.
`versions` (if passed as str) must be parsable into semver using version.parse_to_semver
'''
latest_candidate_semver = None
latest_candidate = 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 = candidate
return latest_candidate
def greatest_version_with_matching_minor(
reference_version: T,
versions: Iterable[T],
ignore_prerelease_versions: bool=False,
) -> T | None:
latest_candidate_semver = None
latest_candidate = 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 = candidate
return latest_candidate
# alias for backwards-compatibility
find_latest_version_with_matching_minor = greatest_version_with_matching_minor
def find_smallest_version_with_matching_minor(
reference_version: Union[semver.VersionInfo, str],
versions: Iterable[Union[semver.VersionInfo, str]],
ignore_prerelease_versions: bool=False,
) -> str | None:
latest_candidate_semver = None
latest_candidate_str = None
if isinstance(reference_version, str):
reference_version = parse_to_semver(reference_version)
# Note: The sorting is done this way to preserve non-semver prefixes we allow, usually 'v'
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 or not latest_candidate_semver:
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 greatest_version_before(
reference_version: Union[semver.VersionInfo, str],
versions: Iterable[T],
ignore_prerelease_versions: bool=False,
) -> T | None:
latest_candidate_semver = None
latest_candidate = 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
if ignore_prerelease_versions and candidate_semver.prerelease:
continue
if candidate_semver < reference_version:
if not latest_candidate_semver or candidate_semver > latest_candidate_semver:
latest_candidate_semver = candidate_semver
latest_candidate = candidate
return latest_candidate
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
def smallest_versions(
versions: typing.Sequence[T],
keep: int,
converter: typing.Callable[[T], str]=None,
) -> list[T]:
'''
find smallest versions from given sequence of versions, excluding the given amount of
greatest versions (keep parameter). This is useful, e.g. to cleanup old versions.
`keep`: specifies how many versions to keep
`converter`: optional value-conversion-callback (for convenience)
'''
if (versions_count := len(versions)) <= keep:
return []
def _parse_version(version: T):
if converter:
version = converter(version)
return parse_to_semver(version)
versions = sorted(
versions,
key=_parse_version,
) # smallest versions come first
purge_idx = versions_count - keep
return versions[:purge_idx]