|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Check local release versions and git tags for consistency.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +ROOT = Path(__file__).resolve().parents[1] |
| 13 | + |
| 14 | +LOCAL_VERSION_SOURCES: dict[str, Path] = { |
| 15 | + "SKILL.md": ROOT / "skills" / "python-project-workflow" / "SKILL.md", |
| 16 | + "CITATION.cff": ROOT / "CITATION.cff", |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +def normalize_version(version: str) -> str: |
| 21 | + return version.removeprefix("v") |
| 22 | + |
| 23 | + |
| 24 | +def read_skill_version(path: Path) -> str | None: |
| 25 | + """Extract metadata version from SKILL.md frontmatter.""" |
| 26 | + try: |
| 27 | + content = path.read_text(encoding="utf-8") |
| 28 | + except OSError: |
| 29 | + return None |
| 30 | + frontmatter = content.split("---", 2) |
| 31 | + if len(frontmatter) < 3: |
| 32 | + return None |
| 33 | + match = re.search(r'(?m)^version:\s*["\']?([^"\'#\s]+)', frontmatter[1]) |
| 34 | + return match.group(1) if match else None |
| 35 | + |
| 36 | + |
| 37 | +def read_citation_version(path: Path) -> str | None: |
| 38 | + """Extract the version field from CITATION.cff.""" |
| 39 | + try: |
| 40 | + content = path.read_text(encoding="utf-8") |
| 41 | + except OSError: |
| 42 | + return None |
| 43 | + match = re.search(r'(?m)^version:\s*["\']?([^"\'#\s]+)', content) |
| 44 | + return match.group(1) if match else None |
| 45 | + |
| 46 | + |
| 47 | +def get_latest_tag() -> tuple[str | None, str | None]: |
| 48 | + """Return the latest v-prefixed tag and an optional query error.""" |
| 49 | + try: |
| 50 | + result = subprocess.run( |
| 51 | + ["git", "tag", "--list", "v*", "--sort=-version:refname"], |
| 52 | + capture_output=True, |
| 53 | + text=True, |
| 54 | + check=False, |
| 55 | + ) |
| 56 | + except OSError as exc: |
| 57 | + return None, str(exc) |
| 58 | + if result.returncode != 0: |
| 59 | + return None, result.stderr.strip() or f"git exited {result.returncode}" |
| 60 | + tags = result.stdout.splitlines() |
| 61 | + return (tags[0], None) if tags else (None, None) |
| 62 | + |
| 63 | + |
| 64 | +def validate_versions( |
| 65 | + local_versions: dict[str, str | None], |
| 66 | + latest_tag: str | None, |
| 67 | + tag_error: str | None, |
| 68 | +) -> tuple[list[str], list[str]]: |
| 69 | + """Validate version consistency and return errors plus informational notes.""" |
| 70 | + errors: list[str] = [] |
| 71 | + notes: list[str] = [] |
| 72 | + comparable: dict[str, str] = {} |
| 73 | + |
| 74 | + for source in LOCAL_VERSION_SOURCES: |
| 75 | + version = local_versions.get(source) |
| 76 | + if not version: |
| 77 | + errors.append(f"Could not extract version from {source}") |
| 78 | + else: |
| 79 | + comparable[source] = normalize_version(version) |
| 80 | + |
| 81 | + if tag_error: |
| 82 | + notes.append(f"SKIP: could not query git tags: {tag_error}") |
| 83 | + elif latest_tag: |
| 84 | + comparable["latest tag"] = normalize_version(latest_tag) |
| 85 | + else: |
| 86 | + notes.append("SKIP: no v-prefixed git tags found") |
| 87 | + |
| 88 | + if comparable and len(set(comparable.values())) > 1: |
| 89 | + rendered = ", ".join( |
| 90 | + f"{source}={version}" for source, version in comparable.items() |
| 91 | + ) |
| 92 | + errors.append(f"Version drift: {rendered}") |
| 93 | + |
| 94 | + return errors, notes |
| 95 | + |
| 96 | + |
| 97 | +def run_self_tests() -> int: |
| 98 | + """Cover aligned, drifted, and no-tag states.""" |
| 99 | + aligned = {source: "0.1.0" for source in LOCAL_VERSION_SOURCES} |
| 100 | + |
| 101 | + errors, notes = validate_versions(aligned, "v0.1.0", None) |
| 102 | + assert errors == [] and notes == [] |
| 103 | + |
| 104 | + drifted = {**aligned, "CITATION.cff": "0.2.0"} |
| 105 | + errors, _ = validate_versions(drifted, "v0.1.0", None) |
| 106 | + assert any(error.startswith("Version drift:") for error in errors) |
| 107 | + |
| 108 | + errors, notes = validate_versions(aligned, None, None) |
| 109 | + assert errors == [] and notes == ["SKIP: no v-prefixed git tags found"] |
| 110 | + |
| 111 | + errors, notes = validate_versions(aligned, None, "not a git repository") |
| 112 | + assert errors == [] and notes == [ |
| 113 | + "SKIP: could not query git tags: not a git repository" |
| 114 | + ] |
| 115 | + |
| 116 | + print("PASS: check-version-consistency.py self-tests") |
| 117 | + return 0 |
| 118 | + |
| 119 | + |
| 120 | +def main() -> int: |
| 121 | + import argparse |
| 122 | + |
| 123 | + parser = argparse.ArgumentParser(description=__doc__) |
| 124 | + parser.add_argument("--self-test", action="store_true") |
| 125 | + args = parser.parse_args() |
| 126 | + |
| 127 | + if args.self_test: |
| 128 | + return run_self_tests() |
| 129 | + |
| 130 | + local_versions = { |
| 131 | + "SKILL.md": read_skill_version(LOCAL_VERSION_SOURCES["SKILL.md"]), |
| 132 | + "CITATION.cff": read_citation_version(LOCAL_VERSION_SOURCES["CITATION.cff"]), |
| 133 | + } |
| 134 | + latest_tag, tag_error = get_latest_tag() |
| 135 | + |
| 136 | + for source, version in local_versions.items(): |
| 137 | + print(f"{source} version: {version or 'unreadable'}") |
| 138 | + print(f"Latest tag: {latest_tag or 'none'}") |
| 139 | + |
| 140 | + errors, notes = validate_versions(local_versions, latest_tag, tag_error) |
| 141 | + for note in notes: |
| 142 | + print(note) |
| 143 | + if errors: |
| 144 | + for error in errors: |
| 145 | + print(f"ERROR: {error}", file=sys.stderr) |
| 146 | + return 1 |
| 147 | + |
| 148 | + print("OK: all available version sources are consistent") |
| 149 | + return 0 |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + sys.exit(main()) |
0 commit comments