|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import re |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import tomllib |
| 11 | + |
| 12 | +ROOT = Path(__file__).resolve().parents[1] |
| 13 | + |
| 14 | + |
| 15 | +def run( |
| 16 | + command: list[str], *, capture: bool = False |
| 17 | +) -> subprocess.CompletedProcess[str]: |
| 18 | + print("+", " ".join(command)) |
| 19 | + return subprocess.run( |
| 20 | + command, |
| 21 | + cwd=ROOT, |
| 22 | + check=True, |
| 23 | + text=True, |
| 24 | + stdout=subprocess.PIPE if capture else None, |
| 25 | + stderr=subprocess.PIPE if capture else None, |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def project_version() -> str: |
| 30 | + with (ROOT / "pyproject.toml").open("rb") as file: |
| 31 | + return tomllib.load(file)["project"]["version"] |
| 32 | + |
| 33 | + |
| 34 | +def require_supported_version(version: str) -> None: |
| 35 | + if not re.fullmatch(r"\d+\.\d+\.\d+((a|b|rc)\d+)?", version): |
| 36 | + print(f"Unsupported release version: {version!r}") |
| 37 | + print("Expected X.Y.Z, X.Y.ZaN, X.Y.ZbN, or X.Y.ZrcN.") |
| 38 | + sys.exit(1) |
| 39 | + |
| 40 | + |
| 41 | +def require_clean_worktree() -> None: |
| 42 | + status = run(["git", "status", "--porcelain"], capture=True).stdout.strip() |
| 43 | + if status: |
| 44 | + print( |
| 45 | + "Release requires a clean git worktree. Commit or stash these changes first:" |
| 46 | + ) |
| 47 | + print(status) |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + |
| 51 | +def require_tag_available(tag: str) -> None: |
| 52 | + local = subprocess.run( |
| 53 | + ["git", "rev-parse", "--verify", "--quiet", f"refs/tags/{tag}"], |
| 54 | + cwd=ROOT, |
| 55 | + text=True, |
| 56 | + stdout=subprocess.DEVNULL, |
| 57 | + stderr=subprocess.DEVNULL, |
| 58 | + ) |
| 59 | + if local.returncode == 0: |
| 60 | + print(f"Tag {tag} already exists locally.") |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + remote = subprocess.run( |
| 64 | + ["git", "ls-remote", "--exit-code", "--tags", "origin", f"refs/tags/{tag}"], |
| 65 | + cwd=ROOT, |
| 66 | + text=True, |
| 67 | + stdout=subprocess.DEVNULL, |
| 68 | + stderr=subprocess.DEVNULL, |
| 69 | + ) |
| 70 | + if remote.returncode == 0: |
| 71 | + print(f"Tag {tag} already exists on origin.") |
| 72 | + sys.exit(1) |
| 73 | + |
| 74 | + |
| 75 | +def main() -> None: |
| 76 | + parser = argparse.ArgumentParser( |
| 77 | + description="Prepare and optionally publish from the releases branch." |
| 78 | + ) |
| 79 | + parser.add_argument( |
| 80 | + "--version", |
| 81 | + help="Expected version. Defaults to the version in pyproject.toml.", |
| 82 | + ) |
| 83 | + parser.add_argument( |
| 84 | + "--push-release-branch", |
| 85 | + action="store_true", |
| 86 | + help="Push the current commit to origin/releases after release checks pass.", |
| 87 | + ) |
| 88 | + args = parser.parse_args() |
| 89 | + |
| 90 | + version = project_version() |
| 91 | + require_supported_version(version) |
| 92 | + if args.version and args.version != version: |
| 93 | + print( |
| 94 | + f"Expected version {args.version}, but pyproject.toml contains {version}." |
| 95 | + ) |
| 96 | + sys.exit(1) |
| 97 | + |
| 98 | + tag = f"v{version}" |
| 99 | + |
| 100 | + require_clean_worktree() |
| 101 | + require_tag_available(tag) |
| 102 | + |
| 103 | + dist = ROOT / "dist" |
| 104 | + if dist.exists(): |
| 105 | + shutil.rmtree(dist) |
| 106 | + |
| 107 | + run(["uv", "run", "ruff", "check", "--select", "I", "."]) |
| 108 | + run(["uv", "run", "ruff", "format", "--check", "."]) |
| 109 | + run(["uv", "run", "basedpyright", "."]) |
| 110 | + run(["uv", "run", "pytest", "tests/test_contracts.py", "-n", "auto"]) |
| 111 | + run(["uv", "build"]) |
| 112 | + run(["uv", "run", "twine", "check", *sorted(str(path) for path in dist.iterdir())]) |
| 113 | + |
| 114 | + if args.push_release_branch: |
| 115 | + run(["git", "push", "origin", "HEAD:releases"]) |
| 116 | + print("Pushed current commit to origin/releases.") |
| 117 | + print("GitHub Actions will create the release tag and publish after approval.") |
| 118 | + else: |
| 119 | + print(f"Release checks passed for {tag}.") |
| 120 | + print("Publish with: git push origin HEAD:releases") |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + main() |
0 commit comments