-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path.update_project_readme.py
executable file
·67 lines (55 loc) · 1.74 KB
/
.update_project_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
#!/usr/bin/env python
import glob
import os
from mistune import BlockLexer
def _find_descr(blocks):
"The first paragraph is interpreted as the description."
for block in blocks:
if block["type"] == "paragraph":
return block["text"]
def generate_project_overview(file):
file.write("## Project Overview\n\n")
for fn in sorted(
glob.glob("projects/*/README.md"), key=lambda fn: (fn.count("."), fn)
):
dirname = os.path.dirname(fn)
project_name = os.path.basename(dirname)
file.write(f"### [{project_name}]({os.path.basename(dirname)}/)\n\n")
with open(fn) as readme:
files = BlockLexer().parse(readme.read())
file.write(_find_descr(files) + "\n\n")
def parse_readme(readme):
for i, line in enumerate(readme):
if "Project Overview" in line:
yield i
elif "Copyright Notice" in line:
yield i
return
def generate_readme(readme, tmp):
start, stop = parse_readme(readme)
readme.seek(0)
written = False
for i, line in enumerate(readme):
if i >= start and i < stop:
if written:
continue
else:
generate_project_overview(tmp)
written = True
continue
else:
tmp.write(line)
if __name__ == "__main__":
fn_readme = "projects/README.md"
fn_readme_tmp = fn_readme + ".tmp"
try:
with open(fn_readme) as readme:
with open(fn_readme_tmp, "x") as tmp:
generate_readme(readme, tmp)
except Exception:
try:
os.remove(fn_readme_tmp)
except OSError:
pass
else:
os.rename(fn_readme_tmp, fn_readme)