forked from TexasInstruments/processor-sdk-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot_index.py
More file actions
executable file
·69 lines (51 loc) · 1.89 KB
/
Copy pathroot_index.py
File metadata and controls
executable file
·69 lines (51 loc) · 1.89 KB
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
#!/usr/bin/env python3
"""Tool to find and make a index of doc root index files
SPDX-License-Identifier: MIT
Copyright (C) 2025 Texas Instruments Incorporated - https://www.ti.com
"""
from pathlib import Path
import logging
from jinja2 import Environment, FileSystemLoader
BUILD_PATH = Path("build/")
TEMPLATE_PATH = Path(__file__).parent
logger = logging.getLogger(__name__)
def get_root_index(path):
"""Get the root index from a list of candidates
:param path: Pathlib path to search in
"""
candidates = [x for x in path.glob("**/index.html") if x.is_file()]
if not candidates:
return None
sorted_paths = sorted(candidates, key=lambda x: len(x.parts))
return sorted_paths[0]
def get_index_list():
"""Get the list of index files to use from the build directory"""
index_list = []
for path in BUILD_PATH.glob("*/"):
root = get_root_index(path)
if root:
logging.info("Found index: %s", root)
root = root.relative_to(BUILD_PATH)
logging.info("Updating path to: %s", root)
index_list.append(root)
return sorted(index_list)
def generate_root_index(index_list):
"""Make the root index file
:param index_list: List of pathlib paths to indexes
"""
root_index_path = BUILD_PATH.joinpath("index.html")
logging.info("Loading jinja env")
env = Environment(loader=FileSystemLoader(TEMPLATE_PATH))
template = env.get_template("root_index.html")
logging.info("Using template: %s", template.name)
output = template.render(path_list=index_list)
logging.info("Writing output to: %s", root_index_path)
with root_index_path.open("w", encoding="utf-8") as f:
f.write(output)
def main():
"""Main processing loop"""
logging.basicConfig(level=logging.INFO)
index_list = get_index_list()
generate_root_index(index_list)
if __name__ == "__main__":
main()