-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_readme.py
executable file
·80 lines (56 loc) · 1.85 KB
/
update_readme.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
74
75
76
77
78
79
80
#!/usr/bin/env python3
import argparse
import contextlib
import glob
import subprocess
import sys
HELP = """
For maintainer use only. Get the --help of all .py files in the current
directory and add them to the given README markdown file, starting at
"## Tools". Ignores scripts whose name starts with an underscore and scripts
that don't exit with 0, when called with --help.
"""
def get_readme_preface(path, stopline="## Tools"):
preface = ""
with open(path) as f:
for l in f.readlines():
preface += l
if l.strip() == stopline:
break
return preface
def get_help_texts():
for script in glob.glob("*.py"):
if script.startswith("_"):
continue
with contextlib.suppress(subprocess.CalledProcessError):
output = subprocess.check_output(f"./{script} --help", shell=True)
yield script, output.decode()
def generate_tools_toc(help_texts):
return "".join(f'* [{s}](#{s.replace(".", "")})\n' for s, _ in help_texts)
def generate_tools_markdown(help_texts):
return "\n".join(
f"""### {script}
```console
$ ./{script} --help
{help}
```
"""
for script, help in help_texts
)
def generate_readme(readme_path):
readme_preface = get_readme_preface(readme_path)
help_texts = list(get_help_texts())
toc = generate_tools_toc(help_texts)
tools_markdown = generate_tools_markdown(help_texts)
markdown = readme_preface + "\n" + toc + "\n" + tools_markdown
with open(readme_path, "w") as f:
f.write(markdown)
def main(argv):
parser = argparse.ArgumentParser(description=HELP)
parser.add_argument(
"--readme-path", default="README.md", help="file to read and write to"
)
args = parser.parse_args(argv)
generate_readme(args.readme_path)
if __name__ == "__main__":
main(sys.argv[1:])