-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmirror.py
More file actions
115 lines (98 loc) · 3.31 KB
/
Copy pathmirror.py
File metadata and controls
115 lines (98 loc) · 3.31 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
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "packaging==23.1",
# "urllib3==2.0.5",
# ]
# ///
#!/usr/bin/env python3
import json
import os
import re
import subprocess
import sys
import time
import urllib.request
from packaging.version import parse as parse_version
from pathlib import Path
def get_latest_rumdl_version():
url = "https://pypi.org/pypi/rumdl/json"
with urllib.request.urlopen(url) as response:
data = response.read()
releases = json.loads(data)["releases"]
versions = sorted(
(parse_version(v) for v in releases if not parse_version(v).is_prerelease),
reverse=True,
)
return str(versions[0])
def wait_for_pypi(version, max_wait=300, interval=15):
"""Wait until the version is available on PyPI. Returns True if available."""
url = f"https://pypi.org/pypi/rumdl/{version}/json"
elapsed = 0
while elapsed < max_wait:
try:
with urllib.request.urlopen(url) as response:
if response.status == 200:
print(f"Version {version} is available on PyPI.")
return True
except urllib.error.HTTPError:
pass
print(f"Waiting for {version} on PyPI... ({elapsed}s/{max_wait}s)")
time.sleep(interval)
elapsed += interval
print(f"Timed out waiting for {version} on PyPI after {max_wait}s.")
return False
def update_pyproject_toml(version):
pyproject_path = Path("pyproject.toml")
content = pyproject_path.read_text()
new_content = content
# Update the package version (appears right after [project] header)
new_content = re.sub(
r'(\[project\]\nname = "[^"]*"\n)version = "[^"]*"',
rf'\1version = "{version}"',
new_content,
)
# Update the rumdl dependency pin
new_content = re.sub(
r'"rumdl([<>=!~]*[0-9\.\*]*)?"', f'"rumdl=={version}"', new_content
)
if new_content != content:
pyproject_path.write_text(new_content)
return True
return False
def update_readme_md(version):
readme_path = Path("README.md")
if not readme_path.exists():
return False
content = readme_path.read_text()
new_content = re.sub(r"rev: v[\d\.]+", f"rev: v{version}", content)
if new_content != content:
readme_path.write_text(new_content)
return True
return False
def main():
# Use version from dispatch payload if available, otherwise query PyPI
version = os.environ.get("DISPATCH_VERSION")
if version:
print(f"Using version from dispatch payload: {version}")
if not wait_for_pypi(version):
sys.exit(1)
else:
version = get_latest_rumdl_version()
print(f"Latest rumdl version from PyPI: {version}")
changed = False
if update_pyproject_toml(version):
print("Updated pyproject.toml")
changed = True
if update_readme_md(version):
print("Updated README.md")
changed = True
if changed:
subprocess.run(["git", "add", "pyproject.toml", "README.md"], check=True)
subprocess.run(["git", "commit", "-m", f"Mirror: {version}"], check=True)
subprocess.run(["git", "tag", f"v{version}"], check=True)
print(f"Committed and tagged v{version}")
else:
print("No changes needed.")
if __name__ == "__main__":
main()