-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.py
More file actions
157 lines (127 loc) · 4.63 KB
/
publish.py
File metadata and controls
157 lines (127 loc) · 4.63 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
#!/usr/bin/env python3
"""Local release helper for publishing spragkit to PyPI."""
from __future__ import annotations
import argparse
import re
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent
INIT_FILE = ROOT / "sprag" / "__init__.py"
DIST_DIR = ROOT / "dist"
BUILD_DIR = ROOT / "build"
DEFAULT_REPOSITORY = "pypi"
def run(cmd: list[str]) -> None:
print(f"\n$ {' '.join(cmd)}")
subprocess.run(cmd, cwd=ROOT, check=True)
def read_version() -> str:
content = INIT_FILE.read_text(encoding="utf-8")
match = re.search(r"^__version__ = ['\"]([^'\"]+)['\"]$", content, re.MULTILINE)
if not match:
raise RuntimeError("Could not find __version__ in sprag/__init__.py")
return match.group(1)
def write_version(version: str) -> None:
content = INIT_FILE.read_text(encoding="utf-8")
updated = re.sub(
r"^__version__ = ['\"][^'\"]+['\"]$",
f'__version__ = "{version}"',
content,
flags=re.MULTILINE,
)
INIT_FILE.write_text(updated, encoding="utf-8")
def bump_version(version: str, bump: str) -> str:
major, minor, patch = map(int, version.split("."))
if bump == "major":
return f"{major + 1}.0.0"
if bump == "minor":
return f"{major}.{minor + 1}.0"
return f"{major}.{minor}.{patch + 1}"
def clean() -> None:
shutil.rmtree(DIST_DIR, ignore_errors=True)
shutil.rmtree(BUILD_DIR, ignore_errors=True)
for egg_info in ROOT.glob("*.egg-info"):
shutil.rmtree(egg_info, ignore_errors=True)
def dist_files() -> list[str]:
files = sorted(str(path) for path in DIST_DIR.glob("*"))
if not files:
raise RuntimeError("No distribution files were built in dist/")
return files
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Build and publish spragkit to PyPI.")
parser.add_argument(
"release",
nargs="?",
default=None,
help="Version bump (patch/minor/major) or an explicit version like 0.2.0",
)
parser.add_argument(
"--build-only",
action="store_true",
help="Bump version and build, but do not upload.",
)
parser.add_argument(
"--testpypi",
action="store_true",
help="Upload to TestPyPI instead of PyPI.",
)
parser.add_argument(
"--repository",
default=DEFAULT_REPOSITORY,
help="Twine repository alias from ~/.pypirc.",
)
parser.add_argument(
"--skip-existing",
action="store_true",
help="Pass --skip-existing to twine upload.",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
current = read_version()
if args.release is not None:
# Explicit bump requested on the command line.
if args.release in {"major", "minor", "patch"}:
target = bump_version(current, args.release)
else:
if not re.fullmatch(r"\d+\.\d+\.\d+", args.release):
raise SystemExit("Release must be patch/minor/major or an explicit x.y.z version.")
target = args.release
else:
# No explicit bump. Ask interactively if the version looks stale.
print(f"Current version: {current}")
answer = input("Bump version? (patch/minor/major/n) [patch]: ").strip().lower()
if answer in {"", "patch", "minor", "major"}:
target = bump_version(current, answer or "patch")
elif answer in {"n", "no"}:
target = current
else:
if not re.fullmatch(r"\d+\.\d+\.\d+", answer):
raise SystemExit("Expected patch/minor/major, an explicit x.y.z, or 'n'.")
target = answer
if target != current:
print(f"Updating version: {current} -> {target}")
write_version(target)
else:
print(f"Version unchanged: {current}")
clean()
run([sys.executable, "-m", "pip", "install", "--upgrade", "build", "twine", "setuptools", "wheel"])
run([sys.executable, "-m", "build"])
files = dist_files()
run([sys.executable, "-m", "twine", "check", *files])
if args.build_only:
print("\nBuild complete. Nothing was uploaded.")
return
upload_cmd = [sys.executable, "-m", "twine", "upload"]
if args.testpypi:
upload_cmd.extend(["--repository", "testpypi"])
elif args.repository:
upload_cmd.extend(["--repository", args.repository])
if args.skip_existing:
upload_cmd.append("--skip-existing")
upload_cmd.extend(files)
run(upload_cmd)
final_version = read_version()
print(f"\nspragkit {final_version} published.")
if __name__ == "__main__":
main()