#!/usr/bin/env python3 """Read a Frappe app's stable __version__ from a local file or Git ref.""" from __future__ import annotations import argparse import re import subprocess def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser() parser.add_argument("version_file") parser.add_argument("--ref") return parser.parse_args() def main() -> None: args = parse_args() try: if args.ref: content = subprocess.check_output( ["git", "show", f"{args.ref}:{args.version_file}"], text=True, ) else: with open(args.version_file, encoding="utf-8") as version_file: content = version_file.read() except (OSError, subprocess.CalledProcessError) as error: raise SystemExit(f"Could not load {args.version_file}: {error}") from error match = re.search(r'^__version__ = "(\d+\.\d+\.\d+)"$', content, re.MULTILINE) if not match: raise SystemExit( f"Could not read a stable MAJOR.MINOR.PATCH __version__ from {args.version_file}" ) print(match.group(1)) if __name__ == "__main__": main()