pytest-mock/scripts/gen-release-notes.py

48 lines
1.3 KiB
Python
Raw Normal View History

2022-05-26 23:59:37 +02:00
"""
Generates the release notes for the latest release, in Markdown.
2023-11-13 00:36:04 +01:00
1. Extracts the latest release from the CHANGELOG.rst file.
2. Converts it to Markdown using pypandoc.
3. Writes to ``scripts/latest-release-notes.md``, which can be
used with https://github.com/softprops/action-gh-release.
2022-05-26 23:59:37 +02:00
"""
from pathlib import Path
import pypandoc
this_dir = Path(__file__).parent
rst_text = (this_dir.parent / "CHANGELOG.rst").read_text(encoding="UTF-8")
output_lines = []
2023-11-13 00:36:04 +01:00
capture = False
for line in rst_text.splitlines():
# Only start capturing after the latest release section.
if line.startswith("-------"):
capture = not capture
if not capture:
# We only need to capture the latest release, so stop.
2022-05-26 23:59:37 +02:00
break
2023-11-13 00:36:04 +01:00
continue
if capture:
2022-05-26 23:59:37 +02:00
output_lines.append(line)
2023-11-13 00:36:04 +01:00
# Drop last line, as it contains the previous release section title.
del output_lines[-1]
trimmed_rst = "\n".join(output_lines).strip()
print(">>Trimmed RST follows:")
print(trimmed_rst)
print(">>Trimmed RST ends")
md_text = pypandoc.convert_text(
trimmed_rst, "md", format="rst", extra_args=["--wrap=preserve"]
)
print(">>Converted Markdown follows:")
print(md_text)
print(">>Converted Markdown ends")
2022-05-26 23:59:37 +02:00
output_fn = this_dir / "latest-release-notes.md"
2023-11-13 00:36:04 +01:00
output_fn.write_text(md_text, encoding="UTF-8")
2022-05-26 23:59:37 +02:00
print(output_fn, "generated.")