2023-12-31 21:25:13 +08:00
|
|
|
# mypy: disallow-untyped-defs
|
2019-08-10 22:03:36 +08:00
|
|
|
"""
|
2024-01-04 18:29:20 +08:00
|
|
|
Script used to generate a Markdown file containing only the changelog entries of a specific pytest release, which
|
|
|
|
is then published as a GitHub Release during deploy (see workflows/deploy.yml).
|
2019-08-10 22:03:36 +08:00
|
|
|
|
2024-01-04 18:29:20 +08:00
|
|
|
The script requires ``pandoc`` to be previously installed in the system -- we need to convert from RST (the format of
|
|
|
|
our CHANGELOG) into Markdown (which is required by GitHub Releases).
|
2019-08-10 22:03:36 +08:00
|
|
|
|
|
|
|
Requires Python3.6+.
|
|
|
|
"""
|
2024-03-13 21:30:18 +08:00
|
|
|
|
2024-02-01 04:12:33 +08:00
|
|
|
from pathlib import Path
|
2019-08-10 22:03:36 +08:00
|
|
|
import re
|
|
|
|
import sys
|
2023-12-31 21:25:13 +08:00
|
|
|
from typing import Sequence
|
2019-08-10 22:03:36 +08:00
|
|
|
|
|
|
|
import pypandoc
|
|
|
|
|
|
|
|
|
2024-01-04 18:29:20 +08:00
|
|
|
def extract_changelog_entries_for(version: str) -> str:
|
2019-12-20 19:54:44 +08:00
|
|
|
p = Path(__file__).parent.parent / "doc/en/changelog.rst"
|
2019-08-10 22:03:36 +08:00
|
|
|
changelog_lines = p.read_text(encoding="UTF-8").splitlines()
|
|
|
|
|
2023-12-31 21:09:33 +08:00
|
|
|
title_regex = re.compile(r"pytest (\d\.\d+\.\d+\w*) \(\d{4}-\d{2}-\d{2}\)")
|
2019-08-10 22:03:36 +08:00
|
|
|
consuming_version = False
|
|
|
|
version_lines = []
|
|
|
|
for line in changelog_lines:
|
|
|
|
m = title_regex.match(line)
|
|
|
|
if m:
|
2024-01-04 18:29:20 +08:00
|
|
|
# Found the version we want: start to consume lines until we find the next version title.
|
|
|
|
if m.group(1) == version:
|
2019-08-10 22:03:36 +08:00
|
|
|
consuming_version = True
|
2024-01-04 18:29:20 +08:00
|
|
|
# Found a new version title while parsing the version we want: break out.
|
2019-08-10 22:03:36 +08:00
|
|
|
elif consuming_version:
|
|
|
|
break
|
|
|
|
if consuming_version:
|
|
|
|
version_lines.append(line)
|
|
|
|
|
|
|
|
return "\n".join(version_lines)
|
|
|
|
|
|
|
|
|
2023-12-31 21:25:13 +08:00
|
|
|
def convert_rst_to_md(text: str) -> str:
|
|
|
|
result = pypandoc.convert_text(
|
2020-01-30 07:28:04 +08:00
|
|
|
text, "md", format="rst", extra_args=["--wrap=preserve"]
|
|
|
|
)
|
2023-12-31 21:25:13 +08:00
|
|
|
assert isinstance(result, str), repr(result)
|
|
|
|
return result
|
2019-08-10 22:03:36 +08:00
|
|
|
|
|
|
|
|
2023-12-31 21:25:13 +08:00
|
|
|
def main(argv: Sequence[str]) -> int:
|
2023-12-31 21:09:33 +08:00
|
|
|
if len(argv) != 3:
|
|
|
|
print("Usage: generate-gh-release-notes VERSION FILE")
|
|
|
|
return 2
|
2019-08-10 22:03:36 +08:00
|
|
|
|
2023-12-31 21:09:33 +08:00
|
|
|
version, filename = argv[1:3]
|
|
|
|
print(f"Generating GitHub release notes for version {version}")
|
2024-01-04 18:29:20 +08:00
|
|
|
rst_body = extract_changelog_entries_for(version)
|
2023-12-31 21:09:33 +08:00
|
|
|
md_body = convert_rst_to_md(rst_body)
|
|
|
|
Path(filename).write_text(md_body, encoding="UTF-8")
|
2019-08-15 05:51:00 +08:00
|
|
|
print()
|
2023-12-31 21:09:33 +08:00
|
|
|
print(f"Done: {filename}")
|
2019-08-15 05:51:00 +08:00
|
|
|
print()
|
2019-08-10 22:03:36 +08:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main(sys.argv))
|