|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Sync release version metadata from pyproject.toml. |
| 3 | +
|
| 4 | +This script treats pyproject.toml as the canonical source of truth and keeps |
| 5 | +the duplicated version fields in sync. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import re |
| 12 | +import sys |
| 13 | +import tomllib |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | + |
| 17 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 18 | +PYPROJECT_PATH = REPO_ROOT / "pyproject.toml" |
| 19 | +VERSION_PY_PATH = REPO_ROOT / "socket_basics" / "version.py" |
| 20 | +INIT_PY_PATH = REPO_ROOT / "socket_basics" / "__init__.py" |
| 21 | +ACTION_YML_PATH = REPO_ROOT / "action.yml" |
| 22 | + |
| 23 | + |
| 24 | +def read_canonical_version() -> str: |
| 25 | + data = tomllib.loads(PYPROJECT_PATH.read_text()) |
| 26 | + return data["project"]["version"] |
| 27 | + |
| 28 | + |
| 29 | +def replace_first(pattern: str, replacement: str, content: str, path: Path) -> str: |
| 30 | + updated, count = re.subn(pattern, replacement, content, count=1, flags=re.MULTILINE) |
| 31 | + if count != 1: |
| 32 | + raise ValueError(f"Could not update expected version field in {path}") |
| 33 | + return updated |
| 34 | + |
| 35 | + |
| 36 | +def build_expected_files(version: str) -> dict[Path, str]: |
| 37 | + expected: dict[Path, str] = {} |
| 38 | + |
| 39 | + version_py = VERSION_PY_PATH.read_text() |
| 40 | + expected[VERSION_PY_PATH] = replace_first( |
| 41 | + r'^__version__ = "[^"]+"$', |
| 42 | + f'__version__ = "{version}"', |
| 43 | + version_py, |
| 44 | + VERSION_PY_PATH, |
| 45 | + ) |
| 46 | + |
| 47 | + init_py = INIT_PY_PATH.read_text() |
| 48 | + expected[INIT_PY_PATH] = replace_first( |
| 49 | + r'^__version__ = "[^"]+"$', |
| 50 | + f'__version__ = "{version}"', |
| 51 | + init_py, |
| 52 | + INIT_PY_PATH, |
| 53 | + ) |
| 54 | + |
| 55 | + action_yml = ACTION_YML_PATH.read_text() |
| 56 | + expected[ACTION_YML_PATH] = replace_first( |
| 57 | + r'^( image: "docker://ghcr\.io/socketdev/socket-basics:)[^"]+(")$', |
| 58 | + rf'\g<1>{version}\2', |
| 59 | + action_yml, |
| 60 | + ACTION_YML_PATH, |
| 61 | + ) |
| 62 | + |
| 63 | + return expected |
| 64 | + |
| 65 | + |
| 66 | +def check_files(expected: dict[Path, str]) -> list[str]: |
| 67 | + mismatches: list[str] = [] |
| 68 | + for path, rendered in expected.items(): |
| 69 | + current = path.read_text() |
| 70 | + if current != rendered: |
| 71 | + mismatches.append(str(path.relative_to(REPO_ROOT))) |
| 72 | + return mismatches |
| 73 | + |
| 74 | + |
| 75 | +def write_files(expected: dict[Path, str]) -> None: |
| 76 | + for path, rendered in expected.items(): |
| 77 | + path.write_text(rendered) |
| 78 | + |
| 79 | + |
| 80 | +def parse_args() -> argparse.Namespace: |
| 81 | + parser = argparse.ArgumentParser( |
| 82 | + description="Sync socket-basics version metadata from pyproject.toml" |
| 83 | + ) |
| 84 | + group = parser.add_mutually_exclusive_group(required=True) |
| 85 | + group.add_argument( |
| 86 | + "--check", |
| 87 | + action="store_true", |
| 88 | + help="Fail if any derived version files differ from pyproject.toml", |
| 89 | + ) |
| 90 | + group.add_argument( |
| 91 | + "--write", |
| 92 | + action="store_true", |
| 93 | + help="Rewrite derived version files to match pyproject.toml", |
| 94 | + ) |
| 95 | + return parser.parse_args() |
| 96 | + |
| 97 | + |
| 98 | +def main() -> int: |
| 99 | + args = parse_args() |
| 100 | + version = read_canonical_version() |
| 101 | + expected = build_expected_files(version) |
| 102 | + |
| 103 | + if args.write: |
| 104 | + write_files(expected) |
| 105 | + print(f"Synchronized release version metadata to {version}") |
| 106 | + return 0 |
| 107 | + |
| 108 | + mismatches = check_files(expected) |
| 109 | + if mismatches: |
| 110 | + print(f"Release version metadata is out of sync with pyproject.toml ({version}):") |
| 111 | + for mismatch in mismatches: |
| 112 | + print(f" - {mismatch}") |
| 113 | + print("Run: python3 scripts/sync_release_version.py --write") |
| 114 | + return 1 |
| 115 | + |
| 116 | + print(f"Release version metadata is in sync: {version}") |
| 117 | + return 0 |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + sys.exit(main()) |
0 commit comments