This repository was archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate_readme
More file actions
executable file
·69 lines (54 loc) · 1.9 KB
/
Copy pathcreate_readme
File metadata and controls
executable file
·69 lines (54 loc) · 1.9 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
#
# Creates the README for this bin directory
import sys
import os
import subprocess
import shlex
from glob import glob
def read_octothrope_readme(contents):
"""Strategy one; described below"""
for line in contents[2:]:
if line.startswith("#"):
line = line.replace("#", "").strip()
if line: # not an empty line connecting to shebang
yield line
else:
break
bin_dir = os.path.abspath(os.path.dirname(__file__))
output_file = f"{bin_dir}/README.md"
readme = {}
# When recursive is set, ** followed by a path separator
# matches 0 or more subdirectories.
for file in sorted(glob(f"{bin_dir}/**/*", recursive=True)):
relative_filename = file.replace(bin_dir, "").lstrip("/")
# ignore readme, hidden files, links and directories
if "README.md" in file or file.startswith(
'.') or os.path.islink(file) or os.path.isdir(file):
continue
# Stragegy one: 'read comments at top of file'
with open(file, 'r') as f:
contents = f.readlines()
# my scripts that don't have docstrings/help have "#" connecting
# the docstring to the shebang
if contents[0].startswith("#!/usr/bin/env") and contents[1].strip() == "#":
readme[relative_filename] = "\n".join(
list(read_octothrope_readme(contents)))
continue
# Strategy two: run 'script -h'
resp = subprocess.run(shlex.split(f"{file} -h"), capture_output=True)
if resp.returncode == 0:
readme[relative_filename] = resp.stdout.decode("utf-8")
else:
print(f"Couldn't get readme for {file}", file=sys.stderr)
with open(output_file, 'w') as readme_f:
readme_f.write("Created by [create_readme](./create_readme)\n")
for script_name, script_readme in readme.items():
readme_f.write(
f"""
### [{script_name}](./{script_name})
```
{script_readme}
```
"""
)