maruel@chromium.org | 725f1c3 | 2011-04-01 20:24:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
maruel@chromium.org | 3bbf294 | 2012-01-10 16:52:06 +0000 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Enables directory-specific presubmit checks to run at upload and/or commit. |
| 7 | """ |
| 8 | |
stip@chromium.org | f7d31f5 | 2014-01-03 20:14:46 +0000 | [diff] [blame] | 9 | __version__ = '1.8.0' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 10 | |
| 11 | # TODO(joi) Add caching where appropriate/needed. The API is designed to allow |
| 12 | # caching (between all different invocations of presubmit scripts for a given |
| 13 | # change). We should add it as our presubmit scripts start feeling slow. |
| 14 | |
enne@chromium.org | e72c5f5 | 2013-04-16 00:36:40 +0000 | [diff] [blame] | 15 | import cpplint |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 16 | import cPickle # Exposed through the API. |
| 17 | import cStringIO # Exposed through the API. |
iannucci@chromium.org | 8a4a2bc | 2013-03-08 08:13:20 +0000 | [diff] [blame] | 18 | import contextlib |
dcheng | 091b7db | 2016-06-16 01:27:51 -0700 | [diff] [blame] | 19 | import fnmatch # Exposed through the API. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 20 | import glob |
asvitkine@chromium.org | 1516995 | 2011-09-27 14:30:53 +0000 | [diff] [blame] | 21 | import inspect |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 22 | import itertools |
maruel@chromium.org | 4f6852c | 2012-04-20 20:39:20 +0000 | [diff] [blame] | 23 | import json # Exposed through the API. |
maruel@chromium.org | df1595a | 2009-06-11 02:00:13 +0000 | [diff] [blame] | 24 | import logging |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 25 | import marshal # Exposed through the API. |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 26 | import multiprocessing |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 27 | import optparse |
| 28 | import os # Somewhat exposed through the API. |
| 29 | import pickle # Exposed through the API. |
maruel@chromium.org | ce8e46b | 2009-06-26 22:31:51 +0000 | [diff] [blame] | 30 | import random |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 31 | import re # Exposed through the API. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 32 | import sys # Parts exposed through API. |
| 33 | import tempfile # Exposed through the API. |
jam@chromium.org | 2a891dc | 2009-08-20 20:33:37 +0000 | [diff] [blame] | 34 | import time |
maruel@chromium.org | d7dccf5 | 2009-06-06 18:51:58 +0000 | [diff] [blame] | 35 | import traceback # Exposed through the API. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 36 | import types |
maruel@chromium.org | 1487d53 | 2009-06-06 00:22:57 +0000 | [diff] [blame] | 37 | import unittest # Exposed through the API. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 38 | import urllib2 # Exposed through the API. |
tandrii@chromium.org | 015ebae | 2016-04-25 19:37:22 +0000 | [diff] [blame] | 39 | import urlparse |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 40 | from warnings import warn |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 41 | |
| 42 | # Local imports. |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 43 | import auth |
maruel@chromium.org | 35625c7 | 2011-03-23 17:34:02 +0000 | [diff] [blame] | 44 | import fix_encoding |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 45 | import gclient_utils |
tandrii@chromium.org | 015ebae | 2016-04-25 19:37:22 +0000 | [diff] [blame] | 46 | import gerrit_util |
dpranke@chromium.org | 2a00962 | 2011-03-01 02:43:31 +0000 | [diff] [blame] | 47 | import owners |
Jochen Eisinger | 76f5fc6 | 2017-04-07 16:27:46 +0200 | [diff] [blame] | 48 | import owners_finder |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 49 | import presubmit_canned_checks |
maruel@chromium.org | 239f411 | 2011-06-03 20:08:23 +0000 | [diff] [blame] | 50 | import rietveld |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 51 | import scm |
maruel@chromium.org | 84f4fe3 | 2011-04-06 13:26:45 +0000 | [diff] [blame] | 52 | import subprocess2 as subprocess # Exposed through the API. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 53 | |
| 54 | |
maruel@chromium.org | ce8e46b | 2009-06-26 22:31:51 +0000 | [diff] [blame] | 55 | # Ask for feedback only once in program lifetime. |
| 56 | _ASKED_FOR_FEEDBACK = False |
| 57 | |
| 58 | |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 59 | class PresubmitFailure(Exception): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 60 | pass |
| 61 | |
| 62 | |
maruel@chromium.org | ffeb2f3 | 2013-12-03 13:55:22 +0000 | [diff] [blame] | 63 | class CommandData(object): |
| 64 | def __init__(self, name, cmd, kwargs, message): |
| 65 | self.name = name |
| 66 | self.cmd = cmd |
| 67 | self.kwargs = kwargs |
| 68 | self.message = message |
| 69 | self.info = None |
| 70 | |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 71 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 72 | def normpath(path): |
| 73 | '''Version of os.path.normpath that also changes backward slashes to |
| 74 | forward slashes when not running on Windows. |
| 75 | ''' |
| 76 | # This is safe to always do because the Windows version of os.path.normpath |
| 77 | # will replace forward slashes with backward slashes. |
| 78 | path = path.replace(os.sep, '/') |
| 79 | return os.path.normpath(path) |
| 80 | |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 81 | |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 82 | def _RightHandSideLinesImpl(affected_files): |
| 83 | """Implements RightHandSideLines for InputApi and GclChange.""" |
| 84 | for af in affected_files: |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 85 | lines = af.ChangedContents() |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 86 | for line in lines: |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 87 | yield (af, line[0], line[1]) |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 88 | |
| 89 | |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 90 | class PresubmitOutput(object): |
| 91 | def __init__(self, input_stream=None, output_stream=None): |
| 92 | self.input_stream = input_stream |
| 93 | self.output_stream = output_stream |
| 94 | self.reviewers = [] |
| 95 | self.written_output = [] |
| 96 | self.error_count = 0 |
| 97 | |
| 98 | def prompt_yes_no(self, prompt_string): |
| 99 | self.write(prompt_string) |
| 100 | if self.input_stream: |
| 101 | response = self.input_stream.readline().strip().lower() |
| 102 | if response not in ('y', 'yes'): |
| 103 | self.fail() |
| 104 | else: |
| 105 | self.fail() |
| 106 | |
| 107 | def fail(self): |
| 108 | self.error_count += 1 |
| 109 | |
| 110 | def should_continue(self): |
| 111 | return not self.error_count |
| 112 | |
| 113 | def write(self, s): |
| 114 | self.written_output.append(s) |
| 115 | if self.output_stream: |
| 116 | self.output_stream.write(s) |
| 117 | |
| 118 | def getvalue(self): |
| 119 | return ''.join(self.written_output) |
| 120 | |
| 121 | |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 122 | # Top level object so multiprocessing can pickle |
| 123 | # Public access through OutputApi object. |
| 124 | class _PresubmitResult(object): |
| 125 | """Base class for result objects.""" |
| 126 | fatal = False |
| 127 | should_prompt = False |
| 128 | |
| 129 | def __init__(self, message, items=None, long_text=''): |
| 130 | """ |
| 131 | message: A short one-line message to indicate errors. |
| 132 | items: A list of short strings to indicate where errors occurred. |
| 133 | long_text: multi-line text output, e.g. from another tool |
| 134 | """ |
| 135 | self._message = message |
| 136 | self._items = items or [] |
| 137 | if items: |
| 138 | self._items = items |
| 139 | self._long_text = long_text.rstrip() |
| 140 | |
| 141 | def handle(self, output): |
| 142 | output.write(self._message) |
| 143 | output.write('\n') |
| 144 | for index, item in enumerate(self._items): |
| 145 | output.write(' ') |
| 146 | # Write separately in case it's unicode. |
| 147 | output.write(str(item)) |
| 148 | if index < len(self._items) - 1: |
| 149 | output.write(' \\') |
| 150 | output.write('\n') |
| 151 | if self._long_text: |
| 152 | output.write('\n***************\n') |
| 153 | # Write separately in case it's unicode. |
| 154 | output.write(self._long_text) |
| 155 | output.write('\n***************\n') |
| 156 | if self.fatal: |
| 157 | output.fail() |
| 158 | |
| 159 | |
| 160 | # Top level object so multiprocessing can pickle |
| 161 | # Public access through OutputApi object. |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 162 | class _PresubmitError(_PresubmitResult): |
| 163 | """A hard presubmit error.""" |
| 164 | fatal = True |
| 165 | |
| 166 | |
| 167 | # Top level object so multiprocessing can pickle |
| 168 | # Public access through OutputApi object. |
| 169 | class _PresubmitPromptWarning(_PresubmitResult): |
| 170 | """An warning that prompts the user if they want to continue.""" |
| 171 | should_prompt = True |
| 172 | |
| 173 | |
| 174 | # Top level object so multiprocessing can pickle |
| 175 | # Public access through OutputApi object. |
| 176 | class _PresubmitNotifyResult(_PresubmitResult): |
| 177 | """Just print something to the screen -- but it's not even a warning.""" |
| 178 | pass |
| 179 | |
| 180 | |
| 181 | # Top level object so multiprocessing can pickle |
| 182 | # Public access through OutputApi object. |
| 183 | class _MailTextResult(_PresubmitResult): |
| 184 | """A warning that should be included in the review request email.""" |
| 185 | def __init__(self, *args, **kwargs): |
| 186 | super(_MailTextResult, self).__init__() |
| 187 | raise NotImplementedError() |
| 188 | |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 189 | class GerritAccessor(object): |
| 190 | """Limited Gerrit functionality for canned presubmit checks to work. |
| 191 | |
| 192 | To avoid excessive Gerrit calls, caches the results. |
| 193 | """ |
| 194 | |
| 195 | def __init__(self, host): |
| 196 | self.host = host |
| 197 | self.cache = {} |
| 198 | |
| 199 | def _FetchChangeDetail(self, issue): |
| 200 | # Separate function to be easily mocked in tests. |
Andrii Shyshkalov | c6c8b4c | 2016-11-09 20:51:20 +0100 | [diff] [blame] | 201 | try: |
| 202 | return gerrit_util.GetChangeDetail( |
| 203 | self.host, str(issue), |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 204 | ['ALL_REVISIONS', 'DETAILED_LABELS', 'ALL_COMMITS']) |
Andrii Shyshkalov | c6c8b4c | 2016-11-09 20:51:20 +0100 | [diff] [blame] | 205 | except gerrit_util.GerritError as e: |
| 206 | if e.http_status == 404: |
| 207 | raise Exception('Either Gerrit issue %s doesn\'t exist, or ' |
| 208 | 'no credentials to fetch issue details' % issue) |
| 209 | raise |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 210 | |
| 211 | def GetChangeInfo(self, issue): |
| 212 | """Returns labels and all revisions (patchsets) for this issue. |
| 213 | |
| 214 | The result is a dictionary according to Gerrit REST Api. |
| 215 | https://gerrit-review.googlesource.com/Documentation/rest-api.html |
| 216 | |
| 217 | However, API isn't very clear what's inside, so see tests for example. |
| 218 | """ |
| 219 | assert issue |
| 220 | cache_key = int(issue) |
| 221 | if cache_key not in self.cache: |
| 222 | self.cache[cache_key] = self._FetchChangeDetail(issue) |
| 223 | return self.cache[cache_key] |
| 224 | |
| 225 | def GetChangeDescription(self, issue, patchset=None): |
| 226 | """If patchset is none, fetches current patchset.""" |
| 227 | info = self.GetChangeInfo(issue) |
| 228 | # info is a reference to cache. We'll modify it here adding description to |
| 229 | # it to the right patchset, if it is not yet there. |
| 230 | |
| 231 | # Find revision info for the patchset we want. |
| 232 | if patchset is not None: |
| 233 | for rev, rev_info in info['revisions'].iteritems(): |
| 234 | if str(rev_info['_number']) == str(patchset): |
| 235 | break |
| 236 | else: |
| 237 | raise Exception('patchset %s doesn\'t exist in issue %s' % ( |
| 238 | patchset, issue)) |
| 239 | else: |
| 240 | rev = info['current_revision'] |
| 241 | rev_info = info['revisions'][rev] |
| 242 | |
Andrii Shyshkalov | 9c3a464 | 2017-01-24 17:41:22 +0100 | [diff] [blame] | 243 | return rev_info['commit']['message'] |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 244 | |
| 245 | def GetChangeOwner(self, issue): |
| 246 | return self.GetChangeInfo(issue)['owner']['email'] |
| 247 | |
| 248 | def GetChangeReviewers(self, issue, approving_only=True): |
agable | 565adb5 | 2016-07-22 14:48:07 -0700 | [diff] [blame] | 249 | cr = self.GetChangeInfo(issue)['labels']['Code-Review'] |
| 250 | max_value = max(int(k) for k in cr['values'].keys()) |
Aaron Gable | f5644a9 | 2016-12-02 15:31:58 -0800 | [diff] [blame] | 251 | return [r.get('email') for r in cr.get('all', []) |
agable | 565adb5 | 2016-07-22 14:48:07 -0700 | [diff] [blame] | 252 | if not approving_only or r.get('value', 0) == max_value] |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 253 | |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 254 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 255 | class OutputApi(object): |
wez@chromium.org | a6d011e | 2013-03-26 17:31:49 +0000 | [diff] [blame] | 256 | """An instance of OutputApi gets passed to presubmit scripts so that they |
| 257 | can output various types of results. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 258 | """ |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 259 | PresubmitResult = _PresubmitResult |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 260 | PresubmitError = _PresubmitError |
| 261 | PresubmitPromptWarning = _PresubmitPromptWarning |
| 262 | PresubmitNotifyResult = _PresubmitNotifyResult |
| 263 | MailTextResult = _MailTextResult |
| 264 | |
wez@chromium.org | a6d011e | 2013-03-26 17:31:49 +0000 | [diff] [blame] | 265 | def __init__(self, is_committing): |
| 266 | self.is_committing = is_committing |
| 267 | |
wez@chromium.org | a6d011e | 2013-03-26 17:31:49 +0000 | [diff] [blame] | 268 | def PresubmitPromptOrNotify(self, *args, **kwargs): |
| 269 | """Warn the user when uploading, but only notify if committing.""" |
| 270 | if self.is_committing: |
| 271 | return self.PresubmitNotifyResult(*args, **kwargs) |
| 272 | return self.PresubmitPromptWarning(*args, **kwargs) |
| 273 | |
Kenneth Russell | 61e2ed4 | 2017-02-15 11:47:13 -0800 | [diff] [blame] | 274 | def EnsureCQIncludeTrybotsAreAdded(self, cl, bots_to_include, message): |
| 275 | """Helper for any PostUploadHook wishing to add CQ_INCLUDE_TRYBOTS. |
| 276 | |
| 277 | Merges the bots_to_include into the current CQ_INCLUDE_TRYBOTS list, |
| 278 | keeping it alphabetically sorted. Returns the results that should be |
| 279 | returned from the PostUploadHook. |
| 280 | |
| 281 | Args: |
| 282 | cl: The git_cl.Changelist object. |
| 283 | bots_to_include: A list of strings of bots to include, in the form |
| 284 | "master:slave". |
| 285 | message: A message to be printed in the case that |
| 286 | CQ_INCLUDE_TRYBOTS was updated. |
| 287 | """ |
| 288 | description = cl.GetDescription(force=True) |
| 289 | all_bots = [] |
| 290 | include_re = re.compile(r'^CQ_INCLUDE_TRYBOTS=(.*)', re.M | re.I) |
| 291 | m = include_re.search(description) |
| 292 | if m: |
| 293 | all_bots = [i.strip() for i in m.group(1).split(';') if i.strip()] |
| 294 | if set(all_bots) >= set(bots_to_include): |
| 295 | return [] |
| 296 | # Sort the bots to keep them in some consistent order -- not required. |
| 297 | all_bots = sorted(set(all_bots) | set(bots_to_include)) |
| 298 | new_include_trybots = 'CQ_INCLUDE_TRYBOTS=%s' % ';'.join(all_bots) |
| 299 | if m: |
| 300 | new_description = include_re.sub(new_include_trybots, description) |
| 301 | else: |
Kenneth Russell | df6e734 | 2017-04-24 17:07:41 -0700 | [diff] [blame^] | 302 | # If we're adding a new CQ_INCLUDE_TRYBOTS line then make |
| 303 | # absolutely sure to add it before any Change-Id: line, to avoid |
| 304 | # breaking Gerrit. |
| 305 | # |
| 306 | # The use of \n outside the capture group causes the last |
| 307 | # newline before Change-Id and any extra newlines after it to be |
| 308 | # consumed. They are re-added during the join operation. |
| 309 | # |
| 310 | # The filter operation drops the trailing empty string after the |
| 311 | # original string is split. |
| 312 | split_desc = filter( |
| 313 | None, re.split('\n(Change-Id: \w*)\n*', description, 1, re.M)) |
| 314 | # Make sure to insert this before the last entry. For backward |
| 315 | # compatibility, ensure the CL description ends in a newline. |
| 316 | if len(split_desc) == 1: |
| 317 | insert_idx = 1 |
| 318 | else: |
| 319 | insert_idx = len(split_desc) - 1 |
| 320 | split_desc.insert(insert_idx, new_include_trybots) |
| 321 | new_description = '\n'.join(split_desc) + '\n' |
Kenneth Russell | 61e2ed4 | 2017-02-15 11:47:13 -0800 | [diff] [blame] | 322 | cl.UpdateDescription(new_description, force=True) |
| 323 | return [self.PresubmitNotifyResult(message)] |
| 324 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 325 | |
| 326 | class InputApi(object): |
| 327 | """An instance of this object is passed to presubmit scripts so they can |
| 328 | know stuff about the change they're looking at. |
| 329 | """ |
maruel@chromium.org | b17b55b | 2010-11-03 14:42:37 +0000 | [diff] [blame] | 330 | # Method could be a function |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 331 | # pylint: disable=no-self-use |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 332 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 333 | # File extensions that are considered source files from a style guide |
| 334 | # perspective. Don't modify this list from a presubmit script! |
maruel@chromium.org | c33455a | 2011-06-24 19:14:18 +0000 | [diff] [blame] | 335 | # |
| 336 | # Files without an extension aren't included in the list. If you want to |
| 337 | # filter them as source files, add r"(^|.*?[\\\/])[^.]+$" to the white list. |
| 338 | # Note that ALL CAPS files are black listed in DEFAULT_BLACK_LIST below. |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 339 | DEFAULT_WHITE_LIST = ( |
| 340 | # C++ and friends |
maruel@chromium.org | fe1211a | 2011-05-28 18:54:17 +0000 | [diff] [blame] | 341 | r".+\.c$", r".+\.cc$", r".+\.cpp$", r".+\.h$", r".+\.m$", r".+\.mm$", |
| 342 | r".+\.inl$", r".+\.asm$", r".+\.hxx$", r".+\.hpp$", r".+\.s$", r".+\.S$", |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 343 | # Scripts |
maruel@chromium.org | fe1211a | 2011-05-28 18:54:17 +0000 | [diff] [blame] | 344 | r".+\.js$", r".+\.py$", r".+\.sh$", r".+\.rb$", r".+\.pl$", r".+\.pm$", |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 345 | # Other |
estade@chromium.org | ae7af92 | 2012-01-27 14:51:13 +0000 | [diff] [blame] | 346 | r".+\.java$", r".+\.mk$", r".+\.am$", r".+\.css$" |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 347 | ) |
| 348 | |
| 349 | # Path regexp that should be excluded from being considered containing source |
| 350 | # files. Don't modify this list from a presubmit script! |
| 351 | DEFAULT_BLACK_LIST = ( |
gavinp@chromium.org | 656326d | 2012-08-13 00:43:57 +0000 | [diff] [blame] | 352 | r"testing_support[\\\/]google_appengine[\\\/].*", |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 353 | r".*\bexperimental[\\\/].*", |
primiano@chromium.org | b9658c3 | 2015-10-06 10:50:13 +0000 | [diff] [blame] | 354 | # Exclude third_party/.* but NOT third_party/WebKit (crbug.com/539768). |
| 355 | r".*\bthird_party[\\\/](?!WebKit[\\\/]).*", |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 356 | # Output directories (just in case) |
| 357 | r".*\bDebug[\\\/].*", |
| 358 | r".*\bRelease[\\\/].*", |
| 359 | r".*\bxcodebuild[\\\/].*", |
thakis@chromium.org | c1c9635 | 2013-10-09 19:50:27 +0000 | [diff] [blame] | 360 | r".*\bout[\\\/].*", |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 361 | # All caps files like README and LICENCE. |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 362 | r".*\b[A-Z0-9_]{2,}$", |
maruel@chromium.org | df1595a | 2009-06-11 02:00:13 +0000 | [diff] [blame] | 363 | # SCM (can happen in dual SCM configuration). (Slightly over aggressive) |
maruel@chromium.org | 5d0dc43 | 2011-01-03 02:40:37 +0000 | [diff] [blame] | 364 | r"(|.*[\\\/])\.git[\\\/].*", |
| 365 | r"(|.*[\\\/])\.svn[\\\/].*", |
maruel@chromium.org | 7ccb4bb | 2011-11-07 20:26:20 +0000 | [diff] [blame] | 366 | # There is no point in processing a patch file. |
| 367 | r".+\.diff$", |
| 368 | r".+\.patch$", |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 369 | ) |
| 370 | |
maruel@chromium.org | cc73ad6 | 2011-07-06 17:39:26 +0000 | [diff] [blame] | 371 | def __init__(self, change, presubmit_path, is_committing, |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 372 | rietveld_obj, verbose, gerrit_obj=None, dry_run=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 373 | """Builds an InputApi object. |
| 374 | |
| 375 | Args: |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 376 | change: A presubmit.Change object. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 377 | presubmit_path: The path to the presubmit script being processed. |
maruel@chromium.org | d7dccf5 | 2009-06-06 18:51:58 +0000 | [diff] [blame] | 378 | is_committing: True if the change is about to be committed. |
maruel@chromium.org | 239f411 | 2011-06-03 20:08:23 +0000 | [diff] [blame] | 379 | rietveld_obj: rietveld.Rietveld client object |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 380 | gerrit_obj: provides basic Gerrit codereview functionality. |
| 381 | dry_run: if true, some Checks will be skipped. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 382 | """ |
maruel@chromium.org | 9711bba | 2009-05-22 23:51:39 +0000 | [diff] [blame] | 383 | # Version number of the presubmit_support script. |
| 384 | self.version = [int(x) for x in __version__.split('.')] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 385 | self.change = change |
maruel@chromium.org | d7dccf5 | 2009-06-06 18:51:58 +0000 | [diff] [blame] | 386 | self.is_committing = is_committing |
maruel@chromium.org | 239f411 | 2011-06-03 20:08:23 +0000 | [diff] [blame] | 387 | self.rietveld = rietveld_obj |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 388 | self.gerrit = gerrit_obj |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 389 | self.dry_run = dry_run |
maruel@chromium.org | cab38e9 | 2011-04-09 00:30:51 +0000 | [diff] [blame] | 390 | # TBD |
| 391 | self.host_url = 'http://codereview.chromium.org' |
| 392 | if self.rietveld: |
maruel@chromium.org | 239f411 | 2011-06-03 20:08:23 +0000 | [diff] [blame] | 393 | self.host_url = self.rietveld.url |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 394 | |
| 395 | # We expose various modules and functions as attributes of the input_api |
| 396 | # so that presubmit scripts don't have to import them. |
| 397 | self.basename = os.path.basename |
| 398 | self.cPickle = cPickle |
enne@chromium.org | e72c5f5 | 2013-04-16 00:36:40 +0000 | [diff] [blame] | 399 | self.cpplint = cpplint |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 400 | self.cStringIO = cStringIO |
dcheng | 091b7db | 2016-06-16 01:27:51 -0700 | [diff] [blame] | 401 | self.fnmatch = fnmatch |
dpranke@chromium.org | 17cc244 | 2012-10-17 21:12:09 +0000 | [diff] [blame] | 402 | self.glob = glob.glob |
maruel@chromium.org | fb11c7b | 2010-03-18 18:22:14 +0000 | [diff] [blame] | 403 | self.json = json |
maruel@chromium.org | 6fba34d | 2011-06-02 13:45:12 +0000 | [diff] [blame] | 404 | self.logging = logging.getLogger('PRESUBMIT') |
maruel@chromium.org | 2b5ce56 | 2011-03-31 16:15:44 +0000 | [diff] [blame] | 405 | self.os_listdir = os.listdir |
| 406 | self.os_walk = os.walk |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 407 | self.os_path = os.path |
pgervais@chromium.org | bd0cace | 2014-10-02 23:23:46 +0000 | [diff] [blame] | 408 | self.os_stat = os.stat |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 409 | self.pickle = pickle |
| 410 | self.marshal = marshal |
| 411 | self.re = re |
| 412 | self.subprocess = subprocess |
| 413 | self.tempfile = tempfile |
dpranke@chromium.org | 0d1bdea | 2011-03-24 22:54:38 +0000 | [diff] [blame] | 414 | self.time = time |
maruel@chromium.org | d7dccf5 | 2009-06-06 18:51:58 +0000 | [diff] [blame] | 415 | self.traceback = traceback |
maruel@chromium.org | 1487d53 | 2009-06-06 00:22:57 +0000 | [diff] [blame] | 416 | self.unittest = unittest |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 417 | self.urllib2 = urllib2 |
| 418 | |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 419 | # To easily fork python. |
| 420 | self.python_executable = sys.executable |
| 421 | self.environ = os.environ |
| 422 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 423 | # InputApi.platform is the platform you're currently running on. |
| 424 | self.platform = sys.platform |
| 425 | |
iannucci@chromium.org | 0af3bb3 | 2015-06-12 20:44:35 +0000 | [diff] [blame] | 426 | self.cpu_count = multiprocessing.cpu_count() |
| 427 | |
iannucci@chromium.org | d61a495 | 2015-07-01 23:21:26 +0000 | [diff] [blame] | 428 | # this is done here because in RunTests, the current working directory has |
| 429 | # changed, which causes Pool() to explode fantastically when run on windows |
| 430 | # (because it tries to load the __main__ module, which imports lots of |
| 431 | # things relative to the current working directory). |
| 432 | self._run_tests_pool = multiprocessing.Pool(self.cpu_count) |
| 433 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 434 | # The local path of the currently-being-processed presubmit script. |
maruel@chromium.org | 3d23524 | 2009-05-15 12:40:48 +0000 | [diff] [blame] | 435 | self._current_presubmit_path = os.path.dirname(presubmit_path) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 436 | |
| 437 | # We carry the canned checks so presubmit scripts can easily use them. |
| 438 | self.canned_checks = presubmit_canned_checks |
| 439 | |
Jochen Eisinger | 72606f8 | 2017-04-04 10:44:18 +0200 | [diff] [blame] | 440 | |
dpranke@chromium.org | 2a00962 | 2011-03-01 02:43:31 +0000 | [diff] [blame] | 441 | # TODO(dpranke): figure out a list of all approved owners for a repo |
| 442 | # in order to be able to handle wildcard OWNERS files? |
| 443 | self.owners_db = owners.Database(change.RepositoryRoot(), |
Jochen Eisinger | 72606f8 | 2017-04-04 10:44:18 +0200 | [diff] [blame] | 444 | fopen=file, os_path=self.os_path) |
Jochen Eisinger | 76f5fc6 | 2017-04-07 16:27:46 +0200 | [diff] [blame] | 445 | self.owners_finder = owners_finder.OwnersFinder |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 446 | self.verbose = verbose |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 447 | self.Command = CommandData |
dpranke@chromium.org | 2a00962 | 2011-03-01 02:43:31 +0000 | [diff] [blame] | 448 | |
enne@chromium.org | e72c5f5 | 2013-04-16 00:36:40 +0000 | [diff] [blame] | 449 | # Replace <hash_map> and <hash_set> as headers that need to be included |
danakj@chromium.org | 1827852 | 2013-06-11 22:42:32 +0000 | [diff] [blame] | 450 | # with "base/containers/hash_tables.h" instead. |
enne@chromium.org | e72c5f5 | 2013-04-16 00:36:40 +0000 | [diff] [blame] | 451 | # Access to a protected member _XX of a client class |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 452 | # pylint: disable=protected-access |
enne@chromium.org | e72c5f5 | 2013-04-16 00:36:40 +0000 | [diff] [blame] | 453 | self.cpplint._re_pattern_templates = [ |
danakj@chromium.org | 1827852 | 2013-06-11 22:42:32 +0000 | [diff] [blame] | 454 | (a, b, 'base/containers/hash_tables.h') |
enne@chromium.org | e72c5f5 | 2013-04-16 00:36:40 +0000 | [diff] [blame] | 455 | if header in ('<hash_map>', '<hash_set>') else (a, b, header) |
| 456 | for (a, b, header) in cpplint._re_pattern_templates |
| 457 | ] |
| 458 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 459 | def PresubmitLocalPath(self): |
| 460 | """Returns the local path of the presubmit script currently being run. |
| 461 | |
| 462 | This is useful if you don't want to hard-code absolute paths in the |
| 463 | presubmit script. For example, It can be used to find another file |
| 464 | relative to the PRESUBMIT.py script, so the whole tree can be branched and |
| 465 | the presubmit script still works, without editing its content. |
| 466 | """ |
maruel@chromium.org | 3d23524 | 2009-05-15 12:40:48 +0000 | [diff] [blame] | 467 | return self._current_presubmit_path |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 468 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 469 | def AffectedFiles(self, include_deletes=True, file_filter=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 470 | """Same as input_api.change.AffectedFiles() except only lists files |
| 471 | (and optionally directories) in the same directory as the current presubmit |
| 472 | script, or subdirectories thereof. |
| 473 | """ |
maruel@chromium.org | 3d23524 | 2009-05-15 12:40:48 +0000 | [diff] [blame] | 474 | dir_with_slash = normpath("%s/" % self.PresubmitLocalPath()) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 475 | if len(dir_with_slash) == 1: |
| 476 | dir_with_slash = '' |
sail@chromium.org | 5538e02 | 2011-05-12 17:53:16 +0000 | [diff] [blame] | 477 | |
maruel@chromium.org | 4661e0c | 2009-06-04 00:45:26 +0000 | [diff] [blame] | 478 | return filter( |
| 479 | lambda x: normpath(x.AbsoluteLocalPath()).startswith(dir_with_slash), |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 480 | self.change.AffectedFiles(include_deletes, file_filter)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 481 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 482 | def LocalPaths(self): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 483 | """Returns local paths of input_api.AffectedFiles().""" |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 484 | paths = [af.LocalPath() for af in self.AffectedFiles()] |
pgervais@chromium.org | 2f64f78 | 2014-04-25 00:12:33 +0000 | [diff] [blame] | 485 | logging.debug("LocalPaths: %s", paths) |
| 486 | return paths |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 487 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 488 | def AbsoluteLocalPaths(self): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 489 | """Returns absolute local paths of input_api.AffectedFiles().""" |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 490 | return [af.AbsoluteLocalPath() for af in self.AffectedFiles()] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 491 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 492 | def AffectedTestableFiles(self, include_deletes=None): |
| 493 | """Same as input_api.change.AffectedTestableFiles() except only lists files |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 494 | in the same directory as the current presubmit script, or subdirectories |
| 495 | thereof. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 496 | """ |
maruel@chromium.org | 77c4f0f | 2009-05-29 18:53:04 +0000 | [diff] [blame] | 497 | if include_deletes is not None: |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 498 | warn("AffectedTestableFiles(include_deletes=%s)" |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 499 | " is deprecated and ignored" % str(include_deletes), |
| 500 | category=DeprecationWarning, |
| 501 | stacklevel=2) |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 502 | return filter(lambda x: x.IsTestableFile(), |
| 503 | self.AffectedFiles(include_deletes=False)) |
| 504 | |
| 505 | def AffectedTextFiles(self, include_deletes=None): |
| 506 | """An alias to AffectedTestableFiles for backwards compatibility.""" |
| 507 | return self.AffectedTestableFiles(include_deletes=include_deletes) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 508 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 509 | def FilterSourceFile(self, affected_file, white_list=None, black_list=None): |
| 510 | """Filters out files that aren't considered "source file". |
| 511 | |
| 512 | If white_list or black_list is None, InputApi.DEFAULT_WHITE_LIST |
| 513 | and InputApi.DEFAULT_BLACK_LIST is used respectively. |
| 514 | |
| 515 | The lists will be compiled as regular expression and |
| 516 | AffectedFile.LocalPath() needs to pass both list. |
| 517 | |
| 518 | Note: Copy-paste this function to suit your needs or use a lambda function. |
| 519 | """ |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 520 | def Find(affected_file, items): |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 521 | local_path = affected_file.LocalPath() |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 522 | for item in items: |
maruel@chromium.org | df1595a | 2009-06-11 02:00:13 +0000 | [diff] [blame] | 523 | if self.re.match(item, local_path): |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 524 | return True |
| 525 | return False |
| 526 | return (Find(affected_file, white_list or self.DEFAULT_WHITE_LIST) and |
| 527 | not Find(affected_file, black_list or self.DEFAULT_BLACK_LIST)) |
| 528 | |
| 529 | def AffectedSourceFiles(self, source_file): |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 530 | """Filter the list of AffectedTestableFiles by the function source_file. |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 531 | |
| 532 | If source_file is None, InputApi.FilterSourceFile() is used. |
| 533 | """ |
| 534 | if not source_file: |
| 535 | source_file = self.FilterSourceFile |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 536 | return filter(source_file, self.AffectedTestableFiles()) |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 537 | |
| 538 | def RightHandSideLines(self, source_file_filter=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 539 | """An iterator over all text lines in "new" version of changed files. |
| 540 | |
| 541 | Only lists lines from new or modified text files in the change that are |
| 542 | contained by the directory of the currently executing presubmit script. |
| 543 | |
| 544 | This is useful for doing line-by-line regex checks, like checking for |
| 545 | trailing whitespace. |
| 546 | |
| 547 | Yields: |
| 548 | a 3 tuple: |
| 549 | the AffectedFile instance of the current file; |
| 550 | integer line number (1-based); and |
| 551 | the contents of the line as a string. |
maruel@chromium.org | 1487d53 | 2009-06-06 00:22:57 +0000 | [diff] [blame] | 552 | |
nick@chromium.org | 2a3ab7e | 2011-04-27 22:06:27 +0000 | [diff] [blame] | 553 | Note: The carriage return (LF or CR) is stripped off. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 554 | """ |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 555 | files = self.AffectedSourceFiles(source_file_filter) |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 556 | return _RightHandSideLinesImpl(files) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 557 | |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 558 | def ReadFile(self, file_item, mode='r'): |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 559 | """Reads an arbitrary file. |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 560 | |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 561 | Deny reading anything outside the repository. |
| 562 | """ |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 563 | if isinstance(file_item, AffectedFile): |
| 564 | file_item = file_item.AbsoluteLocalPath() |
| 565 | if not file_item.startswith(self.change.RepositoryRoot()): |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 566 | raise IOError('Access outside the repository root is denied.') |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 567 | return gclient_utils.FileRead(file_item, mode) |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 568 | |
maruel@chromium.org | cc73ad6 | 2011-07-06 17:39:26 +0000 | [diff] [blame] | 569 | @property |
| 570 | def tbr(self): |
| 571 | """Returns if a change is TBR'ed.""" |
| 572 | return 'TBR' in self.change.tags |
| 573 | |
maruel@chromium.org | ffeb2f3 | 2013-12-03 13:55:22 +0000 | [diff] [blame] | 574 | def RunTests(self, tests_mix, parallel=True): |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 575 | tests = [] |
| 576 | msgs = [] |
| 577 | for t in tests_mix: |
| 578 | if isinstance(t, OutputApi.PresubmitResult): |
| 579 | msgs.append(t) |
| 580 | else: |
| 581 | assert issubclass(t.message, _PresubmitResult) |
| 582 | tests.append(t) |
maruel@chromium.org | ffeb2f3 | 2013-12-03 13:55:22 +0000 | [diff] [blame] | 583 | if self.verbose: |
| 584 | t.info = _PresubmitNotifyResult |
ilevy@chromium.org | 5678d33 | 2013-05-18 01:34:14 +0000 | [diff] [blame] | 585 | if len(tests) > 1 and parallel: |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 586 | # async recipe works around multiprocessing bug handling Ctrl-C |
iannucci@chromium.org | d61a495 | 2015-07-01 23:21:26 +0000 | [diff] [blame] | 587 | msgs.extend(self._run_tests_pool.map_async(CallCommand, tests).get(99999)) |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 588 | else: |
| 589 | msgs.extend(map(CallCommand, tests)) |
| 590 | return [m for m in msgs if m] |
| 591 | |
scottmg | 86099d7 | 2016-09-01 09:16:51 -0700 | [diff] [blame] | 592 | def ShutdownPool(self): |
| 593 | self._run_tests_pool.close() |
| 594 | self._run_tests_pool.join() |
| 595 | self._run_tests_pool = None |
| 596 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 597 | |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 598 | class _DiffCache(object): |
| 599 | """Caches diffs retrieved from a particular SCM.""" |
agable@chromium.org | ea84ef1 | 2014-04-30 19:55:12 +0000 | [diff] [blame] | 600 | def __init__(self, upstream=None): |
| 601 | """Stores the upstream revision against which all diffs will be computed.""" |
| 602 | self._upstream = upstream |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 603 | |
| 604 | def GetDiff(self, path, local_root): |
| 605 | """Get the diff for a particular path.""" |
| 606 | raise NotImplementedError() |
| 607 | |
Daniel Cheng | 7a1f04d | 2017-03-21 19:12:31 -0700 | [diff] [blame] | 608 | def GetOldContents(self, path, local_root): |
| 609 | """Get the old version for a particular path.""" |
| 610 | raise NotImplementedError() |
| 611 | |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 612 | |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 613 | class _GitDiffCache(_DiffCache): |
| 614 | """DiffCache implementation for git; gets all file diffs at once.""" |
agable@chromium.org | ea84ef1 | 2014-04-30 19:55:12 +0000 | [diff] [blame] | 615 | def __init__(self, upstream): |
| 616 | super(_GitDiffCache, self).__init__(upstream=upstream) |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 617 | self._diffs_by_file = None |
| 618 | |
| 619 | def GetDiff(self, path, local_root): |
| 620 | if not self._diffs_by_file: |
| 621 | # Compute a single diff for all files and parse the output; should |
| 622 | # with git this is much faster than computing one diff for each file. |
| 623 | diffs = {} |
| 624 | |
| 625 | # Don't specify any filenames below, because there are command line length |
| 626 | # limits on some platforms and GenerateDiff would fail. |
agable@chromium.org | ea84ef1 | 2014-04-30 19:55:12 +0000 | [diff] [blame] | 627 | unified_diff = scm.GIT.GenerateDiff(local_root, files=[], full_move=True, |
| 628 | branch=self._upstream) |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 629 | |
| 630 | # This regex matches the path twice, separated by a space. Note that |
| 631 | # filename itself may contain spaces. |
| 632 | file_marker = re.compile('^diff --git (?P<filename>.*) (?P=filename)$') |
| 633 | current_diff = [] |
| 634 | keep_line_endings = True |
| 635 | for x in unified_diff.splitlines(keep_line_endings): |
| 636 | match = file_marker.match(x) |
| 637 | if match: |
| 638 | # Marks the start of a new per-file section. |
| 639 | diffs[match.group('filename')] = current_diff = [x] |
| 640 | elif x.startswith('diff --git'): |
| 641 | raise PresubmitFailure('Unexpected diff line: %s' % x) |
| 642 | else: |
| 643 | current_diff.append(x) |
| 644 | |
| 645 | self._diffs_by_file = dict( |
| 646 | (normpath(path), ''.join(diff)) for path, diff in diffs.items()) |
| 647 | |
| 648 | if path not in self._diffs_by_file: |
| 649 | raise PresubmitFailure( |
| 650 | 'Unified diff did not contain entry for file %s' % path) |
| 651 | |
| 652 | return self._diffs_by_file[path] |
| 653 | |
Daniel Cheng | 7a1f04d | 2017-03-21 19:12:31 -0700 | [diff] [blame] | 654 | def GetOldContents(self, path, local_root): |
| 655 | return scm.GIT.GetOldContents(local_root, path, branch=self._upstream) |
| 656 | |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 657 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 658 | class AffectedFile(object): |
| 659 | """Representation of a file in a change.""" |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 660 | |
| 661 | DIFF_CACHE = _DiffCache |
| 662 | |
maruel@chromium.org | b17b55b | 2010-11-03 14:42:37 +0000 | [diff] [blame] | 663 | # Method could be a function |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 664 | # pylint: disable=no-self-use |
agable@chromium.org | ea84ef1 | 2014-04-30 19:55:12 +0000 | [diff] [blame] | 665 | def __init__(self, path, action, repository_root, diff_cache): |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 666 | self._path = path |
| 667 | self._action = action |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 668 | self._local_root = repository_root |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 669 | self._is_directory = None |
nick@chromium.org | 2a3ab7e | 2011-04-27 22:06:27 +0000 | [diff] [blame] | 670 | self._cached_changed_contents = None |
| 671 | self._cached_new_contents = None |
agable@chromium.org | ea84ef1 | 2014-04-30 19:55:12 +0000 | [diff] [blame] | 672 | self._diff_cache = diff_cache |
tobiasjs | 2836bcf | 2016-08-16 04:08:16 -0700 | [diff] [blame] | 673 | logging.debug('%s(%s)', self.__class__.__name__, self._path) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 674 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 675 | def LocalPath(self): |
| 676 | """Returns the path of this file on the local disk relative to client root. |
| 677 | """ |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 678 | return normpath(self._path) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 679 | |
| 680 | def AbsoluteLocalPath(self): |
| 681 | """Returns the absolute path of this file on the local disk. |
| 682 | """ |
chase@chromium.org | 8e416c8 | 2009-10-06 04:30:44 +0000 | [diff] [blame] | 683 | return os.path.abspath(os.path.join(self._local_root, self.LocalPath())) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 684 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 685 | def Action(self): |
| 686 | """Returns the action on this opened file, e.g. A, M, D, etc.""" |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 687 | return self._action |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 688 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 689 | def IsTestableFile(self): |
maruel@chromium.org | 77c4f0f | 2009-05-29 18:53:04 +0000 | [diff] [blame] | 690 | """Returns True if the file is a text file and not a binary file. |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 691 | |
maruel@chromium.org | 77c4f0f | 2009-05-29 18:53:04 +0000 | [diff] [blame] | 692 | Deleted files are not text file.""" |
maruel@chromium.org | 1e08c00 | 2009-05-28 19:09:33 +0000 | [diff] [blame] | 693 | raise NotImplementedError() # Implement when needed |
| 694 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 695 | def IsTextFile(self): |
| 696 | """An alias to IsTestableFile for backwards compatibility.""" |
| 697 | return self.IsTestableFile() |
| 698 | |
Daniel Cheng | 7a1f04d | 2017-03-21 19:12:31 -0700 | [diff] [blame] | 699 | def OldContents(self): |
| 700 | """Returns an iterator over the lines in the old version of file. |
| 701 | |
Daniel Cheng | 2da34fe | 2017-03-21 20:42:12 -0700 | [diff] [blame] | 702 | The old version is the file before any modifications in the user's |
| 703 | workspace, i.e. the "left hand side". |
Daniel Cheng | 7a1f04d | 2017-03-21 19:12:31 -0700 | [diff] [blame] | 704 | |
| 705 | Contents will be empty if the file is a directory or does not exist. |
| 706 | Note: The carriage returns (LF or CR) are stripped off. |
| 707 | """ |
| 708 | return self._diff_cache.GetOldContents(self.LocalPath(), |
| 709 | self._local_root).splitlines() |
| 710 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 711 | def NewContents(self): |
| 712 | """Returns an iterator over the lines in the new version of file. |
| 713 | |
| 714 | The new version is the file in the user's workspace, i.e. the "right hand |
| 715 | side". |
| 716 | |
| 717 | Contents will be empty if the file is a directory or does not exist. |
nick@chromium.org | 2a3ab7e | 2011-04-27 22:06:27 +0000 | [diff] [blame] | 718 | Note: The carriage returns (LF or CR) are stripped off. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 719 | """ |
nick@chromium.org | 2a3ab7e | 2011-04-27 22:06:27 +0000 | [diff] [blame] | 720 | if self._cached_new_contents is None: |
| 721 | self._cached_new_contents = [] |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 722 | try: |
| 723 | self._cached_new_contents = gclient_utils.FileRead( |
| 724 | self.AbsoluteLocalPath(), 'rU').splitlines() |
| 725 | except IOError: |
| 726 | pass # File not found? That's fine; maybe it was deleted. |
nick@chromium.org | 2a3ab7e | 2011-04-27 22:06:27 +0000 | [diff] [blame] | 727 | return self._cached_new_contents[:] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 728 | |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 729 | def ChangedContents(self): |
| 730 | """Returns a list of tuples (line number, line text) of all new lines. |
| 731 | |
| 732 | This relies on the scm diff output describing each changed code section |
| 733 | with a line of the form |
| 734 | |
| 735 | ^@@ <old line num>,<old size> <new line num>,<new size> @@$ |
| 736 | """ |
nick@chromium.org | 2a3ab7e | 2011-04-27 22:06:27 +0000 | [diff] [blame] | 737 | if self._cached_changed_contents is not None: |
| 738 | return self._cached_changed_contents[:] |
| 739 | self._cached_changed_contents = [] |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 740 | line_num = 0 |
| 741 | |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 742 | for line in self.GenerateScmDiff().splitlines(): |
| 743 | m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) |
| 744 | if m: |
| 745 | line_num = int(m.groups(1)[0]) |
| 746 | continue |
| 747 | if line.startswith('+') and not line.startswith('++'): |
nick@chromium.org | 2a3ab7e | 2011-04-27 22:06:27 +0000 | [diff] [blame] | 748 | self._cached_changed_contents.append((line_num, line[1:])) |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 749 | if not line.startswith('-'): |
| 750 | line_num += 1 |
nick@chromium.org | 2a3ab7e | 2011-04-27 22:06:27 +0000 | [diff] [blame] | 751 | return self._cached_changed_contents[:] |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 752 | |
maruel@chromium.org | 5de1397 | 2009-06-10 18:16:06 +0000 | [diff] [blame] | 753 | def __str__(self): |
| 754 | return self.LocalPath() |
| 755 | |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 756 | def GenerateScmDiff(self): |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 757 | return self._diff_cache.GetDiff(self.LocalPath(), self._local_root) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 758 | |
maruel@chromium.org | 58407af | 2011-04-12 23:15:57 +0000 | [diff] [blame] | 759 | |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 760 | class GitAffectedFile(AffectedFile): |
| 761 | """Representation of a file in a change out of a git checkout.""" |
maruel@chromium.org | b17b55b | 2010-11-03 14:42:37 +0000 | [diff] [blame] | 762 | # Method 'NNN' is abstract in class 'NNN' but is not overridden |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 763 | # pylint: disable=abstract-method |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 764 | |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 765 | DIFF_CACHE = _GitDiffCache |
| 766 | |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 767 | def __init__(self, *args, **kwargs): |
| 768 | AffectedFile.__init__(self, *args, **kwargs) |
| 769 | self._server_path = None |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 770 | self._is_testable_file = None |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 771 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 772 | def IsTestableFile(self): |
| 773 | if self._is_testable_file is None: |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 774 | if self.Action() == 'D': |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 775 | # A deleted file is not testable. |
| 776 | self._is_testable_file = False |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 777 | else: |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 778 | self._is_testable_file = os.path.isfile(self.AbsoluteLocalPath()) |
| 779 | return self._is_testable_file |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 780 | |
maruel@chromium.org | c193875 | 2011-04-12 23:11:13 +0000 | [diff] [blame] | 781 | |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 782 | class Change(object): |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 783 | """Describe a change. |
| 784 | |
| 785 | Used directly by the presubmit scripts to query the current change being |
| 786 | tested. |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 787 | |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 788 | Instance members: |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 789 | tags: Dictionary of KEY=VALUE pairs found in the change description. |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 790 | self.KEY: equivalent to tags['KEY'] |
| 791 | """ |
| 792 | |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 793 | _AFFECTED_FILES = AffectedFile |
| 794 | |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 795 | # Matches key/value (or "tag") lines in changelist descriptions. |
maruel@chromium.org | 428342a | 2011-11-10 15:46:33 +0000 | [diff] [blame] | 796 | TAG_LINE_RE = re.compile( |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 797 | '^[ \t]*(?P<key>[A-Z][A-Z_0-9]*)[ \t]*=[ \t]*(?P<value>.*?)[ \t]*$') |
maruel@chromium.org | c193875 | 2011-04-12 23:11:13 +0000 | [diff] [blame] | 798 | scm = '' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 799 | |
maruel@chromium.org | 58407af | 2011-04-12 23:15:57 +0000 | [diff] [blame] | 800 | def __init__( |
agable@chromium.org | ea84ef1 | 2014-04-30 19:55:12 +0000 | [diff] [blame] | 801 | self, name, description, local_root, files, issue, patchset, author, |
| 802 | upstream=None): |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 803 | if files is None: |
| 804 | files = [] |
| 805 | self._name = name |
chase@chromium.org | 8e416c8 | 2009-10-06 04:30:44 +0000 | [diff] [blame] | 806 | # Convert root into an absolute path. |
| 807 | self._local_root = os.path.abspath(local_root) |
agable@chromium.org | ea84ef1 | 2014-04-30 19:55:12 +0000 | [diff] [blame] | 808 | self._upstream = upstream |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 809 | self.issue = issue |
| 810 | self.patchset = patchset |
maruel@chromium.org | 58407af | 2011-04-12 23:15:57 +0000 | [diff] [blame] | 811 | self.author_email = author |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 812 | |
isherman@chromium.org | b5cded6 | 2014-03-25 17:47:57 +0000 | [diff] [blame] | 813 | self._full_description = '' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 814 | self.tags = {} |
isherman@chromium.org | b5cded6 | 2014-03-25 17:47:57 +0000 | [diff] [blame] | 815 | self._description_without_tags = '' |
| 816 | self.SetDescriptionText(description) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 817 | |
maruel@chromium.org | e085d81 | 2011-10-10 19:49:15 +0000 | [diff] [blame] | 818 | assert all( |
| 819 | (isinstance(f, (list, tuple)) and len(f) == 2) for f in files), files |
| 820 | |
agable@chromium.org | ea84ef1 | 2014-04-30 19:55:12 +0000 | [diff] [blame] | 821 | diff_cache = self._AFFECTED_FILES.DIFF_CACHE(self._upstream) |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 822 | self._affected_files = [ |
nick@chromium.org | ff52619 | 2013-06-10 19:30:26 +0000 | [diff] [blame] | 823 | self._AFFECTED_FILES(path, action.strip(), self._local_root, diff_cache) |
| 824 | for action, path in files |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 825 | ] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 826 | |
maruel@chromium.org | 92022ec | 2009-06-11 01:59:28 +0000 | [diff] [blame] | 827 | def Name(self): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 828 | """Returns the change name.""" |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 829 | return self._name |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 830 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 831 | def DescriptionText(self): |
| 832 | """Returns the user-entered changelist description, minus tags. |
| 833 | |
| 834 | Any line in the user-provided description starting with e.g. "FOO=" |
| 835 | (whitespace permitted before and around) is considered a tag line. Such |
| 836 | lines are stripped out of the description this function returns. |
| 837 | """ |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 838 | return self._description_without_tags |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 839 | |
| 840 | def FullDescriptionText(self): |
| 841 | """Returns the complete changelist description including tags.""" |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 842 | return self._full_description |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 843 | |
isherman@chromium.org | b5cded6 | 2014-03-25 17:47:57 +0000 | [diff] [blame] | 844 | def SetDescriptionText(self, description): |
| 845 | """Sets the full description text (including tags) to |description|. |
pgervais@chromium.org | 92c3009 | 2014-04-15 00:30:37 +0000 | [diff] [blame] | 846 | |
isherman@chromium.org | b5cded6 | 2014-03-25 17:47:57 +0000 | [diff] [blame] | 847 | Also updates the list of tags.""" |
| 848 | self._full_description = description |
| 849 | |
| 850 | # From the description text, build up a dictionary of key/value pairs |
| 851 | # plus the description minus all key/value or "tag" lines. |
| 852 | description_without_tags = [] |
| 853 | self.tags = {} |
| 854 | for line in self._full_description.splitlines(): |
| 855 | m = self.TAG_LINE_RE.match(line) |
| 856 | if m: |
| 857 | self.tags[m.group('key')] = m.group('value') |
| 858 | else: |
| 859 | description_without_tags.append(line) |
| 860 | |
| 861 | # Change back to text and remove whitespace at end. |
| 862 | self._description_without_tags = ( |
| 863 | '\n'.join(description_without_tags).rstrip()) |
| 864 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 865 | def RepositoryRoot(self): |
maruel@chromium.org | 92022ec | 2009-06-11 01:59:28 +0000 | [diff] [blame] | 866 | """Returns the repository (checkout) root directory for this change, |
| 867 | as an absolute path. |
| 868 | """ |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 869 | return self._local_root |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 870 | |
| 871 | def __getattr__(self, attr): |
maruel@chromium.org | 92022ec | 2009-06-11 01:59:28 +0000 | [diff] [blame] | 872 | """Return tags directly as attributes on the object.""" |
| 873 | if not re.match(r"^[A-Z_]*$", attr): |
| 874 | raise AttributeError(self, attr) |
maruel@chromium.org | e1a524f | 2009-05-27 14:43:46 +0000 | [diff] [blame] | 875 | return self.tags.get(attr) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 876 | |
agable@chromium.org | 40a3d0b | 2014-05-15 01:59:16 +0000 | [diff] [blame] | 877 | def AllFiles(self, root=None): |
| 878 | """List all files under source control in the repo.""" |
| 879 | raise NotImplementedError() |
| 880 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 881 | def AffectedFiles(self, include_deletes=True, file_filter=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 882 | """Returns a list of AffectedFile instances for all files in the change. |
| 883 | |
| 884 | Args: |
| 885 | include_deletes: If false, deleted files will be filtered out. |
sail@chromium.org | 5538e02 | 2011-05-12 17:53:16 +0000 | [diff] [blame] | 886 | file_filter: An additional filter to apply. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 887 | |
| 888 | Returns: |
| 889 | [AffectedFile(path, action), AffectedFile(path, action)] |
| 890 | """ |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 891 | affected = filter(file_filter, self._affected_files) |
sail@chromium.org | 5538e02 | 2011-05-12 17:53:16 +0000 | [diff] [blame] | 892 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 893 | if include_deletes: |
| 894 | return affected |
Lei Zhang | 9611c4c | 2017-04-04 01:41:56 -0700 | [diff] [blame] | 895 | return filter(lambda x: x.Action() != 'D', affected) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 896 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 897 | def AffectedTestableFiles(self, include_deletes=None): |
maruel@chromium.org | 77c4f0f | 2009-05-29 18:53:04 +0000 | [diff] [blame] | 898 | """Return a list of the existing text files in a change.""" |
| 899 | if include_deletes is not None: |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 900 | warn("AffectedTeestableFiles(include_deletes=%s)" |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 901 | " is deprecated and ignored" % str(include_deletes), |
| 902 | category=DeprecationWarning, |
| 903 | stacklevel=2) |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 904 | return filter(lambda x: x.IsTestableFile(), |
| 905 | self.AffectedFiles(include_deletes=False)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 906 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 907 | def AffectedTextFiles(self, include_deletes=None): |
| 908 | """An alias to AffectedTestableFiles for backwards compatibility.""" |
| 909 | return self.AffectedTestableFiles(include_deletes=include_deletes) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 910 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 911 | def LocalPaths(self): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 912 | """Convenience function.""" |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 913 | return [af.LocalPath() for af in self.AffectedFiles()] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 914 | |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 915 | def AbsoluteLocalPaths(self): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 916 | """Convenience function.""" |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 917 | return [af.AbsoluteLocalPath() for af in self.AffectedFiles()] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 918 | |
| 919 | def RightHandSideLines(self): |
| 920 | """An iterator over all text lines in "new" version of changed files. |
| 921 | |
| 922 | Lists lines from new or modified text files in the change. |
| 923 | |
| 924 | This is useful for doing line-by-line regex checks, like checking for |
| 925 | trailing whitespace. |
| 926 | |
| 927 | Yields: |
| 928 | a 3 tuple: |
| 929 | the AffectedFile instance of the current file; |
| 930 | integer line number (1-based); and |
| 931 | the contents of the line as a string. |
| 932 | """ |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 933 | return _RightHandSideLinesImpl( |
| 934 | x for x in self.AffectedFiles(include_deletes=False) |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 935 | if x.IsTestableFile()) |
agable@chromium.org | 40a3d0b | 2014-05-15 01:59:16 +0000 | [diff] [blame] | 936 | |
Jochen Eisinger | d0573ec | 2017-04-13 10:55:06 +0200 | [diff] [blame] | 937 | def OriginalOwnersFiles(self): |
| 938 | """A map from path names of affected OWNERS files to their old content.""" |
| 939 | def owners_file_filter(f): |
| 940 | return 'OWNERS' in os.path.split(f.LocalPath())[1] |
| 941 | files = self.AffectedFiles(file_filter=owners_file_filter) |
| 942 | return dict([(f.LocalPath(), f.OldContents()) for f in files]) |
| 943 | |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 944 | |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 945 | class GitChange(Change): |
| 946 | _AFFECTED_FILES = GitAffectedFile |
maruel@chromium.org | c193875 | 2011-04-12 23:11:13 +0000 | [diff] [blame] | 947 | scm = 'git' |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 948 | |
agable@chromium.org | 40a3d0b | 2014-05-15 01:59:16 +0000 | [diff] [blame] | 949 | def AllFiles(self, root=None): |
| 950 | """List all files under source control in the repo.""" |
| 951 | root = root or self.RepositoryRoot() |
| 952 | return subprocess.check_output( |
| 953 | ['git', 'ls-files', '--', '.'], cwd=root).splitlines() |
| 954 | |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 955 | |
maruel@chromium.org | 4661e0c | 2009-06-04 00:45:26 +0000 | [diff] [blame] | 956 | def ListRelevantPresubmitFiles(files, root): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 957 | """Finds all presubmit files that apply to a given set of source files. |
| 958 | |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 959 | If inherit-review-settings-ok is present right under root, looks for |
| 960 | PRESUBMIT.py in directories enclosing root. |
| 961 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 962 | Args: |
| 963 | files: An iterable container containing file paths. |
maruel@chromium.org | 4661e0c | 2009-06-04 00:45:26 +0000 | [diff] [blame] | 964 | root: Path where to stop searching. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 965 | |
| 966 | Return: |
maruel@chromium.org | 4661e0c | 2009-06-04 00:45:26 +0000 | [diff] [blame] | 967 | List of absolute paths of the existing PRESUBMIT.py scripts. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 968 | """ |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 969 | files = [normpath(os.path.join(root, f)) for f in files] |
| 970 | |
| 971 | # List all the individual directories containing files. |
| 972 | directories = set([os.path.dirname(f) for f in files]) |
| 973 | |
| 974 | # Ignore root if inherit-review-settings-ok is present. |
| 975 | if os.path.isfile(os.path.join(root, 'inherit-review-settings-ok')): |
| 976 | root = None |
| 977 | |
| 978 | # Collect all unique directories that may contain PRESUBMIT.py. |
| 979 | candidates = set() |
| 980 | for directory in directories: |
| 981 | while True: |
| 982 | if directory in candidates: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 983 | break |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 984 | candidates.add(directory) |
| 985 | if directory == root: |
maruel@chromium.org | 4661e0c | 2009-06-04 00:45:26 +0000 | [diff] [blame] | 986 | break |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 987 | parent_dir = os.path.dirname(directory) |
| 988 | if parent_dir == directory: |
| 989 | # We hit the system root directory. |
| 990 | break |
| 991 | directory = parent_dir |
| 992 | |
| 993 | # Look for PRESUBMIT.py in all candidate directories. |
| 994 | results = [] |
| 995 | for directory in sorted(list(candidates)): |
tobiasjs | ff061c0 | 2016-08-17 03:23:57 -0700 | [diff] [blame] | 996 | try: |
| 997 | for f in os.listdir(directory): |
| 998 | p = os.path.join(directory, f) |
| 999 | if os.path.isfile(p) and re.match( |
| 1000 | r'PRESUBMIT.*\.py$', f) and not f.startswith('PRESUBMIT_test'): |
| 1001 | results.append(p) |
| 1002 | except OSError: |
| 1003 | pass |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 1004 | |
tobiasjs | 2836bcf | 2016-08-16 04:08:16 -0700 | [diff] [blame] | 1005 | logging.debug('Presubmit files: %s', ','.join(results)) |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 1006 | return results |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1007 | |
| 1008 | |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 1009 | class GetTryMastersExecuter(object): |
| 1010 | @staticmethod |
| 1011 | def ExecPresubmitScript(script_text, presubmit_path, project, change): |
| 1012 | """Executes GetPreferredTryMasters() from a single presubmit script. |
| 1013 | |
| 1014 | Args: |
| 1015 | script_text: The text of the presubmit script. |
| 1016 | presubmit_path: Project script to run. |
| 1017 | project: Project name to pass to presubmit script for bot selection. |
| 1018 | |
| 1019 | Return: |
| 1020 | A map of try masters to map of builders to set of tests. |
| 1021 | """ |
| 1022 | context = {} |
| 1023 | try: |
| 1024 | exec script_text in context |
| 1025 | except Exception, e: |
| 1026 | raise PresubmitFailure('"%s" had an exception.\n%s' |
| 1027 | % (presubmit_path, e)) |
| 1028 | |
| 1029 | function_name = 'GetPreferredTryMasters' |
| 1030 | if function_name not in context: |
| 1031 | return {} |
| 1032 | get_preferred_try_masters = context[function_name] |
| 1033 | if not len(inspect.getargspec(get_preferred_try_masters)[0]) == 2: |
| 1034 | raise PresubmitFailure( |
| 1035 | 'Expected function "GetPreferredTryMasters" to take two arguments.') |
| 1036 | return get_preferred_try_masters(project, change) |
| 1037 | |
| 1038 | |
rmistry@google.com | 5626a92 | 2015-02-26 14:03:30 +0000 | [diff] [blame] | 1039 | class GetPostUploadExecuter(object): |
| 1040 | @staticmethod |
| 1041 | def ExecPresubmitScript(script_text, presubmit_path, cl, change): |
| 1042 | """Executes PostUploadHook() from a single presubmit script. |
| 1043 | |
| 1044 | Args: |
| 1045 | script_text: The text of the presubmit script. |
| 1046 | presubmit_path: Project script to run. |
| 1047 | cl: The Changelist object. |
| 1048 | change: The Change object. |
| 1049 | |
| 1050 | Return: |
| 1051 | A list of results objects. |
| 1052 | """ |
| 1053 | context = {} |
| 1054 | try: |
| 1055 | exec script_text in context |
| 1056 | except Exception, e: |
| 1057 | raise PresubmitFailure('"%s" had an exception.\n%s' |
| 1058 | % (presubmit_path, e)) |
| 1059 | |
| 1060 | function_name = 'PostUploadHook' |
| 1061 | if function_name not in context: |
| 1062 | return {} |
| 1063 | post_upload_hook = context[function_name] |
| 1064 | if not len(inspect.getargspec(post_upload_hook)[0]) == 3: |
| 1065 | raise PresubmitFailure( |
| 1066 | 'Expected function "PostUploadHook" to take three arguments.') |
| 1067 | return post_upload_hook(cl, change, OutputApi(False)) |
| 1068 | |
| 1069 | |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 1070 | def _MergeMasters(masters1, masters2): |
| 1071 | """Merges two master maps. Merges also the tests of each builder.""" |
| 1072 | result = {} |
| 1073 | for (master, builders) in itertools.chain(masters1.iteritems(), |
| 1074 | masters2.iteritems()): |
| 1075 | new_builders = result.setdefault(master, {}) |
| 1076 | for (builder, tests) in builders.iteritems(): |
| 1077 | new_builders.setdefault(builder, set([])).update(tests) |
| 1078 | return result |
| 1079 | |
| 1080 | |
| 1081 | def DoGetTryMasters(change, |
| 1082 | changed_files, |
| 1083 | repository_root, |
| 1084 | default_presubmit, |
| 1085 | project, |
| 1086 | verbose, |
| 1087 | output_stream): |
| 1088 | """Get the list of try masters from the presubmit scripts. |
| 1089 | |
| 1090 | Args: |
| 1091 | changed_files: List of modified files. |
| 1092 | repository_root: The repository root. |
| 1093 | default_presubmit: A default presubmit script to execute in any case. |
| 1094 | project: Optional name of a project used in selecting trybots. |
| 1095 | verbose: Prints debug info. |
| 1096 | output_stream: A stream to write debug output to. |
| 1097 | |
| 1098 | Return: |
| 1099 | Map of try masters to map of builders to set of tests. |
| 1100 | """ |
| 1101 | presubmit_files = ListRelevantPresubmitFiles(changed_files, repository_root) |
| 1102 | if not presubmit_files and verbose: |
| 1103 | output_stream.write("Warning, no PRESUBMIT.py found.\n") |
| 1104 | results = {} |
| 1105 | executer = GetTryMastersExecuter() |
| 1106 | |
| 1107 | if default_presubmit: |
| 1108 | if verbose: |
| 1109 | output_stream.write("Running default presubmit script.\n") |
| 1110 | fake_path = os.path.join(repository_root, 'PRESUBMIT.py') |
| 1111 | results = _MergeMasters(results, executer.ExecPresubmitScript( |
| 1112 | default_presubmit, fake_path, project, change)) |
| 1113 | for filename in presubmit_files: |
| 1114 | filename = os.path.abspath(filename) |
| 1115 | if verbose: |
| 1116 | output_stream.write("Running %s\n" % filename) |
| 1117 | # Accept CRLF presubmit script. |
| 1118 | presubmit_script = gclient_utils.FileRead(filename, 'rU') |
| 1119 | results = _MergeMasters(results, executer.ExecPresubmitScript( |
| 1120 | presubmit_script, filename, project, change)) |
| 1121 | |
| 1122 | # Make sets to lists again for later JSON serialization. |
| 1123 | for builders in results.itervalues(): |
| 1124 | for builder in builders: |
| 1125 | builders[builder] = list(builders[builder]) |
| 1126 | |
| 1127 | if results and verbose: |
| 1128 | output_stream.write('%s\n' % str(results)) |
| 1129 | return results |
| 1130 | |
| 1131 | |
rmistry@google.com | 5626a92 | 2015-02-26 14:03:30 +0000 | [diff] [blame] | 1132 | def DoPostUploadExecuter(change, |
| 1133 | cl, |
| 1134 | repository_root, |
| 1135 | verbose, |
| 1136 | output_stream): |
| 1137 | """Execute the post upload hook. |
| 1138 | |
| 1139 | Args: |
| 1140 | change: The Change object. |
| 1141 | cl: The Changelist object. |
| 1142 | repository_root: The repository root. |
| 1143 | verbose: Prints debug info. |
| 1144 | output_stream: A stream to write debug output to. |
| 1145 | """ |
| 1146 | presubmit_files = ListRelevantPresubmitFiles( |
| 1147 | change.LocalPaths(), repository_root) |
| 1148 | if not presubmit_files and verbose: |
| 1149 | output_stream.write("Warning, no PRESUBMIT.py found.\n") |
| 1150 | results = [] |
| 1151 | executer = GetPostUploadExecuter() |
| 1152 | # The root presubmit file should be executed after the ones in subdirectories. |
| 1153 | # i.e. the specific post upload hooks should run before the general ones. |
| 1154 | # Thus, reverse the order provided by ListRelevantPresubmitFiles. |
| 1155 | presubmit_files.reverse() |
| 1156 | |
| 1157 | for filename in presubmit_files: |
| 1158 | filename = os.path.abspath(filename) |
| 1159 | if verbose: |
| 1160 | output_stream.write("Running %s\n" % filename) |
| 1161 | # Accept CRLF presubmit script. |
| 1162 | presubmit_script = gclient_utils.FileRead(filename, 'rU') |
| 1163 | results.extend(executer.ExecPresubmitScript( |
| 1164 | presubmit_script, filename, cl, change)) |
| 1165 | output_stream.write('\n') |
| 1166 | if results: |
| 1167 | output_stream.write('** Post Upload Hook Messages **\n') |
| 1168 | for result in results: |
| 1169 | result.handle(output_stream) |
| 1170 | output_stream.write('\n') |
| 1171 | |
| 1172 | return results |
| 1173 | |
| 1174 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1175 | class PresubmitExecuter(object): |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1176 | def __init__(self, change, committing, rietveld_obj, verbose, |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1177 | gerrit_obj=None, dry_run=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1178 | """ |
| 1179 | Args: |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 1180 | change: The Change object. |
agable | 92bec4f | 2016-08-24 09:27:27 -0700 | [diff] [blame] | 1181 | committing: True if 'git cl land' is running, False if 'git cl upload' is. |
maruel@chromium.org | 239f411 | 2011-06-03 20:08:23 +0000 | [diff] [blame] | 1182 | rietveld_obj: rietveld.Rietveld client object. |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1183 | gerrit_obj: provides basic Gerrit codereview functionality. |
| 1184 | dry_run: if true, some Checks will be skipped. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1185 | """ |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 1186 | self.change = change |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1187 | self.committing = committing |
maruel@chromium.org | 239f411 | 2011-06-03 20:08:23 +0000 | [diff] [blame] | 1188 | self.rietveld = rietveld_obj |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1189 | self.gerrit = gerrit_obj |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1190 | self.verbose = verbose |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1191 | self.dry_run = dry_run |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1192 | |
| 1193 | def ExecPresubmitScript(self, script_text, presubmit_path): |
| 1194 | """Executes a single presubmit script. |
| 1195 | |
| 1196 | Args: |
| 1197 | script_text: The text of the presubmit script. |
| 1198 | presubmit_path: The path to the presubmit file (this will be reported via |
| 1199 | input_api.PresubmitLocalPath()). |
| 1200 | |
| 1201 | Return: |
| 1202 | A list of result objects, empty if no problems. |
| 1203 | """ |
thakis@chromium.org | c6ef53a | 2014-11-04 00:13:54 +0000 | [diff] [blame] | 1204 | |
chase@chromium.org | 8e416c8 | 2009-10-06 04:30:44 +0000 | [diff] [blame] | 1205 | # Change to the presubmit file's directory to support local imports. |
| 1206 | main_path = os.getcwd() |
| 1207 | os.chdir(os.path.dirname(presubmit_path)) |
| 1208 | |
| 1209 | # Load the presubmit script into context. |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1210 | input_api = InputApi(self.change, presubmit_path, self.committing, |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1211 | self.rietveld, self.verbose, |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1212 | gerrit_obj=self.gerrit, dry_run=self.dry_run) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1213 | context = {} |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1214 | try: |
| 1215 | exec script_text in context |
| 1216 | except Exception, e: |
| 1217 | raise PresubmitFailure('"%s" had an exception.\n%s' % (presubmit_path, e)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1218 | |
| 1219 | # These function names must change if we make substantial changes to |
| 1220 | # the presubmit API that are not backwards compatible. |
| 1221 | if self.committing: |
| 1222 | function_name = 'CheckChangeOnCommit' |
| 1223 | else: |
| 1224 | function_name = 'CheckChangeOnUpload' |
| 1225 | if function_name in context: |
wez@chromium.org | a6d011e | 2013-03-26 17:31:49 +0000 | [diff] [blame] | 1226 | context['__args'] = (input_api, OutputApi(self.committing)) |
tobiasjs | 2836bcf | 2016-08-16 04:08:16 -0700 | [diff] [blame] | 1227 | logging.debug('Running %s in %s', function_name, presubmit_path) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1228 | result = eval(function_name + '(*__args)', context) |
tobiasjs | 2836bcf | 2016-08-16 04:08:16 -0700 | [diff] [blame] | 1229 | logging.debug('Running %s done.', function_name) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1230 | if not (isinstance(result, types.TupleType) or |
| 1231 | isinstance(result, types.ListType)): |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1232 | raise PresubmitFailure( |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1233 | 'Presubmit functions must return a tuple or list') |
| 1234 | for item in result: |
| 1235 | if not isinstance(item, OutputApi.PresubmitResult): |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1236 | raise PresubmitFailure( |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1237 | 'All presubmit results must be of types derived from ' |
| 1238 | 'output_api.PresubmitResult') |
| 1239 | else: |
| 1240 | result = () # no error since the script doesn't care about current event. |
| 1241 | |
scottmg | 86099d7 | 2016-09-01 09:16:51 -0700 | [diff] [blame] | 1242 | input_api.ShutdownPool() |
| 1243 | |
chase@chromium.org | 8e416c8 | 2009-10-06 04:30:44 +0000 | [diff] [blame] | 1244 | # Return the process to the original working directory. |
| 1245 | os.chdir(main_path) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1246 | return result |
| 1247 | |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 1248 | def DoPresubmitChecks(change, |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1249 | committing, |
| 1250 | verbose, |
| 1251 | output_stream, |
maruel@chromium.org | 0ff1fab | 2009-05-22 13:08:15 +0000 | [diff] [blame] | 1252 | input_stream, |
maruel@chromium.org | b0dfd35 | 2009-06-10 14:12:54 +0000 | [diff] [blame] | 1253 | default_presubmit, |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1254 | may_prompt, |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1255 | rietveld_obj, |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1256 | gerrit_obj=None, |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1257 | dry_run=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1258 | """Runs all presubmit checks that apply to the files in the change. |
| 1259 | |
| 1260 | This finds all PRESUBMIT.py files in directories enclosing the files in the |
| 1261 | change (up to the repository root) and calls the relevant entrypoint function |
| 1262 | depending on whether the change is being committed or uploaded. |
| 1263 | |
| 1264 | Prints errors, warnings and notifications. Prompts the user for warnings |
| 1265 | when needed. |
| 1266 | |
| 1267 | Args: |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 1268 | change: The Change object. |
agable | 92bec4f | 2016-08-24 09:27:27 -0700 | [diff] [blame] | 1269 | committing: True if 'git cl land' is running, False if 'git cl upload' is. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1270 | verbose: Prints debug info. |
| 1271 | output_stream: A stream to write output from presubmit tests to. |
| 1272 | input_stream: A stream to read input from the user. |
maruel@chromium.org | 0ff1fab | 2009-05-22 13:08:15 +0000 | [diff] [blame] | 1273 | default_presubmit: A default presubmit script to execute in any case. |
Quinten Yearsley | 516fe7f | 2016-12-14 11:50:18 -0800 | [diff] [blame] | 1274 | may_prompt: Enable (y/n) questions on warning or error. If False, |
| 1275 | any questions are answered with yes by default. |
maruel@chromium.org | 239f411 | 2011-06-03 20:08:23 +0000 | [diff] [blame] | 1276 | rietveld_obj: rietveld.Rietveld object. |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1277 | gerrit_obj: provides basic Gerrit codereview functionality. |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1278 | dry_run: if true, some Checks will be skipped. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1279 | |
maruel@chromium.org | ce8e46b | 2009-06-26 22:31:51 +0000 | [diff] [blame] | 1280 | Warning: |
| 1281 | If may_prompt is true, output_stream SHOULD be sys.stdout and input_stream |
| 1282 | SHOULD be sys.stdin. |
| 1283 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1284 | Return: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1285 | A PresubmitOutput object. Use output.should_continue() to figure out |
| 1286 | if there were errors or warnings and the caller should abort. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1287 | """ |
maruel@chromium.org | ea7c855 | 2011-04-18 14:12:07 +0000 | [diff] [blame] | 1288 | old_environ = os.environ |
| 1289 | try: |
| 1290 | # Make sure python subprocesses won't generate .pyc files. |
| 1291 | os.environ = os.environ.copy() |
| 1292 | os.environ['PYTHONDONTWRITEBYTECODE'] = '1' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1293 | |
maruel@chromium.org | ea7c855 | 2011-04-18 14:12:07 +0000 | [diff] [blame] | 1294 | output = PresubmitOutput(input_stream, output_stream) |
| 1295 | if committing: |
| 1296 | output.write("Running presubmit commit checks ...\n") |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1297 | else: |
maruel@chromium.org | ea7c855 | 2011-04-18 14:12:07 +0000 | [diff] [blame] | 1298 | output.write("Running presubmit upload checks ...\n") |
| 1299 | start_time = time.time() |
| 1300 | presubmit_files = ListRelevantPresubmitFiles( |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 1301 | change.AbsoluteLocalPaths(), change.RepositoryRoot()) |
maruel@chromium.org | ea7c855 | 2011-04-18 14:12:07 +0000 | [diff] [blame] | 1302 | if not presubmit_files and verbose: |
maruel@chromium.org | fae707b | 2011-09-15 18:57:58 +0000 | [diff] [blame] | 1303 | output.write("Warning, no PRESUBMIT.py found.\n") |
maruel@chromium.org | ea7c855 | 2011-04-18 14:12:07 +0000 | [diff] [blame] | 1304 | results = [] |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1305 | executer = PresubmitExecuter(change, committing, rietveld_obj, verbose, |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1306 | gerrit_obj, dry_run) |
maruel@chromium.org | ea7c855 | 2011-04-18 14:12:07 +0000 | [diff] [blame] | 1307 | if default_presubmit: |
| 1308 | if verbose: |
| 1309 | output.write("Running default presubmit script.\n") |
| 1310 | fake_path = os.path.join(change.RepositoryRoot(), 'PRESUBMIT.py') |
| 1311 | results += executer.ExecPresubmitScript(default_presubmit, fake_path) |
| 1312 | for filename in presubmit_files: |
| 1313 | filename = os.path.abspath(filename) |
| 1314 | if verbose: |
| 1315 | output.write("Running %s\n" % filename) |
| 1316 | # Accept CRLF presubmit script. |
| 1317 | presubmit_script = gclient_utils.FileRead(filename, 'rU') |
| 1318 | results += executer.ExecPresubmitScript(presubmit_script, filename) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1319 | |
maruel@chromium.org | ea7c855 | 2011-04-18 14:12:07 +0000 | [diff] [blame] | 1320 | errors = [] |
| 1321 | notifications = [] |
| 1322 | warnings = [] |
| 1323 | for result in results: |
| 1324 | if result.fatal: |
| 1325 | errors.append(result) |
| 1326 | elif result.should_prompt: |
| 1327 | warnings.append(result) |
| 1328 | else: |
| 1329 | notifications.append(result) |
pam@chromium.org | ed9a083 | 2009-09-09 22:48:55 +0000 | [diff] [blame] | 1330 | |
maruel@chromium.org | ea7c855 | 2011-04-18 14:12:07 +0000 | [diff] [blame] | 1331 | output.write('\n') |
| 1332 | for name, items in (('Messages', notifications), |
| 1333 | ('Warnings', warnings), |
| 1334 | ('ERRORS', errors)): |
| 1335 | if items: |
| 1336 | output.write('** Presubmit %s **\n' % name) |
| 1337 | for item in items: |
| 1338 | item.handle(output) |
| 1339 | output.write('\n') |
pam@chromium.org | ed9a083 | 2009-09-09 22:48:55 +0000 | [diff] [blame] | 1340 | |
maruel@chromium.org | ea7c855 | 2011-04-18 14:12:07 +0000 | [diff] [blame] | 1341 | total_time = time.time() - start_time |
| 1342 | if total_time > 1.0: |
| 1343 | output.write("Presubmit checks took %.1fs to calculate.\n\n" % total_time) |
maruel@chromium.org | ce8e46b | 2009-06-26 22:31:51 +0000 | [diff] [blame] | 1344 | |
Quinten Yearsley | 516fe7f | 2016-12-14 11:50:18 -0800 | [diff] [blame] | 1345 | if errors: |
| 1346 | output.fail() |
| 1347 | elif warnings: |
| 1348 | output.write('There were presubmit warnings. ') |
| 1349 | if may_prompt: |
| 1350 | output.prompt_yes_no('Are you sure you wish to continue? (y/N): ') |
| 1351 | else: |
| 1352 | output.write('Presubmit checks passed.\n') |
maruel@chromium.org | ea7c855 | 2011-04-18 14:12:07 +0000 | [diff] [blame] | 1353 | |
| 1354 | global _ASKED_FOR_FEEDBACK |
| 1355 | # Ask for feedback one time out of 5. |
| 1356 | if (len(results) and random.randint(0, 4) == 0 and not _ASKED_FOR_FEEDBACK): |
maruel@chromium.org | 1ce8e66 | 2014-01-14 15:23:00 +0000 | [diff] [blame] | 1357 | output.write( |
| 1358 | 'Was the presubmit check useful? If not, run "git cl presubmit -v"\n' |
| 1359 | 'to figure out which PRESUBMIT.py was run, then run git blame\n' |
| 1360 | 'on the file to figure out who to ask for help.\n') |
maruel@chromium.org | ea7c855 | 2011-04-18 14:12:07 +0000 | [diff] [blame] | 1361 | _ASKED_FOR_FEEDBACK = True |
| 1362 | return output |
| 1363 | finally: |
| 1364 | os.environ = old_environ |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1365 | |
| 1366 | |
| 1367 | def ScanSubDirs(mask, recursive): |
| 1368 | if not recursive: |
pgervais@chromium.org | e57b09d | 2014-05-07 00:58:13 +0000 | [diff] [blame] | 1369 | return [x for x in glob.glob(mask) if x not in ('.svn', '.git')] |
Lei Zhang | 9611c4c | 2017-04-04 01:41:56 -0700 | [diff] [blame] | 1370 | |
| 1371 | results = [] |
| 1372 | for root, dirs, files in os.walk('.'): |
| 1373 | if '.svn' in dirs: |
| 1374 | dirs.remove('.svn') |
| 1375 | if '.git' in dirs: |
| 1376 | dirs.remove('.git') |
| 1377 | for name in files: |
| 1378 | if fnmatch.fnmatch(name, mask): |
| 1379 | results.append(os.path.join(root, name)) |
| 1380 | return results |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1381 | |
| 1382 | |
| 1383 | def ParseFiles(args, recursive): |
tobiasjs | 2836bcf | 2016-08-16 04:08:16 -0700 | [diff] [blame] | 1384 | logging.debug('Searching for %s', args) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1385 | files = [] |
| 1386 | for arg in args: |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 1387 | files.extend([('M', f) for f in ScanSubDirs(arg, recursive)]) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1388 | return files |
| 1389 | |
| 1390 | |
maruel@chromium.org | 5c8c6de | 2011-03-18 16:20:18 +0000 | [diff] [blame] | 1391 | def load_files(options, args): |
| 1392 | """Tries to determine the SCM.""" |
maruel@chromium.org | 5c8c6de | 2011-03-18 16:20:18 +0000 | [diff] [blame] | 1393 | files = [] |
maruel@chromium.org | 5c8c6de | 2011-03-18 16:20:18 +0000 | [diff] [blame] | 1394 | if args: |
| 1395 | files = ParseFiles(args, options.recursive) |
agable | 0b65e73 | 2016-11-22 09:25:46 -0800 | [diff] [blame] | 1396 | change_scm = scm.determine_scm(options.root) |
| 1397 | if change_scm == 'git': |
maruel@chromium.org | 9b31f16 | 2012-01-26 19:02:31 +0000 | [diff] [blame] | 1398 | change_class = GitChange |
agable@chromium.org | 2da1ade | 2014-04-30 17:40:45 +0000 | [diff] [blame] | 1399 | upstream = options.upstream or None |
maruel@chromium.org | 9b31f16 | 2012-01-26 19:02:31 +0000 | [diff] [blame] | 1400 | if not files: |
agable@chromium.org | 2da1ade | 2014-04-30 17:40:45 +0000 | [diff] [blame] | 1401 | files = scm.GIT.CaptureStatus([], options.root, upstream) |
maruel@chromium.org | 5c8c6de | 2011-03-18 16:20:18 +0000 | [diff] [blame] | 1402 | else: |
tobiasjs | 2836bcf | 2016-08-16 04:08:16 -0700 | [diff] [blame] | 1403 | logging.info('Doesn\'t seem under source control. Got %d files', len(args)) |
maruel@chromium.org | 9b31f16 | 2012-01-26 19:02:31 +0000 | [diff] [blame] | 1404 | if not files: |
| 1405 | return None, None |
| 1406 | change_class = Change |
maruel@chromium.org | 5c8c6de | 2011-03-18 16:20:18 +0000 | [diff] [blame] | 1407 | return change_class, files |
| 1408 | |
| 1409 | |
iannucci@chromium.org | 8a4a2bc | 2013-03-08 08:13:20 +0000 | [diff] [blame] | 1410 | class NonexistantCannedCheckFilter(Exception): |
| 1411 | pass |
| 1412 | |
| 1413 | |
| 1414 | @contextlib.contextmanager |
| 1415 | def canned_check_filter(method_names): |
| 1416 | filtered = {} |
| 1417 | try: |
| 1418 | for method_name in method_names: |
| 1419 | if not hasattr(presubmit_canned_checks, method_name): |
| 1420 | raise NonexistantCannedCheckFilter(method_name) |
| 1421 | filtered[method_name] = getattr(presubmit_canned_checks, method_name) |
| 1422 | setattr(presubmit_canned_checks, method_name, lambda *_a, **_kw: []) |
| 1423 | yield |
| 1424 | finally: |
| 1425 | for name, method in filtered.iteritems(): |
| 1426 | setattr(presubmit_canned_checks, name, method) |
| 1427 | |
maruel@chromium.org | ffeb2f3 | 2013-12-03 13:55:22 +0000 | [diff] [blame] | 1428 | |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 1429 | def CallCommand(cmd_data): |
maruel@chromium.org | ffeb2f3 | 2013-12-03 13:55:22 +0000 | [diff] [blame] | 1430 | """Runs an external program, potentially from a child process created by the |
| 1431 | multiprocessing module. |
| 1432 | |
| 1433 | multiprocessing needs a top level function with a single argument. |
| 1434 | """ |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 1435 | cmd_data.kwargs['stdout'] = subprocess.PIPE |
| 1436 | cmd_data.kwargs['stderr'] = subprocess.STDOUT |
| 1437 | try: |
maruel@chromium.org | ffeb2f3 | 2013-12-03 13:55:22 +0000 | [diff] [blame] | 1438 | start = time.time() |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 1439 | (out, _), code = subprocess.communicate(cmd_data.cmd, **cmd_data.kwargs) |
maruel@chromium.org | ffeb2f3 | 2013-12-03 13:55:22 +0000 | [diff] [blame] | 1440 | duration = time.time() - start |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 1441 | except OSError as e: |
maruel@chromium.org | ffeb2f3 | 2013-12-03 13:55:22 +0000 | [diff] [blame] | 1442 | duration = time.time() - start |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 1443 | return cmd_data.message( |
maruel@chromium.org | ffeb2f3 | 2013-12-03 13:55:22 +0000 | [diff] [blame] | 1444 | '%s exec failure (%4.2fs)\n %s' % (cmd_data.name, duration, e)) |
| 1445 | if code != 0: |
| 1446 | return cmd_data.message( |
| 1447 | '%s (%4.2fs) failed\n%s' % (cmd_data.name, duration, out)) |
| 1448 | if cmd_data.info: |
| 1449 | return cmd_data.info('%s (%4.2fs)' % (cmd_data.name, duration)) |
ilevy@chromium.org | bc11731 | 2013-04-20 03:57:56 +0000 | [diff] [blame] | 1450 | |
iannucci@chromium.org | 8a4a2bc | 2013-03-08 08:13:20 +0000 | [diff] [blame] | 1451 | |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 1452 | def main(argv=None): |
maruel@chromium.org | 5c8c6de | 2011-03-18 16:20:18 +0000 | [diff] [blame] | 1453 | parser = optparse.OptionParser(usage="%prog [options] <files...>", |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1454 | version="%prog " + str(__version__)) |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1455 | parser.add_option("-c", "--commit", action="store_true", default=False, |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1456 | help="Use commit instead of upload checks") |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1457 | parser.add_option("-u", "--upload", action="store_false", dest='commit', |
| 1458 | help="Use upload instead of commit checks") |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1459 | parser.add_option("-r", "--recursive", action="store_true", |
| 1460 | help="Act recursively") |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1461 | parser.add_option("-v", "--verbose", action="count", default=0, |
| 1462 | help="Use 2 times for more debug info") |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 1463 | parser.add_option("--name", default='no name') |
maruel@chromium.org | 58407af | 2011-04-12 23:15:57 +0000 | [diff] [blame] | 1464 | parser.add_option("--author") |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 1465 | parser.add_option("--description", default='') |
| 1466 | parser.add_option("--issue", type='int', default=0) |
| 1467 | parser.add_option("--patchset", type='int', default=0) |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 1468 | parser.add_option("--root", default=os.getcwd(), |
| 1469 | help="Search for PRESUBMIT.py up to this directory. " |
| 1470 | "If inherit-review-settings-ok is present in this " |
| 1471 | "directory, parent directories up to the root file " |
| 1472 | "system directories will also be searched.") |
agable@chromium.org | 2da1ade | 2014-04-30 17:40:45 +0000 | [diff] [blame] | 1473 | parser.add_option("--upstream", |
| 1474 | help="Git only: the base ref or upstream branch against " |
| 1475 | "which the diff should be computed.") |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1476 | parser.add_option("--default_presubmit") |
| 1477 | parser.add_option("--may_prompt", action='store_true', default=False) |
iannucci@chromium.org | 8a4a2bc | 2013-03-08 08:13:20 +0000 | [diff] [blame] | 1478 | parser.add_option("--skip_canned", action='append', default=[], |
| 1479 | help="A list of checks to skip which appear in " |
| 1480 | "presubmit_canned_checks. Can be provided multiple times " |
| 1481 | "to skip multiple canned checks.") |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1482 | parser.add_option("--dry_run", action='store_true', |
| 1483 | help=optparse.SUPPRESS_HELP) |
tandrii@chromium.org | 015ebae | 2016-04-25 19:37:22 +0000 | [diff] [blame] | 1484 | parser.add_option("--gerrit_url", help=optparse.SUPPRESS_HELP) |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1485 | parser.add_option("--gerrit_fetch", action='store_true', |
| 1486 | help=optparse.SUPPRESS_HELP) |
maruel@chromium.org | 239f411 | 2011-06-03 20:08:23 +0000 | [diff] [blame] | 1487 | parser.add_option("--rietveld_url", help=optparse.SUPPRESS_HELP) |
| 1488 | parser.add_option("--rietveld_email", help=optparse.SUPPRESS_HELP) |
iannucci@chromium.org | 720fd7a | 2013-04-24 04:13:50 +0000 | [diff] [blame] | 1489 | parser.add_option("--rietveld_fetch", action='store_true', default=False, |
| 1490 | help=optparse.SUPPRESS_HELP) |
pgervais@chromium.org | 92c3009 | 2014-04-15 00:30:37 +0000 | [diff] [blame] | 1491 | # These are for OAuth2 authentication for bots. See also apply_issue.py |
| 1492 | parser.add_option("--rietveld_email_file", help=optparse.SUPPRESS_HELP) |
| 1493 | parser.add_option("--rietveld_private_key_file", help=optparse.SUPPRESS_HELP) |
| 1494 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 1495 | auth.add_auth_options(parser) |
maruel@chromium.org | 82e5f28 | 2011-03-17 14:08:55 +0000 | [diff] [blame] | 1496 | options, args = parser.parse_args(argv) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 1497 | auth_config = auth.extract_auth_config_from_options(options) |
pgervais@chromium.org | 92c3009 | 2014-04-15 00:30:37 +0000 | [diff] [blame] | 1498 | |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1499 | if options.verbose >= 2: |
maruel@chromium.org | 7444c50 | 2011-02-09 14:02:11 +0000 | [diff] [blame] | 1500 | logging.basicConfig(level=logging.DEBUG) |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1501 | elif options.verbose: |
| 1502 | logging.basicConfig(level=logging.INFO) |
| 1503 | else: |
| 1504 | logging.basicConfig(level=logging.ERROR) |
pgervais@chromium.org | 92c3009 | 2014-04-15 00:30:37 +0000 | [diff] [blame] | 1505 | |
tandrii@chromium.org | 015ebae | 2016-04-25 19:37:22 +0000 | [diff] [blame] | 1506 | if (any((options.rietveld_url, options.rietveld_email_file, |
| 1507 | options.rietveld_fetch, options.rietveld_private_key_file)) |
| 1508 | and any((options.gerrit_url, options.gerrit_fetch))): |
| 1509 | parser.error('Options for only codereview --rietveld_* or --gerrit_* ' |
| 1510 | 'allowed') |
| 1511 | |
pgervais@chromium.org | 92c3009 | 2014-04-15 00:30:37 +0000 | [diff] [blame] | 1512 | if options.rietveld_email and options.rietveld_email_file: |
| 1513 | parser.error("Only one of --rietveld_email or --rietveld_email_file " |
| 1514 | "can be passed to this program.") |
pgervais@chromium.org | 92c3009 | 2014-04-15 00:30:37 +0000 | [diff] [blame] | 1515 | if options.rietveld_email_file: |
| 1516 | with open(options.rietveld_email_file, "rb") as f: |
| 1517 | options.rietveld_email = f.read().strip() |
| 1518 | |
maruel@chromium.org | 5c8c6de | 2011-03-18 16:20:18 +0000 | [diff] [blame] | 1519 | change_class, files = load_files(options, args) |
| 1520 | if not change_class: |
| 1521 | parser.error('For unversioned directory, <files> is not optional.') |
tobiasjs | 2836bcf | 2016-08-16 04:08:16 -0700 | [diff] [blame] | 1522 | logging.info('Found %d file(s).', len(files)) |
pgervais@chromium.org | 92c3009 | 2014-04-15 00:30:37 +0000 | [diff] [blame] | 1523 | |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1524 | rietveld_obj, gerrit_obj = None, None |
| 1525 | |
maruel@chromium.org | 239f411 | 2011-06-03 20:08:23 +0000 | [diff] [blame] | 1526 | if options.rietveld_url: |
pgervais@chromium.org | 92c3009 | 2014-04-15 00:30:37 +0000 | [diff] [blame] | 1527 | # The empty password is permitted: '' is not None. |
djacques@chromium.org | 7b654f5 | 2014-04-15 04:02:32 +0000 | [diff] [blame] | 1528 | if options.rietveld_private_key_file: |
pgervais@chromium.org | 92c3009 | 2014-04-15 00:30:37 +0000 | [diff] [blame] | 1529 | rietveld_obj = rietveld.JwtOAuth2Rietveld( |
| 1530 | options.rietveld_url, |
| 1531 | options.rietveld_email, |
| 1532 | options.rietveld_private_key_file) |
| 1533 | else: |
djacques@chromium.org | 7b654f5 | 2014-04-15 04:02:32 +0000 | [diff] [blame] | 1534 | rietveld_obj = rietveld.CachingRietveld( |
| 1535 | options.rietveld_url, |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 1536 | auth_config, |
| 1537 | options.rietveld_email) |
iannucci@chromium.org | 720fd7a | 2013-04-24 04:13:50 +0000 | [diff] [blame] | 1538 | if options.rietveld_fetch: |
| 1539 | assert options.issue |
| 1540 | props = rietveld_obj.get_issue_properties(options.issue, False) |
| 1541 | options.author = props['owner_email'] |
| 1542 | options.description = props['description'] |
| 1543 | logging.info('Got author: "%s"', options.author) |
| 1544 | logging.info('Got description: """\n%s\n"""', options.description) |
tandrii@chromium.org | 015ebae | 2016-04-25 19:37:22 +0000 | [diff] [blame] | 1545 | |
| 1546 | if options.gerrit_url and options.gerrit_fetch: |
tandrii@chromium.org | 83b1b23 | 2016-04-29 16:33:19 +0000 | [diff] [blame] | 1547 | assert options.issue and options.patchset |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1548 | rietveld_obj = None |
| 1549 | gerrit_obj = GerritAccessor(urlparse.urlparse(options.gerrit_url).netloc) |
| 1550 | options.author = gerrit_obj.GetChangeOwner(options.issue) |
| 1551 | options.description = gerrit_obj.GetChangeDescription(options.issue, |
| 1552 | options.patchset) |
tandrii@chromium.org | 015ebae | 2016-04-25 19:37:22 +0000 | [diff] [blame] | 1553 | logging.info('Got author: "%s"', options.author) |
| 1554 | logging.info('Got description: """\n%s\n"""', options.description) |
| 1555 | |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1556 | try: |
iannucci@chromium.org | 8a4a2bc | 2013-03-08 08:13:20 +0000 | [diff] [blame] | 1557 | with canned_check_filter(options.skip_canned): |
| 1558 | results = DoPresubmitChecks( |
| 1559 | change_class(options.name, |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1560 | options.description, |
| 1561 | options.root, |
| 1562 | files, |
| 1563 | options.issue, |
| 1564 | options.patchset, |
| 1565 | options.author, |
| 1566 | upstream=options.upstream), |
iannucci@chromium.org | 8a4a2bc | 2013-03-08 08:13:20 +0000 | [diff] [blame] | 1567 | options.commit, |
| 1568 | options.verbose, |
| 1569 | sys.stdout, |
| 1570 | sys.stdin, |
| 1571 | options.default_presubmit, |
| 1572 | options.may_prompt, |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1573 | rietveld_obj, |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1574 | gerrit_obj, |
tandrii@chromium.org | 57bafac | 2016-04-28 05:09:03 +0000 | [diff] [blame] | 1575 | options.dry_run) |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1576 | return not results.should_continue() |
iannucci@chromium.org | 8a4a2bc | 2013-03-08 08:13:20 +0000 | [diff] [blame] | 1577 | except NonexistantCannedCheckFilter, e: |
| 1578 | print >> sys.stderr, ( |
| 1579 | 'Attempted to skip nonexistent canned presubmit check: %s' % e.message) |
| 1580 | return 2 |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1581 | except PresubmitFailure, e: |
| 1582 | print >> sys.stderr, e |
| 1583 | print >> sys.stderr, 'Maybe your depot_tools is out of date?' |
| 1584 | print >> sys.stderr, 'If all fails, contact maruel@' |
| 1585 | return 2 |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1586 | |
| 1587 | |
| 1588 | if __name__ == '__main__': |
maruel@chromium.org | 35625c7 | 2011-03-23 17:34:02 +0000 | [diff] [blame] | 1589 | fix_encoding.fix_encoding() |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 1590 | try: |
| 1591 | sys.exit(main()) |
| 1592 | except KeyboardInterrupt: |
| 1593 | sys.stderr.write('interrupted\n') |
sergiyb | f8a3b38 | 2016-07-05 11:21:30 -0700 | [diff] [blame] | 1594 | sys.exit(2) |