|
| 1 | +"""Script responsible for preparing a release of the robust-python-demo package.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import re |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +from pathlib import Path |
| 8 | +from re import Match |
| 9 | +from typing import Literal |
| 10 | +from typing import Optional |
| 11 | +from typing import Pattern |
| 12 | +from typing import TypeAlias |
| 13 | + |
| 14 | + |
| 15 | +from util import check_dependencies |
| 16 | +from util import remove_readonly |
| 17 | +from util import REPO_FOLDER |
| 18 | + |
| 19 | + |
| 20 | +Increment: TypeAlias = Literal["major", "minor", "patch", "prerelease"] |
| 21 | +CZ_PATTERN: Pattern[str] = re.compile(r"bump: version (?P<current_version>.*?) → (?P<new_version>.*?)") |
| 22 | + |
| 23 | + |
| 24 | +def main() -> None: |
| 25 | + """Parses args and passes through to prepare_release.""" |
| 26 | + parser: argparse.ArgumentParser = get_parser() |
| 27 | + args: argparse.Namespace = parser.parse_args() |
| 28 | + prepare_release(path=args.path, python_version=args.python_version) |
| 29 | + |
| 30 | + |
| 31 | +def get_parser() -> argparse.ArgumentParser: |
| 32 | + """Creates the argument parser for prepare-release.""" |
| 33 | + parser: argparse.ArgumentParser = argparse.ArgumentParser( |
| 34 | + prog="prepare-release", usage="python ./scripts/prepare-release.py patch" |
| 35 | + ) |
| 36 | + parser.add_argument( |
| 37 | + "increment", |
| 38 | + type=str, |
| 39 | + help="Increment type to use when preparing the release.", |
| 40 | + choices=["major", "minor", "patch", "prerelease"], |
| 41 | + ) |
| 42 | + return parser |
| 43 | + |
| 44 | + |
| 45 | +def prepare_release(increment: Optional[str] = None) -> None: |
| 46 | + """Prepares a release of the robust-python-demo package. |
| 47 | +
|
| 48 | + Sets up a release branch from the branch develop, bumps the version, and creates a release commit. Does not tag the |
| 49 | + release or push any changes. |
| 50 | + """ |
| 51 | + dry_run_cmd: list[str] = ["uvx", "cz", "bump", "--dry-run", "--yes"] |
| 52 | + bump_cmd: list[str] = ["uvx", "cz", "bump", "--yes", "--files-only", "--changelog"] |
| 53 | + if increment is not None: |
| 54 | + dry_run_cmd.extend(["--increment", increment]) |
| 55 | + bump_cmd.extend(["--increment", increment]) |
| 56 | + |
| 57 | + result: subprocess.CompletedProcess = subprocess.run(dry_run_cmd, cwd=REPO_FOLDER, capture_output=True) |
| 58 | + match: Match = re.match(CZ_PATTERN, result.stdout) |
| 59 | + current_version: str = match.group("current_version") |
| 60 | + new_version: str = match.group("new_version") |
| 61 | + |
| 62 | + commands: list[list[str]] = [ |
| 63 | + ["git", "status", "--porcelain"], |
| 64 | + ["git", "branch", "-b", f"release/{new_version}", "develop"], |
| 65 | + ["git", "checkout", f"release/{new_version}"], |
| 66 | + bump_cmd, |
| 67 | + ["git", "add", "."], |
| 68 | + ["git", "commit", "-m", f"bump: version {current_version} → {new_version}"] |
| 69 | + ] |
| 70 | + |
| 71 | + check_dependencies(path=REPO_FOLDER, dependencies=["git", "cz"]) |
| 72 | + |
| 73 | + for command in commands: |
| 74 | + subprocess.run(command, cwd=REPO_FOLDER, capture_output=True, check=True) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + main() |
| 79 | + |
| 80 | + |
0 commit comments