#!/usr/bin/env python3 """Prints the CHANGELOG.md section of one version (used as the release text). changelog_section.py 0.2.1 [CHANGELOG.md] A section starts at a "## " heading (anything may follow the version, e.g. " – changes since 0.1.0") and ends before the next "## " heading. Exit code 1 if the version has no section. """ import re import sys def section(text, version): out, inside = [], False for line in text.splitlines(): if line.startswith("## "): if inside: break inside = re.match(r"## v?" + re.escape(version) + r"(\s|$)", line) is not None continue if inside: out.append(line) return "\n".join(out).strip() if inside or out else None def main(): if len(sys.argv) < 2: sys.exit(__doc__) path = sys.argv[2] if len(sys.argv) > 2 else "CHANGELOG.md" with open(path, encoding="utf-8") as f: s = section(f.read(), sys.argv[1]) if not s: sys.exit("no section for version %s in %s" % (sys.argv[1], path)) print(s) if __name__ == "__main__": main()