Repository navigation
Expand file tree
/
Copy pathgen_doc.py
More file actions
executable file
·111 lines (92 loc) · 3.69 KB
/
Copy pathgen_doc.py
File metadata and controls
executable file
·111 lines (92 loc) · 3.69 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
"""Generate README indexes from Python module docstrings in source and test directories."""
import ast
import os
from pathlib import Path
DIRECTORIES = ["src", "tests/unit", "tests/integration", "tests/e2e"]
# README.md files won't be generated in following directories
DIRS_TO_SKIP = {
"src/pii_redaction_engine",
"src/providers/inference",
"src/providers/inference/bedrock",
"tests/integration",
"tests/e2e",
"tests/e2e/configuration",
"tests/e2e/rag",
"tests/e2e/secrets",
"tests/e2e/skills",
"tests/e2e/skills/echo",
"tests/e2e/skills/echo/references",
"tests/e2e/skills/summarize",
"tests/e2e/skills/summarize/references",
"tests/e2e/mock_guardian_server",
"tests/e2e/mock_otel_collector",
}
def generate_docfile(directory: Path) -> None:
"""
Write or overwrite a README.md in the current working directory with module docstring summaries.
The file will begin with a header indicating the provided `directory` path.
For each `.py` file in the current working directory the function writes a
second-level Markdown header linking to the file, then writes the first
line of the module docstring if present. Files with syntax errors retain
their headers but have no summary. File access and UTF-8 decoding errors
propagate, potentially leaving a partially written README.
"""
with open("README.md", "w", encoding="utf-8", newline="\n") as indexfile:
print(
f"# List of source files stored in `{directory}` directory",
file=indexfile,
)
print(file=indexfile)
files = sorted(os.listdir())
for file in files:
if file.endswith(".py"):
print(f"## [{file}]({file})", file=indexfile)
print(file=indexfile)
with open(file, encoding="utf-8") as fin:
source = fin.read()
try:
mod = ast.parse(source)
doc = ast.get_docstring(mod)
except SyntaxError:
doc = None
if doc:
print(doc.splitlines()[0], file=indexfile)
print(file=indexfile)
def generate_documentation_on_path(path: Path) -> None:
"""Generate a README index for an included directory.
Skip paths listed in DIRS_TO_SKIP. Temporarily change the working directory
to the selected path and restore it after generation. File access and UTF-8
decoding errors propagate.
Args:
path: Directory in which to generate README.md.
"""
# directory can be skipped if it's part of DIRS_TO_SKIP global list.
if path.as_posix() in DIRS_TO_SKIP:
print(f"[gendoc] Skipping {path}")
return
cwd = os.getcwd()
os.chdir(path)
try:
print(f"[gendoc] Generating README.md in: {path}")
generate_docfile(path)
finally:
os.chdir(cwd)
def main() -> None:
"""Regenerate READMEs under DIRECTORIES, subject to the configured exclusions."""
for directory in DIRECTORIES:
generate_documentation_on_path(Path(f"{directory}/"))
for path in Path(directory).rglob("*"):
if path.is_dir():
# LCORE-679: Script to generate documentation should create
# README.md files just for source modules
if (
path.name == "lightspeed_stack.egg-info"
or path.name == "__pycache__"
or ".ruff_cache" in str(path)
or ".mypy_cache" in str(path)
):
continue
generate_documentation_on_path(path)
if __name__ == "__main__":
main()