-
Notifications
You must be signed in to change notification settings - Fork 11
/
generate_shell_docs.py
executable file
·73 lines (56 loc) · 1.79 KB
/
generate_shell_docs.py
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
#!/usr/bin/env python3
"""Generate a readme for the shell scripts."""
import os
OMIT = ["README.rst"]
README_HEADER = """
Shell script documentation
==========================
Note: this documentation is automatically generated from the comments in the
shell scripts.
"""
SCRIPT_TEMPLATE = """
{script}
------------------------------------------------------------------------
{documentation}
Source code::
{code}
"""
def extract(script):
"""Return extracted code and documentation from script.
Note that the code is indented four spaces to make it fit in the
restructured text documentation.
"""
lines = [line.rstrip() for line in open(script).readlines()]
first_line = lines.pop(0)
code_lines = [first_line]
doc_lines = []
in_doc_part = True
for line in lines:
if in_doc_part:
if line.startswith("#"):
doc_lines.append(line[2:])
continue
else:
in_doc_part = False
code_lines.append(line)
doc = "\n".join(doc_lines)
code_lines = [" " + line for line in code_lines]
# The next line re-strips the spaces from space-only lines.
code_lines = [line.rstrip() for line in code_lines]
code = "\n".join(code_lines)
return code, doc
def main():
"""Find the shell files and collect their inline documentation."""
readme = README_HEADER
os.chdir("shell")
scripts = [script for script in os.listdir(".") if script not in OMIT]
scripts.sort()
for script in scripts:
code, documentation = extract(script)
readme += SCRIPT_TEMPLATE.format(
script=script, code=code, documentation=documentation
)
open("README.rst", "w").write(readme)
print("Wrote shell/README.rst")
if __name__ == "__main__":
main()