|
| 1 | +"""Check that release metadata stays synchronized.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import ast |
| 6 | +import re |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +ROOT = Path(__file__).resolve().parents[1] |
| 11 | +PYPROJECT = ROOT / "pyproject.toml" |
| 12 | +PACKAGE_INIT = ROOT / "src" / "archunitpython" / "__init__.py" |
| 13 | +CHANGELOG = ROOT / "CHANGELOG.md" |
| 14 | + |
| 15 | + |
| 16 | +def read_project_version() -> str: |
| 17 | + content = PYPROJECT.read_text(encoding="utf-8") |
| 18 | + match = re.search(r'^version = "([^"]+)"$', content, re.MULTILINE) |
| 19 | + if match is None: |
| 20 | + raise RuntimeError("Could not find project.version in pyproject.toml") |
| 21 | + return match.group(1) |
| 22 | + |
| 23 | + |
| 24 | +def read_package_version() -> str: |
| 25 | + module = ast.parse(PACKAGE_INIT.read_text(encoding="utf-8")) |
| 26 | + for statement in module.body: |
| 27 | + if ( |
| 28 | + isinstance(statement, ast.Assign) |
| 29 | + and len(statement.targets) == 1 |
| 30 | + and isinstance(statement.targets[0], ast.Name) |
| 31 | + and statement.targets[0].id == "__version__" |
| 32 | + and isinstance(statement.value, ast.Constant) |
| 33 | + and isinstance(statement.value.value, str) |
| 34 | + ): |
| 35 | + return statement.value.value |
| 36 | + raise RuntimeError("Could not find __version__ in src/archunitpython/__init__.py") |
| 37 | + |
| 38 | + |
| 39 | +def changelog_contains_version(version: str) -> bool: |
| 40 | + content = CHANGELOG.read_text(encoding="utf-8") |
| 41 | + heading_pattern = re.compile( |
| 42 | + rf"^#+\s+(?:\[)?{re.escape(version)}(?:\])?(?:\s|\(|$)", |
| 43 | + re.MULTILINE, |
| 44 | + ) |
| 45 | + return heading_pattern.search(content) is not None |
| 46 | + |
| 47 | + |
| 48 | +def main() -> int: |
| 49 | + project_version = read_project_version() |
| 50 | + package_version = read_package_version() |
| 51 | + |
| 52 | + errors = [] |
| 53 | + if package_version != project_version: |
| 54 | + errors.append( |
| 55 | + f"Package __version__ ({package_version}) does not match " |
| 56 | + f"pyproject.toml version ({project_version})." |
| 57 | + ) |
| 58 | + if not changelog_contains_version(project_version): |
| 59 | + errors.append(f"CHANGELOG.md does not contain a heading for version {project_version}.") |
| 60 | + |
| 61 | + if errors: |
| 62 | + print("Release metadata check failed:", file=sys.stderr) |
| 63 | + for error in errors: |
| 64 | + print(f"- {error}", file=sys.stderr) |
| 65 | + return 1 |
| 66 | + |
| 67 | + print(f"Release metadata is synchronized for version {project_version}.") |
| 68 | + return 0 |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + raise SystemExit(main()) |
0 commit comments