Files
bump-erp-app-version/scripts/read_version.py
T
NikoKS 59e040994b
Test action / integration (push) Failing after 12s
Update major version tag / update-major-tag (push) Successful in 5s
fix: bootstrap apps without version config
2026-08-26 23:34:46 +07:00

75 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""Read bump-my-version's current_version from a local file or Git ref."""
from __future__ import annotations
import argparse
import re
import subprocess
import sys
import tomllib
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("config_file")
parser.add_argument("--ref")
parser.add_argument("--fallback-version-file")
return parser.parse_args()
def read_bootstrap_version(args: argparse.Namespace, config_error: KeyError) -> str:
if not args.ref or not args.fallback_version_file:
raise SystemExit(
f"Could not read tool.bumpversion.current_version from {args.config_file}: {config_error}"
) from config_error
try:
version_content = subprocess.check_output(
["git", "show", f"{args.ref}:{args.fallback_version_file}"],
text=True,
)
except (OSError, subprocess.CalledProcessError) as error:
raise SystemExit(
f"Could not load bootstrap version file {args.fallback_version_file}: {error}"
) from error
match = re.search(r'^__version__ = "(\d+\.\d+\.\d+)"$', version_content, re.MULTILINE)
if not match:
raise SystemExit(f"Could not read __version__ from bootstrap file {args.fallback_version_file}")
version = match.group(1)
print(f"Using bootstrap version {version} from {args.fallback_version_file}", file=sys.stderr)
return version
def main() -> None:
args = parse_args()
try:
if args.ref:
content = subprocess.check_output(
["git", "show", f"{args.ref}:{args.config_file}"],
)
else:
with open(args.config_file, "rb") as config_file:
content = config_file.read()
except (OSError, subprocess.CalledProcessError) as error:
raise SystemExit(f"Could not load {args.config_file}: {error}") from error
try:
version = tomllib.loads(content.decode())["tool"]["bumpversion"]["current_version"]
except KeyError as error:
version = read_bootstrap_version(args, error)
except (TypeError, UnicodeDecodeError, tomllib.TOMLDecodeError) as error:
raise SystemExit(
f"Could not read tool.bumpversion.current_version from {args.config_file}: {error}"
) from error
if not isinstance(version, str):
raise SystemExit("tool.bumpversion.current_version must be a string")
print(version)
if __name__ == "__main__":
main()