93 lines
3.2 KiB
Python
93 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
||
"""Check that a change advances the PLBA package version by one SemVer step."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import re
|
||
import subprocess
|
||
import sys
|
||
import tomllib
|
||
from pathlib import Path
|
||
|
||
VERSION_PATTERN = re.compile(r"^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$")
|
||
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
||
|
||
|
||
def parse_version(raw_version: object, source: str) -> tuple[int, int, int]:
|
||
if not isinstance(raw_version, str):
|
||
raise ValueError(f"В {source} поле project.version должно быть строкой")
|
||
match = VERSION_PATTERN.fullmatch(raw_version)
|
||
if match is None:
|
||
raise ValueError(
|
||
f"Версия {raw_version!r} в {source} должна иметь формат MAJOR.MINOR.PATCH"
|
||
)
|
||
return tuple(int(part) for part in match.groups())
|
||
|
||
|
||
def read_version(toml_text: str, source: str) -> tuple[int, int, int]:
|
||
project = tomllib.loads(toml_text).get("project", {})
|
||
if not isinstance(project, dict):
|
||
raise ValueError(f"В {source} отсутствует секция [project]")
|
||
return parse_version(project.get("version"), source)
|
||
|
||
|
||
def read_base_version(base_ref: str) -> tuple[int, int, int]:
|
||
result = subprocess.run(
|
||
["git", "show", f"{base_ref}:pyproject.toml"],
|
||
check=True,
|
||
capture_output=True,
|
||
text=True,
|
||
cwd=PROJECT_ROOT,
|
||
)
|
||
return read_version(result.stdout, f"{base_ref}:pyproject.toml")
|
||
|
||
|
||
def allowed_next_versions(base: tuple[int, int, int]) -> set[tuple[int, int, int]]:
|
||
major, minor, patch = base
|
||
patch_bump = (major, minor, patch + 1)
|
||
minor_bump = (major, minor + 1, 0)
|
||
if major == 0:
|
||
return {patch_bump, minor_bump, (1, 0, 0)}
|
||
return {patch_bump, minor_bump, (major + 1, 0, 0)}
|
||
|
||
|
||
def format_version(version: tuple[int, int, int]) -> str:
|
||
return ".".join(str(part) for part in version)
|
||
|
||
|
||
def main() -> int:
|
||
parser = argparse.ArgumentParser(description=__doc__)
|
||
parser.add_argument(
|
||
"--base-ref",
|
||
required=True,
|
||
help="Git ref целевой ветки pull request, например origin/main",
|
||
)
|
||
args = parser.parse_args()
|
||
|
||
try:
|
||
base = read_base_version(args.base_ref)
|
||
current_text = (PROJECT_ROOT / "pyproject.toml").read_text(encoding="utf-8")
|
||
current = read_version(current_text, "pyproject.toml")
|
||
except (OSError, ValueError, tomllib.TOMLDecodeError, subprocess.CalledProcessError) as error:
|
||
print(f"Ошибка проверки версии: {error}", file=sys.stderr)
|
||
return 1
|
||
|
||
if current not in allowed_next_versions(base):
|
||
allowed = ", ".join(
|
||
format_version(version) for version in sorted(allowed_next_versions(base))
|
||
)
|
||
print(
|
||
f"Версия {format_version(current)} должна быть ровно одним шагом выше "
|
||
f"{format_version(base)}. Допустимые версии: {allowed}.",
|
||
file=sys.stderr,
|
||
)
|
||
return 1
|
||
|
||
print(f"Версия пакета увеличена: {format_version(base)} -> {format_version(current)}")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|