forked from developers-against-repressions/case-212
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_readme.py
executable file
·65 lines (50 loc) · 1.88 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
#!/usr/bin/env python3
import os
import re
import hashlib
class InvalidFileFormatException(Exception):
pass
def load_signed():
signed = []
signed_new = set()
pattern1 = re.compile(r'([^|]+)\|([^|]+)$')
pattern2 = re.compile(r'\s*\|([^|]+)\|([^|]+)\|\s*$')
dir = 'signed'
for basename in os.listdir(dir):
filename = os.path.join(dir, basename)
if not os.path.isfile(filename):
print('Skipping non-file "%s"' % filename)
continue
with open(filename) as inp:
for i, line in enumerate(inp):
line = line.strip()
if not line:
continue
m = re.match(pattern1, line) or re.match(pattern2, line)
if not m and line:
raise InvalidFileFormatException(
'File "%s", line %d: line does not follow the format:\n\t"%s"'
% (filename, i + 1, line)
)
if "old_list.txt" in filename:
signed.append((m.group(1).strip(), m.group(2).strip()))
else:
signed_new.add((m.group(1).strip(), m.group(2).strip()))
for signature in signed_new:
signed.append(signature)
return sorted(signed, key=lambda pair: hashlib.sha256(repr(pair).encode('utf-8')).hexdigest())
def write_signed(signed, outp):
for i, signature in enumerate(signed):
outp.write('| {:<4} | {:<34} | {:<39} |\n'.format(i+1, signature[0], signature[1]))
def update_readme(signed):
with open('pre-readme.md') as inp, open('README.md', 'w') as outp:
for line in inp:
if line.strip() == '<!-- Signed -->':
write_signed(signed, outp)
else:
outp.write(line)
def main():
signed = load_signed()
update_readme(signed)
if __name__ == '__main__':
main()