-
Notifications
You must be signed in to change notification settings - Fork 364
/
version_scheme.py
72 lines (63 loc) · 2.92 KB
/
version_scheme.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
# Parses and validates the version scheme chosen for mamba versions.
# Specifically, we use a dot to separate pre-release names and use complete names for `alpha` and `beta`.
#
# See:
# - discussion in https://github.com/mamba-org/mamba/issues/3638
# - https://conda-forge.org/docs/maintainer/knowledge_base/#pre-release-version-sorting
# - https://github.com/conda/conda/blob/cc21508563912268649f207723fd5114fa21b906/conda/models/version.py#L115-L143
class version_info:
major = ""
minor = ""
patch = ""
pre_release = ""
name = ""
def __init__(self, version: str):
if "-" in version:
raise ValueError(
"'{}' is not a valid version name : `-` is reserved for another usage in conda packages version names".format(
version
)
)
VALID_VERSION_PRERELEASE_TYPES = ("alpha", "beta", "rc", "dev")
version_fields = version.split(".")
version_fields_count = len(version_fields)
if version_fields_count < 3:
raise ValueError(
"'{}' is not a valid version name : valid version scheme contains 3 or more dots-separated fields, the pre-release name starting with the 4th field (valid examples: 1.2.3, 0.1.2.alpha3, 0.1.2.alpha.3)".format(
version
)
)
self.major = version_fields[0]
self.minor = version_fields[1]
self.patch = version_fields[2]
self.pre_release = ""
if version_fields_count > 3:
# we assume here that all the additional dot-separated values are part of the pre-release name
self.pre_release = ".".join(version_fields[3:])
version_errors = []
if not self.major.isdigit():
version_errors.append(f"'{self.major}' is not a valid major version number")
if not self.minor.isdigit():
version_errors.append(f"'{self.minor}' is not a valid minor version number")
if not self.patch.isdigit():
version_errors.append(f"'{self.patch}' is not a valid patch version number")
if self.pre_release != "" and not self.pre_release.startswith(
VALID_VERSION_PRERELEASE_TYPES
):
version_errors.append(
"'{}' is not a valid pre-release name, pre-release names must start with either : {} ".format(
self.pre_release, VALID_VERSION_PRERELEASE_TYPES
)
)
if len(version_errors) > 0:
error_message = f"'{version}' is not a valid version name:"
for error in version_errors:
error_message += f"\n - {error}"
hint = (
"examples of valid versions: 1.2.3, 0.1.2, 1.2.3.alpha0, 1.2.3.beta1, 3.4.5.beta.2"
)
error_message += f"\n{hint}"
raise ValueError(error_message)
self.name = version
def __str__(self):
return self.name