-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmirror.py
More file actions
77 lines (65 loc) · 2.31 KB
/
mirror.py
File metadata and controls
77 lines (65 loc) · 2.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
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "tomli>=2.2.1",
# "tomli-w>=1.2.0",
# "urllib3>=2",
# "packaging>=21.0",
# ]
# ///
import subprocess
from pathlib import Path
import tomli
import tomli_w
import urllib3
from packaging.requirements import Requirement
from packaging.version import Version
def main():
# Load pyproject.toml
with open(Path(__file__).parent / "pyproject.toml", "rb") as f:
pyproject = tomli.load(f)
# Load README.md
with open(Path(__file__).parent / "README.md", "r") as f:
readme = f.read()
# 获取当前版本的 ast-grep
deps = pyproject["project"]["dependencies"]
assert len(deps) == 1
ast_grep_dep = Requirement(deps[0])
assert ast_grep_dep.name == "ast-grep-cli"
ast_grep_specs = list(ast_grep_dep.specifier)
assert len(ast_grep_specs) == 1
assert ast_grep_specs[0].operator == "=="
current_version = Version(ast_grep_specs[0].version)
# Get ast-grep versions from PyPI
http = urllib3.PoolManager()
resp = http.request("GET", "https://pypi.org/pypi/ast-grep-cli/json")
if resp.status != 200:
raise RuntimeError("Failed to fetch data from PyPI")
versions = [Version(release) for release in resp.json()["releases"]]
versions = [v for v in versions if v > current_version and not v.is_prerelease]
versions.sort()
# Update ast-grep for each version
for version in versions:
# Update pyproject.toml
pyproject["project"]["version"] = str(version)
pyproject["project"]["dependencies"] = [f"ast-grep-cli=={version}"]
# Update README.md
updated_readme = readme.replace(str(current_version), str(version))
# Write pyproject.toml and README.md
with open(Path(__file__).parent / "pyproject.toml", "wb") as f:
tomli_w.dump(pyproject, f)
with open(Path(__file__).parent / "README.md", "w") as f:
f.write(updated_readme)
# Commit and tag
subprocess.run(["git", "add", "pyproject.toml", "README.md"])
subprocess.run(
[
"git",
"commit",
"-m",
f":arrow_up: bump ast-grep version to {version}",
]
)
subprocess.run(["git", "tag", f"v{version}"])
if __name__ == "__main__":
main()