Skip to content

Commit

Permalink
docs: impl tool to list .md missing in SUMMARY.md
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
obrusvit committed Feb 7, 2024
1 parent 4d0d6e5 commit d6fea1f
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 178 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ PY_FILES = $(shell find . -type f -name '*.py' | sed 'sO^\./OO' | grep -f ./to
C_FILES = $(shell find . -type f -name '*.[ch]' | grep -f ./tools/style.c.include | grep -v -f ./tools/style.c.exclude )


style_check: pystyle_check ruststyle_check cstyle_check changelog_check yaml_check editor_check ## run all style checks
style_check: pystyle_check ruststyle_check cstyle_check changelog_check yaml_check docs_summary_check editor_check ## run all style checks

style: pystyle ruststyle cstyle ## apply all code styles (C+Rust+Py)

Expand Down Expand Up @@ -138,6 +138,10 @@ ci_docs: ## generate CI documentation
ci_docs_check: ## check that generated CI documentation is up to date
./tools/generate_ci_docs.py --check

docs_summary_check: ## check if there are unlinked documentation files
@echo [DOCS-SUMMARY-MARKDOWN-CHECK]
python3 tools/check_docs_summary.py

vendorheader: ## generate vendor header
./core/embed/vendorheader/generate.sh --quiet

Expand Down
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [SLIP-39](core/misc/slip0039.md)
- [Exceptions usage](core/misc/exceptions.md)
- [Memory fragmentation management](core/misc/fragmentation.md)
- [DISC1](core/misc/disc1.md)
- [Legacy](legacy/index.md)
- [Firmware format](legacy/firmware-format.md)
- [Python](python/index.md)
Expand Down
Binary file removed docs/python/show-text-01.png
Binary file not shown.
Binary file removed docs/python/show-text-02.png
Binary file not shown.
Binary file removed docs/python/show-text-03.png
Binary file not shown.
Binary file removed docs/python/show-text-04.png
Binary file not shown.
Binary file removed docs/python/show-text-05.png
Binary file not shown.
Binary file removed docs/python/show-text-06.png
Binary file not shown.
Binary file removed docs/python/show-text-07.png
Binary file not shown.
137 changes: 0 additions & 137 deletions docs/python/show-text.md

This file was deleted.

40 changes: 0 additions & 40 deletions docs/storage/addon-block.md

This file was deleted.

82 changes: 82 additions & 0 deletions tools/check_docs_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()

0 comments on commit d6fea1f

Please sign in to comment.