|
| 1 | +"""Keep the release version consistent across the three metadata files. |
| 2 | +
|
| 3 | +A pamica release version lives in three places that must agree, because each is |
| 4 | +consumed by a different tool at release time: |
| 5 | +
|
| 6 | +- ``pyproject.toml`` -> the wheel/sdist that ``publish.yml`` uploads to PyPI. |
| 7 | +- ``CITATION.cff`` -> the citation record (``version`` + ``date-released``). |
| 8 | +- ``.zenodo.json`` -> the Zenodo archive metadata (``version`` + |
| 9 | + ``publication_date``); Zenodo reads it from the *tag's* tree when the GitHub |
| 10 | + release is published, so it must be correct **before** tagging, not committed |
| 11 | + afterward. |
| 12 | +
|
| 13 | +Two modes: |
| 14 | +
|
| 15 | + python scripts/sync_version.py sync 0.1.3 # writes all three |
| 16 | + python scripts/sync_version.py sync 0.1.3 --date 2026-08-01 |
| 17 | + python scripts/sync_version.py check 0.1.3 # verifies, exit 1 on drift |
| 18 | +
|
| 19 | +``sync`` is the release-prep step: run it, then ``uv lock`` (so ``uv.lock`` |
| 20 | +tracks the new version), commit "Bump version to X.Y.Z", and tag. |
| 21 | +``check`` is the release gate that ``publish.yml`` runs against the release tag, |
| 22 | +so a mistagged or half-bumped release fails before anything reaches PyPI. |
| 23 | +
|
| 24 | +Only string-level substitutions are used so the files' formatting, key order, |
| 25 | +and comments are preserved untouched. |
| 26 | +""" |
| 27 | + |
| 28 | +from __future__ import annotations |
| 29 | + |
| 30 | +import argparse |
| 31 | +import json |
| 32 | +import re |
| 33 | +import sys |
| 34 | +import tomllib |
| 35 | +from datetime import date |
| 36 | +from pathlib import Path |
| 37 | + |
| 38 | +ROOT = Path(__file__).resolve().parent.parent |
| 39 | +PYPROJECT = ROOT / "pyproject.toml" |
| 40 | +CITATION = ROOT / "CITATION.cff" |
| 41 | +ZENODO = ROOT / ".zenodo.json" |
| 42 | + |
| 43 | +_SEMVER = re.compile(r"^\d+\.\d+\.\d+([.\-+][0-9A-Za-z.\-+]+)?$") |
| 44 | +_ISO_DATE = re.compile(r"^\d{4}-\d{2}-\d{2}$") |
| 45 | + |
| 46 | + |
| 47 | +def read_versions() -> dict[str, str]: |
| 48 | + """Return the version string currently declared in each metadata file.""" |
| 49 | + pyproject = tomllib.loads(PYPROJECT.read_text())["project"]["version"] |
| 50 | + |
| 51 | + m = re.search(r"(?m)^version:\s*(.+?)\s*$", CITATION.read_text()) |
| 52 | + if m is None: |
| 53 | + raise ValueError("CITATION.cff has no top-level 'version:' key") |
| 54 | + citation = m.group(1).strip().strip('"') |
| 55 | + |
| 56 | + zenodo = json.loads(ZENODO.read_text()).get("version") |
| 57 | + if zenodo is None: |
| 58 | + raise ValueError(".zenodo.json has no 'version' key") |
| 59 | + |
| 60 | + return { |
| 61 | + "pyproject.toml": pyproject, |
| 62 | + "CITATION.cff": citation, |
| 63 | + ".zenodo.json": zenodo, |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +def _sub_once(text: str, pattern: str, repl: str, *, where: str) -> str: |
| 68 | + """Substitute exactly one match, raising if the pattern is absent. |
| 69 | +
|
| 70 | + ``re.sub`` returns the input unchanged (no error) when nothing matches, which |
| 71 | + would let ``sync`` write a file back untouched while reporting success -- a |
| 72 | + silent no-op if a file's formatting ever drifts from what the pattern expects. |
| 73 | + Raising instead turns that drift into a loud, actionable failure. |
| 74 | + """ |
| 75 | + new, n = re.subn(pattern, repl, text, count=1) |
| 76 | + if n == 0: |
| 77 | + raise ValueError(f"{where}: no line matched {pattern!r}; nothing written") |
| 78 | + return new |
| 79 | + |
| 80 | + |
| 81 | +def sync(version: str, released: str) -> None: |
| 82 | + PYPROJECT.write_text( |
| 83 | + _sub_once( |
| 84 | + PYPROJECT.read_text(), |
| 85 | + r'(?m)^version = "[^"]*"', |
| 86 | + f'version = "{version}"', |
| 87 | + where="pyproject.toml version", |
| 88 | + ) |
| 89 | + ) |
| 90 | + |
| 91 | + citation = CITATION.read_text() |
| 92 | + citation = _sub_once( |
| 93 | + citation, |
| 94 | + r"(?m)^version:.*$", |
| 95 | + f"version: {version}", |
| 96 | + where="CITATION.cff version", |
| 97 | + ) |
| 98 | + citation = _sub_once( |
| 99 | + citation, |
| 100 | + r"(?m)^date-released:.*$", |
| 101 | + f'date-released: "{released}"', |
| 102 | + where="CITATION.cff date-released", |
| 103 | + ) |
| 104 | + CITATION.write_text(citation) |
| 105 | + |
| 106 | + zenodo = ZENODO.read_text() |
| 107 | + zenodo = _sub_once( |
| 108 | + zenodo, |
| 109 | + r'"version": "[^"]*"', |
| 110 | + f'"version": "{version}"', |
| 111 | + where=".zenodo.json version", |
| 112 | + ) |
| 113 | + zenodo = _sub_once( |
| 114 | + zenodo, |
| 115 | + r'"publication_date": "[^"]*"', |
| 116 | + f'"publication_date": "{released}"', |
| 117 | + where=".zenodo.json publication_date", |
| 118 | + ) |
| 119 | + ZENODO.write_text(zenodo) |
| 120 | + |
| 121 | + # Belt-and-suspenders: never report success while a file is still out of sync |
| 122 | + # (e.g. a substitution that matched an unexpected line and wrote a wrong value). |
| 123 | + drift = {name: got for name, got in read_versions().items() if got != version} |
| 124 | + if drift: |
| 125 | + raise RuntimeError(f"sync wrote the files but they still disagree: {drift}") |
| 126 | + |
| 127 | + print(f"Set version {version} (released {released}) in all three metadata files.") |
| 128 | + |
| 129 | + |
| 130 | +def check(version: str) -> int: |
| 131 | + versions = read_versions() |
| 132 | + drift = {name: got for name, got in versions.items() if got != version} |
| 133 | + if drift: |
| 134 | + print(f"Version mismatch (expected {version}):", file=sys.stderr) |
| 135 | + for name, got in versions.items(): |
| 136 | + mark = "!=" if name in drift else "==" |
| 137 | + print(f" {name}: {got} {mark} {version}", file=sys.stderr) |
| 138 | + return 1 |
| 139 | + print(f"All metadata files agree on version {version}.") |
| 140 | + return 0 |
| 141 | + |
| 142 | + |
| 143 | +def main() -> int: |
| 144 | + parser = argparse.ArgumentParser(description=__doc__) |
| 145 | + sub = parser.add_subparsers(dest="mode", required=True) |
| 146 | + |
| 147 | + p_sync = sub.add_parser("sync", help="write the version into all three files") |
| 148 | + p_sync.add_argument("version", help="release version, e.g. 0.1.3") |
| 149 | + p_sync.add_argument( |
| 150 | + "--date", |
| 151 | + default=date.today().isoformat(), |
| 152 | + help="release date (ISO YYYY-MM-DD); defaults to today", |
| 153 | + ) |
| 154 | + |
| 155 | + p_check = sub.add_parser("check", help="verify all three files match the version") |
| 156 | + p_check.add_argument("version", help="expected version, e.g. 0.1.3") |
| 157 | + |
| 158 | + args = parser.parse_args() |
| 159 | + version = args.version.removeprefix("v") |
| 160 | + |
| 161 | + if not _SEMVER.match(version): |
| 162 | + parser.error(f"'{version}' is not a MAJOR.MINOR.PATCH version") |
| 163 | + |
| 164 | + if args.mode == "sync": |
| 165 | + if not _ISO_DATE.match(args.date): |
| 166 | + parser.error(f"--date '{args.date}' is not ISO YYYY-MM-DD") |
| 167 | + sync(version, args.date) |
| 168 | + return 0 |
| 169 | + return check(version) |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + raise SystemExit(main()) |
0 commit comments