-
Notifications
You must be signed in to change notification settings - Fork 23
/
commit-report.py
executable file
·608 lines (517 loc) · 21 KB
/
commit-report.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#!/usr/bin/python3
"""Generate a commit report
Usage:
commit-report.py [options] [-v...] [-c KEY=VAL...]
Options:
--since=WHEN Start date [default: one.month.ago]
--until=WHEN End commit
--from=COMMIT Starting commit
--to=COMMIT Ending commit (defaults to {remote}/master)
-h, --help Display this help and exit
-v, --verbose Increase verbosity
--config FILE Configuration file [default: ~/.ipa/pushpatch.yaml]
-c, --set-conf=KEY=VAL Set a config option(s). VAL is in YAML format.
--no-trac Do not contact Trac
--no-fetch Do not synchronize before reporting
--mailto=EMAIL Mail results to this address
--mailfrom=EMAIL Mail results from this address
--color=(auto|always|never) Colorize output [default: auto]
Generate a commit report.
The --mailto and --mailfrom options must be specified together.
Configuration can be specified in the file given by --config.
Here is an example
project-name: FreeIPA
clean-repo-path: ~/dev/freeipa-clean
ticket-url: https://fedorahosted.org/freeipa/ticket/
commit-url: https://fedorahosted.org/freeipa/changeset/
bugzilla-bug-url: https://bugzilla.redhat.com/show_bug.cgi?id=
trac-xmlrpc-url: https://fedorahosted.org/freeipa/xmlrpc
remote: origin
smtp-host: smtp.example.com
smtp-port: 25
"""
# TODO: Much of this is copied from pushpatches.py. Make a common library.
import os
import sys
import subprocess
import re
import collections
import pprint
import xmlrpc.client
import functools
import datetime
import smtplib
import math
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import csv
import docopt # yum install python3-docopt
import yaml # yum install python3-PyYAML
import blessings # yum install python3-blessings
import pytz # yum install python3-pytz
COLOR_OPT_MAP = {'auto': False, 'always': True, 'never': None}
SubprocessResult = collections.namedtuple(
'SubprocessResult', 'stdout stderr returncode')
TracTicketData = collections.namedtuple(
'TracTicketData', 'id time_created time_changed attributes')
CommitGroup = collections.namedtuple(
'CommitGroup', 'info commits')
AuthorInfo = collections.namedtuple(
'AuthorInfo', 'name email date')
class reify(object):
# https://github.com/Pylons/pyramid/blob/1.4-branch/pyramid/decorator.py
def __init__(self, wrapped):
self.wrapped = wrapped
self.__doc__ = getattr(wrapped, '__doc__', None)
def __get__(self, inst, objtype=None):
if inst is None:
return self
val = self.wrapped(inst)
setattr(inst, self.wrapped.__name__, val)
return val
@functools.total_ordering
class Ticket(object):
"""Trac ticket with lazily fetched information"""
_memo = {}
def __init__(self, cli, number):
self.cli = cli
self.number = number
@reify
def data(self):
try:
return self._memo[self]
except KeyError:
self.cli.eprint('Retrieving ticket %s' % self.number)
data = TracTicketData(*self.cli.trac.ticket.get(self.number))
self._memo[self] = self
return data
@property
def attributes(self):
return self.data.attributes
@property
def url(self):
return self.cli.config['ticket-url'] + str(self.number)
def __hash__(self):
return hash(self.number) ^ hash(self.cli)
def __eq__(self, other):
return self.number == other.number
def __lt__(self, other):
return self.number < other.number
class CLIHelper(object):
def __init__(self, options):
self.output = []
self.options = options
with open(os.path.expanduser(options['--config'])) as conf_file:
self.config = yaml.safe_load(conf_file)
for opt in options['--set-conf']:
key, sep, value = opt.partition('=')
self.config[key] = yaml.safe_load(value)
self.term = blessings.Terminal(
force_styling=COLOR_OPT_MAP[options['--color']])
self.verbosity = self.options['--verbose']
if self.verbosity:
self.eprint('Options:')
self.eprint(pprint.pformat(self.options))
self.eprint('Config:')
self.eprint(pprint.pformat(self.config))
if self.options['--no-trac']:
self.trac = None
else:
url = self.config['trac-xmlrpc-url']
self.trac = xmlrpc.client.ServerProxy(url)
self.color_arg = self.options['--color']
if self.color_arg == 'auto':
if self.term.is_a_tty:
self.color_arg = 'always'
else:
self.color_arg = 'never'
def die(self, message):
self.eprint(self.term.red(message), file=sys.stderr)
exit(1)
def eprint(self, *messages, **kwargs):
"""Print to stderr"""
kwargs.setdefault('file', sys.stderr)
self.print(*messages, **kwargs)
def print(self, *objects, sep=' ', end='\n', file=sys.stdout):
if file == sys.stdout:
self.output.append(sep.join(str(o) for o in objects) + end)
print(*objects, sep=sep, end=end, file=file)
def write(self, string, file=sys.stdout):
self.output.append(string)
print(string, end='')
sys.stdout.flush()
def runcommand(self, argv, *, check_stdout=None, check_stderr=None,
check_returncode=0, stdin_string='', fail_message=None,
timeout=5, verbosity=None):
"""Run a command in a subprocess, check & return result"""
argv_repr = ' '.join(self.shell_quote(a) for a in argv)
if verbosity is None:
verbosity = self.verbosity
if verbosity:
self.eprint('+', self.term.blue(argv_repr))
if verbosity > 2:
self.eprint(self.term.yellow(stdin_string.rstrip()))
PIPE = subprocess.PIPE
proc = subprocess.Popen(argv, stdout=PIPE, stderr=PIPE, stdin=PIPE)
try:
stdout, stderr = proc.communicate(stdin_string.encode('utf-8'),
timeout=timeout)
timeout_expired = False
except subprocess.TimeoutExpired:
proc.kill()
stdout = stderr = b''
timeout_expired = True
stdout = stdout.decode('utf-8')
stderr = stderr.decode('utf-8')
returncode = proc.returncode
failed = any([
timeout_expired,
(check_stdout is not None and check_stdout != stdout),
(check_stderr is not None and check_stderr != stderr),
(check_returncode is not None and check_returncode != returncode),
])
if failed and not verbosity:
self.eprint('+', self.term.blue(argv_repr))
if failed or verbosity >= 2:
if stdout:
self.eprint(stdout.rstrip())
if stderr:
self.eprint(self.term.yellow(stderr.rstrip()))
self.eprint('→ %s' % self.term.blue(str(proc.returncode)))
if failed:
if timeout_expired:
self.die('Command timeout expired')
elif fail_message:
self.die(fail_message)
else:
self.die('Command failed')
return SubprocessResult(stdout, stderr, returncode)
@staticmethod
def shell_quote(arg):
"""Quote an argument for the shell"""
if re.match('^[-_.:/=a-zA-Z0-9]*$', arg):
return arg
else:
return "'%s'" % arg.replace("'", r"'\''")
@staticmethod
def cleanpath(path):
"""Return absolute path with leading ~ expanded"""
path = os.path.expanduser(path)
path = os.path.abspath(path)
return path
def pprint(text, language=None):
'TODO'
def groupby(commits, get_groupkeys,
get_sortkey=lambda group: (-group.info['num_commits'],
-group.info['changed'])):
groups = {}
for commit in commits:
for key in get_groupkeys(commit):
group = groups.setdefault(key, CommitGroup({}, []))
group.commits.append(commit)
for key, group in groups.items():
group.info['key'] = key
group.info['files'] = {}
group.info['added'] = 0
group.info['removed'] = 0
group.info['tickets'] = set()
group.info['num_commits'] = len(group.commits)
for commit in group.commits:
for filename, (added, removed) in commit.files.items():
add1, rem1 = group.info['files'].get(filename, (0, 0))
add1 += added
rem1 += removed
group.info['added'] += added
group.info['removed'] += removed
group.info['files'][filename] = add1, rem1
group.info['tickets'].update(commit.tickets)
group.info['changed'] = group.info['added'] + group.info['removed']
return sorted(groups.values(), key=get_sortkey)
class Commit(object):
def __init__(self, sha1):
self.sha1 = sha1
self.message = []
self.parents = []
self.tickets = set()
self.files = {}
self.reviewers = []
@staticmethod
def parse_commit_info(line):
match = re.match(r'(.+ <[^>]+>) (\d+) ([+-])(\d\d)(\d\d)', line)
if not match:
print(line)
offset = 60 * int(match.group(4)) + int(match.group(5))
if match.group(3) == '-':
offset = -offset
date = datetime.datetime.fromtimestamp(int(match.group(2)),
pytz.FixedOffset(offset))
name_mail = check_mailmap(match.group(1))
name, _sep, mail = name_mail.partition(' <')
mail = mail.strip('>')
return AuthorInfo(name, mail, date)
@property
def reviewers_short(self):
return ','.join(shorten_author(r) for r in self.reviewers)
@property
def author(self):
return '%s <%s>' % (self.author_info.name, self.author_info.email)
@property
def commit_date(self):
return self.committer_info.date
@property
def author_short(self):
return shorten_author(self.author)
@property
def added(self):
return sum(a for a, r in self.files.values())
@property
def removed(self):
return sum(r for a, r in self.files.values())
@property
def summary(self):
return self.message[0]
def shorten_author(author):
match = re.match(r'^.*<(.*)@.*> *$', author)
if match:
return match.group(1)
else:
return author
def git_date(cli, spec):
output = cli.runcommand(['git', 'rev-parse', '--since=%s' % spec]).stdout.strip()
name, sep, seconds = output.partition('=')
return datetime.datetime.fromtimestamp(int(seconds))
def git_describe(cli, commit):
argv = ['git', 'describe', '--tags', commit]
return cli.runcommand(argv).stdout.strip()
def check_mailmap(name):
cmd = cli.runcommand(
['git', '-c', 'mailmap.blob=origin/master:.mailmap',
'check-mailmap', name],
check_returncode=None)
if cmd.returncode == 0:
return cmd.stdout.strip()
else:
return name + ' <bad-entry@invalid>'
def safelogdiv(numerator, denominator):
if not denominator:
return 'inf'
if not numerator:
return '-inf'
else:
return math.log(numerator / denominator)
def safediv(numerator, denominator):
if not denominator:
return 'inf'
else:
return numerator / denominator
def csv_num_repr(number):
if isinstance(number, (int, float)):
if number == int(number):
return int(number)
elif abs(number) > 1:
return round(number, 2)
else:
return round(number, int(-math.log(abs(number), 10) + 3))
else:
return number
def run(cli):
cli.print('Generated:', datetime.datetime.now())
os.chdir(cli.cleanpath(cli.config['clean-repo-path']))
remote = cli.config['remote']
if not cli.options['--no-fetch']:
cli.eprint('Fetching...')
cli.runcommand(['git', 'fetch', remote], timeout=60)
log_args = []
end_commit = cli.options['--to'] or '%s/master' % remote
start_commit = cli.options['--from']
if start_commit:
log_args.append('%s..%s' % (start_commit, end_commit))
cli.print('From: ', start_commit,
'(%s)' % git_describe(cli, start_commit))
else:
log_args.append(end_commit)
cli.print('To: ', end_commit, '(%s)' % git_describe(cli, end_commit))
since = cli.options['--since']
if since:
log_args.append('--since=%s' % since)
cli.print('Since: ', git_date(cli, since))
until = cli.options['--until']
if until:
log_args.append('--until=%s' % until)
cli.print('Until: ', git_date(cli, until))
cli.print()
static_argv = ['git', '-c', 'mailmap.blob=origin/master:.mailmap',
'log', '--boundary', '--format=raw', '--numstat',
'--use-mailmap']
output = cli.runcommand(static_argv + log_args, timeout=60).stdout
ticket_re = re.compile(re.escape(cli.config['ticket-url']) + '(\d+)')
all_tickets = set()
commits = []
boundary_commits = []
on_gpgsig = False
for line in output.splitlines():
header, sep, content = line.partition(' ')
content = content.strip()
if not line:
# gpgsig is multi-line field, starting with ' '
on_gpgsig = False
elif header == 'commit':
current_commit = Commit(content.strip(' -'))
if content.startswith('-'):
boundary_commits.append(current_commit)
else:
commits.append(current_commit)
elif header == 'tree':
current_commit.tree = content
elif header == 'parent':
current_commit.parents.append(content)
elif header == 'author':
current_commit.author_info = Commit.parse_commit_info(content)
elif header == 'committer':
current_commit.committer_info = Commit.parse_commit_info(content)
elif line.startswith(' '):
current_commit.message.append(line[4:].rstrip('\n'))
for match in ticket_re.finditer(line):
ticket = Ticket(cli, int(match.group(1)))
all_tickets.add(ticket)
current_commit.tickets.add(ticket)
mheader, sep, mcontent = line.strip().partition(' ')
if mheader.lower() == 'reviewed-by:':
current_commit.reviewers.append(check_mailmap(mcontent))
elif line.startswith('gpgsig') or on_gpgsig:
on_gpgsig = True
else:
added, removed, filename = line.split('\t')
try:
added_removed = int(added), int(removed)
except ValueError:
print(line)
added_removed = 0, 0
current_commit.files[filename] = added_removed
cli.print('git log:', ' '.join(cli.shell_quote(arg) for arg in log_args))
for commit in commits:
cli.print('* {c.sha1:7.7} ({c.commit_date}) [{c.author_short};{c.reviewers_short}] {c.message[0]}'.format(c=commit))
for commit in boundary_commits:
cli.print('^ {c.sha1:7.7} ({c.commit_date}) [{c.author_short};{c.reviewers_short}] {c.message[0]}'.format(c=commit))
cli.print(len(commits), 'commits')
cli.print()
rep = lambda title, key: print_report(cli, commits, title, key)
rep('By patch author', lambda commit: [commit.author])
rep('By reviewer', lambda commit: commit.reviewers)
report_text = cli.output
cli.output = []
print(cli.term.cyan('=== CSV report ==='))
if until:
subject_date = git_date(cli, until).date()
else:
subject_date = datetime.date.today()
if commits:
commmit_range = '%s..%s' % (boundary_commits[0].sha1[:7],
commits[0].sha1[:7])
else:
# Use empty trees
empty_tree = cli.runcommand(['git', 'hash-object',
'-t', 'tree', '/dev/null']).stdout
commmit_range = '{0}..{0}'.format(empty_tree)
outputter = csv.writer(cli)
outputter.writerow(['id', 'summary', '+', '-',
'author', 'reviewer'])
for commit in commits:
outputter.writerow([commit.sha1[:7], commit.summary,
commit.added, commit.removed,
commit.author] +
list(commit.reviewers))
main_csv_text = cli.output
cli.output = []
print(cli.term.cyan('=== People report ==='))
outputter = csv.writer(cli)
outputter.writerow(['name', 'patches', 'reviews',
'lines changed', 'lines reviewed',
'patches:reviews',
'lines changed:reviewed'])
people = set(c.author for c in commits)
for commit in commits:
people.update(commit.reviewers)
people_info = {p: {'authored': [c for c in commits if c.author == p],
'reviewed': [c for c in commits if p in c.reviewers]}
for p in people}
# In case of multiple reviewers, the patch is counted for all of them,
# but the lines changed are split between the reviewers.
for info in people_info.values():
info['lines_authored'] = sum(c.added + c.removed
for c in info['authored'])
info['lines_reviewed'] = sum((c.added + c.removed) / len(c.reviewers)
for c in info['reviewed'])
def sort_key(k_v):
k, v = k_v
if v['reviewed']:
backup_key = -len(v['authored']), -len(v['reviewed'])
backup_key += -v['lines_authored'], -v['lines_reviewed']
return 0, len(v['authored']) / len(v['reviewed']), backup_key
else:
return 1, len(v['authored']), v['lines_authored']
for person, info in sorted(people_info.items(), key=sort_key):
outputter.writerow([
person,
len(info['authored']),
len(info['reviewed']),
info['lines_authored'],
csv_num_repr(info['lines_reviewed']),
csv_num_repr(safediv(len(info['authored']), len(info['reviewed']))),
csv_num_repr(safediv(info['lines_authored'], info['lines_reviewed'])),
])
people_csv_text = cli.output
cli.output = []
if cli.options['--mailto']:
project_name = cli.config['project-name']
msg = MIMEMultipart('mixed')
text_part = MIMEText(''.join(report_text), 'plain')
msg.attach(text_part)
csv_part = MIMEText(''.join(main_csv_text), 'csv')
csv_part.add_header('Content-Disposition',
'attachment; filename="{}-commits-{}.csv"'.format(
project_name.lower(),
subject_date.strftime('%Y-%m')))
msg.attach(csv_part)
ppl_part = MIMEText(''.join(people_csv_text), 'csv')
ppl_part.add_header('Content-Disposition',
'attachment; filename="{}-people-{}.csv"'.format(
project_name.lower(),
subject_date.strftime('%Y-%m')))
msg.attach(ppl_part)
msg['Subject'] = '{project} commit report {date} ({commmit_range})'.format(
project=project_name,
date=subject_date,
commmit_range=commmit_range)
msg['From'] = cli.options['--mailfrom']
to = cli.options['--mailto']
msg['To'] = to
host = cli.config['smtp-host']
port = int(cli.config['smtp-port'])
cli.eprint('Sending results to {to} via {host}:{port}...'.format(
to=to,
host=host,
port=port))
smtp = smtplib.SMTP(host, port)
smtp.send_message(msg)
smtp.quit()
cli.eprint('Sent!')
def print_report(cli, commits, title, keyfunc):
cli.print('%s:' % title)
for group in groupby(commits, keyfunc):
cli.print('{g.info[num_commits]:4} {g.info[key]} (+{g.info[added]} -{g.info[removed]})'.format(g=group))
for commit in group.commits:
cli.print(' {c.sha1:7.7} [{c.author_short};{c.reviewers_short}] {c.summary} (+{c.added} -{c.removed})'.format(url=cli.config['commit-url'], c=commit))
for ticket in sorted(group.info['tickets']):
if cli.trac:
if ticket.attributes['status'] == 'closed':
cli.print(' #{t.number} [{t.attributes[owner]}\'s {t.attributes[component]} {t.attributes[type]}] {t.attributes[summary]}'.format(t=ticket))
if ticket.attributes['rhbz'] and ticket.attributes['rhbz'] != '0':
rhbz = ticket.attributes['rhbz'].split(' ', 1)[-1].strip('[]')
cli.print(' BZ {rhbz}'.format(rhbz=rhbz))
else:
cli.print(' {t.url}'.format(t=ticket))
cli.print()
if __name__ == '__main__':
cli = CLIHelper(docopt.docopt(__doc__))
run(cli)