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