-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_readme.py
executable file
·166 lines (133 loc) · 5.1 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
import os
import re
import subprocess
def parse_toml(file_path):
result = {}
with open(file_path, "r") as f:
for line in f:
line = line.strip()
if "=" in line:
key, value = line.split("=", 1)
key = key.strip()
value = value.strip().strip('"')
result[key] = value
return result
def get_new_readmes():
cmd = "git diff --cached --name-only --diff-filter=A"
output = subprocess.check_output(cmd, shell=True).decode("utf-8")
return [
f
for f in output.split("\n")
if (f.endswith("/README.md") and f.count("/") == 2)
]
def extract_info(readme_path):
path = "./" + readme_path[: readme_path.index("/README.md")]
with open(readme_path, "r") as f:
content = f.read()
header_match = re.search(r"---\n(.*?)\n---", content, re.DOTALL)
if header_match:
header = header_match.group(1)
challenge = re.search(r"challenge:\s*(.*)", header)
challenge = challenge.group(1) if challenge else ""
tags = re.findall(r"- (.*)", header)
keywords = ", ".join(tags)
else:
challenge = os.path.basename(os.path.dirname(readme_path))
keywords = ""
info_path = os.path.join(os.path.dirname(os.path.dirname(readme_path)), "info.toml")
if os.path.exists(info_path):
info = parse_toml(info_path)
source = info.get("contest", "")
link = info.get("link", "")
else:
source = os.path.basename(os.path.dirname(os.path.dirname(readme_path)))
link = ""
return source, link, challenge, path, keywords
def update_main_readme(new_entries):
main_readme = "README.md"
with open(main_readme, "r") as f:
content = f.read()
table_pattern = (
r"(\|[\s]*Source[\s]*\|[\s]*Challenge[\s]*\|[\s]*Keywords[\s]*\|.*?)\n\n"
)
table_match = re.search(table_pattern, content, re.DOTALL)
if not table_match:
print("Failed to find table.")
raise Exception("No table found")
table_start = table_match.start()
table_end = table_match.end()
table_content = table_match.group(1).split("\n")
existing_entries = []
current_source = None
current_link = None
for row in table_content[2:]:
if row.strip():
parts = re.findall(r"\[(.*?)\]\((.*?)\)", row)
if parts:
if len(parts) >= 2:
source, link = parts[0]
challenge, path = parts[1]
current_source = source
current_link = link
elif len(parts) == 1:
challenge, path = parts[0]
source = current_source
link = current_link
keywords = row.split("|")[-2].strip()
existing_entries.append((source, link, challenge, path, keywords))
elif "👆" in row:
challenge_path = re.findall(r"\[(.*?)\]\((.*?)\)", row)
if challenge_path:
challenge, path = challenge_path[0]
keywords = row.split("|")[-2].strip()
existing_entries.append(
(current_source, current_link, challenge, path, keywords)
)
for new_entry in new_entries:
source, link, challenge, path, keywords = new_entry
insert_index = len(existing_entries)
for i, entry in enumerate(existing_entries):
if entry[0] == source:
insert_index = i + 1
while (
insert_index < len(existing_entries)
and existing_entries[insert_index][0] == "👆"
):
insert_index += 1
break
if insert_index < len(existing_entries):
existing_entries.insert(insert_index, new_entry)
else:
existing_entries.append(new_entry)
updated_table = [table_content[0], table_content[1]]
current_source = None
for entry in existing_entries:
source, link, challenge, path, keywords = entry
if source == current_source:
source_cell = "👆"
else:
source_cell = f"[{source}]({link})"
current_source = source
new_row = f"| {source_cell} | [{challenge}]({path}) | {keywords} |"
updated_table.append(new_row)
updated_table_content = "\n".join(updated_table)
updated_content = (
content[:table_start] + updated_table_content + "\n\n" + content[table_end:]
)
print("================Updating README================")
print(updated_table_content)
print("================Updating README================")
print("Success.")
with open(main_readme, "w") as f:
f.write(updated_content)
subprocess.run(["git", "add", main_readme])
def main():
new_readmes = get_new_readmes()
if not new_readmes:
print("Nothing new, exiting..")
return
new_entries = [extract_info(readme) for readme in new_readmes]
update_main_readme(new_entries)
if __name__ == "__main__":
main()