-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatetables.py
executable file
·98 lines (76 loc) · 2.57 KB
/
updatetables.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
import datetime
import os
import re
import subprocess
TITLE_PATTERN = re.compile(r'^# \[([0-9]+)\. (.+)]\((http.+)\) - (.+)$')
TABLE_HEADER = '''
| # | Title | Difficulty | Date |
|---| ----- | ---------- | ---- |
'''
TABLE_ROW_TEMPLATE = '|{no}|[{title}]({file_path})|{level}|{date}|'
now = datetime.datetime.now().timestamp()
def parse_title(path: str):
with open(path, 'r+') as f:
for line in f.readlines():
line = line.strip()
result = TITLE_PATTERN.findall(line)
if result:
return result[0]
def parse_readme(path: str, values: dict):
new_lines = []
with open(path, 'r+') as f:
session = ''
for i in f.readlines():
if i.startswith('### '):
new_lines.append(i)
session = i.strip().split(' ', 1)[-1]
vs = values.get(session)
if not vs:
session = ''
continue
new_lines.append(f'{TABLE_HEADER}')
new_lines.extend([f'{i}\n' for i in vs])
if session:
continue
new_lines.append(i)
return new_lines
def execute(commands):
p = subprocess.Popen(
commands, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate() # type: bytes, bytes
if p.returncode != 0:
raise Exception(
f'failed to execute the command {commands}. exit code: {p.returncode}. {stderr.decode("utf-8")}')
return stdout.decode('utf-8'), stderr.decode('utf-8')
def get_file_date(path: str):
cmd = f'git log --pretty=format:%at -- {path}'
stdout, _ = execute(cmd.split(' '))
return float(stdout.split('\n')[-1] or str(now))
def do(dir_path: str):
items = []
for i in os.listdir(dir_path):
path = os.path.join(dir_path, i)
t = parse_title(path)
cts = get_file_date(path)
items.append({
'no': t[0],
'title': t[1],
'link': t[2],
'level': t[3],
'file_path': path,
'cts': cts,
'date': datetime.datetime.fromtimestamp(cts).strftime("%Y/%m/%d")
})
items = sorted(items, key=lambda k: k.get('cts', 0), reverse=True)
for i in items:
yield TABLE_ROW_TEMPLATE.format(**i)
def main():
readme_filepath = './README.md'
result = parse_readme(readme_filepath, {
'Leetcode Algorithms': do('./algorithms/')
})
with open(readme_filepath, 'w+') as f:
f.writelines(result)
if __name__ == '__main__':
main()