File: generate-gh-release-notes.py

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (67 lines) | stat: -rw-r--r-- 2,109 bytes parent folder | download | duplicates (13)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# mypy: disallow-untyped-defs
"""
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).

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).

Requires Python3.6+.
"""

from pathlib import Path
import re
import sys
from typing import Sequence

import pypandoc


def extract_changelog_entries_for(version: str) -> str:
    p = Path(__file__).parent.parent / "doc/en/changelog.rst"
    changelog_lines = p.read_text(encoding="UTF-8").splitlines()

    title_regex = re.compile(r"pytest (\d\.\d+\.\d+\w*) \(\d{4}-\d{2}-\d{2}\)")
    consuming_version = False
    version_lines = []
    for line in changelog_lines:
        m = title_regex.match(line)
        if m:
            # Found the version we want: start to consume lines until we find the next version title.
            if m.group(1) == version:
                consuming_version = True
            # Found a new version title while parsing the version we want: break out.
            elif consuming_version:
                break
        if consuming_version:
            version_lines.append(line)

    return "\n".join(version_lines)


def convert_rst_to_md(text: str) -> str:
    result = pypandoc.convert_text(
        text, "md", format="rst", extra_args=["--wrap=preserve"]
    )
    assert isinstance(result, str), repr(result)
    return result


def main(argv: Sequence[str]) -> int:
    if len(argv) != 3:
        print("Usage: generate-gh-release-notes VERSION FILE")
        return 2

    version, filename = argv[1:3]
    print(f"Generating GitHub release notes for version {version}")
    rst_body = extract_changelog_entries_for(version)
    md_body = convert_rst_to_md(rst_body)
    Path(filename).write_text(md_body, encoding="UTF-8")
    print()
    print(f"Done: {filename}")
    print()
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv))