|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Strip the development suffix from all version strings in the repo. |
| 3 | +
|
| 4 | +Reads the current ``kernels`` version from ``kernels/pyproject.toml`` (the |
| 5 | +source-of-truth in the codebase — no install required) and, assuming it is a |
| 6 | +development version like ``0.14.0.dev0``, rewrites every version site to the |
| 7 | +corresponding release: ``0.14.0``. The inverse of ``bump_to_dev.py``. |
| 8 | +
|
| 9 | +Example: codebase at ``0.14.0.dev0`` -> all sites become ``0.14.0``. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import argparse |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +from packaging.version import Version |
| 18 | + |
| 19 | +from _version_common import ( |
| 20 | + CARGO_FILES, |
| 21 | + PRIMARY_PYPROJECT, |
| 22 | + PYPROJECT_FILES, |
| 23 | + confirm, |
| 24 | + display_path, |
| 25 | + get_codebase_version, |
| 26 | + replace_top_level_version, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +def next_release_version(current: Version) -> str: |
| 31 | + if ( |
| 32 | + not current.is_devrelease |
| 33 | + or current.pre is not None |
| 34 | + or current.post is not None |
| 35 | + or current.local is not None |
| 36 | + ): |
| 37 | + raise SystemExit( |
| 38 | + f"Codebase version `{current}` is not a development version " |
| 39 | + "(e.g. 0.14.0.dev0). This tool strips the dev suffix ahead of a " |
| 40 | + f"release. Set {display_path(PRIMARY_PYPROJECT)} to a dev version first." |
| 41 | + ) |
| 42 | + |
| 43 | + release = current.release |
| 44 | + major = release[0] if len(release) > 0 else 0 |
| 45 | + minor = release[1] if len(release) > 1 else 0 |
| 46 | + patch = release[2] if len(release) > 2 else 0 |
| 47 | + return f"{major}.{minor}.{patch}" |
| 48 | + |
| 49 | + |
| 50 | +def main(argv: list[str] | None = None) -> int: |
| 51 | + parser = argparse.ArgumentParser(description=__doc__) |
| 52 | + parser.add_argument( |
| 53 | + "--dry-run", |
| 54 | + action="store_true", |
| 55 | + help="Show which files would change without writing them.", |
| 56 | + ) |
| 57 | + parser.add_argument( |
| 58 | + "-y", |
| 59 | + "--yes", |
| 60 | + action="store_true", |
| 61 | + help="Skip the interactive confirmation prompt.", |
| 62 | + ) |
| 63 | + args = parser.parse_args(argv) |
| 64 | + |
| 65 | + current = get_codebase_version() |
| 66 | + release = next_release_version(current) |
| 67 | + |
| 68 | + print(f"Codebase kernels version : {current}") |
| 69 | + print(f"Next release version : {release}") |
| 70 | + print() |
| 71 | + |
| 72 | + if not args.dry_run and not args.yes: |
| 73 | + if not confirm(f"Strip dev suffix from all version sites -> {release}?"): |
| 74 | + print("Aborted; no files changed.") |
| 75 | + return 1 |
| 76 | + print() |
| 77 | + |
| 78 | + changed: list[tuple[Path, str, str]] = [] |
| 79 | + for path in PYPROJECT_FILES: |
| 80 | + old = replace_top_level_version(path, release, dry_run=args.dry_run) |
| 81 | + if old is not None: |
| 82 | + changed.append((path, old, release)) |
| 83 | + for path in CARGO_FILES: |
| 84 | + old = replace_top_level_version(path, release, dry_run=args.dry_run) |
| 85 | + if old is not None: |
| 86 | + changed.append((path, old, release)) |
| 87 | + |
| 88 | + verb = "Would update" if args.dry_run else "Updated" |
| 89 | + if not changed: |
| 90 | + print("All files already at the target version; nothing to do.") |
| 91 | + return 0 |
| 92 | + |
| 93 | + print(f"{verb} {len(changed)} file(s):") |
| 94 | + for path, old, new in changed: |
| 95 | + print(f" {display_path(path)}: {old} -> {new}") |
| 96 | + |
| 97 | + if not args.dry_run: |
| 98 | + print() |
| 99 | + print("Note: Cargo.lock and kernels/uv.lock are refreshed by the `pre-release`") |
| 100 | + print("Makefile target; if you ran this script directly, regenerate them with") |
| 101 | + print("`cargo check --workspace` and `(cd kernels && uv lock)`.") |
| 102 | + |
| 103 | + return 0 |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + raise SystemExit(main()) |
0 commit comments