|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import pathlib |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | + |
| 9 | + |
| 10 | +ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 11 | +DEFAULT_CHANGELOG = ROOT / "CHANGELOG.md" |
| 12 | +DEFAULT_DOCKERFILE = ROOT / "Dockerfile" |
| 13 | +DEFAULT_UPSTREAM = ROOT / "upstream.toml" |
| 14 | + |
| 15 | + |
| 16 | +def load_upstream_version_key(path: pathlib.Path) -> str: |
| 17 | + in_upstream = False |
| 18 | + pattern = re.compile(r'^version_key\s*=\s*"([^"]+)"\s*$') |
| 19 | + for raw_line in path.read_text().splitlines(): |
| 20 | + line = raw_line.strip() |
| 21 | + if not line or line.startswith("#"): |
| 22 | + continue |
| 23 | + if line.startswith("[") and line.endswith("]"): |
| 24 | + in_upstream = line == "[upstream]" |
| 25 | + continue |
| 26 | + if not in_upstream: |
| 27 | + continue |
| 28 | + match = pattern.match(line) |
| 29 | + if match: |
| 30 | + return match.group(1) |
| 31 | + return "UPSTREAM_VERSION" |
| 32 | + |
| 33 | + |
| 34 | +def read_upstream_version(dockerfile: pathlib.Path, upstream: pathlib.Path) -> str: |
| 35 | + version_key = load_upstream_version_key(upstream) |
| 36 | + pattern = re.compile(rf"^ARG {re.escape(version_key)}=(.+)$") |
| 37 | + for line in dockerfile.read_text().splitlines(): |
| 38 | + match = pattern.match(line.strip()) |
| 39 | + if match: |
| 40 | + return match.group(1).split("@", 1)[0] |
| 41 | + raise SystemExit(f"Unable to find ARG {version_key} in {dockerfile}") |
| 42 | + |
| 43 | + |
| 44 | +def git_tags() -> list[str]: |
| 45 | + output = subprocess.check_output(["git", "tag", "--list"], cwd=ROOT, text=True) |
| 46 | + return [line.strip() for line in output.splitlines() if line.strip()] |
| 47 | + |
| 48 | + |
| 49 | +def next_release_version(dockerfile: pathlib.Path, upstream: pathlib.Path) -> str: |
| 50 | + upstream_version = read_upstream_version(dockerfile, upstream) |
| 51 | + pattern = re.compile(rf"^{re.escape(upstream_version)}-aio\.(\d+)$") |
| 52 | + revisions = [] |
| 53 | + for tag in git_tags(): |
| 54 | + match = pattern.match(tag) |
| 55 | + if match: |
| 56 | + revisions.append(int(match.group(1))) |
| 57 | + next_revision = max(revisions, default=0) + 1 |
| 58 | + return f"{upstream_version}-aio.{next_revision}" |
| 59 | + |
| 60 | + |
| 61 | +def latest_changelog_version(changelog: pathlib.Path) -> str: |
| 62 | + pattern = re.compile(r"^##\s+([^\s]+)") |
| 63 | + for line in changelog.read_text().splitlines(): |
| 64 | + match = pattern.match(line.strip()) |
| 65 | + if match and match.group(1) != "Unreleased": |
| 66 | + return match.group(1) |
| 67 | + raise SystemExit(f"Unable to find a released version heading in {changelog}") |
| 68 | + |
| 69 | + |
| 70 | +def extract_release_notes(version: str, changelog: pathlib.Path) -> str: |
| 71 | + heading = re.compile(rf"^##\s+{re.escape(version)}(?:\s+-\s+.+)?$") |
| 72 | + next_heading = re.compile(r"^##\s+") |
| 73 | + lines = changelog.read_text().splitlines() |
| 74 | + start = None |
| 75 | + for index, line in enumerate(lines): |
| 76 | + if heading.match(line.strip()): |
| 77 | + start = index + 1 |
| 78 | + break |
| 79 | + if start is None: |
| 80 | + raise SystemExit(f"Unable to find release section for {version} in {changelog}") |
| 81 | + end = len(lines) |
| 82 | + for index in range(start, len(lines)): |
| 83 | + if next_heading.match(lines[index].strip()): |
| 84 | + end = index |
| 85 | + break |
| 86 | + notes = "\n".join(lines[start:end]).strip() |
| 87 | + if not notes: |
| 88 | + raise SystemExit(f"Release section for {version} in {changelog} is empty") |
| 89 | + return notes |
| 90 | + |
| 91 | + |
| 92 | +def main() -> None: |
| 93 | + parser = argparse.ArgumentParser(description="Release helpers for AIO repos.") |
| 94 | + subparsers = parser.add_subparsers(dest="command", required=True) |
| 95 | + upstream_parser = subparsers.add_parser("upstream-version") |
| 96 | + upstream_parser.add_argument("--dockerfile", type=pathlib.Path, default=DEFAULT_DOCKERFILE) |
| 97 | + upstream_parser.add_argument("--upstream-config", type=pathlib.Path, default=DEFAULT_UPSTREAM) |
| 98 | + next_parser = subparsers.add_parser("next-version") |
| 99 | + next_parser.add_argument("--dockerfile", type=pathlib.Path, default=DEFAULT_DOCKERFILE) |
| 100 | + next_parser.add_argument("--upstream-config", type=pathlib.Path, default=DEFAULT_UPSTREAM) |
| 101 | + latest_parser = subparsers.add_parser("latest-changelog-version") |
| 102 | + latest_parser.add_argument("--changelog", type=pathlib.Path, default=DEFAULT_CHANGELOG) |
| 103 | + notes_parser = subparsers.add_parser("extract-release-notes") |
| 104 | + notes_parser.add_argument("version") |
| 105 | + notes_parser.add_argument("--changelog", type=pathlib.Path, default=DEFAULT_CHANGELOG) |
| 106 | + args = parser.parse_args() |
| 107 | + if args.command == "upstream-version": |
| 108 | + print(read_upstream_version(args.dockerfile, args.upstream_config)) |
| 109 | + elif args.command == "next-version": |
| 110 | + print(next_release_version(args.dockerfile, args.upstream_config)) |
| 111 | + elif args.command == "latest-changelog-version": |
| 112 | + print(latest_changelog_version(args.changelog)) |
| 113 | + else: |
| 114 | + print(extract_release_notes(args.version, args.changelog)) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments