cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Get stats about your activity. |
| 7 | |
| 8 | Example: |
| 9 | - my_activity.py for stats for the current week (last week on mondays). |
| 10 | - my_activity.py -Q for stats for last quarter. |
| 11 | - my_activity.py -Y for stats for this year. |
| 12 | - my_activity.py -b 4/5/12 for stats since 4/5/12. |
| 13 | - my_activity.py -b 4/5/12 -e 6/7/12 for stats between 4/5/12 and 6/7/12. |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 14 | - my_activity.py -jd to output stats for the week to json with deltas data. |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 15 | """ |
| 16 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 17 | # TODO(vadimsh): This script knows too much about ClientLogin and cookies. It |
| 18 | # will stop to work on ~20 Apr 2015. |
| 19 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 20 | # These services typically only provide a created time and a last modified time |
| 21 | # for each item for general queries. This is not enough to determine if there |
| 22 | # was activity in a given time period. So, we first query for all things created |
| 23 | # before end and modified after begin. Then, we get the details of each item and |
| 24 | # check those details to determine if there was activity in the given period. |
| 25 | # This means that query time scales mostly with (today() - begin). |
| 26 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 27 | from datetime import datetime |
| 28 | from datetime import timedelta |
| 29 | from functools import partial |
| 30 | import json |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 31 | import logging |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 32 | import optparse |
| 33 | import os |
| 34 | import subprocess |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 35 | from string import Formatter |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 36 | import sys |
| 37 | import urllib |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 38 | import re |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 39 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 40 | import auth |
stevefung@chromium.org | 832d51e | 2015-05-27 12:52:51 +0000 | [diff] [blame] | 41 | import fix_encoding |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 42 | import gerrit_util |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 43 | import rietveld |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 44 | |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 45 | from third_party import httplib2 |
| 46 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 47 | try: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 48 | import dateutil # pylint: disable=import-error |
| 49 | import dateutil.parser |
| 50 | from dateutil.relativedelta import relativedelta |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 51 | except ImportError: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 52 | logging.error('python-dateutil package required') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 53 | exit(1) |
| 54 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 55 | |
| 56 | class DefaultFormatter(Formatter): |
| 57 | def __init__(self, default = ''): |
| 58 | super(DefaultFormatter, self).__init__() |
| 59 | self.default = default |
| 60 | |
| 61 | def get_value(self, key, args, kwds): |
| 62 | if isinstance(key, basestring) and key not in kwds: |
| 63 | return self.default |
| 64 | return Formatter.get_value(self, key, args, kwds) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 65 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 66 | rietveld_instances = [ |
| 67 | { |
| 68 | 'url': 'codereview.chromium.org', |
| 69 | 'shorturl': 'crrev.com', |
| 70 | 'supports_owner_modified_query': True, |
| 71 | 'requires_auth': False, |
| 72 | 'email_domain': 'chromium.org', |
| 73 | }, |
| 74 | { |
| 75 | 'url': 'chromereviews.googleplex.com', |
| 76 | 'shorturl': 'go/chromerev', |
| 77 | 'supports_owner_modified_query': True, |
| 78 | 'requires_auth': True, |
| 79 | 'email_domain': 'google.com', |
| 80 | }, |
| 81 | { |
| 82 | 'url': 'codereview.appspot.com', |
| 83 | 'supports_owner_modified_query': True, |
| 84 | 'requires_auth': False, |
| 85 | 'email_domain': 'chromium.org', |
| 86 | }, |
| 87 | { |
| 88 | 'url': 'breakpad.appspot.com', |
| 89 | 'supports_owner_modified_query': False, |
| 90 | 'requires_auth': False, |
| 91 | 'email_domain': 'chromium.org', |
| 92 | }, |
| 93 | ] |
| 94 | |
| 95 | gerrit_instances = [ |
| 96 | { |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 97 | 'url': 'chromium-review.googlesource.com', |
Jeremy Roman | f475a47 | 2017-06-06 16:49:11 -0400 | [diff] [blame] | 98 | 'shorturl': 'crrev.com/c', |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 99 | }, |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 100 | { |
| 101 | 'url': 'chrome-internal-review.googlesource.com', |
Jeremy Roman | f475a47 | 2017-06-06 16:49:11 -0400 | [diff] [blame] | 102 | 'shorturl': 'crrev.com/i', |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 103 | }, |
deymo@chromium.org | 56dc57a | 2015-09-10 18:26:54 +0000 | [diff] [blame] | 104 | { |
| 105 | 'url': 'android-review.googlesource.com', |
| 106 | }, |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 107 | ] |
| 108 | |
| 109 | google_code_projects = [ |
| 110 | { |
| 111 | 'name': 'chromium', |
| 112 | 'shorturl': 'crbug.com', |
| 113 | }, |
| 114 | { |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 115 | 'name': 'google-breakpad', |
| 116 | }, |
| 117 | { |
| 118 | 'name': 'gyp', |
enne@chromium.org | f01fad3 | 2012-11-26 18:09:38 +0000 | [diff] [blame] | 119 | }, |
| 120 | { |
| 121 | 'name': 'skia', |
| 122 | }, |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 123 | ] |
| 124 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 125 | def username(email): |
| 126 | """Keeps the username of an email address.""" |
| 127 | return email and email.split('@', 1)[0] |
| 128 | |
| 129 | |
cjhopman@chromium.org | 426557a | 2012-10-22 20:18:52 +0000 | [diff] [blame] | 130 | def datetime_to_midnight(date): |
| 131 | return date - timedelta(hours=date.hour, minutes=date.minute, |
| 132 | seconds=date.second, microseconds=date.microsecond) |
| 133 | |
| 134 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 135 | def get_quarter_of(date): |
cjhopman@chromium.org | 426557a | 2012-10-22 20:18:52 +0000 | [diff] [blame] | 136 | begin = (datetime_to_midnight(date) - |
| 137 | relativedelta(months=(date.month % 3) - 1, days=(date.day - 1))) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 138 | return begin, begin + relativedelta(months=3) |
| 139 | |
| 140 | |
| 141 | def get_year_of(date): |
cjhopman@chromium.org | 426557a | 2012-10-22 20:18:52 +0000 | [diff] [blame] | 142 | begin = (datetime_to_midnight(date) - |
| 143 | relativedelta(months=(date.month - 1), days=(date.day - 1))) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 144 | return begin, begin + relativedelta(years=1) |
| 145 | |
| 146 | |
| 147 | def get_week_of(date): |
cjhopman@chromium.org | 426557a | 2012-10-22 20:18:52 +0000 | [diff] [blame] | 148 | begin = (datetime_to_midnight(date) - timedelta(days=date.weekday())) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 149 | return begin, begin + timedelta(days=7) |
| 150 | |
| 151 | |
| 152 | def get_yes_or_no(msg): |
| 153 | while True: |
| 154 | response = raw_input(msg + ' yes/no [no] ') |
| 155 | if response == 'y' or response == 'yes': |
| 156 | return True |
| 157 | elif not response or response == 'n' or response == 'no': |
| 158 | return False |
| 159 | |
| 160 | |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 161 | def datetime_from_gerrit(date_string): |
| 162 | return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f000') |
| 163 | |
| 164 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 165 | def datetime_from_rietveld(date_string): |
deymo@chromium.org | 29eb6e6 | 2014-03-20 01:55:55 +0000 | [diff] [blame] | 166 | try: |
| 167 | return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f') |
| 168 | except ValueError: |
| 169 | # Sometimes rietveld returns a value without the milliseconds part, so we |
| 170 | # attempt to parse those cases as well. |
| 171 | return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 172 | |
| 173 | |
| 174 | def datetime_from_google_code(date_string): |
| 175 | return datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%fZ') |
| 176 | |
| 177 | |
| 178 | class MyActivity(object): |
| 179 | def __init__(self, options): |
| 180 | self.options = options |
| 181 | self.modified_after = options.begin |
| 182 | self.modified_before = options.end |
| 183 | self.user = options.user |
| 184 | self.changes = [] |
| 185 | self.reviews = [] |
| 186 | self.issues = [] |
| 187 | self.check_cookies() |
| 188 | self.google_code_auth_token = None |
| 189 | |
| 190 | # Check the codereview cookie jar to determine which Rietveld instances to |
| 191 | # authenticate to. |
| 192 | def check_cookies(self): |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 193 | filtered_instances = [] |
| 194 | |
| 195 | def has_cookie(instance): |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 196 | auth_config = auth.extract_auth_config_from_options(self.options) |
| 197 | a = auth.get_authenticator_for_host(instance['url'], auth_config) |
| 198 | return a.has_cached_credentials() |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 199 | |
| 200 | for instance in rietveld_instances: |
| 201 | instance['auth'] = has_cookie(instance) |
| 202 | |
| 203 | if filtered_instances: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 204 | logging.warning('No cookie found for the following Rietveld instance%s:', |
| 205 | 's' if len(filtered_instances) > 1 else '') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 206 | for instance in filtered_instances: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 207 | logging.warning('\t' + instance['url']) |
| 208 | logging.warning('Use --auth if you would like to authenticate to them.') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 209 | |
| 210 | def rietveld_search(self, instance, owner=None, reviewer=None): |
| 211 | if instance['requires_auth'] and not instance['auth']: |
| 212 | return [] |
| 213 | |
| 214 | |
| 215 | email = None if instance['auth'] else '' |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 216 | auth_config = auth.extract_auth_config_from_options(self.options) |
| 217 | remote = rietveld.Rietveld('https://' + instance['url'], auth_config, email) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 218 | |
| 219 | # See def search() in rietveld.py to see all the filters you can use. |
| 220 | query_modified_after = None |
| 221 | |
| 222 | if instance['supports_owner_modified_query']: |
| 223 | query_modified_after = self.modified_after.strftime('%Y-%m-%d') |
| 224 | |
| 225 | # Rietveld does not allow search by both created_before and modified_after. |
| 226 | # (And some instances don't allow search by both owner and modified_after) |
| 227 | owner_email = None |
| 228 | reviewer_email = None |
| 229 | if owner: |
| 230 | owner_email = owner + '@' + instance['email_domain'] |
| 231 | if reviewer: |
| 232 | reviewer_email = reviewer + '@' + instance['email_domain'] |
| 233 | issues = remote.search( |
| 234 | owner=owner_email, |
| 235 | reviewer=reviewer_email, |
| 236 | modified_after=query_modified_after, |
| 237 | with_messages=True) |
| 238 | |
| 239 | issues = filter( |
| 240 | lambda i: (datetime_from_rietveld(i['created']) < self.modified_before), |
| 241 | issues) |
| 242 | issues = filter( |
| 243 | lambda i: (datetime_from_rietveld(i['modified']) > self.modified_after), |
| 244 | issues) |
| 245 | |
| 246 | should_filter_by_user = True |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 247 | issues = map(partial(self.process_rietveld_issue, remote, instance), issues) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 248 | issues = filter( |
| 249 | partial(self.filter_issue, should_filter_by_user=should_filter_by_user), |
| 250 | issues) |
| 251 | issues = sorted(issues, key=lambda i: i['modified'], reverse=True) |
| 252 | |
| 253 | return issues |
| 254 | |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 255 | def extract_bug_number_from_description(self, issue): |
| 256 | description = None |
| 257 | |
| 258 | if 'description' in issue: |
| 259 | # Getting the description for Rietveld |
| 260 | description = issue['description'] |
| 261 | elif 'revisions' in issue: |
| 262 | # Getting the description for REST Gerrit |
| 263 | revision = issue['revisions'][issue['current_revision']] |
| 264 | description = revision['commit']['message'] |
| 265 | |
| 266 | bugs = [] |
| 267 | if description: |
| 268 | matches = re.findall('BUG=(((\d+)(,\s?)?)+)', description) |
| 269 | if matches: |
| 270 | for match in matches: |
| 271 | bugs.extend(match[0].replace(' ', '').split(',')) |
| 272 | |
| 273 | return bugs |
| 274 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 275 | def process_rietveld_issue(self, remote, instance, issue): |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 276 | ret = {} |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 277 | if self.options.deltas: |
| 278 | patchset_props = remote.get_patchset_properties( |
| 279 | issue['issue'], |
| 280 | issue['patchsets'][-1]) |
| 281 | ret['delta'] = '+%d,-%d' % ( |
| 282 | sum(f['num_added'] for f in patchset_props['files'].itervalues()), |
| 283 | sum(f['num_removed'] for f in patchset_props['files'].itervalues())) |
| 284 | |
| 285 | if issue['landed_days_ago'] != 'unknown': |
| 286 | ret['status'] = 'committed' |
| 287 | elif issue['closed']: |
| 288 | ret['status'] = 'closed' |
| 289 | elif len(issue['reviewers']) and issue['all_required_reviewers_approved']: |
| 290 | ret['status'] = 'ready' |
| 291 | else: |
| 292 | ret['status'] = 'open' |
| 293 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 294 | ret['owner'] = issue['owner_email'] |
| 295 | ret['author'] = ret['owner'] |
| 296 | |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 297 | ret['reviewers'] = set(issue['reviewers']) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 298 | |
| 299 | shorturl = instance['url'] |
| 300 | if 'shorturl' in instance: |
| 301 | shorturl = instance['shorturl'] |
| 302 | |
| 303 | ret['review_url'] = 'http://%s/%d' % (shorturl, issue['issue']) |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 304 | |
| 305 | # Rietveld sometimes has '\r\n' instead of '\n'. |
| 306 | ret['header'] = issue['description'].replace('\r', '').split('\n')[0] |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 307 | |
| 308 | ret['modified'] = datetime_from_rietveld(issue['modified']) |
| 309 | ret['created'] = datetime_from_rietveld(issue['created']) |
| 310 | ret['replies'] = self.process_rietveld_replies(issue['messages']) |
| 311 | |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 312 | ret['bug'] = self.extract_bug_number_from_description(issue) |
| 313 | ret['landed_days_ago'] = issue['landed_days_ago'] |
| 314 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 315 | return ret |
| 316 | |
| 317 | @staticmethod |
| 318 | def process_rietveld_replies(replies): |
| 319 | ret = [] |
| 320 | for reply in replies: |
| 321 | r = {} |
| 322 | r['author'] = reply['sender'] |
| 323 | r['created'] = datetime_from_rietveld(reply['date']) |
| 324 | r['content'] = '' |
| 325 | ret.append(r) |
| 326 | return ret |
| 327 | |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 328 | @staticmethod |
| 329 | def gerrit_changes_over_ssh(instance, filters): |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 330 | # See https://review.openstack.org/Documentation/cmd-query.html |
| 331 | # Gerrit doesn't allow filtering by created time, only modified time. |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 332 | gquery_cmd = ['ssh', '-p', str(instance['port']), instance['host'], |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 333 | 'gerrit', 'query', |
| 334 | '--format', 'JSON', |
| 335 | '--comments', |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 336 | '--'] + filters |
| 337 | (stdout, _) = subprocess.Popen(gquery_cmd, stdout=subprocess.PIPE, |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 338 | stderr=subprocess.PIPE).communicate() |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 339 | # Drop the last line of the output with the stats. |
| 340 | issues = stdout.splitlines()[:-1] |
| 341 | return map(json.loads, issues) |
| 342 | |
| 343 | @staticmethod |
| 344 | def gerrit_changes_over_rest(instance, filters): |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame^] | 345 | # Convert the "key:value" filter to a list of (key, value) pairs. |
| 346 | req = list(f.split(':', 1) for f in filters) |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 347 | try: |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 348 | # Instantiate the generator to force all the requests now and catch the |
| 349 | # errors here. |
| 350 | return list(gerrit_util.GenerateAllChanges(instance['url'], req, |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 351 | o_params=['MESSAGES', 'LABELS', 'DETAILED_ACCOUNTS', |
| 352 | 'CURRENT_REVISION', 'CURRENT_COMMIT'])) |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 353 | except gerrit_util.GerritError, e: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 354 | logging.error('Looking up %r: %s', instance['url'], e) |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 355 | return [] |
| 356 | |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 357 | def gerrit_search(self, instance, owner=None, reviewer=None): |
| 358 | max_age = datetime.today() - self.modified_after |
| 359 | max_age = max_age.days * 24 * 3600 + max_age.seconds |
| 360 | user_filter = 'owner:%s' % owner if owner else 'reviewer:%s' % reviewer |
| 361 | filters = ['-age:%ss' % max_age, user_filter] |
| 362 | |
| 363 | # Determine the gerrit interface to use: SSH or REST API: |
| 364 | if 'host' in instance: |
| 365 | issues = self.gerrit_changes_over_ssh(instance, filters) |
| 366 | issues = [self.process_gerrit_ssh_issue(instance, issue) |
| 367 | for issue in issues] |
| 368 | elif 'url' in instance: |
| 369 | issues = self.gerrit_changes_over_rest(instance, filters) |
| 370 | issues = [self.process_gerrit_rest_issue(instance, issue) |
| 371 | for issue in issues] |
| 372 | else: |
| 373 | raise Exception('Invalid gerrit_instances configuration.') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 374 | |
| 375 | # TODO(cjhopman): should we filter abandoned changes? |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 376 | issues = filter(self.filter_issue, issues) |
| 377 | issues = sorted(issues, key=lambda i: i['modified'], reverse=True) |
| 378 | |
| 379 | return issues |
| 380 | |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 381 | def process_gerrit_ssh_issue(self, instance, issue): |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 382 | ret = {} |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 383 | if self.options.deltas: |
| 384 | ret['delta'] = DefaultFormatter().format( |
| 385 | '+{insertions},-{deletions}', |
| 386 | **issue) |
| 387 | ret['status'] = issue['status'] |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 388 | ret['review_url'] = issue['url'] |
deymo@chromium.org | e52bd5a | 2013-08-29 18:05:21 +0000 | [diff] [blame] | 389 | if 'shorturl' in instance: |
| 390 | ret['review_url'] = 'http://%s/%s' % (instance['shorturl'], |
| 391 | issue['number']) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 392 | ret['header'] = issue['subject'] |
| 393 | ret['owner'] = issue['owner']['email'] |
| 394 | ret['author'] = ret['owner'] |
| 395 | ret['created'] = datetime.fromtimestamp(issue['createdOn']) |
| 396 | ret['modified'] = datetime.fromtimestamp(issue['lastUpdated']) |
| 397 | if 'comments' in issue: |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 398 | ret['replies'] = self.process_gerrit_ssh_issue_replies(issue['comments']) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 399 | else: |
| 400 | ret['replies'] = [] |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 401 | ret['reviewers'] = set(r['author'] for r in ret['replies']) |
| 402 | ret['reviewers'].discard(ret['author']) |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 403 | ret['bug'] = self.extract_bug_number_from_description(issue) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 404 | return ret |
| 405 | |
| 406 | @staticmethod |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 407 | def process_gerrit_ssh_issue_replies(replies): |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 408 | ret = [] |
| 409 | replies = filter(lambda r: 'email' in r['reviewer'], replies) |
| 410 | for reply in replies: |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 411 | ret.append({ |
| 412 | 'author': reply['reviewer']['email'], |
| 413 | 'created': datetime.fromtimestamp(reply['timestamp']), |
| 414 | 'content': '', |
| 415 | }) |
| 416 | return ret |
| 417 | |
| 418 | def process_gerrit_rest_issue(self, instance, issue): |
| 419 | ret = {} |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 420 | if self.options.deltas: |
| 421 | ret['delta'] = DefaultFormatter().format( |
| 422 | '+{insertions},-{deletions}', |
| 423 | **issue) |
| 424 | ret['status'] = issue['status'] |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 425 | ret['review_url'] = 'https://%s/%s' % (instance['url'], issue['_number']) |
| 426 | if 'shorturl' in instance: |
| 427 | # TODO(deymo): Move this short link to https once crosreview.com supports |
| 428 | # it. |
| 429 | ret['review_url'] = 'http://%s/%s' % (instance['shorturl'], |
| 430 | issue['_number']) |
| 431 | ret['header'] = issue['subject'] |
| 432 | ret['owner'] = issue['owner']['email'] |
| 433 | ret['author'] = ret['owner'] |
| 434 | ret['created'] = datetime_from_gerrit(issue['created']) |
| 435 | ret['modified'] = datetime_from_gerrit(issue['updated']) |
| 436 | if 'messages' in issue: |
| 437 | ret['replies'] = self.process_gerrit_rest_issue_replies(issue['messages']) |
| 438 | else: |
| 439 | ret['replies'] = [] |
| 440 | ret['reviewers'] = set(r['author'] for r in ret['replies']) |
| 441 | ret['reviewers'].discard(ret['author']) |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 442 | ret['bug'] = self.extract_bug_number_from_description(issue) |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 443 | return ret |
| 444 | |
| 445 | @staticmethod |
| 446 | def process_gerrit_rest_issue_replies(replies): |
| 447 | ret = [] |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 448 | replies = filter(lambda r: 'author' in r and 'email' in r['author'], |
| 449 | replies) |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 450 | for reply in replies: |
| 451 | ret.append({ |
| 452 | 'author': reply['author']['email'], |
| 453 | 'created': datetime_from_gerrit(reply['date']), |
| 454 | 'content': reply['message'], |
| 455 | }) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 456 | return ret |
| 457 | |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 458 | def project_hosting_issue_search(self, instance): |
| 459 | auth_config = auth.extract_auth_config_from_options(self.options) |
| 460 | authenticator = auth.get_authenticator_for_host( |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 461 | 'bugs.chromium.org', auth_config) |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 462 | http = authenticator.authorize(httplib2.Http()) |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 463 | url = ('https://monorail-prod.appspot.com/_ah/api/monorail/v1/projects' |
| 464 | '/%s/issues') % instance['name'] |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 465 | epoch = datetime.utcfromtimestamp(0) |
| 466 | user_str = '%s@chromium.org' % self.user |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 467 | |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 468 | query_data = urllib.urlencode({ |
| 469 | 'maxResults': 10000, |
| 470 | 'q': user_str, |
| 471 | 'publishedMax': '%d' % (self.modified_before - epoch).total_seconds(), |
| 472 | 'updatedMin': '%d' % (self.modified_after - epoch).total_seconds(), |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 473 | }) |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 474 | url = url + '?' + query_data |
| 475 | _, body = http.request(url) |
| 476 | content = json.loads(body) |
| 477 | if not content: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 478 | logging.error('Unable to parse %s response from projecthosting.', |
| 479 | instance['name']) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 480 | return [] |
| 481 | |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 482 | issues = [] |
| 483 | if 'items' in content: |
| 484 | items = content['items'] |
| 485 | for item in items: |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 486 | if instance.get('shorturl'): |
| 487 | item_url = 'https://%s/%d' % (instance['shorturl'], item['id']) |
| 488 | else: |
| 489 | item_url = 'https://bugs.chromium.org/p/%s/issues/detail?id=%d' % ( |
| 490 | instance['name'], item['id']) |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 491 | issue = { |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 492 | 'header': item['title'], |
| 493 | 'created': dateutil.parser.parse(item['published']), |
| 494 | 'modified': dateutil.parser.parse(item['updated']), |
| 495 | 'author': item['author']['name'], |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 496 | 'url': item_url, |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 497 | 'comments': [], |
| 498 | 'status': item['status'], |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 499 | 'labels': [], |
| 500 | 'components': [] |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 501 | } |
jdoerrie | 6428784 | 2017-01-09 14:40:26 +0100 | [diff] [blame] | 502 | if 'shorturl' in instance: |
| 503 | issue['url'] = 'http://%s/%d' % (instance['shorturl'], item['id']) |
| 504 | |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 505 | if 'owner' in item: |
| 506 | issue['owner'] = item['owner']['name'] |
| 507 | else: |
| 508 | issue['owner'] = 'None' |
| 509 | if issue['owner'] == user_str or issue['author'] == user_str: |
| 510 | issues.append(issue) |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 511 | if 'labels' in item: |
| 512 | issue['labels'] = item['labels'] |
| 513 | if 'components' in item: |
| 514 | issue['components'] = item['components'] |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 515 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 516 | return issues |
| 517 | |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 518 | def print_heading(self, heading): |
| 519 | print |
| 520 | print self.options.output_format_heading.format(heading=heading) |
| 521 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 522 | def match(self, author): |
| 523 | if '@' in self.user: |
| 524 | return author == self.user |
| 525 | return author.startswith(self.user + '@') |
| 526 | |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 527 | def print_change(self, change): |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 528 | activity = len([ |
| 529 | reply |
| 530 | for reply in change['replies'] |
| 531 | if self.match(reply['author']) |
| 532 | ]) |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 533 | optional_values = { |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 534 | 'created': change['created'].date().isoformat(), |
| 535 | 'modified': change['modified'].date().isoformat(), |
| 536 | 'reviewers': ', '.join(change['reviewers']), |
| 537 | 'status': change['status'], |
| 538 | 'activity': activity, |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 539 | } |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 540 | if self.options.deltas: |
| 541 | optional_values['delta'] = change['delta'] |
| 542 | |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 543 | self.print_generic(self.options.output_format, |
| 544 | self.options.output_format_changes, |
| 545 | change['header'], |
| 546 | change['review_url'], |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 547 | change['author'], |
| 548 | optional_values) |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 549 | |
| 550 | def print_issue(self, issue): |
| 551 | optional_values = { |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 552 | 'created': issue['created'].date().isoformat(), |
| 553 | 'modified': issue['modified'].date().isoformat(), |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 554 | 'owner': issue['owner'], |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 555 | 'status': issue['status'], |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 556 | } |
| 557 | self.print_generic(self.options.output_format, |
| 558 | self.options.output_format_issues, |
| 559 | issue['header'], |
| 560 | issue['url'], |
| 561 | issue['author'], |
| 562 | optional_values) |
| 563 | |
| 564 | def print_review(self, review): |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 565 | activity = len([ |
| 566 | reply |
| 567 | for reply in review['replies'] |
| 568 | if self.match(reply['author']) |
| 569 | ]) |
| 570 | optional_values = { |
| 571 | 'created': review['created'].date().isoformat(), |
| 572 | 'modified': review['modified'].date().isoformat(), |
| 573 | 'activity': activity, |
| 574 | } |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 575 | self.print_generic(self.options.output_format, |
| 576 | self.options.output_format_reviews, |
| 577 | review['header'], |
| 578 | review['review_url'], |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 579 | review['author'], |
| 580 | optional_values) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 581 | |
| 582 | @staticmethod |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 583 | def print_generic(default_fmt, specific_fmt, |
| 584 | title, url, author, |
| 585 | optional_values=None): |
| 586 | output_format = specific_fmt if specific_fmt is not None else default_fmt |
| 587 | output_format = unicode(output_format) |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 588 | values = { |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 589 | 'title': title, |
| 590 | 'url': url, |
| 591 | 'author': author, |
| 592 | } |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 593 | if optional_values is not None: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 594 | values.update(optional_values) |
| 595 | print DefaultFormatter().format(output_format, **values).encode( |
| 596 | sys.getdefaultencoding()) |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 597 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 598 | |
| 599 | def filter_issue(self, issue, should_filter_by_user=True): |
| 600 | def maybe_filter_username(email): |
| 601 | return not should_filter_by_user or username(email) == self.user |
| 602 | if (maybe_filter_username(issue['author']) and |
| 603 | self.filter_modified(issue['created'])): |
| 604 | return True |
| 605 | if (maybe_filter_username(issue['owner']) and |
| 606 | (self.filter_modified(issue['created']) or |
| 607 | self.filter_modified(issue['modified']))): |
| 608 | return True |
| 609 | for reply in issue['replies']: |
| 610 | if self.filter_modified(reply['created']): |
| 611 | if not should_filter_by_user: |
| 612 | break |
| 613 | if (username(reply['author']) == self.user |
| 614 | or (self.user + '@') in reply['content']): |
| 615 | break |
| 616 | else: |
| 617 | return False |
| 618 | return True |
| 619 | |
| 620 | def filter_modified(self, modified): |
| 621 | return self.modified_after < modified and modified < self.modified_before |
| 622 | |
| 623 | def auth_for_changes(self): |
| 624 | #TODO(cjhopman): Move authentication check for getting changes here. |
| 625 | pass |
| 626 | |
| 627 | def auth_for_reviews(self): |
| 628 | # Reviews use all the same instances as changes so no authentication is |
| 629 | # required. |
| 630 | pass |
| 631 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 632 | def get_changes(self): |
| 633 | for instance in rietveld_instances: |
| 634 | self.changes += self.rietveld_search(instance, owner=self.user) |
| 635 | |
| 636 | for instance in gerrit_instances: |
| 637 | self.changes += self.gerrit_search(instance, owner=self.user) |
| 638 | |
| 639 | def print_changes(self): |
| 640 | if self.changes: |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 641 | self.print_heading('Changes') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 642 | for change in self.changes: |
| 643 | self.print_change(change) |
| 644 | |
| 645 | def get_reviews(self): |
| 646 | for instance in rietveld_instances: |
| 647 | self.reviews += self.rietveld_search(instance, reviewer=self.user) |
| 648 | |
| 649 | for instance in gerrit_instances: |
| 650 | reviews = self.gerrit_search(instance, reviewer=self.user) |
| 651 | reviews = filter(lambda r: not username(r['owner']) == self.user, reviews) |
| 652 | self.reviews += reviews |
| 653 | |
| 654 | def print_reviews(self): |
| 655 | if self.reviews: |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 656 | self.print_heading('Reviews') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 657 | for review in self.reviews: |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 658 | self.print_review(review) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 659 | |
| 660 | def get_issues(self): |
| 661 | for project in google_code_projects: |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 662 | self.issues += self.project_hosting_issue_search(project) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 663 | |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 664 | def print_issues(self): |
| 665 | if self.issues: |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 666 | self.print_heading('Issues') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 667 | for issue in self.issues: |
| 668 | self.print_issue(issue) |
| 669 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 670 | def print_activity(self): |
| 671 | self.print_changes() |
| 672 | self.print_reviews() |
| 673 | self.print_issues() |
| 674 | |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 675 | def dump_json(self, ignore_keys=None): |
| 676 | if ignore_keys is None: |
| 677 | ignore_keys = ['replies'] |
| 678 | |
| 679 | def format_for_json_dump(in_array): |
| 680 | output = {} |
| 681 | for item in in_array: |
| 682 | url = item.get('url') or item.get('review_url') |
| 683 | if not url: |
| 684 | raise Exception('Dumped item %s does not specify url' % item) |
| 685 | output[url] = dict( |
| 686 | (k, v) for k,v in item.iteritems() if k not in ignore_keys) |
| 687 | return output |
| 688 | |
| 689 | class PythonObjectEncoder(json.JSONEncoder): |
| 690 | def default(self, obj): # pylint: disable=method-hidden |
| 691 | if isinstance(obj, datetime): |
| 692 | return obj.isoformat() |
| 693 | if isinstance(obj, set): |
| 694 | return list(obj) |
| 695 | return json.JSONEncoder.default(self, obj) |
| 696 | |
| 697 | output = { |
| 698 | 'reviews': format_for_json_dump(self.reviews), |
| 699 | 'changes': format_for_json_dump(self.changes), |
| 700 | 'issues': format_for_json_dump(self.issues) |
| 701 | } |
| 702 | print json.dumps(output, indent=2, cls=PythonObjectEncoder) |
| 703 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 704 | |
| 705 | def main(): |
| 706 | # Silence upload.py. |
| 707 | rietveld.upload.verbosity = 0 |
| 708 | |
| 709 | parser = optparse.OptionParser(description=sys.modules[__name__].__doc__) |
| 710 | parser.add_option( |
| 711 | '-u', '--user', metavar='<email>', |
| 712 | default=os.environ.get('USER'), |
| 713 | help='Filter on user, default=%default') |
| 714 | parser.add_option( |
| 715 | '-b', '--begin', metavar='<date>', |
wychen@chromium.org | 85cab63 | 2015-05-28 21:04:37 +0000 | [diff] [blame] | 716 | help='Filter issues created after the date (mm/dd/yy)') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 717 | parser.add_option( |
| 718 | '-e', '--end', metavar='<date>', |
wychen@chromium.org | 85cab63 | 2015-05-28 21:04:37 +0000 | [diff] [blame] | 719 | help='Filter issues created before the date (mm/dd/yy)') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 720 | quarter_begin, quarter_end = get_quarter_of(datetime.today() - |
| 721 | relativedelta(months=2)) |
| 722 | parser.add_option( |
| 723 | '-Q', '--last_quarter', action='store_true', |
jsbell@chromium.org | 74bfde0 | 2014-04-09 17:14:54 +0000 | [diff] [blame] | 724 | help='Use last quarter\'s dates, i.e. %s to %s' % ( |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 725 | quarter_begin.strftime('%Y-%m-%d'), quarter_end.strftime('%Y-%m-%d'))) |
| 726 | parser.add_option( |
| 727 | '-Y', '--this_year', action='store_true', |
| 728 | help='Use this year\'s dates') |
| 729 | parser.add_option( |
| 730 | '-w', '--week_of', metavar='<date>', |
wychen@chromium.org | 85cab63 | 2015-05-28 21:04:37 +0000 | [diff] [blame] | 731 | help='Show issues for week of the date (mm/dd/yy)') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 732 | parser.add_option( |
wychen@chromium.org | 8ba1ddb | 2015-04-29 00:04:25 +0000 | [diff] [blame] | 733 | '-W', '--last_week', action='count', |
| 734 | help='Show last week\'s issues. Use more times for more weeks.') |
jsbell@chromium.org | 74bfde0 | 2014-04-09 17:14:54 +0000 | [diff] [blame] | 735 | parser.add_option( |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 736 | '-a', '--auth', |
| 737 | action='store_true', |
| 738 | help='Ask to authenticate for instances with no auth cookie') |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 739 | parser.add_option( |
| 740 | '-d', '--deltas', |
| 741 | action='store_true', |
| 742 | help='Fetch deltas for changes (slow).') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 743 | |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 744 | activity_types_group = optparse.OptionGroup(parser, 'Activity Types', |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 745 | 'By default, all activity will be looked up and ' |
| 746 | 'printed. If any of these are specified, only ' |
| 747 | 'those specified will be searched.') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 748 | activity_types_group.add_option( |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 749 | '-c', '--changes', |
| 750 | action='store_true', |
| 751 | help='Show changes.') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 752 | activity_types_group.add_option( |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 753 | '-i', '--issues', |
| 754 | action='store_true', |
| 755 | help='Show issues.') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 756 | activity_types_group.add_option( |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 757 | '-r', '--reviews', |
| 758 | action='store_true', |
| 759 | help='Show reviews.') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 760 | parser.add_option_group(activity_types_group) |
| 761 | |
| 762 | output_format_group = optparse.OptionGroup(parser, 'Output Format', |
| 763 | 'By default, all activity will be printed in the ' |
| 764 | 'following format: {url} {title}. This can be ' |
| 765 | 'changed for either all activity types or ' |
| 766 | 'individually for each activity type. The format ' |
| 767 | 'is defined as documented for ' |
| 768 | 'string.format(...). The variables available for ' |
| 769 | 'all activity types are url, title and author. ' |
| 770 | 'Format options for specific activity types will ' |
| 771 | 'override the generic format.') |
| 772 | output_format_group.add_option( |
| 773 | '-f', '--output-format', metavar='<format>', |
| 774 | default=u'{url} {title}', |
| 775 | help='Specifies the format to use when printing all your activity.') |
| 776 | output_format_group.add_option( |
| 777 | '--output-format-changes', metavar='<format>', |
| 778 | default=None, |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 779 | help='Specifies the format to use when printing changes. Supports the ' |
| 780 | 'additional variable {reviewers}') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 781 | output_format_group.add_option( |
| 782 | '--output-format-issues', metavar='<format>', |
| 783 | default=None, |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 784 | help='Specifies the format to use when printing issues. Supports the ' |
| 785 | 'additional variable {owner}.') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 786 | output_format_group.add_option( |
| 787 | '--output-format-reviews', metavar='<format>', |
| 788 | default=None, |
| 789 | help='Specifies the format to use when printing reviews.') |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 790 | output_format_group.add_option( |
| 791 | '--output-format-heading', metavar='<format>', |
| 792 | default=u'{heading}:', |
| 793 | help='Specifies the format to use when printing headings.') |
| 794 | output_format_group.add_option( |
| 795 | '-m', '--markdown', action='store_true', |
| 796 | help='Use markdown-friendly output (overrides --output-format ' |
| 797 | 'and --output-format-heading)') |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 798 | output_format_group.add_option( |
| 799 | '-j', '--json', action='store_true', |
| 800 | help='Output json data (overrides other format options)') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 801 | parser.add_option_group(output_format_group) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 802 | auth.add_auth_options(parser) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 803 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 804 | parser.add_option( |
| 805 | '-v', '--verbose', |
| 806 | action='store_const', |
| 807 | dest='verbosity', |
| 808 | default=logging.WARN, |
| 809 | const=logging.INFO, |
| 810 | help='Output extra informational messages.' |
| 811 | ) |
| 812 | parser.add_option( |
| 813 | '-q', '--quiet', |
| 814 | action='store_const', |
| 815 | dest='verbosity', |
| 816 | const=logging.ERROR, |
| 817 | help='Suppress non-error messages.' |
| 818 | ) |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 819 | parser.add_option( |
| 820 | '-o', '--output', metavar='<file>', |
| 821 | help='Where to output the results. By default prints to stdout.') |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 822 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 823 | # Remove description formatting |
| 824 | parser.format_description = ( |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 825 | lambda _: parser.description) # pylint: disable=no-member |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 826 | |
| 827 | options, args = parser.parse_args() |
| 828 | options.local_user = os.environ.get('USER') |
| 829 | if args: |
| 830 | parser.error('Args unsupported') |
| 831 | if not options.user: |
| 832 | parser.error('USER is not set, please use -u') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 833 | options.user = username(options.user) |
| 834 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 835 | logging.basicConfig(level=options.verbosity) |
| 836 | |
| 837 | # python-keyring provides easy access to the system keyring. |
| 838 | try: |
| 839 | import keyring # pylint: disable=unused-import,unused-variable,F0401 |
| 840 | except ImportError: |
| 841 | logging.warning('Consider installing python-keyring') |
| 842 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 843 | if not options.begin: |
| 844 | if options.last_quarter: |
| 845 | begin, end = quarter_begin, quarter_end |
| 846 | elif options.this_year: |
| 847 | begin, end = get_year_of(datetime.today()) |
| 848 | elif options.week_of: |
| 849 | begin, end = (get_week_of(datetime.strptime(options.week_of, '%m/%d/%y'))) |
jsbell@chromium.org | 74bfde0 | 2014-04-09 17:14:54 +0000 | [diff] [blame] | 850 | elif options.last_week: |
wychen@chromium.org | 8ba1ddb | 2015-04-29 00:04:25 +0000 | [diff] [blame] | 851 | begin, end = (get_week_of(datetime.today() - |
| 852 | timedelta(days=1 + 7 * options.last_week))) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 853 | else: |
| 854 | begin, end = (get_week_of(datetime.today() - timedelta(days=1))) |
| 855 | else: |
| 856 | begin = datetime.strptime(options.begin, '%m/%d/%y') |
| 857 | if options.end: |
| 858 | end = datetime.strptime(options.end, '%m/%d/%y') |
| 859 | else: |
| 860 | end = datetime.today() |
| 861 | options.begin, options.end = begin, end |
| 862 | |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 863 | if options.markdown: |
| 864 | options.output_format = ' * [{title}]({url})' |
| 865 | options.output_format_heading = '### {heading} ###' |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 866 | logging.info('Searching for activity by %s', options.user) |
| 867 | logging.info('Using range %s to %s', options.begin, options.end) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 868 | |
| 869 | my_activity = MyActivity(options) |
| 870 | |
| 871 | if not (options.changes or options.reviews or options.issues): |
| 872 | options.changes = True |
| 873 | options.issues = True |
| 874 | options.reviews = True |
| 875 | |
| 876 | # First do any required authentication so none of the user interaction has to |
| 877 | # wait for actual work. |
| 878 | if options.changes: |
| 879 | my_activity.auth_for_changes() |
| 880 | if options.reviews: |
| 881 | my_activity.auth_for_reviews() |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 882 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 883 | logging.info('Looking up activity.....') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 884 | |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 885 | try: |
| 886 | if options.changes: |
| 887 | my_activity.get_changes() |
| 888 | if options.reviews: |
| 889 | my_activity.get_reviews() |
| 890 | if options.issues: |
| 891 | my_activity.get_issues() |
| 892 | except auth.AuthenticationError as e: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 893 | logging.error('auth.AuthenticationError: %s', e) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 894 | |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 895 | output_file = None |
| 896 | try: |
| 897 | if options.output: |
| 898 | output_file = open(options.output, 'w') |
| 899 | logging.info('Printing output to "%s"', options.output) |
| 900 | sys.stdout = output_file |
| 901 | except (IOError, OSError) as e: |
| 902 | logging.error('Unable to write output: %s', e) |
| 903 | else: |
| 904 | if options.json: |
| 905 | my_activity.dump_json() |
| 906 | else: |
| 907 | my_activity.print_changes() |
| 908 | my_activity.print_reviews() |
| 909 | my_activity.print_issues() |
| 910 | finally: |
| 911 | if output_file: |
| 912 | logging.info('Done printing to file.') |
| 913 | sys.stdout = sys.__stdout__ |
| 914 | output_file.close() |
| 915 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 916 | return 0 |
| 917 | |
| 918 | |
| 919 | if __name__ == '__main__': |
stevefung@chromium.org | 832d51e | 2015-05-27 12:52:51 +0000 | [diff] [blame] | 920 | # Fix encoding to support non-ascii issue titles. |
| 921 | fix_encoding.fix_encoding() |
| 922 | |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 923 | try: |
| 924 | sys.exit(main()) |
| 925 | except KeyboardInterrupt: |
| 926 | sys.stderr.write('interrupted\n') |
| 927 | sys.exit(1) |