-
Notifications
You must be signed in to change notification settings - Fork 7
/
mirror.py
75 lines (55 loc) · 2.43 KB
/
mirror.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
import re
import subprocess
import tomllib
import typing
from pathlib import Path
import urllib3
from packaging.requirements import Requirement
from packaging.version import Version
def main():
with open(Path(__file__).parent / "pyproject.toml", "rb") as f:
pyproject = tomllib.load(f)
all_versions = get_all_versions()
current_version = get_current_version(pyproject=pyproject)
target_versions = [v for v in all_versions if v > current_version]
for version in target_versions:
paths = process_version(version)
if subprocess.check_output(["git", "status", "-s"]).strip():
subprocess.run(["git", "add", *paths], check=True)
subprocess.run(["git", "commit", "-m", f"Mirror: {version}"], check=True)
subprocess.run(["git", "tag", f"{version}"], check=True)
else:
print(f"No change {version}")
def get_all_versions() -> list[Version]:
response = urllib3.request("GET", "https://pypi.org/pypi/uv/json")
if response.status != 200:
raise RuntimeError("Failed to fetch versions from pypi")
versions = [Version(release) for release in response.json()["releases"]]
return sorted(versions)
def get_current_version(pyproject: dict) -> Version:
requirements = [Requirement(d) for d in pyproject["project"]["dependencies"]]
requirement = next((r for r in requirements if r.name == "uv"), None)
assert requirement is not None, "pyproject.toml does not have uv requirement"
specifiers = list(requirement.specifier)
assert (
len(specifiers) == 1 and specifiers[0].operator == "=="
), f"uv's specifier should be exact matching, but `{requirement}`"
return Version(specifiers[0].version)
def process_version(version: Version) -> typing.Sequence[str]:
def replace_pyproject_toml(content: str) -> str:
return re.sub(r'"uv==.*"', f'"uv=={version}"', content)
def replace_readme_md(content: str) -> str:
content = re.sub(r"rev: \d+\.\d+\.\d+", f"rev: {version}", content)
return re.sub(r"/uv/\d+\.\d+\.\d+\.svg", f"/uv/{version}.svg", content)
paths = {
"pyproject.toml": replace_pyproject_toml,
"README.md": replace_readme_md,
}
for path, replacer in paths.items():
with open(path) as f:
content = replacer(f.read())
with open(path, mode="w") as f:
f.write(content)
return tuple(paths.keys())
if __name__ == "__main__":
main()