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