forked from linux-netdev/ml-stat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml-stat.py
executable file
·411 lines (325 loc) · 11.5 KB
/
ml-stat.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python
# SPDX-License-Identifier: GPL-2.0
import argparse
import email
import email.utils
import json
import filecmp
import os
import shutil
import subprocess
import sys
from email.policy import default
email_roots = dict()
email_grps = dict()
class EmailMsg:
def __init__(self, msg):
self.msg = msg
def subject(self):
return self.msg.get('subject')
def is_patch(self):
return self.subject()[0] == '[' and not self.is_pr()
def is_pr(self):
return self.subject().find('pull req') != -1
def is_discussion(self):
subj = self.subject()
return not self.is_pr() and (subj.find('[') == -1 and subj.find(']') == -1)
def is_unknown(self):
return not self.is_patch() and not self.is_discussion() and not self.is_pr()
def is_bad(self):
return self.is_patch() + self.is_discussion() + self.is_pr() > 1
def get(self, key):
return self.msg.get(key)
def get_all(self, key):
return self.msg.get_all(key)
def get_from_mapped(self, mappings):
ret = []
for addr in self.msg.get_all('from'):
if addr.find('<') < 0:
addr = '<' + addr + '>'
for mapping in mappings:
for m in mapping:
if addr.find(m[0]) >= 0:
addr = m[1]
break
ret.append(addr)
return ret
class EmailThread:
def __init__(self, grp):
self.grp = grp
self.root = grp['root']
self.msgs = []
self.root_msg = None
for msg in self.grp['emails']:
emsg = EmailMsg(msg)
if msg is self.root:
self.root_msg = emsg
self.msgs.append(emsg)
def root(self):
return self.grp['root']
def root_subj(self):
return self.grp['root'].get('subject')
def is_patch(self):
return self.root_subj()[0] == '[' and not self.is_pr()
def is_pr(self):
return self.root_subj().find('pull req') != -1
def is_discussion(self):
subj = self.root_subj()
return not self.is_pr() and (subj.find('[') == -1 and subj.find(']') == -1)
def is_unknown(self):
return not self.is_patch() and not self.is_discussion() and not self.is_pr()
def is_bad(self):
return self.is_patch() + self.is_discussion() + self.is_pr() > 1
def participants(self, mapping):
people = dict()
for msg in self.msgs:
for person in msg.get_from_mapped(mapping):
if person not in people:
people[person] = 0
people[person] += 1
remove_bots(people)
return people
def authors(self, mapping):
people = dict()
for msg in self.msgs:
if msg is self.root_msg or msg.is_pr() or msg.is_patch():
for person in msg.get_from_mapped(mapping):
if person not in people:
people[person] = 0
people[person] += 1
remove_bots(people)
return people
def remove_bots(people_dict):
for bot in ['<patchwork-bot+netdevbpf@kernel.org>',
'kernel test robot <lkp@intel.com>',
'<pr-tracker-bot@kernel.org>',
'<patchwork-bot+bluetooth@kernel.org>']:
people_dict.pop(bot, 0)
def git(cmd):
# print(' '.join(['git'] + cmd))
with subprocess.Popen(['/usr/bin/git'] + cmd, cwd='netdev-2.git',
stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
p.wait()
# print(p.stdout.read().decode('utf-8'))
# print(p.stderr.read().decode('utf-8'))
# print(p.returncode)
def refset_add(refs, msg, key):
ref = msg.get_all(key)
if not ref:
return
refs.update(set(ref))
def print_top(ppl_stat, key, subkey, n):
print(f'Top {n} {key}s ({subkey}):')
ppl = sorted(ppl_stat.keys(), key=lambda x: ppl_stat[x][key][subkey])
for i in range(1, n + 1):
p = ppl[-i]
print(f" {i:2}. [{ppl_stat[p][key][subkey]:3}] {p}")
print()
def print_one(ppl_stat, key, subkey, p):
if p not in ppl_stat:
return
ppl = sorted(ppl_stat.keys(), key=lambda x: ppl_stat[x][key][subkey])
i = ppl.index(p)
print(f"{key} ({subkey}):")
print(f" #{i:2}. [{ppl_stat[p][key][subkey]:3}] {p}")
def prep_files(file_dir, git_dir, n):
# os.listdir
# os.path import isfile, join
if not os.path.isdir(file_dir):
os.mkdir(file_dir)
files = set()
for f in os.listdir(file_dir):
if not os.path.isfile(os.path.join(file_dir, f)):
continue
if not f.isnumeric():
continue
files.add(int(f))
# Sanity check
if len(files):
id_to_check = min(files)
git(['checkout', f'master~{id_to_check}'])
ret = filecmp.cmp(os.path.join(file_dir, str(id_to_check)), os.path.join(git_dir, 'm'))
if not ret:
print(f'Files look stale id: {id_to_check}: {ret}')
sys.exit(1)
for i in range(n):
if i in files:
continue
git(['checkout', f'master~{i}'])
shutil.copy2(os.path.join(git_dir, 'm'), os.path.join(file_dir, str(i)))
if (i % 100) == 0:
print(f"Checking out {i}/{n}", end='\r')
git(['reset', '--hard', 'master'])
def name_selfcheck(ppl_stat):
names = dict()
for p in ppl_stat:
if p.find('<') > 0:
continue
for p2 in ppl_stat:
if p in p2 and p != p2:
idx = p2.find('<')
if p.find('<') < 1:
continue
name = p[:idx]
if name not in names:
names[name] = []
names[name].append(p)
print(f'Mapped {p} {p2}')
break
else:
print(f'No map for {p}')
for p in ppl_stat:
idx = p.find('<')
if idx == -1:
continue
name = p[:idx]
if name not in names:
names[name] = []
names[name].append(p)
for n in names:
if len(names[n]) > 1:
print(f"{n}: {names[n]}")
def group_one_msg(msg, stats):
refs = set()
refset_add(refs, msg, 'references')
refset_add(refs, msg, 'in-reply-to')
mid = msg.get('message-id')
if not refs:
grp = {'root': msg, 'emails': [msg]}
email_roots[mid] = grp
email_grps[mid] = grp
stats['root'] += 1
else:
for r in refs:
if r in refs and r in email_grps:
grp = email_grps[r]
grp['emails'].append(msg)
email_grps[mid] = grp
stats['match'] += 1
return True
else:
return False
return True
def main():
parser = argparse.ArgumentParser(description='Mailing list stats')
parser.add_argument('--corp', dest='corp', action='store_true', default=False,
help="Print the stats by company rather than by person")
parser.add_argument('--db', type=str, required=True)
parser.add_argument('--email-count', type=int, required=True,
help="How many emails to look back into the archive")
# Development options
parser.add_argument('--name-dump', dest='name_dump', action='store_true', default=False)
parser.add_argument('--check', dest='check', action='store_true', default=False)
parser.add_argument('--name', nargs='+', default=[])
parser.add_argument('--proc', dest='proc', action='store_true', default=False)
parser.add_argument('--dump-miss', dest='misses', action='store_true', default=False)
parser.add_argument('--top-extra', type=int, required=False, default=0,
help="How many extra entries to add to the top n")
args = parser.parse_args()
with open(args.db, 'r') as f:
db = json.load(f)
stats = {
'root': 0,
'match': 0,
'miss': 0,
'skip-asel': 0,
}
misses = []
prep_files('msg-files', 'netdev-2.git', args.email_count)
dated = False
for i in reversed(range(args.email_count)):
with open(f'msg-files/{i}', 'rb') as fp:
msg = email.message_from_binary_file(fp, policy=default)
if not dated:
print(msg.get('date'))
dated = True
if (i % 100) == 0:
print(args.email_count - i, end='\r')
subj = msg.get('subject')
if not subj or subj.find('PATCH AUTOSEL') != -1:
stats['skip-asel'] += 1
continue
if not group_one_msg(msg, stats):
misses.append(msg)
# Re-try misses, apparently git-send-email sends out of order
n_misses = 0
while n_misses != len(misses):
n_misses = len(misses)
i = 0
while i < len(misses):
while group_one_msg(misses[i], stats):
del misses[i]
i += 1
stats['miss'] = len(misses)
threads = dict()
for mid, grp in email_roots.items():
threads[mid] = EmailThread(grp)
print('Unknown:')
for mid, thr in threads.items():
if thr.is_unknown():
print(' ' + thr.root_subj())
print('Bad:')
for mid, thr in threads.items():
if thr.is_bad():
print(' ' + thr.root_subj())
mailmap = db['mailmap']
corpmap = db['corpmap']
for m in mailmap:
for c in corpmap:
if m[1].find(c[0]) != -1:
corpmap.append((m[0], c[1],))
break
use_map = [mailmap]
if args.corp:
use_map.append(corpmap)
ppl_stat = dict()
for mid, thr in threads.items():
authors = thr.authors(use_map)
parti = thr.participants(use_map)
for p in parti:
if p not in ppl_stat:
ppl_stat[p] = {'author': {'thr': 0, 'msg': 0},
'reviewer': {'thr': 0, 'msg': 0}}
if p in authors:
ppl_stat[p]['author']['thr'] += 1
ppl_stat[p]['author']['msg'] += authors[p]
else:
ppl_stat[p]['reviewer']['thr'] += 1
ppl_stat[p]['reviewer']['msg'] += parti[p]
for p in ppl_stat.keys():
score = 10 * ppl_stat[p]['reviewer']['thr'] + 2 * (ppl_stat[p]['reviewer']['msg'] - 1) \
- 3 * ppl_stat[p]['author']['thr'] - (ppl_stat[p]['author']['msg'] // 2)
ppl_stat[p]['score'] = {'positive': score, 'negative': -score}
print(stats)
print()
if args.proc:
pass
elif args.misses:
l = []
for m in misses:
l.append(m.get('subject') + '\t' + m.get('date') + '\t' + m.get('message-id'))
for m in sorted(l):
print(m)
elif args.check:
name_selfcheck(ppl_stat)
elif args.name_dump:
print(f'Names ({len(ppl_stat)}):')
print(' ' + '\n '.join(sorted(ppl_stat.keys())))
else:
out_keys = [
('reviewer', 'thr', 10),
('reviewer', 'msg', 10),
('author', 'thr', 15),
('author', 'msg', 10),
('score', 'positive', 15),
('score', 'negative', 15)
]
if args.name:
for name in args.name:
for ok in out_keys:
print_one(ppl_stat, ok[0], ok[1], name)
else:
for ok in out_keys:
print_top(ppl_stat, ok[0], ok[1], ok[2] + args.top_extra)
if __name__ == "__main__":
main()