Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions scripts/check_version_match.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,62 @@
#!/usr/bin/env python3
"""Check that pyproject version matches the latest git tag."""
"""Check that pyproject version matches the latest git tag. Optionally fix it by tagging."""
from pathlib import Path
import subprocess
import sys

if sys.version_info >= (3, 11):
import tomllib as tomli
else:
import tomli


ROOT = Path(__file__).resolve().parents[1]


def pyproject_version() -> str:
pyproject_path = ROOT / "pyproject.toml"
with pyproject_path.open("rb") as f:
data = tomli.load(f)
return data["tool"]["poetry"]["version"]


def latest_tag() -> str:
try:
tag = subprocess.check_output(
["git", "describe", "--tags", "--abbrev=0"], cwd=ROOT, text=True
).strip()
return tag.lstrip("v")
except subprocess.CalledProcessError:
return "0.0.0"
return ""

def create_tag(version: str) -> None:
print(f"Tagging repository with version: v{version}")
subprocess.run(["git", "tag", f"v{version}"], cwd=ROOT, check=True)
subprocess.run(["git", "push", "origin", f"v{version}"], cwd=ROOT, check=True)
print(f"✅ Git tag v{version} created and pushed.")

def main() -> int:
tag = latest_tag()
fix = "--fix" in sys.argv
version = pyproject_version()
tag = latest_tag()

if not tag:
print("No git tag found", file=sys.stderr)
return 1
print("⚠️ No git tag found.", file=sys.stderr)
if fix:
create_tag(version)
return 0
else:
return 1

if version != tag:
print(
f"Version mismatch: pyproject.toml has {version} but latest tag is {tag}",
f"Version mismatch: pyproject.toml has {version} but latest tag is {tag}",
file=sys.stderr,
)
if fix:
create_tag(version)
return 0
return 1

print(f"Version matches latest tag: {version}")
print(f"✔️ Version matches latest tag: {version}")
return 0


if __name__ == "__main__":
sys.exit(main())