-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-changelog.py
More file actions
executable file
·36 lines (26 loc) · 1000 Bytes
/
extract-changelog.py
File metadata and controls
executable file
·36 lines (26 loc) · 1000 Bytes
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
#!/usr/bin/env python3
"""Print the Keep a Changelog section for a release version."""
from __future__ import annotations
import re
import sys
from pathlib import Path
def extract(changelog: str, version: str) -> str:
pattern = rf"^## \[{re.escape(version)}\].*$"
match = re.search(pattern, changelog, re.M)
if not match:
raise SystemExit(f"no changelog section for {version}")
start = match.start()
rest = changelog[match.end() :]
next_heading = re.search(r"^## \[", rest, re.M)
end = match.end() + (next_heading.start() if next_heading else len(rest))
section = changelog[start:end].strip()
title, _, body = section.partition("\n")
return body.strip() or f"Release {version}."
def main() -> None:
if len(sys.argv) != 2:
raise SystemExit("usage: extract-changelog.py VERSION")
version = sys.argv[1]
changelog = Path("CHANGELOG.md").read_text()
print(extract(changelog, version))
if __name__ == "__main__":
main()