-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_contribution.py
279 lines (247 loc) · 9.39 KB
/
stats_contribution.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import argparse
import glob
import re
import os
import sys
from subprocess import Popen
point_dict = {
"cpprefjp/typo": 1,
"cpprefjp/link": 2,
"cpprefjp/addref": 20,
"cpprefjp/addlang": 20,
"cpprefjp/addpage": 20,
"cpprefjp/fixs": 2,
"cpprefjp/fixm": 5,
"cpprefjp/fixl": 10,
"cpprefjp/compiler": 2,
"boostjp/typo": 1,
"boostjp/link": 2,
"boostjp/releases": 5,
"boostjp/releasem": 10,
"boostjp/releasel": 20,
"boostjp/fixs": 2,
"boostjp/fixm": 5,
"boostjp/addrefs": 10,
"boostjp/addrefm": 20,
"tool/fixbug": 30,
"tool/improves": 10,
"tool/improvem": 30,
"tool/improvel": 50,
"tool/updatelib": 20,
"tool/updatelang": 10,
"tool/updatelang": 30,
"tool/updatelang": 50,
"tool/adds": 30,
"tool/addm": 50,
"tool/addl": 100,
}
target_repos = [
"cpprefjp/site",
"cpprefjp/site_generator",
"cpprefjp/kunai",
"cpprefjp/kunai_config",
"cpprefjp/crsearch",
"cpprefjp/markdown_to_html",
"boostjp/site",
]
def stats_contribution(text: str,
filename: str,
year: int,
target_year: int,
receive_users: list[str],
exclude_users: list[str],
max_user_point_dict: dict[str, int],
additional_user_point_dict: dict[str, int]) -> dict[str, set[str]]:
def is_active_user(name: str) -> bool:
if name in exclude_users:
return False
if len(receive_users) == 0:
return True
return name in receive_users;
user_name: str | None = None
user_point = 0
commit_dict: dict[str, set[str]] = dict(set())
is_target_year = year == target_year
users = dict()
for line in text.split("\n"):
m = re.fullmatch(r'## (.*?)', line)
if m:
m = re.fullmatch(r'\[(.*?)\]\((.*?)\)', m.group(1))
user_name = m.group(1)
user_point = 0
continue
if not user_name:
continue
if line.startswith("| ["):
cols = line.split("|")
for m in re.finditer(r'\[commit (.*?)\]', cols[1].strip()):
c = m[1].split(", ")
repo = c[0]
commit_ids = set()
for id in c[1:]:
id = id.strip()
if len(id) == 0:
continue
if len(id) != 7:
raise Exception("{}: {} (len:{}) commit-id length should be 7".format(filename, id, len(id)))
commit_ids.add(id)
if repo in commit_dict:
commit_dict[repo] |= commit_ids
else:
commit_dict[repo] = commit_ids
points = cols[2].strip().split(",")
for point in points:
if len(point) == 0:
continue
point_values = point.split(":")
point_name = point_values[0].strip()
if len(point_values) != 2:
raise Exception("{}: quantity is empty: {}".format(filename, point))
point_value = point_values[1].strip()
if len(point_value) == 0:
raise Exception("{}: invalid quantity: {}".format(filename, point))
point_quantity = int(point_value)
point_value = point_dict.get(point_name)
if not point_value:
raise KeyError("{}: invalid point tag `{}`".format(filename, point_name))
user_point += point_value * point_quantity
users[user_name] = user_point
base_sum_point = 0
sum_point = 0
for name, point in users.items():
base_sum_point += point
if is_active_user(name):
user_point = point
if name in additional_user_point_dict:
user_point += additional_user_point_dict[name]
if name in max_user_point_dict:
user_point = min(max_user_point_dict[name], point)
sum_point += user_point
if is_target_year:
print("| No. | user | base point | point | base rate | rate |")
print("|-----|------|------------|-------|-----------|------|")
number = 0
acc_number = 0
prev_point = 0
for name, point in sorted(users.items(), key=lambda item: item[1], reverse=True):
base_rate = point / base_sum_point * 100.0
user_point = point
if name in additional_user_point_dict:
user_point += additional_user_point_dict[name]
if name in max_user_point_dict:
user_point = min(max_user_point_dict[name], point)
if prev_point == 0 or point != prev_point:
number += 1 + acc_number
acc_number = 0
else:
acc_number += 1
prev_point = point
rate = (user_point / sum_point * 100.0) if is_active_user(name) else 0.0
print("| {} | @{} | {} | {} | {:.3}% | {:.3}% |".format(
number,
name,
point,
user_point,
base_rate,
rate))
return commit_dict
def check_commit_dict(commit_dict: dict[str, set[str]]) -> None:
for repo in commit_dict.keys():
if repo not in target_repos:
raise Exception("unknown repo name:{}".format(repo))
commit_log_set = set()
for repo in target_repos:
wd = os.getcwd()
command = "git log --after \'2023-01-01\' --pretty=oneline --no-merges".split(" ")
os.chdir(repo)
proc = Popen(command, stdin=-1,stdout=-1,stderr=-1)
out, err = proc.communicate()
if len(err) > 0:
print(err)
return
os.chdir(wd)
commit_set: set[str] = set()
for line in out.decode().split("\n"):
if len(line) <= 0:
continue
cols = line.split(" ")
commit_id = cols[0][:7]
if len(commit_id) != 7:
raise Exception("git log: {} commit-id length should be 7".format(commit_id))
commit_set.add(commit_id)
repo_commit_set: set[str] = commit_dict[repo] if repo in commit_dict else set()
diff = commit_set - repo_commit_set
if len(diff) > 0:
print("unstats commits {}: {}\n{}".format(repo, len(diff), diff))
if __name__ == '__main__':
argparser = argparse.ArgumentParser(description="")
argparser.add_argument("--year",
dest='target_year',
type=int,
default=0,
help="target year")
argparser.add_argument("--exclude-users",
dest='exclude_users_str',
type=str,
default="",
help="comma separated userid list")
argparser.add_argument("--receive-users",
dest='receive_users_str',
type=str,
default="",
help="comma separated userid list")
argparser.add_argument("--max-user-points",
dest='max_user_points_str',
type=str,
default="",
help="comma separated max point list")
argparser.add_argument("--additional-user-points",
dest='additional_user_points_str',
type=str,
default="",
help="comma separated additional point list")
args = argparser.parse_args()
if args.target_year == 0:
raise Exception("you must specify `--year N` option")
receive_users = [] if len(args.receive_users_str) == 0 else args.receive_users_str.split(",")
exclude_users = [] if len(args.exclude_users_str) == 0 else args.exclude_users_str.split(",")
max_user_points = args.max_user_points_str.split(",")
max_user_point_dict = dict()
for s in max_user_points:
if len(s) == 0:
continue
values = s.split("=")
max_user_point_dict[values[0]] = int(values[1])
additional_user_points = args.additional_user_points_str.split(",")
additional_user_point_dict = dict()
for s in additional_user_points:
if len(s) == 0:
continue
values = s.split("=")
additional_user_point_dict[values[0]] = int(values[1])
commit_dict: dict[str, set[str]] = dict(set())
for p in sorted(list(glob.glob("cpprefjp/site/start_editing/*.md", recursive=True))):
filename = os.path.basename(p)
m = re.fullmatch(r"contribution_stats_([0-9]*?)\.md", filename)
if not m:
continue
year = int(m[1])
with open(p) as f:
text = f.read()
commits = stats_contribution(
text,
p,
year,
args.target_year,
receive_users,
exclude_users,
max_user_point_dict,
additional_user_point_dict
)
for repo in target_repos:
repo_commits: set[str] = commits[repo] if repo in commits else set()
if repo in commit_dict:
commit_dict[repo] |= repo_commits
else:
commit_dict[repo] = repo_commits
check_commit_dict(commit_dict)