-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_activity.py
executable file
·1029 lines (873 loc) · 32.6 KB
/
my_activity.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Get stats about your activity.
Example:
- my_activity.py for stats for the current week (last week on mondays).
- my_activity.py -Q for stats for last quarter.
- my_activity.py -Y for stats for this year.
- my_activity.py -b 4/5/12 for stats since 4/5/12.
- my_activity.py -b 4/5/12 -e 6/7/12 for stats between 4/5/12 and 6/7/12.
"""
# These services typically only provide a created time and a last modified time
# for each item for general queries. This is not enough to determine if there
# was activity in a given time period. So, we first query for all things created
# before end and modified after begin. Then, we get the details of each item and
# check those details to determine if there was activity in the given period.
# This means that query time scales mostly with (today() - begin).
import cookielib
import csv
import datetime
from datetime import datetime
from datetime import timedelta
from functools import partial
import json
import optparse
import os
import re
import subprocess
import sys
import urllib
import urllib2
import rietveld
from third_party import upload
# Imported later, once options are set.
webkitpy = None
try:
from dateutil.relativedelta import relativedelta # pylint: disable=F0401
except ImportError:
print 'python-dateutil package required'
exit(1)
# python-keyring provides easy access to the system keyring.
try:
import keyring # pylint: disable=W0611,F0401
except ImportError:
print 'Consider installing python-keyring'
def webkit_account(user):
if not webkitpy:
return None
committer_list = webkitpy.common.config.committers.CommitterList()
email = user + "@chromium.org"
return committer_list.account_by_email(email)
def user_to_webkit_email(user):
account = webkit_account(user)
if not account:
return None
return account.emails[0]
def user_to_webkit_owner_search(user):
account = webkit_account(user)
if not account:
return ['--author=%[email protected]' % user]
search = []
for email in account.emails:
search.append('--author=' + email)
# commit-bot is author for contributors who are not committers.
search.append('--grep=Patch by ' + account.full_name)
return search
def user_to_webkit_reviewer_search(user):
committer_list = webkitpy.common.config.committers.CommitterList()
email = user + "@chromium.org"
account = committer_list.reviewer_by_email(email)
if not account:
return []
return ['--grep=Reviewed by ' + account.full_name]
rietveld_instances = [
{
'url': 'codereview.chromium.org',
'shorturl': 'crrev.com',
'supports_owner_modified_query': True,
'requires_auth': False,
'email_domain': 'chromium.org',
},
{
'url': 'chromereviews.googleplex.com',
'shorturl': 'go/chromerev',
'supports_owner_modified_query': True,
'requires_auth': True,
'email_domain': 'google.com',
},
{
'url': 'codereview.appspot.com',
'supports_owner_modified_query': True,
'requires_auth': False,
'email_domain': 'chromium.org',
},
{
'url': 'breakpad.appspot.com',
'supports_owner_modified_query': False,
'requires_auth': False,
'email_domain': 'chromium.org',
},
]
gerrit_instances = [
{
'url': 'gerrit.chromium.org',
'port': 29418,
},
{
'url': 'gerrit-int.chromium.org',
'port': 29419,
},
]
google_code_projects = [
{
'name': 'chromium',
'shorturl': 'crbug.com',
},
{
'name': 'chromium-os',
'shorturl': 'crosbug.com',
},
{
'name': 'chrome-os-partner',
},
{
'name': 'google-breakpad',
},
{
'name': 'gyp',
},
{
'name': 'skia',
},
]
bugzilla_instances = [
{
'search_url': 'http://bugs.webkit.org/buglist.cgi',
'url': 'wkb.ug',
'user_func': user_to_webkit_email,
},
]
git_instances = [
{
'option': 'webkit_repo',
'change_re':
r'git-svn-id: http://svn\.webkit\.org/repository/webkit/trunk@(\d*)',
'change_url': 'trac.webkit.org/changeset',
'review_re': r'https://bugs\.webkit\.org/show_bug\.cgi\?id\=(\d*)',
'review_url': 'wkb.ug',
'review_prop': 'webkit_bug_id',
'owner_search_func': user_to_webkit_owner_search,
'reviewer_search_func': user_to_webkit_reviewer_search,
},
]
# Uses ClientLogin to authenticate the user for Google Code issue trackers.
def get_auth_token(email):
# KeyringCreds will use the system keyring on the first try, and prompt for
# a password on the next ones.
creds = upload.KeyringCreds('code.google.com', 'code.google.com', email)
for _ in xrange(3):
email, password = creds.GetUserCredentials()
url = 'https://www.google.com/accounts/ClientLogin'
data = urllib.urlencode({
'Email': email,
'Passwd': password,
'service': 'code',
'source': 'chrome-my-activity',
'accountType': 'GOOGLE',
})
req = urllib2.Request(url, data=data, headers={'Accept': 'text/plain'})
try:
response = urllib2.urlopen(req)
response_body = response.read()
response_dict = dict(x.split('=')
for x in response_body.split('\n') if x)
return response_dict['Auth']
except urllib2.HTTPError, e:
print e
print 'Unable to authenticate to code.google.com.'
print 'Some issues may be missing.'
return None
def username(email):
"""Keeps the username of an email address."""
return email and email.split('@', 1)[0]
def datetime_to_midnight(date):
return date - timedelta(hours=date.hour, minutes=date.minute,
seconds=date.second, microseconds=date.microsecond)
def get_quarter_of(date):
begin = (datetime_to_midnight(date) -
relativedelta(months=(date.month % 3) - 1, days=(date.day - 1)))
return begin, begin + relativedelta(months=3)
def get_year_of(date):
begin = (datetime_to_midnight(date) -
relativedelta(months=(date.month - 1), days=(date.day - 1)))
return begin, begin + relativedelta(years=1)
def get_week_of(date):
begin = (datetime_to_midnight(date) - timedelta(days=date.weekday()))
return begin, begin + timedelta(days=7)
def get_yes_or_no(msg):
while True:
response = raw_input(msg + ' yes/no [no] ')
if response == 'y' or response == 'yes':
return True
elif not response or response == 'n' or response == 'no':
return False
def datetime_from_rietveld(date_string):
return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f')
def datetime_from_google_code(date_string):
return datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%fZ')
class MyActivity(object):
def __init__(self, options):
self.options = options
self.modified_after = options.begin
self.modified_before = options.end
self.user = options.user
self.changes = []
self.reviews = []
self.issues = []
self.check_cookies()
self.google_code_auth_token = None
self.webkit_repo = options.webkit_repo
if self.webkit_repo:
self.setup_webkit_info()
# Check the codereview cookie jar to determine which Rietveld instances to
# authenticate to.
def check_cookies(self):
cookie_file = os.path.expanduser('~/.codereview_upload_cookies')
cookie_jar = cookielib.MozillaCookieJar(cookie_file)
if not os.path.exists(cookie_file):
exit(1)
try:
cookie_jar.load()
print 'Found cookie file: %s' % cookie_file
except (cookielib.LoadError, IOError):
exit(1)
filtered_instances = []
def has_cookie(instance):
for cookie in cookie_jar:
if cookie.name == 'SACSID' and cookie.domain == instance['url']:
return True
if self.options.auth:
return get_yes_or_no('No cookie found for %s. Authorize for this '
'instance? (may require application-specific '
'password)' % instance['url'])
filtered_instances.append(instance)
return False
for instance in rietveld_instances:
instance['auth'] = has_cookie(instance)
if filtered_instances:
print ('No cookie found for the following Rietveld instance%s:' %
('s' if len(filtered_instances) > 1 else ''))
for instance in filtered_instances:
print '\t' + instance['url']
print 'Use --auth if you would like to authenticate to them.\n'
def rietveld_search(self, instance, owner=None, reviewer=None):
if instance['requires_auth'] and not instance['auth']:
return []
email = None if instance['auth'] else ''
remote = rietveld.Rietveld('https://' + instance['url'], email, None)
# See def search() in rietveld.py to see all the filters you can use.
query_modified_after = None
if instance['supports_owner_modified_query']:
query_modified_after = self.modified_after.strftime('%Y-%m-%d')
# Rietveld does not allow search by both created_before and modified_after.
# (And some instances don't allow search by both owner and modified_after)
owner_email = None
reviewer_email = None
if owner:
owner_email = owner + '@' + instance['email_domain']
if reviewer:
reviewer_email = reviewer + '@' + instance['email_domain']
issues = remote.search(
owner=owner_email,
reviewer=reviewer_email,
modified_after=query_modified_after,
with_messages=True)
issues = filter(
lambda i: (datetime_from_rietveld(i['created']) < self.modified_before),
issues)
issues = filter(
lambda i: (datetime_from_rietveld(i['modified']) > self.modified_after),
issues)
should_filter_by_user = True
issues = map(partial(self.process_rietveld_issue, instance), issues)
issues = filter(
partial(self.filter_issue, should_filter_by_user=should_filter_by_user),
issues)
issues = sorted(issues, key=lambda i: i['modified'], reverse=True)
return issues
def process_rietveld_issue(self, instance, issue):
ret = {}
ret['owner'] = issue['owner_email']
ret['author'] = ret['owner']
ret['reviewers'] = set(issue['reviewers'])
shorturl = instance['url']
if 'shorturl' in instance:
shorturl = instance['shorturl']
ret['review_url'] = 'http://%s/%d' % (shorturl, issue['issue'])
# Rietveld sometimes has '\r\n' instead of '\n'.
ret['header'] = issue['description'].replace('\r', '').split('\n')[0]
ret['modified'] = datetime_from_rietveld(issue['modified'])
ret['created'] = datetime_from_rietveld(issue['created'])
ret['replies'] = self.process_rietveld_replies(issue['messages'])
return ret
@staticmethod
def process_rietveld_replies(replies):
ret = []
for reply in replies:
r = {}
r['author'] = reply['sender']
r['created'] = datetime_from_rietveld(reply['date'])
r['content'] = ''
ret.append(r)
return ret
def gerrit_search(self, instance, owner=None, reviewer=None):
max_age = datetime.today() - self.modified_after
max_age = max_age.days * 24 * 3600 + max_age.seconds
# See https://review.openstack.org/Documentation/cmd-query.html
# Gerrit doesn't allow filtering by created time, only modified time.
user_filter = 'owner:%s' % owner if owner else 'reviewer:%s' % reviewer
gquery_cmd = ['ssh', '-p', str(instance['port']), instance['url'],
'gerrit', 'query',
'--format', 'JSON',
'--comments',
'--',
'-age:%ss' % str(max_age),
user_filter]
[stdout, _] = subprocess.Popen(gquery_cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
issues = str(stdout).split('\n')[:-2]
issues = map(json.loads, issues)
# TODO(cjhopman): should we filter abandoned changes?
issues = map(self.process_gerrit_issue, issues)
issues = filter(self.filter_issue, issues)
issues = sorted(issues, key=lambda i: i['modified'], reverse=True)
return issues
def process_gerrit_issue(self, issue):
ret = {}
ret['review_url'] = issue['url']
ret['header'] = issue['subject']
ret['owner'] = issue['owner']['email']
ret['author'] = ret['owner']
ret['created'] = datetime.fromtimestamp(issue['createdOn'])
ret['modified'] = datetime.fromtimestamp(issue['lastUpdated'])
if 'comments' in issue:
ret['replies'] = self.process_gerrit_issue_replies(issue['comments'])
else:
ret['replies'] = []
ret['reviewers'] = set()
for reply in ret['replies']:
if reply['author'] != ret['author']:
ret['reviewers'].add(reply['author'])
return ret
@staticmethod
def process_gerrit_issue_replies(replies):
ret = []
replies = filter(lambda r: 'email' in r['reviewer'], replies)
for reply in replies:
r = {}
r['author'] = reply['reviewer']['email']
r['created'] = datetime.fromtimestamp(reply['timestamp'])
r['content'] = ''
ret.append(r)
return ret
def google_code_issue_search(self, instance):
time_format = '%Y-%m-%dT%T'
# See http://code.google.com/p/support/wiki/IssueTrackerAPI
# q=<owner>@chromium.org does a full text search for <owner>@chromium.org.
# This will accept the issue if owner is the owner or in the cc list. Might
# have some false positives, though.
# Don't filter normally on modified_before because it can filter out things
# that were modified in the time period and then modified again after it.
gcode_url = ('https://code.google.com/feeds/issues/p/%s/issues/full' %
instance['name'])
gcode_data = urllib.urlencode({
'alt': 'json',
'max-results': '100000',
'q': '%s' % self.user,
'published-max': self.modified_before.strftime(time_format),
'updated-min': self.modified_after.strftime(time_format),
})
opener = urllib2.build_opener()
if self.google_code_auth_token:
opener.addheaders = [('Authorization', 'GoogleLogin auth=%s' %
self.google_code_auth_token)]
gcode_json = None
try:
gcode_get = opener.open(gcode_url + '?' + gcode_data)
gcode_json = json.load(gcode_get)
gcode_get.close()
except urllib2.HTTPError, _:
print 'Unable to access ' + instance['name'] + ' issue tracker.'
if not gcode_json or 'entry' not in gcode_json['feed']:
return []
issues = gcode_json['feed']['entry']
issues = map(partial(self.process_google_code_issue, instance), issues)
issues = filter(self.filter_issue, issues)
issues = sorted(issues, key=lambda i: i['modified'], reverse=True)
return issues
def process_google_code_issue(self, project, issue):
ret = {}
ret['created'] = datetime_from_google_code(issue['published']['$t'])
ret['modified'] = datetime_from_google_code(issue['updated']['$t'])
ret['owner'] = ''
if 'issues$owner' in issue:
ret['owner'] = issue['issues$owner']['issues$username']['$t']
ret['author'] = issue['author'][0]['name']['$t']
if 'shorturl' in project:
issue_id = issue['id']['$t']
issue_id = issue_id[issue_id.rfind('/') + 1:]
ret['url'] = 'http://%s/%d' % (project['shorturl'], int(issue_id))
else:
issue_url = issue['link'][1]
if issue_url['rel'] != 'alternate':
raise RuntimeError
ret['url'] = issue_url['href']
ret['header'] = issue['title']['$t']
ret['replies'] = self.get_google_code_issue_replies(issue)
return ret
def get_google_code_issue_replies(self, issue):
"""Get all the comments on the issue."""
replies_url = issue['link'][0]
if replies_url['rel'] != 'replies':
raise RuntimeError
replies_data = urllib.urlencode({
'alt': 'json',
'fields': 'entry(published,author,content)',
})
opener = urllib2.build_opener()
opener.addheaders = [('Authorization', 'GoogleLogin auth=%s' %
self.google_code_auth_token)]
try:
replies_get = opener.open(replies_url['href'] + '?' + replies_data)
except urllib2.HTTPError, _:
return []
replies_json = json.load(replies_get)
replies_get.close()
return self.process_google_code_issue_replies(replies_json)
@staticmethod
def process_google_code_issue_replies(replies):
if 'entry' not in replies['feed']:
return []
ret = []
for entry in replies['feed']['entry']:
e = {}
e['created'] = datetime_from_google_code(entry['published']['$t'])
e['content'] = entry['content']['$t']
e['author'] = entry['author'][0]['name']['$t']
ret.append(e)
return ret
@staticmethod
def git_cmd(repo, *args):
cmd = ['git', '--git-dir=%s/.git' % repo]
cmd.extend(args)
[stdout, _] = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
lines = str(stdout).split('\n')[:-1]
return lines
def git_search(self, instance, owner=None, reviewer=None):
repo = getattr(self, instance['option'])
if not repo:
return []
search = []
if owner:
search.extend(instance['owner_search_func'](owner))
if reviewer:
search.extend(instance['reviewer_search_func'](reviewer))
if not len(search):
return []
self.git_cmd(repo, 'fetch', 'origin')
time_format = '%Y-%m-%d %H:%M:%S'
log_args = [
'--after=' + self.modified_after.strftime(time_format),
'--before=' + self.modified_before.strftime(time_format),
'--format=%H'
]
commits = set()
for query in search:
query_args = [query]
query_args.extend(log_args)
commits |= set(self.git_cmd(repo, 'log', 'origin/master', *query_args))
ret = []
for commit in commits:
output = self.git_cmd(repo, 'log', commit + "^!", "--format=%cn%n%cd%n%B")
author = output[0]
date = datetime.strptime(output[1], "%a %b %d %H:%M:%S %Y +0000")
processed = self.process_git_commit(instance, author, date, output[2:])
if processed:
ret.append(processed)
ret = sorted(ret, key=lambda i: i['modified'], reverse=True)
return ret
@staticmethod
def process_git_commit(instance, author, date, log):
ret = {}
ret['owner'] = author
ret['author'] = author
ret['modified'] = date
ret['created'] = date
ret['header'] = log[0]
reviews = []
reviewers = []
changes = []
for line in log:
match = re.match(r'Reviewed by ([^.]*)', line)
if match:
reviewers.append(match.group(1))
if instance['review_re']:
match = re.match(instance['review_re'], line)
if match:
reviews.append(int(match.group(1)))
if instance['change_re']:
match = re.match(instance['change_re'], line)
if match:
changes.append(int(match.group(1)))
committer_list = webkitpy.common.config.committers.CommitterList()
ret['reviewers'] = set(
(committer_list.contributor_by_name(r).emails[0] for r in reviewers))
# Reviews more useful than change link itself, but tricky if multiple
# Reviews == bugs for WebKit changes
if len(reviews) == 1:
url = 'http://%s/%d' % (instance['review_url'], reviews[0])
if instance['review_prop']:
ret[instance['review_prop']] = reviews[0]
elif len(changes) == 1:
url = 'http://%s/%d' % (instance['change_url'], changes[0])
else:
# Couldn't find anything.
return None
ret['review_url'] = url
return ret
def bugzilla_issues(self, instance, user):
if instance['user_func']:
user = instance['user_func'](user)
if not user:
return []
# This search is a little iffy, as it returns any bug that has been
# modified over a time period in any way and that a user has ever commented
# on, but that's the best that Bugzilla can get us. Oops.
commented = { 'emaillongdesc1': 1 }
issues = self.bugzilla_search(instance, user, commented)
issues = filter(lambda issue: issue['owner'] != user, issues)
reported = { 'emailreporter1': 1, 'chfield': '[Bug creation]' }
issues.extend(self.bugzilla_search(instance, user, reported))
# Remove duplicates by bug id
seen = {}
pruned = []
for issue in issues:
bug_id = issue['webkit_bug_id']
if bug_id in seen:
continue
seen[bug_id] = True
pruned.append(issue)
# Bugzilla has no modified time, so sort by id?
pruned = sorted(pruned, key=lambda i: i['webkit_bug_id'])
return issues
def bugzilla_search(self, instance, user, params):
time_format = '%Y-%m-%d'
values = {
'chfieldfrom': self.modified_after.strftime(time_format),
'chfieldto': self.modified_before.strftime(time_format),
'ctype': 'csv',
'emailtype1': 'substring',
'email1': '%s' % user,
}
values.update(params)
# Must be GET not POST
data = urllib.urlencode(values)
req = urllib2.Request("%s?%s" % (instance['search_url'], data))
response = urllib2.urlopen(req)
reader = csv.reader(response)
reader.next() # skip the header line
issues = map(partial(self.process_bugzilla_issue, instance), reader)
return issues
@staticmethod
def process_bugzilla_issue(instance, issue):
bug_id, owner, desc = int(issue[0]), issue[4], issue[7]
ret = {}
ret['owner'] = owner
ret['author'] = owner
ret['review_url'] = 'http://%s/%d' % (instance['url'], bug_id)
ret['url'] = ret['review_url']
ret['header'] = desc
ret['webkit_bug_id'] = bug_id
return ret
def setup_webkit_info(self):
assert(self.webkit_repo)
git_dir = os.path.normpath(self.webkit_repo + "/.git")
if not os.path.exists(git_dir):
print "%s doesn't exist, skipping WebKit checks." % git_dir
self.webkit_repo = None
return
try:
self.git_cmd(self.webkit_repo, "fetch", "origin")
except subprocess.CalledProcessError:
print "Failed to update WebKit repo, skipping WebKit checks."
self.webkit_repo = None
return
path = "Tools/Scripts"
full_path = os.path.normpath("%s/%s" % (self.options.webkit_repo, path))
sys.path.append(full_path)
try:
global webkitpy
webkitpy = __import__('webkitpy.common.config.committers')
except ImportError:
print "Failed to import WebKit committer list, skipping WebKit checks."
self.webkit_repo = None
return
if not webkit_account(self.user):
email = self.user + "@chromium.org"
print "No %s in committers.py, skipping WebKit checks." % email
self.webkit_repo = None
def print_change(self, change):
optional_values = {
'reviewers': ', '.join(change['reviewers'])
}
self.print_generic(self.options.output_format,
self.options.output_format_changes,
change['header'],
change['review_url'],
change['author'],
optional_values)
def print_issue(self, issue):
optional_values = {
'owner': issue['owner'],
}
self.print_generic(self.options.output_format,
self.options.output_format_issues,
issue['header'],
issue['url'],
issue['author'],
optional_values)
def print_review(self, review):
self.print_generic(self.options.output_format,
self.options.output_format_reviews,
review['header'],
review['review_url'],
review['author'])
@staticmethod
def print_generic(default_fmt, specific_fmt,
title, url, author,
optional_values=None):
output_format = specific_fmt if specific_fmt is not None else default_fmt
output_format = unicode(output_format)
required_values = {
'title': title,
'url': url,
'author': author,
}
# Merge required and optional values.
if optional_values is not None:
values = dict(required_values.items() + optional_values.items())
else:
values = required_values
print output_format.format(**values)
def filter_issue(self, issue, should_filter_by_user=True):
def maybe_filter_username(email):
return not should_filter_by_user or username(email) == self.user
if (maybe_filter_username(issue['author']) and
self.filter_modified(issue['created'])):
return True
if (maybe_filter_username(issue['owner']) and
(self.filter_modified(issue['created']) or
self.filter_modified(issue['modified']))):
return True
for reply in issue['replies']:
if self.filter_modified(reply['created']):
if not should_filter_by_user:
break
if (username(reply['author']) == self.user
or (self.user + '@') in reply['content']):
break
else:
return False
return True
def filter_modified(self, modified):
return self.modified_after < modified and modified < self.modified_before
def auth_for_changes(self):
#TODO(cjhopman): Move authentication check for getting changes here.
pass
def auth_for_reviews(self):
# Reviews use all the same instances as changes so no authentication is
# required.
pass
def auth_for_issues(self):
self.google_code_auth_token = (
get_auth_token(self.options.local_user + '@chromium.org'))
def get_changes(self):
for instance in rietveld_instances:
self.changes += self.rietveld_search(instance, owner=self.user)
for instance in gerrit_instances:
self.changes += self.gerrit_search(instance, owner=self.user)
for instance in git_instances:
self.changes += self.git_search(instance, owner=self.user)
def print_changes(self):
if self.changes:
print '\nChanges:'
for change in self.changes:
self.print_change(change)
def get_reviews(self):
for instance in rietveld_instances:
self.reviews += self.rietveld_search(instance, reviewer=self.user)
for instance in gerrit_instances:
reviews = self.gerrit_search(instance, reviewer=self.user)
reviews = filter(lambda r: not username(r['owner']) == self.user, reviews)
self.reviews += reviews
for instance in git_instances:
self.reviews += self.git_search(instance, reviewer=self.user)
def print_reviews(self):
if self.reviews:
print '\nReviews:'
for review in self.reviews:
self.print_review(review)
def get_issues(self):
for project in google_code_projects:
self.issues += self.google_code_issue_search(project)
for instance in bugzilla_instances:
self.issues += self.bugzilla_issues(instance, self.user)
def print_issues(self):
if self.issues:
print '\nIssues:'
for issue in self.issues:
self.print_issue(issue)
def process_activities(self):
# If a webkit bug was a review, don't list it as an issue.
ids = {}
for review in self.reviews + self.changes:
if 'webkit_bug_id' in review:
ids[review['webkit_bug_id']] = True
def duplicate_issue(issue):
if 'webkit_bug_id' not in issue:
return False
return issue['webkit_bug_id'] in ids
self.issues = filter(lambda issue: not duplicate_issue(issue), self.issues)
def print_activity(self):
self.print_changes()
self.print_reviews()
self.print_issues()
def main():
# Silence upload.py.
rietveld.upload.verbosity = 0
parser = optparse.OptionParser(description=sys.modules[__name__].__doc__)
parser.add_option(
'-u', '--user', metavar='<email>',
default=os.environ.get('USER'),
help='Filter on user, default=%default')
parser.add_option(
'--webkit_repo', metavar='<dir>',
default='%s' % os.environ.get('WEBKIT_DIR'),
help='Local path to WebKit repository, default=%default')
parser.add_option(
'-b', '--begin', metavar='<date>',
help='Filter issues created after the date')
parser.add_option(
'-e', '--end', metavar='<date>',
help='Filter issues created before the date')
quarter_begin, quarter_end = get_quarter_of(datetime.today() -
relativedelta(months=2))
parser.add_option(
'-Q', '--last_quarter', action='store_true',
help='Use last quarter\'s dates, e.g. %s to %s' % (
quarter_begin.strftime('%Y-%m-%d'), quarter_end.strftime('%Y-%m-%d')))
parser.add_option(
'-Y', '--this_year', action='store_true',
help='Use this year\'s dates')
parser.add_option(
'-w', '--week_of', metavar='<date>',
help='Show issues for week of the date')
parser.add_option(
'-a', '--auth',
action='store_true',
help='Ask to authenticate for instances with no auth cookie')
activity_types_group = optparse.OptionGroup(parser, 'Activity Types',
'By default, all activity will be looked up and '
'printed. If any of these are specified, only '
'those specified will be searched.')
activity_types_group.add_option(
'-c', '--changes',
action='store_true',
help='Show changes.')
activity_types_group.add_option(
'-i', '--issues',
action='store_true',
help='Show issues.')
activity_types_group.add_option(
'-r', '--reviews',
action='store_true',
help='Show reviews.')
parser.add_option_group(activity_types_group)
output_format_group = optparse.OptionGroup(parser, 'Output Format',
'By default, all activity will be printed in the '
'following format: {url} {title}. This can be '
'changed for either all activity types or '
'individually for each activity type. The format '
'is defined as documented for '
'string.format(...). The variables available for '
'all activity types are url, title and author. '
'Format options for specific activity types will '
'override the generic format.')
output_format_group.add_option(
'-f', '--output-format', metavar='<format>',
default=u'{url} {title}',
help='Specifies the format to use when printing all your activity.')
output_format_group.add_option(
'--output-format-changes', metavar='<format>',
default=None,
help='Specifies the format to use when printing changes. Supports the '
'additional variable {reviewers}')
output_format_group.add_option(
'--output-format-issues', metavar='<format>',
default=None,
help='Specifies the format to use when printing issues. Supports the '
'additional variable {owner}.')
output_format_group.add_option(
'--output-format-reviews', metavar='<format>',
default=None,
help='Specifies the format to use when printing reviews.')
parser.add_option_group(output_format_group)
# Remove description formatting
parser.format_description = (
lambda _: parser.description) # pylint: disable=E1101
options, args = parser.parse_args()
options.local_user = os.environ.get('USER')
if args:
parser.error('Args unsupported')
if not options.user:
parser.error('USER is not set, please use -u')
options.user = username(options.user)
if not options.begin:
if options.last_quarter:
begin, end = quarter_begin, quarter_end
elif options.this_year:
begin, end = get_year_of(datetime.today())
elif options.week_of:
begin, end = (get_week_of(datetime.strptime(options.week_of, '%m/%d/%y')))
else:
begin, end = (get_week_of(datetime.today() - timedelta(days=1)))
else:
begin = datetime.strptime(options.begin, '%m/%d/%y')
if options.end:
end = datetime.strptime(options.end, '%m/%d/%y')
else:
end = datetime.today()
options.begin, options.end = begin, end
print 'Searching for activity by %s' % options.user
print 'Using range %s to %s' % (options.begin, options.end)
my_activity = MyActivity(options)
if not (options.changes or options.reviews or options.issues):
options.changes = True
options.issues = True
options.reviews = True
# First do any required authentication so none of the user interaction has to