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