|
1 | 1 | from collections import OrderedDict |
2 | 2 | import os |
3 | | -from typing import Iterable, List |
4 | 3 |
|
5 | | -from cfbs.utils import dict_sorted_by_key, file_sha256 |
| 4 | +from cfbs.utils import dict_sorted_by_key, file_sha256, version_as_comparable_list |
6 | 5 |
|
7 | 6 | Version = str |
8 | 7 |
|
@@ -92,99 +91,3 @@ def finalize_vcf(versions_dict, checksums_dict, files_dict): |
92 | 91 | ) |
93 | 92 |
|
94 | 93 | return versions_dict, checksums_dict, files_dict |
95 | | - |
96 | | - |
97 | | -def version_as_comparable_list(version: str): |
98 | | - """Also supports versions containing exactly one of `b` or `-`. |
99 | | -
|
100 | | - Example of the version ordering: `3.24.0b1 < 3.24.0 < 3.24.0-1`. |
101 | | -
|
102 | | - Examples: |
103 | | - * `version_as_comparable_list("3.24.0b1")` is `[[3, 24, 0], [-1, 1]]` |
104 | | - * `version_as_comparable_list("3.24.0-2")` is `[[3, 24, 0], [1, 2]]` |
105 | | - * `version_as_comparable_list("3.24.x")` is `[[3, 24, 99999], [0, 0]]`""" |
106 | | - if version == "master": |
107 | | - version = "x" |
108 | | - |
109 | | - if "b" not in version: |
110 | | - if "-" not in version: |
111 | | - version += "|0.0" |
112 | | - version = version.replace("x", "99999").replace("-", "|1.").replace("b", "|-1.") |
113 | | - versionpair = version.split("|") |
114 | | - versions_str = [versionpair[0].split("."), versionpair[1].split(".")] |
115 | | - |
116 | | - versions_int = [ |
117 | | - [int(s) for s in versions_str[0]], |
118 | | - [int(s) for s in versions_str[1]], |
119 | | - ] |
120 | | - |
121 | | - return versions_int |
122 | | - |
123 | | - |
124 | | -def version_as_comparable_list_negated(version): |
125 | | - vcl = version_as_comparable_list(version) |
126 | | - |
127 | | - vcl[0] = [-x for x in vcl[0]] |
128 | | - vcl[1] = [-x for x in vcl[1]] |
129 | | - |
130 | | - return vcl |
131 | | - |
132 | | - |
133 | | -def version_is_at_least(version, min_version): |
134 | | - return min_version is None or ( |
135 | | - version_as_comparable_list(version) >= version_as_comparable_list(min_version) |
136 | | - ) |
137 | | - |
138 | | - |
139 | | -def sort_versions(versions: list, reverse: bool = True): |
140 | | - """Sorts a list of versions, in descending order by default.""" |
141 | | - versions.sort( |
142 | | - key=version_as_comparable_list, |
143 | | - reverse=reverse, |
144 | | - ) |
145 | | - |
146 | | - |
147 | | -def highest_version(versions: Iterable[Version]): |
148 | | - return max(versions, key=version_as_comparable_list, default=None) |
149 | | - |
150 | | - |
151 | | -def lowest_version(versions: Iterable[Version]): |
152 | | - return min(versions, key=version_as_comparable_list, default=None) |
153 | | - |
154 | | - |
155 | | -def is_lower_version(version_a: Version, version_b: Version): |
156 | | - """Returns `True` if and only if `version_a` is lower than `version_b`.""" |
157 | | - |
158 | | - return version_as_comparable_list(version_a) < version_as_comparable_list(version_b) |
159 | | - |
160 | | - |
161 | | -def most_relevant_version( |
162 | | - other_versions: List[Version], reference_version: Version |
163 | | -) -> Version: |
164 | | - """ |
165 | | - The most relevant version is the highest version among older other versions, |
166 | | - or if there are no older other versions, the lowest version among other versions. |
167 | | -
|
168 | | - `other_versions` is assumed to be non-empty and not contain `reference_version`.""" |
169 | | - assert len(other_versions) > 0 |
170 | | - assert reference_version not in other_versions |
171 | | - |
172 | | - highest_other_version = highest_version(other_versions) |
173 | | - lowest_other_version = lowest_version(other_versions) |
174 | | - # Unfortunately, Pyright can not infer that these can't be `None` when `other_versions` is non-empty. |
175 | | - assert highest_other_version is not None |
176 | | - assert lowest_other_version is not None |
177 | | - |
178 | | - if is_lower_version(highest_other_version, reference_version): |
179 | | - # all other versions are older |
180 | | - return highest_other_version |
181 | | - if is_lower_version(reference_version, lowest_other_version): |
182 | | - # all other versions are newer |
183 | | - return lowest_other_version |
184 | | - # there are both older and newer versions |
185 | | - lower_other_versions = [ |
186 | | - o_v for o_v in other_versions if is_lower_version(o_v, reference_version) |
187 | | - ] |
188 | | - highest_lower = highest_version(lower_other_versions) |
189 | | - assert highest_lower is not None |
190 | | - return highest_lower |
0 commit comments