1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#!/usr/bin/python3
import argparse
import pathlib
import re
import subprocess
ROOT = pathlib.Path(__file__).resolve().parent.parent
PYPROJECT = ROOT / "pyproject.toml"
BACKUP = ROOT / "debian" / ".pyproject.toml.orig"
def get_upstream_version() -> str:
cmd = ["dpkg-parsechangelog", "-S", "Version"]
version = subprocess.check_output(cmd, text=True).strip()
match = re.match(r"^(?:\\d+:)?([^-]+)(?:-.*)?$", version)
if not match:
raise ValueError(f"Unexpected changelog version format: {version!r}")
return match.group(1)
def apply_fixup() -> None:
text = PYPROJECT.read_text(encoding="utf-8")
if not BACKUP.exists():
BACKUP.write_text(text, encoding="utf-8")
version = get_upstream_version()
text = re.sub(
r'^dynamic = \["version"\]\n', f'version = "{version}"\n', text, flags=re.M
)
PYPROJECT.write_text(text, encoding="utf-8")
def restore_fixup() -> None:
if BACKUP.exists():
PYPROJECT.write_text(BACKUP.read_text(encoding="utf-8"), encoding="utf-8")
BACKUP.unlink()
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--apply", action="store_true")
parser.add_argument("--restore", action="store_true")
args = parser.parse_args()
if args.apply == args.restore:
raise SystemExit("Use exactly one of --apply or --restore")
if args.apply:
apply_fixup()
return
restore_fixup()
if __name__ == "__main__":
main()
|