Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit fa99751

Browse files
committed
linting + added validation for admin section
1 parent dac2037 commit fa99751

1 file changed

Lines changed: 57 additions & 32 deletions

File tree

docs/scripts/patch_devsite_toc.py

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"""
2121

2222

23+
import glob
2324
import yaml
2425
import os
2526
import shutil
@@ -153,18 +154,38 @@ def copy_markdown(self):
153154
f"_build/html/docfx_yaml",
154155
)
155156

157+
def validate_section(self, toc):
158+
# Make sure each rst file is listed in the toc.
159+
items_in_toc = [
160+
d["items"] for d in toc[0]["items"] if d["name"] == self.title and ".rst"
161+
][0]
162+
items_in_dir = [f for f in os.listdir(self.dir_name) if f.endswith(".rst")]
163+
# subtract 1 for index
164+
assert len(items_in_toc) == len(items_in_dir) - 1
165+
for file in items_in_dir:
166+
if file != self.index_file_name:
167+
base_name, _ = os.path.splitext(file)
168+
assert any(d["href"] == f"{base_name}.md" for d in items_in_toc)
169+
# make sure the markdown files are present in the docfx_yaml directory
170+
md_files = [d["href"] for d in items_in_toc]
171+
for file in md_files:
172+
assert os.path.exists(f"_build/html/docfx_yaml/{file}")
173+
156174

157175
class UIDFilteredTocSection(TocSection):
158-
def __init__(self,toc_file_path, section_name, title, uid_prefix):
176+
def __init__(self, toc_file_path, section_name, title, uid_prefix):
159177
"""Creates a filtered section denoted by section_name in the toc_file_path to items with the given UID prefix.
160178
161179
The section is then renamed to the title.
162180
"""
163181
current_toc = yaml.safe_load(open(toc_file_path, "r"))
182+
self.uid_prefix = uid_prefix
164183

165184
# Since we are looking for a specific section_name there should only
166185
# be one match.
167-
section_items = [d for d in current_toc[0]["items"] if d["name"] == section_name][0]["items"]
186+
section_items = [
187+
d for d in current_toc[0]["items"] if d["name"] == section_name
188+
][0]["items"]
168189
filtered_items = [d for d in section_items if d["uid"].startswith(uid_prefix)]
169190
self.items = filtered_items
170191
self.title = title
@@ -175,6 +196,39 @@ def copy_markdown(self):
175196
"""
176197
pass
177198

199+
def validate_section(self, toc):
200+
uids_in_toc = set()
201+
202+
# A UID-filtered TOC tree looks like the following:
203+
# - items:
204+
# <optional> items: <more stuff>
205+
# name: <name>
206+
# uid: <fully qualified path to a class>
207+
#
208+
# Walk through the TOC tree to find all UIDs recursively.
209+
def find_uids_in_items(items):
210+
uids_in_toc.add(items["uid"])
211+
for subitem in items.get("items", []):
212+
find_uids_in_items(subitem)
213+
214+
items_in_toc = [d["items"] for d in toc[0]["items"] if d["name"] == self.title][
215+
0
216+
]
217+
for item in items_in_toc:
218+
find_uids_in_items(item)
219+
220+
# Now that we have all the UIDs, first match all of them
221+
# with corresponding .yml files.
222+
for uid in uids_in_toc:
223+
assert os.path.exists(f"_build/html/docfx_yaml/{uid}.yml")
224+
225+
# Also validate that every uid yml file that starts with the uid_prefix
226+
# exists in the section.
227+
for filename in glob.glob(
228+
f"{self.uid_prefix}*.yml", root_dir="_build/html/docfx_yaml"
229+
):
230+
assert filename[:-4] in uids_in_toc
231+
178232

179233
def validate_toc(toc_file_path, expected_section_list, added_sections):
180234
current_toc = yaml.safe_load(open(toc_file_path, "r"))
@@ -186,36 +240,7 @@ def validate_toc(toc_file_path, expected_section_list, added_sections):
186240
# make sure each customs ection is in the toc
187241
for section in added_sections:
188242
assert section.title in found_sections
189-
# make sure each rst file in each custom section dir is listed in the toc
190-
for section in added_sections:
191-
items_in_toc = [
192-
d["items"]
193-
for d in current_toc[0]["items"]
194-
if d["name"] == section.title and ".rst"
195-
][0]
196-
hrefs_in_toc = [
197-
d
198-
for d in items_in_toc
199-
if "href" in d
200-
]
201-
if hrefs_in_toc:
202-
items_in_dir = [f for f in os.listdir(section.dir_name) if f.endswith(".rst")]
203-
# subtract 1 for index
204-
assert len(hrefs_in_toc) == len(items_in_dir) - 1
205-
for file in items_in_dir:
206-
if file != section.index_file_name:
207-
base_name, _ = os.path.splitext(file)
208-
assert any(d["href"] == f"{base_name}.md" for d in hrefs_in_toc)
209-
# make sure the markdown files are present in the docfx_yaml directory
210-
for section in added_sections:
211-
items_in_toc = [
212-
d["items"]
213-
for d in current_toc[0]["items"]
214-
if d["name"] == section.title and ".rst"
215-
][0]
216-
md_files = [d["href"] for d in items_in_toc if "href" in d]
217-
for file in md_files:
218-
assert os.path.exists(f"_build/html/docfx_yaml/{file}")
243+
section.validate_section(current_toc)
219244
print("Toc validation passed")
220245

221246

0 commit comments

Comments
 (0)