|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Generate version.json with git hash and timestamp. |
| 4 | +
|
| 5 | +This file is used by the frontend to detect when a new version is deployed. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import subprocess |
| 10 | +from datetime import datetime, timezone |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +def get_git_hash() -> str: |
| 15 | + """Get the current git commit hash.""" |
| 16 | + try: |
| 17 | + result = subprocess.run( |
| 18 | + ["git", "rev-parse", "HEAD"], |
| 19 | + capture_output=True, |
| 20 | + text=True, |
| 21 | + check=True, |
| 22 | + ) |
| 23 | + return result.stdout.strip() |
| 24 | + except subprocess.CalledProcessError: |
| 25 | + return "unknown" |
| 26 | + |
| 27 | + |
| 28 | +def get_git_branch() -> str: |
| 29 | + """Get the current git branch name.""" |
| 30 | + try: |
| 31 | + result = subprocess.run( |
| 32 | + ["git", "rev-parse", "--abbrev-ref", "HEAD"], |
| 33 | + capture_output=True, |
| 34 | + text=True, |
| 35 | + check=True, |
| 36 | + ) |
| 37 | + return result.stdout.strip() |
| 38 | + except subprocess.CalledProcessError: |
| 39 | + return "unknown" |
| 40 | + |
| 41 | + |
| 42 | +def generate_version_file() -> None: |
| 43 | + """Generate static/version.json with current git info.""" |
| 44 | + version_data = { |
| 45 | + "hash": get_git_hash(), |
| 46 | + "branch": get_git_branch(), |
| 47 | + "timestamp": datetime.now(timezone.utc).isoformat(), |
| 48 | + } |
| 49 | + |
| 50 | + # Write to static directory |
| 51 | + static_dir = Path(__file__).parent / "static" |
| 52 | + static_dir.mkdir(exist_ok=True) |
| 53 | + |
| 54 | + version_file = static_dir / "version.json" |
| 55 | + with open(version_file, "w") as f: |
| 56 | + json.dump(version_data, f, indent=2) |
| 57 | + |
| 58 | + print(f"✅ Generated {version_file}") |
| 59 | + print(f" Hash: {version_data['hash'][:8]}") |
| 60 | + print(f" Branch: {version_data['branch']}") |
| 61 | + print(f" Timestamp: {version_data['timestamp']}") |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + generate_version_file() |
0 commit comments