-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathgenerate_index.py
84 lines (72 loc) · 2.67 KB
/
generate_index.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
81
82
83
84
import os
from operator import itemgetter
from pathlib import Path
import re
import sys
status_list = ["PROPOSED", "ACCEPTED", "ADOPTED", "SUPERSEDED"]
def collect_rfcs():
rfcs = []
top_paths = ["text"]
for top_path in top_paths:
for rfc_path in Path(top_path).iterdir():
if os.path.isdir(rfc_path):
rfcs.append({
'type': top_path,
'path': rfc_path / "README.md",
'number-title': rfc_path.name,
})
return rfcs
def analyze_status(rfcs):
#example status line - Status: [ACCEPTED](/README.md#hipe-lifecycle)
status_re = pattern = re.compile(r'^\s*(?:[-*]\s*)?Status:[ \t/[]*(\w+)', re.I | re.M)
for rfc in rfcs:
# extract number and title from folder name
rfc['number'], _, rfc['title'] = rfc['number-title'].partition("-")
# read README file for status
with open(rfc['path'], "rt", encoding='utf-8') as f:
rfc_text = f.read()
m = status_re.search(rfc_text)
if m:
rfc['status'] = m.group(1).upper()
else:
print("Didn't find status line in %s" % rfc['path'])
sys.exit(1)
def update(fname, tmp_fname):
if not os.path.exists(fname):
os.rename(tmp_fname, fname)
print('Generated %s.' % fname)
return
with open(fname, 'rt') as f:
old = f.read()
with open(tmp_fname, 'rt') as f:
new = f.read()
if old == new:
print('No change to %s.' % fname)
return
os.remove(fname)
os.rename(tmp_fname, fname)
print('Updated %s.' % fname)
def dump(rfcs, fname):
rfcs.sort(key=itemgetter('number'))
tmp_fname = fname + '.tmp'
#group rfcs by status
with open(tmp_fname, 'w', encoding='utf-8') as out:
out.write("(This file is machine-generated; see [code/generate_index.py](code/generate_index.py).)\n\n")
out.write("# Hyperleger Indy HIPEs by Status\n")
for status in status_list:
out.write(f"\n## Status: {status}\n")
for rfc in [rfc for rfc in rfcs if rfc['status'] == status]:
out.write(f"* [{rfc['number']}: {rfc['title']}]({rfc['path'].as_posix()})\n")
update(fname, tmp_fname)
def main(target_fname = None):
root_path = Path(os.path.abspath(os.path.dirname(__file__))).parent
# We need all paths that we walk and store to be relative to the root
# of the repo, even if that's not where we're running the script.
os.chdir(root_path)
if target_fname is None:
target_fname = 'index.md'
rfcs = collect_rfcs()
analyze_status(rfcs)
dump(rfcs, target_fname)
if __name__ == '__main__':
main()