|
1 | 1 | #!/usr/bin/env python3 |
2 | | -"""Check that pyproject version matches the latest git tag.""" |
| 2 | +"""Check that pyproject version matches the latest git tag. Optionally fix it by tagging.""" |
3 | 3 | from pathlib import Path |
4 | 4 | import subprocess |
5 | 5 | import sys |
| 6 | + |
6 | 7 | if sys.version_info >= (3, 11): |
7 | 8 | import tomllib as tomli |
8 | 9 | else: |
9 | 10 | import tomli |
10 | 11 |
|
11 | | - |
12 | 12 | ROOT = Path(__file__).resolve().parents[1] |
13 | 13 |
|
14 | | - |
15 | 14 | def pyproject_version() -> str: |
16 | 15 | pyproject_path = ROOT / "pyproject.toml" |
17 | 16 | with pyproject_path.open("rb") as f: |
18 | 17 | data = tomli.load(f) |
19 | 18 | return data["tool"]["poetry"]["version"] |
20 | 19 |
|
21 | | - |
22 | 20 | def latest_tag() -> str: |
23 | 21 | try: |
24 | 22 | tag = subprocess.check_output( |
25 | 23 | ["git", "describe", "--tags", "--abbrev=0"], cwd=ROOT, text=True |
26 | 24 | ).strip() |
27 | 25 | return tag.lstrip("v") |
28 | 26 | except subprocess.CalledProcessError: |
29 | | - return "0.0.0" |
| 27 | + return "" |
30 | 28 |
|
| 29 | +def create_tag(version: str) -> None: |
| 30 | + print(f"Tagging repository with version: v{version}") |
| 31 | + subprocess.run(["git", "tag", f"v{version}"], cwd=ROOT, check=True) |
| 32 | + subprocess.run(["git", "push", "origin", f"v{version}"], cwd=ROOT, check=True) |
| 33 | + print(f"✅ Git tag v{version} created and pushed.") |
31 | 34 |
|
32 | 35 | def main() -> int: |
33 | | - tag = latest_tag() |
| 36 | + fix = "--fix" in sys.argv |
34 | 37 | version = pyproject_version() |
| 38 | + tag = latest_tag() |
35 | 39 |
|
36 | 40 | if not tag: |
37 | | - print("No git tag found", file=sys.stderr) |
38 | | - return 1 |
| 41 | + print("⚠️ No git tag found.", file=sys.stderr) |
| 42 | + if fix: |
| 43 | + create_tag(version) |
| 44 | + return 0 |
| 45 | + else: |
| 46 | + return 1 |
39 | 47 |
|
40 | 48 | if version != tag: |
41 | 49 | print( |
42 | | - f"Version mismatch: pyproject.toml has {version} but latest tag is {tag}", |
| 50 | + f"❌ Version mismatch: pyproject.toml has {version} but latest tag is {tag}", |
43 | 51 | file=sys.stderr, |
44 | 52 | ) |
| 53 | + if fix: |
| 54 | + create_tag(version) |
| 55 | + return 0 |
45 | 56 | return 1 |
46 | 57 |
|
47 | | - print(f"Version matches latest tag: {version}") |
| 58 | + print(f"✔️ Version matches latest tag: {version}") |
48 | 59 | return 0 |
49 | 60 |
|
50 | | - |
51 | 61 | if __name__ == "__main__": |
52 | 62 | sys.exit(main()) |
0 commit comments