forked from developers-against-repressions/case-212
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_readme.py
executable file
·82 lines (59 loc) · 2.47 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
81
82
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re
import hashlib
class InvalidFileFormatException(Exception):
pass
def load_signed():
signed_set = set() # a set of tuples: ( name, other_info )
# nb: lines are already whitespace-stripped from both ends
sig_pattern_1 = r""" ([^|]+) # name
\| # separator
([^|]+) # company, position etc
"""
# вариант с '|' - обрамлением: "old_list.txt" и некоторые другие
sig_pattern_2 = r""" \| # left-sep
([^|]+) # name
\| # middle-separator
([^|]+) # company, position etc
\| # right-sep
"""
## pattern = re.compile( '%s | %s' % ( sig_pattern_1, sig_pattern_2 ), re.X )
pattern1 = re.compile( sig_pattern_1, re.VERBOSE )
pattern2 = re.compile( sig_pattern_2, re.VERBOSE )
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 :
raise InvalidFileFormatException(
'File "%s", line %d: line does not follow the format:\n\t"%s"'
% (filename, i + 1, line)
)
signed_set.add((m.group(1).strip(), m.group(2).strip()))
signed_list = sorted(signed_set, key=lambda pair: hashlib.sha256(repr(pair).encode('utf-8')).hexdigest())
return signed_list
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()