-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.py
More file actions
178 lines (143 loc) · 4.9 KB
/
Copy pathrelease.py
File metadata and controls
178 lines (143 loc) · 4.9 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
173
174
175
176
177
178
from __future__ import annotations
import argparse
import re
import shutil
import subprocess
import sys
import tomllib
from pathlib import Path
from typing import NamedTuple
ROOT = Path(__file__).resolve().parents[1]
VERSION_PATTERN = re.compile(
r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)"
r"(?:(?P<stage>a|b|rc)(?P<stage_number>\d+))?"
)
STAGE_ORDER = {"a": 0, "b": 1, "rc": 2, None: 3}
class Version(NamedTuple):
major: int
minor: int
patch: int
stage: int
stage_number: int
def run(
command: list[str], *, capture: bool = False
) -> subprocess.CompletedProcess[str]:
print("+", " ".join(command))
return subprocess.run(
command,
cwd=ROOT,
check=True,
text=True,
stdout=subprocess.PIPE if capture else None,
stderr=subprocess.PIPE if capture else None,
)
def project_version() -> str:
with (ROOT / "pyproject.toml").open("rb") as file:
return tomllib.load(file)["project"]["version"]
def parse_version(version: str) -> Version | None:
match = VERSION_PATTERN.fullmatch(version)
if not match:
return None
stage = match.group("stage")
stage_number = match.group("stage_number")
return Version(
int(match.group("major")),
int(match.group("minor")),
int(match.group("patch")),
STAGE_ORDER[stage],
int(stage_number or 0),
)
def require_supported_version(version: str) -> Version:
parsed = parse_version(version)
if parsed is None:
print(f"Unsupported release version: {version!r}")
print("Expected X.Y.Z, X.Y.ZaN, X.Y.ZbN, or X.Y.ZrcN.")
sys.exit(1)
return parsed
def require_next_version(current: str, next_version: str) -> None:
if require_supported_version(next_version) <= require_supported_version(current):
print(
f"Next version must be higher than the current version: "
f"{next_version} <= {current}."
)
sys.exit(1)
def require_clean_worktree() -> None:
status = run(["git", "status", "--porcelain"], capture=True).stdout.strip()
if status:
print(
"Release requires a clean git worktree. Commit or stash these changes first:"
)
print(status)
sys.exit(1)
def require_branch(branch: str) -> None:
current = run(["git", "branch", "--show-current"], capture=True).stdout.strip()
if current != branch:
print(
f"Release must be run from {branch!r}, but current branch is {current!r}."
)
sys.exit(1)
def require_tag_available(tag: str) -> None:
local = subprocess.run(
["git", "rev-parse", "--verify", "--quiet", f"refs/tags/{tag}"],
cwd=ROOT,
text=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if local.returncode == 0:
print(f"Tag {tag} already exists locally.")
sys.exit(1)
remote = subprocess.run(
["git", "ls-remote", "--exit-code", "--tags", "origin", f"refs/tags/{tag}"],
cwd=ROOT,
text=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if remote.returncode == 0:
print(f"Tag {tag} already exists on origin.")
sys.exit(1)
def commit_version_bump(tag: str) -> None:
run(["git", "add", "pyproject.toml", "uv.lock"])
staged = run(["git", "diff", "--cached", "--name-only"], capture=True).stdout
if not staged.strip():
print("Version bump did not change pyproject.toml or uv.lock.")
sys.exit(1)
run(["git", "commit", "-m", f"Release {tag}"])
def main() -> None:
parser = argparse.ArgumentParser(
description="Run local checks and build distributions before releasing."
)
parser.add_argument(
"--version",
help="Next release version. Prompts when omitted.",
)
args = parser.parse_args()
current_version = project_version()
require_supported_version(current_version)
print(f"Current version: {current_version}")
next_version = args.version or input("Next version: ").strip()
if not next_version:
print("A next version is required.")
sys.exit(1)
require_next_version(current_version, next_version)
tag = f"v{next_version}"
require_clean_worktree()
require_branch("main")
require_tag_available(tag)
run(["uv", "version", next_version, "--no-sync"])
dist = ROOT / "dist"
if dist.exists():
shutil.rmtree(dist)
run(["uv", "run", "ruff", "check", "--select", "I", "."])
run(["uv", "run", "ruff", "format", "--check", "."])
run(["uv", "run", "ty", "check", "."])
run(["uv", "run", "pytest", "-n", "auto"])
run(["uv", "build"])
commit_version_bump(tag)
run(["git", "push", "origin", "main"])
run(["git", "push", "origin", "HEAD:releases"])
print(f"Release checks passed for {tag}.")
print("Release pushed to main and releases.")
if __name__ == "__main__":
main()