-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwhat_should_maint.py
executable file
·297 lines (237 loc) · 8.01 KB
/
what_should_maint.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
#!/usr/bin/env python3
# Have a person in mind, analyze git logs to extract areas where they
# could be listed as a maintainer.
#
# Workflow:
#
# 1. Extract the commit info:
#
# $path/what_should_maint.py --who $name --paths "net/" "include/net/" "include/linux/" "include/uapi/linux/" \
# --save data.json
#
# 2. Show the stats
#
# $path/what_should_maint.py --who $name --load data.json --top 25
#
# 3. Double check entry
#
# $path/what_should_maint.py --who $name --load data.json \
# --entry net/core/net_namespace.c include/net/net_namespace.h 'include/net/netns/*'
import argparse
import fnmatch
import json
import os
import subprocess
args = None
def commitify(haystack):
commits = []
commit = None
for line in haystack.split('\n'):
if line.startswith("commit"):
commit = {
"hash": line.split()[1],
"author": None,
"reviewers": [],
}
commits.append(commit)
if line.startswith("Author"):
commit["author"] = line[8:]
line = line.strip()
if line.startswith("Reviewed-by:"):
who = " ".join(line.split()[1:])
commit["reviewers"].append(who)
if line.startswith("Acked-by:"):
who = " ".join(line.split()[1:])
commit["reviewers"].append(who)
return commits
def is_excluded(path):
global args
for excluded in args.exclude:
if path.startswith(excluded):
return True
return False
def analyze():
global args
cmd = subprocess.run(["find"] + args.paths + ['-type', 'f'],
stdout=subprocess.PIPE)
files = cmd.stdout.decode("utf-8").strip().split()
stats = []
seq = 0
for path in files:
seq += 1
print(f"Analyze {seq}/{len(files)} ({path})" + " " * max(58 - len(path), 0),
end="\r")
if is_excluded(path):
continue
cmd = subprocess.run(["git", "log", "--no-merges", "--since=" + args.since, "--", path],
stdout=subprocess.PIPE)
git_log = cmd.stdout.decode("utf-8").strip()
commits = commitify(git_log)
auths = 0
revs = 0
for c in commits:
if args.who in c["author"]:
auths += 1
continue
for r in c["reviewers"]:
if args.who in r:
revs += 1
break
stat = {
"path": path,
"commits": commits,
}
stats.append(stat)
print()
return stats
def fnmatch_any(path, globs):
for g in globs:
if fnmatch.fnmatch(path, g):
return True
return False
def entry_mode(stats, entry):
files = 0
commits = {}
authors = {}
reviewers = {}
for stat in stats:
if not fnmatch_any(stat["path"], entry):
continue
files += 1
for c in stat["commits"]:
if c["hash"] in commits:
continue
for r in c["reviewers"]:
if r not in reviewers:
reviewers[r] = []
reviewers[r].append(c)
if c["author"] not in authors:
authors[c["author"]] = []
authors[c["author"]].append(c)
commits[c["hash"]] = c
top_a = sorted(authors.items(), key=lambda x: len(x[1]), reverse=True)[:10]
top_r = sorted(reviewers.items(), key=lambda x: len(x[1]), reverse=True)[:10]
print("Files:", files)
print("Commits:", len(commits))
print("Authors:", len(authors))
for name, l in top_a:
print(f" {name} {len(l)}")
print("Reviewers:", len(reviewers))
for name, l in top_r:
print(f" {name} {len(l)}")
def commit_cnt(d):
if d["commits"]:
return len(d["commits"])
return 1
def of_reviewed_pct(d):
global args
rev = 0
all_rev = 0
for c in d["commits"]:
if args.who in c["author"]:
continue
for r in c["reviewers"]:
if args.who in r:
rev += 1
break
if c["reviewers"]:
all_rev += 1
return rev / max(all_rev, 1)
def pr_header():
print("{:33} {:>6} {:<12} {:<12} {:<8}".format("", "total", "author", "review", "metric"))
def pr_stat(i, s, extra=0):
if extra:
extra = "{:5}%".format(round(extra * 100, 2))
else:
extra = ""
tot = len(s["commits"])
print("{:2} {:30} {:6} {:3} ({:5}%) {:3} ({:5}%) {:2}".
format(i, s["path"], tot,
s["author"],
round(s["author"] / tot * 100, 2),
s["reviewer"],
round(s["reviewer"] / tot * 100, 2),
extra))
def main():
parser = argparse.ArgumentParser(description='Extract reviewer metrics')
parser.add_argument('--linux', type=str, default=os.getcwd(),
help="Path to the Linux kernel git tree")
parser.add_argument('--since', type=str, help="Lookback period / argument to pass to --since for git log",
default="3 years ago")
parser.add_argument('--paths', type=str, nargs='*', default=[],
help="Directories to narrow the search down to")
parser.add_argument('--exclude', type=str, nargs='*', default=[],
help="Directories (prefixes of files) to skip")
parser.add_argument('--who', type=str, required=True,
help='Author of interest (needle, as in "John" will match all "Johns")')
parser.add_argument('--save', type=str,
help='Save full analysis to a JSON file for faster querying')
parser.add_argument('--load', type=str,
help='Load analysis from previously saved JSON file')
parser.add_argument('--top', type=int, default=15,
help='How many top files to display')
parser.add_argument('--entry', type=str, nargs='*',
help='Inverse mode, list maintainers for given files')
global args
args = parser.parse_args()
if args.load:
with open(args.load, "r") as fp:
stats = json.load(fp)
else:
stats = analyze()
if args.save:
with open(args.save, 'w') as fp:
json.dump(stats, fp)
for stat in stats:
auths = 0
revs = 0
for c in stat["commits"]:
if args.who in c["author"]:
auths += 1
continue
for r in c["reviewers"]:
if args.who in r:
revs += 1
break
stat["reviewer"] = revs
stat["author"] = auths
if args.entry:
entry_mode(stats, args.entry)
return
pr_header()
print("Top reviewer pct (not counting authored by self)")
stats = sorted(stats, key=lambda d: d['reviewer'] / max(commit_cnt(d) - d["author"], 1))
for i in range(1, 1 + args.top):
d = stats[-i]
pr_stat(i, d, d['reviewer'] / max(commit_cnt(d) - d["author"], 1))
print()
print("Top reviewer pct (reviewed patches only, not counting authored by self)")
stats = sorted(stats, key=lambda d: of_reviewed_pct(d))
for i in range(1, 1 + args.top):
d = stats[-i]
pr_stat(i, d, of_reviewed_pct(d))
print()
pr_header()
print("Top review pct")
stats = sorted(stats, key=lambda d: d['reviewer'] / commit_cnt(d))
for i in range(1, 1 + args.top):
pr_stat(i, stats[-i])
print()
print("Top author pct")
stats = sorted(stats, key=lambda d: d['author'] / commit_cnt(d))
for i in range(1, 1 + args.top):
pr_stat(i, stats[-i])
print()
pr_header()
print("Top review abs")
stats = sorted(stats, key=lambda d: d['reviewer'])
for i in range(1, 1 + args.top):
pr_stat(i, stats[-i])
print()
print("Top author abs")
stats = sorted(stats, key=lambda d: d['author'])
for i in range(1, 1 + args.top):
pr_stat(i, stats[-i])
print()
if __name__ == "__main__":
main()