#!/usr/bin/env python3 """Print the body of the CHANGELOG.md section for a version ("## [X.Y.Z] ..."). Exits non-zero if the section is missing or empty. Usage: extract_changelog.py VERSION [CHANGELOG.md] """ import re import sys def main() -> int: if len(sys.argv) < 2: sys.exit(__doc__) version = sys.argv[1].lstrip("v") path = sys.argv[2] if len(sys.argv) > 2 else "CHANGELOG.md" with open(path, encoding="utf-8") as f: lines = f.read().splitlines() head = re.compile(r"^## \[" + re.escape(version) + r"\]") out, inside = [], False for line in lines: if inside and line.startswith("## "): break if inside: out.append(line) elif head.match(line): inside = True body = "\n".join(out).strip() if not inside or not body: sys.exit(f"CHANGELOG.md has no non-empty section for [{version}]") print(body) return 0 if __name__ == "__main__": sys.exit(main())