Debian Codegraph @

scripts/generate-gh-release-notes.py

raw
1 # mypy: disallow-untyped-defs
2 """
3 Script used to generate a Markdown file containing only the changelog entries of a specific pytest release, which
4 is then published as a GitHub Release during deploy (see workflows/deploy.yml).
5
6 The script requires ``pandoc`` to be previously installed in the system -- we need to convert from RST (the format of
7 our CHANGELOG) into Markdown (which is required by GitHub Releases).
8
9 Requires Python3.6+.
10 """
11
12 from __future__ import annotations
13
14 from collections.abc import Sequence
15 from pathlib import Path
16 import re
17 import sys
18
19 import pypandoc
20
21
22 def extract_changelog_entries_for(version: str) -> str:
23 p = Path(__file__).parent.parent / "doc/en/changelog.rst"
24 changelog_lines = p.read_text(encoding="UTF-8").splitlines()
25
26 title_regex = re.compile(r"pytest (\d\.\d+\.\d+\w*) \(\d{4}-\d{2}-\d{2}\)")
27 consuming_version = False
28 version_lines = []
29 for line in changelog_lines:
30 m = title_regex.match(line)
31 if m:
32 # Found the version we want: start to consume lines until we find the next version title.
33 if m.group(1) == version:
34 consuming_version = True
35 # Found a new version title while parsing the version we want: break out.
36 elif consuming_version:
37 break
38 if consuming_version:
39 version_lines.append(line)
40
41 return "\n".join(version_lines)
42
43
44 def convert_rst_to_md(text: str) -> str:
45 result = pypandoc.convert_text(
46 text, "gfm", format="rst", extra_args=["--wrap=preserve"]
47 )
48 assert isinstance(result, str), repr(result)
49 return result
50
51
52 def main(argv: Sequence[str]) -> int:
53 if len(argv) != 3:
54 print("Usage: generate-gh-release-notes VERSION FILE")
55 return 2
56
57 version, filename = argv[1:3]
58 print(f"Generating GitHub release notes for version {version}")
59 rst_body = extract_changelog_entries_for(version)
60 md_body = convert_rst_to_md(rst_body)
61 Path(filename).write_text(md_body, encoding="UTF-8")
62 print()
63 print(f"Done: {filename}")
64 print()
65 return 0
66
67
68 if __name__ == "__main__":
69 sys.exit(main(sys.argv))