Files
llamamod/tools/release/changelog_section.py
T

39 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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 "## <version>" 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()