-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathcheck_changelog_fragments.py
executable file
·58 lines (43 loc) · 1.92 KB
/
check_changelog_fragments.py
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
#!/usr/bin/env python3
import configparser
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
def main():
scriv_config = configparser.ConfigParser()
scriv_config.read(REPO_ROOT / "changelog.d/scriv.ini")
scriv_section = scriv_config["scriv"]
assert scriv_section["format"] == "md"
md_header_level = int(scriv_section["md_header_level"])
md_header_prefix = "#" * md_header_level + "# "
categories = {s.strip() for s in scriv_section["categories"].split(",")}
success = True
def complain(message):
nonlocal success
success = False
print(f"{fragment_path.relative_to(REPO_ROOT)}:{line_index+1}: {message}", file=sys.stderr)
for fragment_path in REPO_ROOT.glob("changelog.d/*.md"):
with open(fragment_path) as fragment_file:
for line_index, line in enumerate(fragment_file):
line = line.rstrip("\n")
if line_index == 0:
# The first line should always be a header.
if not line.startswith("#"):
complain("line should be a header")
elif (
line
and not line.startswith("#")
and not line.startswith("-")
and not line.startswith(" ")
):
complain("line should be a header, a list item, or indented")
if line.startswith("#"):
if line.startswith(md_header_prefix):
category = line.removeprefix(md_header_prefix).strip()
if category not in categories:
complain(f"unknown category: {category}")
else:
# All headers should be of the same level.
complain(f"header should start with {md_header_prefix!r}")
sys.exit(0 if success else 1)
main()