Skip to content

Commit 10b391b

Browse files
committed
ci(check_toc_txt): add warning for files not in toc
Add a new warning for RST files in the source directory that are unreachable, as they are not listed in any device table of contents. Signed-off-by: Randolph Sapp <[email protected]>
1 parent 53320c0 commit 10b391b

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

bin/check_toc_txt.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,57 @@
1818
logger = logging.getLogger(__name__)
1919

2020

21-
def is_slug(slug):
22-
"""Check to see if the given slug is valid. Assumes extension will be rst.
21+
def get_slug_path(slug):
22+
"""Convert the given slug to a pathlib path. Assumes extension will be rst.
2323
2424
:param slug: String path slug
2525
"""
2626
path = SOURCE_PATH.joinpath(slug + ".rst")
2727
logging.debug("Expanded slug: %s", path)
28-
return path.is_file()
28+
return path
2929

3030

3131
def process_toc_txt(path):
3232
"""Process a single toc.txt file
3333
3434
:param path: Pathlib path to toc.txt
35+
:return: Set of valid paths
3536
"""
37+
valid_paths = set()
3638
with path.open("r", encoding="utf-8") as file:
3739
logging.debug("Processing %s", path)
3840
for line_number, line in enumerate(file):
3941
clean_line = COMMENT_REGEX.sub("", line).strip()
40-
if clean_line and not is_slug(clean_line):
41-
logging.warning("Invalid slug: %s:%i %s", path, line_number + 1, clean_line)
42+
if clean_line:
43+
slug_path = get_slug_path(clean_line)
44+
if slug_path.is_file():
45+
valid_paths.add(slug_path)
46+
else:
47+
logging.warning(
48+
"Invalid slug: %s:%i %s", path, line_number + 1, clean_line
49+
)
50+
return valid_paths
51+
52+
53+
def process_src_tree(toc_paths):
54+
"""Ensure all RST files in the src tree are listed in the given set"""
55+
src_paths = set()
56+
for path in SOURCE_PATH.glob("**/*.rst"):
57+
# files that begin with an underscore are included by other rst files
58+
# they should not be listed in the toc
59+
if path.stem[0] != "_":
60+
src_paths.add(path)
61+
for path in src_paths - toc_paths:
62+
logging.warning("File not in any toc: %s", path)
4263

4364

4465
def process_all():
4566
"""Process all toc.txt files in the config directory"""
67+
toc_paths = set()
4668
for path in CONFIG_PATH.glob("**/*toc.txt"):
4769
if path.is_file():
48-
process_toc_txt(path)
70+
toc_paths |= process_toc_txt(path)
71+
process_src_tree(toc_paths)
4972

5073

5174
def main():

0 commit comments

Comments
 (0)