-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
172 lines (130 loc) · 4.82 KB
/
Copy pathmain.py
File metadata and controls
172 lines (130 loc) · 4.82 KB
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
import os
from pathlib import Path
import httpx
from ruamel.yaml import YAML
from semver import Version
yaml = YAML()
def parse(version_str: str) -> Version:
"""
Robustly parse versions, even if a little wonky
semver package is very strict about matching the semver spec
spec for reference: https://semver.org/
if we can't parse the version string, it's most likely because
hatch uses "." as a separator between the version number and
prerelease/build information, but semver requires "-"
we are going to support hatch's shenanigans by splitting and
reconstituting the string
hatch example: "0.0.17.dev0+g0151069.d20230928"
"""
if version_str.startswith("v"):
version_str = version_str[1:]
try:
version = Version.parse(version_str)
except ValueError:
dot_split = version_str.split(".")
version_str = "-".join(
".".join(fragments) for fragments in (dot_split[:3], dot_split[3:])
)
version = Version.parse(version_str)
return version
def clean_version(verion: Version) -> Version:
"""
Clean a version by dropping any prerelease or build information
"""
return Version(
verion.major,
verion.minor,
verion.patch,
)
OPERATORS = ("*", "^", "~", "!", "==", ">=", "<=", ">", "<")
def match(spec: str, version: Version) -> bool:
"""
Check if a version matches a given specifier
:param spec: the specifier to match against
:param version: the version to check
:return: True if the version matches the specifier, False otherwise
"""
# first clean up the spec string
spec = spec.strip()
if spec == "*":
return True
if "||" in spec:
for s in spec.split("||"):
if match(s, version):
return True
return False
if "," in spec:
for s in spec.split(","):
if not match(s, version):
return False
return True
for operator in OPERATORS:
if spec.startswith(operator):
specd_version = parse(spec[len(operator) :])
break
else:
# if there's not operator, default to ^ (up to next major)
specd_version = parse(spec)
operator = "^"
if operator == "^":
# semver doesn't support ^, so we have to do it ourselves
# ^1.2.3 is equivalent to >=1.2.3 <2.0.0
return version >= specd_version and version < specd_version.bump_major()
if operator == "~":
# semver doesn't support ~, so we have to do it ourselves
# ~1.2.3 is equivalent to >=1.2.3 <1.3.0
return version >= specd_version and version < specd_version.bump_minor()
if operator == "!":
return version != specd_version
if operator == "==":
return version == specd_version
if operator == ">=":
return version >= specd_version
if operator == "<=":
return version <= specd_version
if operator == ">":
return version > specd_version
if operator == "<":
return version < specd_version
else:
raise ValueError(f"Invalid operator: {operator}")
def get_released_versions() -> list[Version]:
response = httpx.get("https://pypi.org/pypi/atopile/json", timeout=3)
response.raise_for_status()
versions = []
for version in response.json()["releases"]:
try:
versions.append(parse(version))
except ValueError:
pass
return versions
def main():
# Docker tag takes highest precedence - use it directly
if docker_tag := os.environ.get("DOCKER_TAG"):
print(f"version={docker_tag}")
return
if os.environ.get("ATO_CONFIG") and os.environ.get("SPECIFIED_VERSION"):
raise ValueError("Cannot specify both ATO_CONFIG and SPECIFIED_VERSION")
if specified_version := os.environ.get("SPECIFIED_VERSION"):
print(f"version={specified_version}")
return
ato_config = os.environ.get("ATO_CONFIG")
DEFAULT_ATO_CONFIG = Path("ato.yaml")
if ato_config or DEFAULT_ATO_CONFIG.is_file():
if not ato_config:
ato_config = DEFAULT_ATO_CONFIG
with open(ato_config, "r") as f:
config = yaml.load(f)
requires_atopile = config["requires-atopile"]
available_versions = get_released_versions()
for semver_candidate in sorted(available_versions, reverse=True):
if semver_candidate.build or semver_candidate.prerelease:
continue
if match(requires_atopile, semver_candidate):
print(
f"version={semver_candidate.major}.{semver_candidate.minor}.{semver_candidate.patch}"
)
return
raise RuntimeError("No version specified or detected.")
if __name__ == "__main__":
main()