forked from lightspeed-core/lightspeed-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_doc.py
More file actions
executable file
·84 lines (67 loc) · 2.75 KB
/
gen_doc.py
File metadata and controls
executable file
·84 lines (67 loc) · 2.75 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
#!/usr/bin/env python3
"""Generate documentation for all modules from Lightspeed Core Stack service."""
import os
import ast
from pathlib import Path
DIRECTORIES = ["src", "tests/unit", "tests/integration", "tests/e2e"]
def generate_docfile(directory):
"""
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, and finally writes the filename as
a separate entry.
"""
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)
with open(file, "r", 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):
"""Generate documentation for all the sources found in path.
This function generate README.md for Python sources in the given directory.
Parameters:
path (str or os.PathLike): Directory in which to generate the README.md file.
"""
directory = path
cwd = os.getcwd()
os.chdir(directory)
print(f"[gendoc] Generating README.md in: {directory}")
try:
generate_docfile(directory)
finally:
os.chdir(cwd)
def main():
"""Entry point to this script, regenerates documentation in all directories."""
for directory in DIRECTORIES:
generate_documentation_on_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()