-
-
Notifications
You must be signed in to change notification settings - Fork 658
/
check_docs_summary.py
executable file
·82 lines (61 loc) · 2.25 KB
/
check_docs_summary.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
This script lists names of markdown files (.md extenstion) present in docs/ directory
which are not referenced in SUMMARY.md file which serves as a firmware docu mainpage.
Running the script:
- `python tools/check_docs_summary.py` from trezor-firmware root directory.
"""
import re
import sys
from pathlib import Path
from typing import Generator, Iterable, Set
DOCS_DIR = "docs/"
SUMMARY_FILENAME = "SUMMARY.md"
RE_MARKDOWN_LINK = r"\[.*?\]\((.+.md)\)"
def gen_pat_in_file(
filepath: str, pat: re.Pattern, grp_idx: int
) -> Generator[str, None, None]:
with open(filepath, "r", encoding="utf-8") as f:
for line in f.readlines():
match = re.search(pat, line)
if match:
yield match.group(grp_idx)
def gen_convert_to_str(inputs: Iterable[Path]) -> Generator[str, None, None]:
for i in inputs:
yield str(i)
def gen_ltrim_pat(inputs: Iterable[str], pat: str) -> Generator[str, None, None]:
for i in inputs:
if i.startswith(pat):
yield i[len(pat) :]
def gen_skip(inputs: Iterable[str], what: str) -> Generator[str, None, None]:
for i in inputs:
if i != what:
yield i
def difference(g1: Iterable[str], g2: Iterable[str]) -> Generator[str, None, None]:
set_g2: Set[str] = set(g2)
for item in g1:
if item not in set_g2:
yield item
def print_result(filenames: Iterable[str]) -> None:
if not filenames:
print("OK")
sys.exit(0)
else:
print(
f"ERROR: these files exist in {DOCS_DIR} but are not linked in {DOCS_DIR + SUMMARY_FILENAME}"
)
for f in filenames:
print(f"\t- {f}")
sys.exit(1)
def main():
re_md_link = re.compile(RE_MARKDOWN_LINK)
md_files_in_docs_dir = Path(DOCS_DIR).rglob("*.md")
md_files_in_docs_dir = gen_convert_to_str(md_files_in_docs_dir)
md_files_in_docs_dir = gen_ltrim_pat(md_files_in_docs_dir, DOCS_DIR)
md_files_in_docs_dir = gen_skip(md_files_in_docs_dir, SUMMARY_FILENAME)
md_files_linked_in_summary = gen_pat_in_file(
DOCS_DIR + SUMMARY_FILENAME, re_md_link, 1
)
diff = difference(md_files_in_docs_dir, md_files_linked_in_summary)
print_result(list(diff))
if __name__ == "__main__":
main()