maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 2 | # Copyright (c) 2010 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 | |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 9 | __version__ = '1.3.5' |
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 | |
| 15 | import cPickle # Exposed through the API. |
| 16 | import cStringIO # Exposed through the API. |
| 17 | import exceptions |
| 18 | import fnmatch |
| 19 | import glob |
maruel@chromium.org | df1595a | 2009-06-11 02:00:13 +0000 | [diff] [blame] | 20 | import logging |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 21 | import marshal # Exposed through the API. |
| 22 | import optparse |
| 23 | import os # Somewhat exposed through the API. |
| 24 | import pickle # Exposed through the API. |
maruel@chromium.org | ce8e46b | 2009-06-26 22:31:51 +0000 | [diff] [blame] | 25 | import random |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 26 | import re # Exposed through the API. |
| 27 | import subprocess # Exposed through the API. |
| 28 | import sys # Parts exposed through API. |
| 29 | import tempfile # Exposed through the API. |
jam@chromium.org | 2a891dc | 2009-08-20 20:33:37 +0000 | [diff] [blame] | 30 | import time |
maruel@chromium.org | d7dccf5 | 2009-06-06 18:51:58 +0000 | [diff] [blame] | 31 | import traceback # Exposed through the API. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 32 | import types |
maruel@chromium.org | 1487d53 | 2009-06-06 00:22:57 +0000 | [diff] [blame] | 33 | import unittest # Exposed through the API. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 34 | import urllib2 # Exposed through the API. |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 35 | from warnings import warn |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 36 | |
maruel@chromium.org | fb11c7b | 2010-03-18 18:22:14 +0000 | [diff] [blame] | 37 | try: |
dpranke@chromium.org | d945f36 | 2011-03-11 22:52:19 +0000 | [diff] [blame] | 38 | import simplejson as json # pylint: disable=F0401 |
maruel@chromium.org | fb11c7b | 2010-03-18 18:22:14 +0000 | [diff] [blame] | 39 | except ImportError: |
| 40 | try: |
| 41 | import json |
tony@chromium.org | 5e9f2ed | 2010-04-08 01:38:19 +0000 | [diff] [blame] | 42 | # Some versions of python2.5 have an incomplete json module. Check to make |
| 43 | # sure loads exists. |
maruel@chromium.org | b17b55b | 2010-11-03 14:42:37 +0000 | [diff] [blame] | 44 | # Statement seems to have no effect |
| 45 | # pylint: disable=W0104 |
tony@chromium.org | 5e9f2ed | 2010-04-08 01:38:19 +0000 | [diff] [blame] | 46 | json.loads |
| 47 | except (ImportError, AttributeError): |
maruel@chromium.org | 59c7ba6 | 2010-03-20 00:13:07 +0000 | [diff] [blame] | 48 | # Import the one included in depot_tools. |
maruel@chromium.org | d08cb1e | 2010-03-20 00:25:19 +0000 | [diff] [blame] | 49 | sys.path.append(os.path.join(os.path.dirname(__file__), 'third_party')) |
dpranke@chromium.org | d945f36 | 2011-03-11 22:52:19 +0000 | [diff] [blame] | 50 | import simplejson as json # pylint: disable=F0401 |
maruel@chromium.org | fb11c7b | 2010-03-18 18:22:14 +0000 | [diff] [blame] | 51 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 52 | # Local imports. |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 53 | import gclient_utils |
dpranke@chromium.org | 2a00962 | 2011-03-01 02:43:31 +0000 | [diff] [blame] | 54 | import owners |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 55 | import presubmit_canned_checks |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 56 | import scm |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 57 | |
| 58 | |
maruel@chromium.org | ce8e46b | 2009-06-26 22:31:51 +0000 | [diff] [blame] | 59 | # Ask for feedback only once in program lifetime. |
| 60 | _ASKED_FOR_FEEDBACK = False |
| 61 | |
| 62 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 63 | class NotImplementedException(Exception): |
| 64 | """We're leaving placeholders in a bunch of places to remind us of the |
| 65 | design of the API, but we have not implemented all of it yet. Implement as |
| 66 | the need arises. |
| 67 | """ |
| 68 | pass |
| 69 | |
| 70 | |
| 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 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 121 | class OutputApi(object): |
| 122 | """This class (more like a module) gets passed to presubmit scripts so that |
| 123 | they can specify various types of results. |
| 124 | """ |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 125 | class PresubmitResult(object): |
| 126 | """Base class for result objects.""" |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 127 | fatal = False |
| 128 | should_prompt = False |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 129 | |
| 130 | def __init__(self, message, items=None, long_text=''): |
| 131 | """ |
| 132 | message: A short one-line message to indicate errors. |
| 133 | items: A list of short strings to indicate where errors occurred. |
| 134 | long_text: multi-line text output, e.g. from another tool |
| 135 | """ |
| 136 | self._message = message |
| 137 | self._items = [] |
| 138 | if items: |
| 139 | self._items = items |
| 140 | self._long_text = long_text.rstrip() |
| 141 | |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 142 | def handle(self, output): |
| 143 | output.write(self._message) |
| 144 | output.write('\n') |
estade@chromium.org | 593258b | 2010-01-28 00:04:36 +0000 | [diff] [blame] | 145 | if len(self._items) > 0: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 146 | output.write(' ' + ' \\\n '.join(map(str, self._items)) + '\n') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 147 | if self._long_text: |
maruel@chromium.org | 310b355 | 2010-11-01 13:23:35 +0000 | [diff] [blame] | 148 | # Sometimes self._long_text is a ascii string, a codepage string |
| 149 | # (on windows), or a unicode object. |
| 150 | try: |
| 151 | long_text = self._long_text.decode() |
| 152 | except UnicodeDecodeError: |
| 153 | long_text = self._long_text.decode('ascii', 'replace') |
| 154 | |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 155 | output.write('\n***************\n%s\n***************\n' % |
maruel@chromium.org | 310b355 | 2010-11-01 13:23:35 +0000 | [diff] [blame] | 156 | long_text) |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 157 | if self.fatal: |
| 158 | output.fail() |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 159 | |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 160 | class PresubmitAddReviewers(PresubmitResult): |
| 161 | """Add some suggested reviewers to the change.""" |
| 162 | def __init__(self, reviewers): |
| 163 | super(OutputApi.PresubmitAddReviewers, self).__init__('') |
| 164 | self.reviewers = reviewers |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 165 | |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 166 | def handle(self, output): |
| 167 | output.reviewers.extend(self.reviewers) |
dpranke@chromium.org | 3ae183f | 2011-03-09 21:40:32 +0000 | [diff] [blame] | 168 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 169 | class PresubmitError(PresubmitResult): |
| 170 | """A hard presubmit error.""" |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 171 | fatal = True |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 172 | |
| 173 | class PresubmitPromptWarning(PresubmitResult): |
| 174 | """An warning that prompts the user if they want to continue.""" |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 175 | should_prompt = True |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 176 | |
| 177 | class PresubmitNotifyResult(PresubmitResult): |
| 178 | """Just print something to the screen -- but it's not even a warning.""" |
| 179 | pass |
| 180 | |
| 181 | class MailTextResult(PresubmitResult): |
| 182 | """A warning that should be included in the review request email.""" |
| 183 | def __init__(self, *args, **kwargs): |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 184 | super(OutputApi.MailTextResult, self).__init__() |
| 185 | raise NotImplementedException() |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 186 | |
| 187 | |
| 188 | class InputApi(object): |
| 189 | """An instance of this object is passed to presubmit scripts so they can |
| 190 | know stuff about the change they're looking at. |
| 191 | """ |
maruel@chromium.org | b17b55b | 2010-11-03 14:42:37 +0000 | [diff] [blame] | 192 | # Method could be a function |
| 193 | # pylint: disable=R0201 |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 194 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 195 | # File extensions that are considered source files from a style guide |
| 196 | # perspective. Don't modify this list from a presubmit script! |
| 197 | DEFAULT_WHITE_LIST = ( |
| 198 | # C++ and friends |
maruel@chromium.org | a73d793 | 2010-01-28 23:50:06 +0000 | [diff] [blame] | 199 | r".*\.c$", r".*\.cc$", r".*\.cpp$", r".*\.h$", r".*\.m$", r".*\.mm$", |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 200 | 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] | 201 | # Scripts |
maruel@chromium.org | a73d793 | 2010-01-28 23:50:06 +0000 | [diff] [blame] | 202 | r".*\.js$", r".*\.py$", r".*\.sh$", r".*\.rb$", r".*\.pl$", r".*\.pm$", |
maruel@chromium.org | ef77648 | 2010-01-28 16:04:32 +0000 | [diff] [blame] | 203 | # No extension at all, note that ALL CAPS files are black listed in |
| 204 | # DEFAULT_BLACK_LIST below. |
maruel@chromium.org | a73d793 | 2010-01-28 23:50:06 +0000 | [diff] [blame] | 205 | r"(^|.*?[\\\/])[^.]+$", |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 206 | # Other |
maruel@chromium.org | a73d793 | 2010-01-28 23:50:06 +0000 | [diff] [blame] | 207 | r".*\.java$", r".*\.mk$", r".*\.am$", |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 208 | ) |
| 209 | |
| 210 | # Path regexp that should be excluded from being considered containing source |
| 211 | # files. Don't modify this list from a presubmit script! |
| 212 | DEFAULT_BLACK_LIST = ( |
| 213 | r".*\bexperimental[\\\/].*", |
| 214 | r".*\bthird_party[\\\/].*", |
| 215 | # Output directories (just in case) |
| 216 | r".*\bDebug[\\\/].*", |
| 217 | r".*\bRelease[\\\/].*", |
| 218 | r".*\bxcodebuild[\\\/].*", |
| 219 | r".*\bsconsbuild[\\\/].*", |
| 220 | # All caps files like README and LICENCE. |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 221 | r".*\b[A-Z0-9_]{2,}$", |
maruel@chromium.org | df1595a | 2009-06-11 02:00:13 +0000 | [diff] [blame] | 222 | # SCM (can happen in dual SCM configuration). (Slightly over aggressive) |
maruel@chromium.org | 5d0dc43 | 2011-01-03 02:40:37 +0000 | [diff] [blame] | 223 | r"(|.*[\\\/])\.git[\\\/].*", |
| 224 | r"(|.*[\\\/])\.svn[\\\/].*", |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 225 | ) |
| 226 | |
dpranke@chromium.org | 627ea67 | 2011-03-11 23:29:03 +0000 | [diff] [blame] | 227 | # TODO(dpranke): Update callers to pass in tbr, host_url, remove |
| 228 | # default arguments. |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 229 | def __init__(self, change, presubmit_path, is_committing, tbr, host_url=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 230 | """Builds an InputApi object. |
| 231 | |
| 232 | Args: |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 233 | change: A presubmit.Change object. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 234 | presubmit_path: The path to the presubmit script being processed. |
maruel@chromium.org | d7dccf5 | 2009-06-06 18:51:58 +0000 | [diff] [blame] | 235 | is_committing: True if the change is about to be committed. |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 236 | tbr: True if '--tbr' was passed to skip any reviewer/owner checks |
| 237 | host_url: scheme, host, and path of rietveld instance |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 238 | """ |
maruel@chromium.org | 9711bba | 2009-05-22 23:51:39 +0000 | [diff] [blame] | 239 | # Version number of the presubmit_support script. |
| 240 | self.version = [int(x) for x in __version__.split('.')] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 241 | self.change = change |
dpranke@chromium.org | 627ea67 | 2011-03-11 23:29:03 +0000 | [diff] [blame] | 242 | self.host_url = host_url |
maruel@chromium.org | d7dccf5 | 2009-06-06 18:51:58 +0000 | [diff] [blame] | 243 | self.is_committing = is_committing |
dpranke@chromium.org | 627ea67 | 2011-03-11 23:29:03 +0000 | [diff] [blame] | 244 | self.tbr = tbr |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 245 | self.host_url = host_url or 'http://codereview.chromium.org' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 246 | |
| 247 | # We expose various modules and functions as attributes of the input_api |
| 248 | # so that presubmit scripts don't have to import them. |
| 249 | self.basename = os.path.basename |
| 250 | self.cPickle = cPickle |
| 251 | self.cStringIO = cStringIO |
maruel@chromium.org | fb11c7b | 2010-03-18 18:22:14 +0000 | [diff] [blame] | 252 | self.json = json |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 253 | self.os_path = os.path |
| 254 | self.pickle = pickle |
| 255 | self.marshal = marshal |
| 256 | self.re = re |
| 257 | self.subprocess = subprocess |
| 258 | self.tempfile = tempfile |
maruel@chromium.org | d7dccf5 | 2009-06-06 18:51:58 +0000 | [diff] [blame] | 259 | self.traceback = traceback |
maruel@chromium.org | 1487d53 | 2009-06-06 00:22:57 +0000 | [diff] [blame] | 260 | self.unittest = unittest |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 261 | self.urllib2 = urllib2 |
| 262 | |
maruel@chromium.org | c0b2297 | 2009-06-25 16:19:14 +0000 | [diff] [blame] | 263 | # To easily fork python. |
| 264 | self.python_executable = sys.executable |
| 265 | self.environ = os.environ |
| 266 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 267 | # InputApi.platform is the platform you're currently running on. |
| 268 | self.platform = sys.platform |
| 269 | |
| 270 | # The local path of the currently-being-processed presubmit script. |
maruel@chromium.org | 3d23524 | 2009-05-15 12:40:48 +0000 | [diff] [blame] | 271 | self._current_presubmit_path = os.path.dirname(presubmit_path) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 272 | |
| 273 | # We carry the canned checks so presubmit scripts can easily use them. |
| 274 | self.canned_checks = presubmit_canned_checks |
| 275 | |
dpranke@chromium.org | 2a00962 | 2011-03-01 02:43:31 +0000 | [diff] [blame] | 276 | # TODO(dpranke): figure out a list of all approved owners for a repo |
| 277 | # in order to be able to handle wildcard OWNERS files? |
| 278 | self.owners_db = owners.Database(change.RepositoryRoot(), |
| 279 | fopen=file, os_path=self.os_path) |
| 280 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 281 | def PresubmitLocalPath(self): |
| 282 | """Returns the local path of the presubmit script currently being run. |
| 283 | |
| 284 | This is useful if you don't want to hard-code absolute paths in the |
| 285 | presubmit script. For example, It can be used to find another file |
| 286 | relative to the PRESUBMIT.py script, so the whole tree can be branched and |
| 287 | the presubmit script still works, without editing its content. |
| 288 | """ |
maruel@chromium.org | 3d23524 | 2009-05-15 12:40:48 +0000 | [diff] [blame] | 289 | return self._current_presubmit_path |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 290 | |
maruel@chromium.org | 1e08c00 | 2009-05-28 19:09:33 +0000 | [diff] [blame] | 291 | def DepotToLocalPath(self, depot_path): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 292 | """Translate a depot path to a local path (relative to client root). |
| 293 | |
| 294 | Args: |
| 295 | Depot path as a string. |
| 296 | |
| 297 | Returns: |
| 298 | The local path of the depot path under the user's current client, or None |
| 299 | if the file is not mapped. |
| 300 | |
| 301 | Remember to check for the None case and show an appropriate error! |
| 302 | """ |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 303 | local_path = scm.SVN.CaptureInfo(depot_path).get('Path') |
maruel@chromium.org | 1e08c00 | 2009-05-28 19:09:33 +0000 | [diff] [blame] | 304 | if local_path: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 305 | return local_path |
| 306 | |
maruel@chromium.org | 1e08c00 | 2009-05-28 19:09:33 +0000 | [diff] [blame] | 307 | def LocalToDepotPath(self, local_path): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 308 | """Translate a local path to a depot path. |
| 309 | |
| 310 | Args: |
| 311 | Local path (relative to current directory, or absolute) as a string. |
| 312 | |
| 313 | Returns: |
| 314 | The depot path (SVN URL) of the file if mapped, otherwise None. |
| 315 | """ |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 316 | depot_path = scm.SVN.CaptureInfo(local_path).get('URL') |
maruel@chromium.org | 1e08c00 | 2009-05-28 19:09:33 +0000 | [diff] [blame] | 317 | if depot_path: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 318 | return depot_path |
| 319 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 320 | def AffectedFiles(self, include_dirs=False, include_deletes=True): |
| 321 | """Same as input_api.change.AffectedFiles() except only lists files |
| 322 | (and optionally directories) in the same directory as the current presubmit |
| 323 | script, or subdirectories thereof. |
| 324 | """ |
maruel@chromium.org | 3d23524 | 2009-05-15 12:40:48 +0000 | [diff] [blame] | 325 | dir_with_slash = normpath("%s/" % self.PresubmitLocalPath()) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 326 | if len(dir_with_slash) == 1: |
| 327 | dir_with_slash = '' |
maruel@chromium.org | 4661e0c | 2009-06-04 00:45:26 +0000 | [diff] [blame] | 328 | return filter( |
| 329 | lambda x: normpath(x.AbsoluteLocalPath()).startswith(dir_with_slash), |
| 330 | self.change.AffectedFiles(include_dirs, include_deletes)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 331 | |
| 332 | def LocalPaths(self, include_dirs=False): |
| 333 | """Returns local paths of input_api.AffectedFiles().""" |
| 334 | return [af.LocalPath() for af in self.AffectedFiles(include_dirs)] |
| 335 | |
| 336 | def AbsoluteLocalPaths(self, include_dirs=False): |
| 337 | """Returns absolute local paths of input_api.AffectedFiles().""" |
| 338 | return [af.AbsoluteLocalPath() for af in self.AffectedFiles(include_dirs)] |
| 339 | |
| 340 | def ServerPaths(self, include_dirs=False): |
| 341 | """Returns server paths of input_api.AffectedFiles().""" |
| 342 | return [af.ServerPath() for af in self.AffectedFiles(include_dirs)] |
| 343 | |
maruel@chromium.org | 77c4f0f | 2009-05-29 18:53:04 +0000 | [diff] [blame] | 344 | def AffectedTextFiles(self, include_deletes=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 345 | """Same as input_api.change.AffectedTextFiles() except only lists files |
| 346 | in the same directory as the current presubmit script, or subdirectories |
| 347 | thereof. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 348 | """ |
maruel@chromium.org | 77c4f0f | 2009-05-29 18:53:04 +0000 | [diff] [blame] | 349 | if include_deletes is not None: |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 350 | warn("AffectedTextFiles(include_deletes=%s)" |
| 351 | " is deprecated and ignored" % str(include_deletes), |
| 352 | category=DeprecationWarning, |
| 353 | stacklevel=2) |
maruel@chromium.org | 77c4f0f | 2009-05-29 18:53:04 +0000 | [diff] [blame] | 354 | return filter(lambda x: x.IsTextFile(), |
| 355 | self.AffectedFiles(include_dirs=False, include_deletes=False)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 356 | |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 357 | def FilterSourceFile(self, affected_file, white_list=None, black_list=None): |
| 358 | """Filters out files that aren't considered "source file". |
| 359 | |
| 360 | If white_list or black_list is None, InputApi.DEFAULT_WHITE_LIST |
| 361 | and InputApi.DEFAULT_BLACK_LIST is used respectively. |
| 362 | |
| 363 | The lists will be compiled as regular expression and |
| 364 | AffectedFile.LocalPath() needs to pass both list. |
| 365 | |
| 366 | Note: Copy-paste this function to suit your needs or use a lambda function. |
| 367 | """ |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 368 | def Find(affected_file, items): |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 369 | local_path = affected_file.LocalPath() |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 370 | for item in items: |
maruel@chromium.org | df1595a | 2009-06-11 02:00:13 +0000 | [diff] [blame] | 371 | if self.re.match(item, local_path): |
| 372 | logging.debug("%s matched %s" % (item, local_path)) |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 373 | return True |
| 374 | return False |
| 375 | return (Find(affected_file, white_list or self.DEFAULT_WHITE_LIST) and |
| 376 | not Find(affected_file, black_list or self.DEFAULT_BLACK_LIST)) |
| 377 | |
| 378 | def AffectedSourceFiles(self, source_file): |
| 379 | """Filter the list of AffectedTextFiles by the function source_file. |
| 380 | |
| 381 | If source_file is None, InputApi.FilterSourceFile() is used. |
| 382 | """ |
| 383 | if not source_file: |
| 384 | source_file = self.FilterSourceFile |
| 385 | return filter(source_file, self.AffectedTextFiles()) |
| 386 | |
| 387 | def RightHandSideLines(self, source_file_filter=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 388 | """An iterator over all text lines in "new" version of changed files. |
| 389 | |
| 390 | Only lists lines from new or modified text files in the change that are |
| 391 | contained by the directory of the currently executing presubmit script. |
| 392 | |
| 393 | This is useful for doing line-by-line regex checks, like checking for |
| 394 | trailing whitespace. |
| 395 | |
| 396 | Yields: |
| 397 | a 3 tuple: |
| 398 | the AffectedFile instance of the current file; |
| 399 | integer line number (1-based); and |
| 400 | the contents of the line as a string. |
maruel@chromium.org | 1487d53 | 2009-06-06 00:22:57 +0000 | [diff] [blame] | 401 | |
| 402 | Note: The cariage return (LF or CR) is stripped off. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 403 | """ |
maruel@chromium.org | 3410d91 | 2009-06-09 20:56:16 +0000 | [diff] [blame] | 404 | files = self.AffectedSourceFiles(source_file_filter) |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 405 | return _RightHandSideLinesImpl(files) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 406 | |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 407 | def ReadFile(self, file_item, mode='r'): |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 408 | """Reads an arbitrary file. |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 409 | |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 410 | Deny reading anything outside the repository. |
| 411 | """ |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 412 | if isinstance(file_item, AffectedFile): |
| 413 | file_item = file_item.AbsoluteLocalPath() |
| 414 | if not file_item.startswith(self.change.RepositoryRoot()): |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 415 | raise IOError('Access outside the repository root is denied.') |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 416 | return gclient_utils.FileRead(file_item, mode) |
maruel@chromium.org | 44a17ad | 2009-06-08 14:14:35 +0000 | [diff] [blame] | 417 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 418 | |
| 419 | class AffectedFile(object): |
| 420 | """Representation of a file in a change.""" |
maruel@chromium.org | b17b55b | 2010-11-03 14:42:37 +0000 | [diff] [blame] | 421 | # Method could be a function |
| 422 | # pylint: disable=R0201 |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 423 | def __init__(self, path, action, repository_root=''): |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 424 | self._path = path |
| 425 | self._action = action |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 426 | self._local_root = repository_root |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 427 | self._is_directory = None |
| 428 | self._properties = {} |
maruel@chromium.org | 5d0dc43 | 2011-01-03 02:40:37 +0000 | [diff] [blame] | 429 | logging.debug('%s(%s)' % (self.__class__.__name__, self._path)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 430 | |
| 431 | def ServerPath(self): |
| 432 | """Returns a path string that identifies the file in the SCM system. |
| 433 | |
| 434 | Returns the empty string if the file does not exist in SCM. |
| 435 | """ |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 436 | return "" |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 437 | |
| 438 | def LocalPath(self): |
| 439 | """Returns the path of this file on the local disk relative to client root. |
| 440 | """ |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 441 | return normpath(self._path) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 442 | |
| 443 | def AbsoluteLocalPath(self): |
| 444 | """Returns the absolute path of this file on the local disk. |
| 445 | """ |
chase@chromium.org | 8e416c8 | 2009-10-06 04:30:44 +0000 | [diff] [blame] | 446 | 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] | 447 | |
| 448 | def IsDirectory(self): |
| 449 | """Returns true if this object is a directory.""" |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 450 | if self._is_directory is None: |
| 451 | path = self.AbsoluteLocalPath() |
| 452 | self._is_directory = (os.path.exists(path) and |
| 453 | os.path.isdir(path)) |
| 454 | return self._is_directory |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 455 | |
| 456 | def Action(self): |
| 457 | """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] | 458 | # TODO(maruel): Somewhat crappy, Could be "A" or "A +" for svn but |
| 459 | # different for other SCM. |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 460 | return self._action |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 461 | |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 462 | def Property(self, property_name): |
| 463 | """Returns the specified SCM property of this file, or None if no such |
| 464 | property. |
| 465 | """ |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 466 | return self._properties.get(property_name, None) |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 467 | |
maruel@chromium.org | 1e08c00 | 2009-05-28 19:09:33 +0000 | [diff] [blame] | 468 | def IsTextFile(self): |
maruel@chromium.org | 77c4f0f | 2009-05-29 18:53:04 +0000 | [diff] [blame] | 469 | """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] | 470 | |
maruel@chromium.org | 77c4f0f | 2009-05-29 18:53:04 +0000 | [diff] [blame] | 471 | Deleted files are not text file.""" |
maruel@chromium.org | 1e08c00 | 2009-05-28 19:09:33 +0000 | [diff] [blame] | 472 | raise NotImplementedError() # Implement when needed |
| 473 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 474 | def NewContents(self): |
| 475 | """Returns an iterator over the lines in the new version of file. |
| 476 | |
| 477 | The new version is the file in the user's workspace, i.e. the "right hand |
| 478 | side". |
| 479 | |
| 480 | Contents will be empty if the file is a directory or does not exist. |
maruel@chromium.org | 1487d53 | 2009-06-06 00:22:57 +0000 | [diff] [blame] | 481 | Note: The cariage returns (LF or CR) are stripped off. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 482 | """ |
| 483 | if self.IsDirectory(): |
| 484 | return [] |
| 485 | else: |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 486 | return gclient_utils.FileRead(self.AbsoluteLocalPath(), |
| 487 | 'rU').splitlines() |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 488 | |
| 489 | def OldContents(self): |
| 490 | """Returns an iterator over the lines in the old version of file. |
| 491 | |
| 492 | The old version is the file in depot, i.e. the "left hand side". |
| 493 | """ |
| 494 | raise NotImplementedError() # Implement when needed |
| 495 | |
| 496 | def OldFileTempPath(self): |
| 497 | """Returns the path on local disk where the old contents resides. |
| 498 | |
| 499 | The old version is the file in depot, i.e. the "left hand side". |
| 500 | This is a read-only cached copy of the old contents. *DO NOT* try to |
| 501 | modify this file. |
| 502 | """ |
| 503 | raise NotImplementedError() # Implement if/when needed. |
| 504 | |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 505 | def ChangedContents(self): |
| 506 | """Returns a list of tuples (line number, line text) of all new lines. |
| 507 | |
| 508 | This relies on the scm diff output describing each changed code section |
| 509 | with a line of the form |
| 510 | |
| 511 | ^@@ <old line num>,<old size> <new line num>,<new size> @@$ |
| 512 | """ |
| 513 | new_lines = [] |
| 514 | line_num = 0 |
| 515 | |
| 516 | if self.IsDirectory(): |
| 517 | return [] |
| 518 | |
| 519 | for line in self.GenerateScmDiff().splitlines(): |
| 520 | m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) |
| 521 | if m: |
| 522 | line_num = int(m.groups(1)[0]) |
| 523 | continue |
| 524 | if line.startswith('+') and not line.startswith('++'): |
| 525 | new_lines.append((line_num, line[1:])) |
| 526 | if not line.startswith('-'): |
| 527 | line_num += 1 |
| 528 | return new_lines |
| 529 | |
maruel@chromium.org | 5de1397 | 2009-06-10 18:16:06 +0000 | [diff] [blame] | 530 | def __str__(self): |
| 531 | return self.LocalPath() |
| 532 | |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 533 | def GenerateScmDiff(self): |
| 534 | raise NotImplementedError() # Implemented in derived classes. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 535 | |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 536 | class SvnAffectedFile(AffectedFile): |
| 537 | """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] | 538 | # Method 'NNN' is abstract in class 'NNN' but is not overridden |
| 539 | # pylint: disable=W0223 |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 540 | |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 541 | def __init__(self, *args, **kwargs): |
| 542 | AffectedFile.__init__(self, *args, **kwargs) |
| 543 | self._server_path = None |
| 544 | self._is_text_file = None |
| 545 | |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 546 | def ServerPath(self): |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 547 | if self._server_path is None: |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 548 | self._server_path = scm.SVN.CaptureInfo( |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 549 | self.AbsoluteLocalPath()).get('URL', '') |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 550 | return self._server_path |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 551 | |
| 552 | def IsDirectory(self): |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 553 | if self._is_directory is None: |
| 554 | path = self.AbsoluteLocalPath() |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 555 | if os.path.exists(path): |
| 556 | # Retrieve directly from the file system; it is much faster than |
| 557 | # querying subversion, especially on Windows. |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 558 | self._is_directory = os.path.isdir(path) |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 559 | else: |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 560 | self._is_directory = scm.SVN.CaptureInfo( |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 561 | path).get('Node Kind') in ('dir', 'directory') |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 562 | return self._is_directory |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 563 | |
| 564 | def Property(self, property_name): |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 565 | if not property_name in self._properties: |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 566 | self._properties[property_name] = scm.SVN.GetFileProperty( |
maruel@chromium.org | 196f8cb | 2009-06-11 00:32:06 +0000 | [diff] [blame] | 567 | self.AbsoluteLocalPath(), property_name).rstrip() |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 568 | return self._properties[property_name] |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 569 | |
maruel@chromium.org | 1e08c00 | 2009-05-28 19:09:33 +0000 | [diff] [blame] | 570 | def IsTextFile(self): |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 571 | if self._is_text_file is None: |
| 572 | if self.Action() == 'D': |
| 573 | # A deleted file is not a text file. |
| 574 | self._is_text_file = False |
| 575 | elif self.IsDirectory(): |
| 576 | self._is_text_file = False |
| 577 | else: |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 578 | mime_type = scm.SVN.GetFileProperty(self.AbsoluteLocalPath(), |
| 579 | 'svn:mime-type') |
maruel@chromium.org | 15bdffa | 2009-05-29 11:16:29 +0000 | [diff] [blame] | 580 | self._is_text_file = (not mime_type or mime_type.startswith('text/')) |
| 581 | return self._is_text_file |
maruel@chromium.org | 1e08c00 | 2009-05-28 19:09:33 +0000 | [diff] [blame] | 582 | |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 583 | def GenerateScmDiff(self): |
maruel@chromium.org | 1f31281 | 2011-02-10 01:33:57 +0000 | [diff] [blame] | 584 | return scm.SVN.GenerateDiff([self.AbsoluteLocalPath()]) |
| 585 | |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 586 | |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 587 | class GitAffectedFile(AffectedFile): |
| 588 | """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] | 589 | # Method 'NNN' is abstract in class 'NNN' but is not overridden |
| 590 | # pylint: disable=W0223 |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 591 | |
| 592 | def __init__(self, *args, **kwargs): |
| 593 | AffectedFile.__init__(self, *args, **kwargs) |
| 594 | self._server_path = None |
| 595 | self._is_text_file = None |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 596 | |
| 597 | def ServerPath(self): |
| 598 | if self._server_path is None: |
| 599 | raise NotImplementedException() # TODO(maruel) Implement. |
| 600 | return self._server_path |
| 601 | |
| 602 | def IsDirectory(self): |
| 603 | if self._is_directory is None: |
| 604 | path = self.AbsoluteLocalPath() |
| 605 | if os.path.exists(path): |
| 606 | # Retrieve directly from the file system; it is much faster than |
| 607 | # querying subversion, especially on Windows. |
| 608 | self._is_directory = os.path.isdir(path) |
| 609 | else: |
| 610 | # raise NotImplementedException() # TODO(maruel) Implement. |
| 611 | self._is_directory = False |
| 612 | return self._is_directory |
| 613 | |
| 614 | def Property(self, property_name): |
| 615 | if not property_name in self._properties: |
| 616 | raise NotImplementedException() # TODO(maruel) Implement. |
| 617 | return self._properties[property_name] |
| 618 | |
| 619 | def IsTextFile(self): |
| 620 | if self._is_text_file is None: |
| 621 | if self.Action() == 'D': |
| 622 | # A deleted file is not a text file. |
| 623 | self._is_text_file = False |
| 624 | elif self.IsDirectory(): |
| 625 | self._is_text_file = False |
| 626 | else: |
| 627 | # raise NotImplementedException() # TODO(maruel) Implement. |
| 628 | self._is_text_file = os.path.isfile(self.AbsoluteLocalPath()) |
| 629 | return self._is_text_file |
| 630 | |
maruel@chromium.org | ab05d58 | 2011-02-09 23:41:22 +0000 | [diff] [blame] | 631 | def GenerateScmDiff(self): |
| 632 | return scm.GIT.GenerateDiff(self._local_root, files=[self.LocalPath(),]) |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 633 | |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 634 | class Change(object): |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 635 | """Describe a change. |
| 636 | |
| 637 | Used directly by the presubmit scripts to query the current change being |
| 638 | tested. |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 639 | |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 640 | Instance members: |
| 641 | tags: Dictionnary of KEY=VALUE pairs found in the change description. |
| 642 | self.KEY: equivalent to tags['KEY'] |
| 643 | """ |
| 644 | |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 645 | _AFFECTED_FILES = AffectedFile |
| 646 | |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 647 | # Matches key/value (or "tag") lines in changelist descriptions. |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 648 | _TAG_LINE_RE = re.compile( |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 649 | '^\s*(?P<key>[A-Z][A-Z_0-9]*)\s*=\s*(?P<value>.*?)\s*$') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 650 | |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 651 | def __init__(self, name, description, local_root, files, issue, patchset): |
| 652 | if files is None: |
| 653 | files = [] |
| 654 | self._name = name |
| 655 | self._full_description = description |
chase@chromium.org | 8e416c8 | 2009-10-06 04:30:44 +0000 | [diff] [blame] | 656 | # Convert root into an absolute path. |
| 657 | self._local_root = os.path.abspath(local_root) |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 658 | self.issue = issue |
| 659 | self.patchset = patchset |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 660 | self.scm = '' |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 661 | |
| 662 | # From the description text, build up a dictionary of key/value pairs |
| 663 | # plus the description minus all key/value or "tag" lines. |
maruel@chromium.org | fa41037 | 2010-09-10 17:01:01 +0000 | [diff] [blame] | 664 | description_without_tags = [] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 665 | self.tags = {} |
maruel@chromium.org | 8d5c9a5 | 2009-06-12 15:59:08 +0000 | [diff] [blame] | 666 | for line in self._full_description.splitlines(): |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 667 | m = self._TAG_LINE_RE.match(line) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 668 | if m: |
| 669 | self.tags[m.group('key')] = m.group('value') |
| 670 | else: |
maruel@chromium.org | fa41037 | 2010-09-10 17:01:01 +0000 | [diff] [blame] | 671 | description_without_tags.append(line) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 672 | |
| 673 | # Change back to text and remove whitespace at end. |
maruel@chromium.org | fa41037 | 2010-09-10 17:01:01 +0000 | [diff] [blame] | 674 | self._description_without_tags = ( |
| 675 | '\n'.join(description_without_tags).rstrip()) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 676 | |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 677 | self._affected_files = [ |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 678 | self._AFFECTED_FILES(info[1], info[0].strip(), self._local_root) |
| 679 | for info in files |
maruel@chromium.org | dbbeedc | 2009-05-22 20:26:17 +0000 | [diff] [blame] | 680 | ] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 681 | |
maruel@chromium.org | 92022ec | 2009-06-11 01:59:28 +0000 | [diff] [blame] | 682 | def Name(self): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 683 | """Returns the change name.""" |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 684 | return self._name |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 685 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 686 | def DescriptionText(self): |
| 687 | """Returns the user-entered changelist description, minus tags. |
| 688 | |
| 689 | Any line in the user-provided description starting with e.g. "FOO=" |
| 690 | (whitespace permitted before and around) is considered a tag line. Such |
| 691 | lines are stripped out of the description this function returns. |
| 692 | """ |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 693 | return self._description_without_tags |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 694 | |
| 695 | def FullDescriptionText(self): |
| 696 | """Returns the complete changelist description including tags.""" |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 697 | return self._full_description |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 698 | |
| 699 | def RepositoryRoot(self): |
maruel@chromium.org | 92022ec | 2009-06-11 01:59:28 +0000 | [diff] [blame] | 700 | """Returns the repository (checkout) root directory for this change, |
| 701 | as an absolute path. |
| 702 | """ |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 703 | return self._local_root |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 704 | |
| 705 | def __getattr__(self, attr): |
maruel@chromium.org | 92022ec | 2009-06-11 01:59:28 +0000 | [diff] [blame] | 706 | """Return tags directly as attributes on the object.""" |
| 707 | if not re.match(r"^[A-Z_]*$", attr): |
| 708 | raise AttributeError(self, attr) |
maruel@chromium.org | e1a524f | 2009-05-27 14:43:46 +0000 | [diff] [blame] | 709 | return self.tags.get(attr) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 710 | |
| 711 | def AffectedFiles(self, include_dirs=False, include_deletes=True): |
| 712 | """Returns a list of AffectedFile instances for all files in the change. |
| 713 | |
| 714 | Args: |
| 715 | include_deletes: If false, deleted files will be filtered out. |
| 716 | include_dirs: True to include directories in the list |
| 717 | |
| 718 | Returns: |
| 719 | [AffectedFile(path, action), AffectedFile(path, action)] |
| 720 | """ |
| 721 | if include_dirs: |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 722 | affected = self._affected_files |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 723 | else: |
maruel@chromium.org | 6ebe68a | 2009-05-27 23:43:40 +0000 | [diff] [blame] | 724 | affected = filter(lambda x: not x.IsDirectory(), self._affected_files) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 725 | |
| 726 | if include_deletes: |
| 727 | return affected |
| 728 | else: |
| 729 | return filter(lambda x: x.Action() != 'D', affected) |
| 730 | |
maruel@chromium.org | 77c4f0f | 2009-05-29 18:53:04 +0000 | [diff] [blame] | 731 | def AffectedTextFiles(self, include_deletes=None): |
| 732 | """Return a list of the existing text files in a change.""" |
| 733 | if include_deletes is not None: |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 734 | warn("AffectedTextFiles(include_deletes=%s)" |
| 735 | " is deprecated and ignored" % str(include_deletes), |
| 736 | category=DeprecationWarning, |
| 737 | stacklevel=2) |
maruel@chromium.org | 77c4f0f | 2009-05-29 18:53:04 +0000 | [diff] [blame] | 738 | return filter(lambda x: x.IsTextFile(), |
| 739 | self.AffectedFiles(include_dirs=False, include_deletes=False)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 740 | |
| 741 | def LocalPaths(self, include_dirs=False): |
| 742 | """Convenience function.""" |
| 743 | return [af.LocalPath() for af in self.AffectedFiles(include_dirs)] |
| 744 | |
| 745 | def AbsoluteLocalPaths(self, include_dirs=False): |
| 746 | """Convenience function.""" |
| 747 | return [af.AbsoluteLocalPath() for af in self.AffectedFiles(include_dirs)] |
| 748 | |
| 749 | def ServerPaths(self, include_dirs=False): |
| 750 | """Convenience function.""" |
| 751 | return [af.ServerPath() for af in self.AffectedFiles(include_dirs)] |
| 752 | |
| 753 | def RightHandSideLines(self): |
| 754 | """An iterator over all text lines in "new" version of changed files. |
| 755 | |
| 756 | Lists lines from new or modified text files in the change. |
| 757 | |
| 758 | This is useful for doing line-by-line regex checks, like checking for |
| 759 | trailing whitespace. |
| 760 | |
| 761 | Yields: |
| 762 | a 3 tuple: |
| 763 | the AffectedFile instance of the current file; |
| 764 | integer line number (1-based); and |
| 765 | the contents of the line as a string. |
| 766 | """ |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 767 | return _RightHandSideLinesImpl( |
| 768 | x for x in self.AffectedFiles(include_deletes=False) |
| 769 | if x.IsTextFile()) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 770 | |
| 771 | |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 772 | class SvnChange(Change): |
| 773 | _AFFECTED_FILES = SvnAffectedFile |
| 774 | |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 775 | def __init__(self, *args, **kwargs): |
| 776 | Change.__init__(self, *args, **kwargs) |
| 777 | self.scm = 'svn' |
thestig@chromium.org | 6bd3170 | 2009-09-02 23:29:07 +0000 | [diff] [blame] | 778 | self._changelists = None |
| 779 | |
| 780 | def _GetChangeLists(self): |
| 781 | """Get all change lists.""" |
| 782 | if self._changelists == None: |
| 783 | previous_cwd = os.getcwd() |
| 784 | os.chdir(self.RepositoryRoot()) |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 785 | # Need to import here to avoid circular dependency. |
| 786 | import gcl |
thestig@chromium.org | 6bd3170 | 2009-09-02 23:29:07 +0000 | [diff] [blame] | 787 | self._changelists = gcl.GetModifiedFiles() |
| 788 | os.chdir(previous_cwd) |
| 789 | return self._changelists |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 790 | |
| 791 | def GetAllModifiedFiles(self): |
| 792 | """Get all modified files.""" |
thestig@chromium.org | 6bd3170 | 2009-09-02 23:29:07 +0000 | [diff] [blame] | 793 | changelists = self._GetChangeLists() |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 794 | all_modified_files = [] |
| 795 | for cl in changelists.values(): |
thestig@chromium.org | 6bd3170 | 2009-09-02 23:29:07 +0000 | [diff] [blame] | 796 | all_modified_files.extend( |
| 797 | [os.path.join(self.RepositoryRoot(), f[1]) for f in cl]) |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 798 | return all_modified_files |
| 799 | |
| 800 | def GetModifiedFiles(self): |
| 801 | """Get modified files in the current CL.""" |
thestig@chromium.org | 6bd3170 | 2009-09-02 23:29:07 +0000 | [diff] [blame] | 802 | changelists = self._GetChangeLists() |
| 803 | return [os.path.join(self.RepositoryRoot(), f[1]) |
| 804 | for f in changelists[self.Name()]] |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 805 | |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 806 | |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 807 | class GitChange(Change): |
| 808 | _AFFECTED_FILES = GitAffectedFile |
| 809 | |
thestig@chromium.org | da8cddd | 2009-08-13 00:25:55 +0000 | [diff] [blame] | 810 | def __init__(self, *args, **kwargs): |
| 811 | Change.__init__(self, *args, **kwargs) |
| 812 | self.scm = 'git' |
| 813 | |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 814 | |
maruel@chromium.org | 4661e0c | 2009-06-04 00:45:26 +0000 | [diff] [blame] | 815 | def ListRelevantPresubmitFiles(files, root): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 816 | """Finds all presubmit files that apply to a given set of source files. |
| 817 | |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 818 | If inherit-review-settings-ok is present right under root, looks for |
| 819 | PRESUBMIT.py in directories enclosing root. |
| 820 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 821 | Args: |
| 822 | files: An iterable container containing file paths. |
maruel@chromium.org | 4661e0c | 2009-06-04 00:45:26 +0000 | [diff] [blame] | 823 | root: Path where to stop searching. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 824 | |
| 825 | Return: |
maruel@chromium.org | 4661e0c | 2009-06-04 00:45:26 +0000 | [diff] [blame] | 826 | List of absolute paths of the existing PRESUBMIT.py scripts. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 827 | """ |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 828 | files = [normpath(os.path.join(root, f)) for f in files] |
| 829 | |
| 830 | # List all the individual directories containing files. |
| 831 | directories = set([os.path.dirname(f) for f in files]) |
| 832 | |
| 833 | # Ignore root if inherit-review-settings-ok is present. |
| 834 | if os.path.isfile(os.path.join(root, 'inherit-review-settings-ok')): |
| 835 | root = None |
| 836 | |
| 837 | # Collect all unique directories that may contain PRESUBMIT.py. |
| 838 | candidates = set() |
| 839 | for directory in directories: |
| 840 | while True: |
| 841 | if directory in candidates: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 842 | break |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 843 | candidates.add(directory) |
| 844 | if directory == root: |
maruel@chromium.org | 4661e0c | 2009-06-04 00:45:26 +0000 | [diff] [blame] | 845 | break |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 846 | parent_dir = os.path.dirname(directory) |
| 847 | if parent_dir == directory: |
| 848 | # We hit the system root directory. |
| 849 | break |
| 850 | directory = parent_dir |
| 851 | |
| 852 | # Look for PRESUBMIT.py in all candidate directories. |
| 853 | results = [] |
| 854 | for directory in sorted(list(candidates)): |
| 855 | p = os.path.join(directory, 'PRESUBMIT.py') |
| 856 | if os.path.isfile(p): |
| 857 | results.append(p) |
| 858 | |
maruel@chromium.org | 5d0dc43 | 2011-01-03 02:40:37 +0000 | [diff] [blame] | 859 | logging.debug('Presubmit files: %s' % ','.join(results)) |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 860 | return results |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 861 | |
| 862 | |
thestig@chromium.org | de24345 | 2009-10-06 21:02:56 +0000 | [diff] [blame] | 863 | class GetTrySlavesExecuter(object): |
maruel@chromium.org | cb2985f | 2010-11-03 14:08:31 +0000 | [diff] [blame] | 864 | @staticmethod |
| 865 | def ExecPresubmitScript(script_text): |
thestig@chromium.org | de24345 | 2009-10-06 21:02:56 +0000 | [diff] [blame] | 866 | """Executes GetPreferredTrySlaves() from a single presubmit script. |
| 867 | |
| 868 | Args: |
| 869 | script_text: The text of the presubmit script. |
| 870 | |
| 871 | Return: |
| 872 | A list of try slaves. |
| 873 | """ |
| 874 | context = {} |
| 875 | exec script_text in context |
| 876 | |
| 877 | function_name = 'GetPreferredTrySlaves' |
| 878 | if function_name in context: |
| 879 | result = eval(function_name + '()', context) |
| 880 | if not isinstance(result, types.ListType): |
| 881 | raise exceptions.RuntimeError( |
| 882 | 'Presubmit functions must return a list, got a %s instead: %s' % |
| 883 | (type(result), str(result))) |
| 884 | for item in result: |
| 885 | if not isinstance(item, basestring): |
| 886 | raise exceptions.RuntimeError('All try slaves names must be strings.') |
| 887 | if item != item.strip(): |
| 888 | raise exceptions.RuntimeError('Try slave names cannot start/end' |
| 889 | 'with whitespace') |
| 890 | else: |
| 891 | result = [] |
| 892 | return result |
| 893 | |
| 894 | |
| 895 | def DoGetTrySlaves(changed_files, |
| 896 | repository_root, |
| 897 | default_presubmit, |
| 898 | verbose, |
| 899 | output_stream): |
| 900 | """Get the list of try servers from the presubmit scripts. |
| 901 | |
| 902 | Args: |
| 903 | changed_files: List of modified files. |
| 904 | repository_root: The repository root. |
| 905 | default_presubmit: A default presubmit script to execute in any case. |
| 906 | verbose: Prints debug info. |
| 907 | output_stream: A stream to write debug output to. |
| 908 | |
| 909 | Return: |
| 910 | List of try slaves |
| 911 | """ |
| 912 | presubmit_files = ListRelevantPresubmitFiles(changed_files, repository_root) |
| 913 | if not presubmit_files and verbose: |
| 914 | output_stream.write("Warning, no presubmit.py found.\n") |
| 915 | results = [] |
| 916 | executer = GetTrySlavesExecuter() |
| 917 | if default_presubmit: |
| 918 | if verbose: |
| 919 | output_stream.write("Running default presubmit script.\n") |
| 920 | results += executer.ExecPresubmitScript(default_presubmit) |
| 921 | for filename in presubmit_files: |
| 922 | filename = os.path.abspath(filename) |
| 923 | if verbose: |
| 924 | output_stream.write("Running %s\n" % filename) |
| 925 | # Accept CRLF presubmit script. |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 926 | presubmit_script = gclient_utils.FileRead(filename, 'rU') |
thestig@chromium.org | de24345 | 2009-10-06 21:02:56 +0000 | [diff] [blame] | 927 | results += executer.ExecPresubmitScript(presubmit_script) |
| 928 | |
| 929 | slaves = list(set(results)) |
| 930 | if slaves and verbose: |
| 931 | output_stream.write(', '.join(slaves)) |
| 932 | output_stream.write('\n') |
| 933 | return slaves |
| 934 | |
| 935 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 936 | class PresubmitExecuter(object): |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 937 | def __init__(self, change, committing, tbr, host_url): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 938 | """ |
| 939 | Args: |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 940 | change: The Change object. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 941 | committing: True if 'gcl commit' is running, False if 'gcl upload' is. |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 942 | tbr: True if '--tbr' was passed to skip any reviewer/owner checks |
| 943 | host_url: scheme, host, and path of rietveld instance |
| 944 | (or None for default) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 945 | """ |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 946 | self.change = change |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 947 | self.committing = committing |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 948 | self.tbr = tbr |
| 949 | self.host_url = host_url |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 950 | |
| 951 | def ExecPresubmitScript(self, script_text, presubmit_path): |
| 952 | """Executes a single presubmit script. |
| 953 | |
| 954 | Args: |
| 955 | script_text: The text of the presubmit script. |
| 956 | presubmit_path: The path to the presubmit file (this will be reported via |
| 957 | input_api.PresubmitLocalPath()). |
| 958 | |
| 959 | Return: |
| 960 | A list of result objects, empty if no problems. |
| 961 | """ |
chase@chromium.org | 8e416c8 | 2009-10-06 04:30:44 +0000 | [diff] [blame] | 962 | |
| 963 | # Change to the presubmit file's directory to support local imports. |
| 964 | main_path = os.getcwd() |
| 965 | os.chdir(os.path.dirname(presubmit_path)) |
| 966 | |
| 967 | # Load the presubmit script into context. |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 968 | input_api = InputApi(self.change, presubmit_path, self.committing, |
| 969 | self.tbr, self.host_url) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 970 | context = {} |
| 971 | exec script_text in context |
| 972 | |
| 973 | # These function names must change if we make substantial changes to |
| 974 | # the presubmit API that are not backwards compatible. |
| 975 | if self.committing: |
| 976 | function_name = 'CheckChangeOnCommit' |
| 977 | else: |
| 978 | function_name = 'CheckChangeOnUpload' |
| 979 | if function_name in context: |
| 980 | context['__args'] = (input_api, OutputApi()) |
maruel@chromium.org | 5d0dc43 | 2011-01-03 02:40:37 +0000 | [diff] [blame] | 981 | logging.debug('Running %s in %s' % (function_name, presubmit_path)) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 982 | result = eval(function_name + '(*__args)', context) |
maruel@chromium.org | 5d0dc43 | 2011-01-03 02:40:37 +0000 | [diff] [blame] | 983 | logging.debug('Running %s done.' % function_name) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 984 | if not (isinstance(result, types.TupleType) or |
| 985 | isinstance(result, types.ListType)): |
| 986 | raise exceptions.RuntimeError( |
| 987 | 'Presubmit functions must return a tuple or list') |
| 988 | for item in result: |
| 989 | if not isinstance(item, OutputApi.PresubmitResult): |
| 990 | raise exceptions.RuntimeError( |
| 991 | 'All presubmit results must be of types derived from ' |
| 992 | 'output_api.PresubmitResult') |
| 993 | else: |
| 994 | result = () # no error since the script doesn't care about current event. |
| 995 | |
chase@chromium.org | 8e416c8 | 2009-10-06 04:30:44 +0000 | [diff] [blame] | 996 | # Return the process to the original working directory. |
| 997 | os.chdir(main_path) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 998 | return result |
| 999 | |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1000 | |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1001 | # TODO(dpranke): make all callers pass in tbr, host_url? |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 1002 | def DoPresubmitChecks(change, |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1003 | committing, |
| 1004 | verbose, |
| 1005 | output_stream, |
maruel@chromium.org | 0ff1fab | 2009-05-22 13:08:15 +0000 | [diff] [blame] | 1006 | input_stream, |
maruel@chromium.org | b0dfd35 | 2009-06-10 14:12:54 +0000 | [diff] [blame] | 1007 | default_presubmit, |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1008 | may_prompt, |
| 1009 | tbr=False, |
| 1010 | host_url=None): |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1011 | """Runs all presubmit checks that apply to the files in the change. |
| 1012 | |
| 1013 | This finds all PRESUBMIT.py files in directories enclosing the files in the |
| 1014 | change (up to the repository root) and calls the relevant entrypoint function |
| 1015 | depending on whether the change is being committed or uploaded. |
| 1016 | |
| 1017 | Prints errors, warnings and notifications. Prompts the user for warnings |
| 1018 | when needed. |
| 1019 | |
| 1020 | Args: |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 1021 | change: The Change object. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1022 | committing: True if 'gcl commit' is running, False if 'gcl upload' is. |
| 1023 | verbose: Prints debug info. |
| 1024 | output_stream: A stream to write output from presubmit tests to. |
| 1025 | input_stream: A stream to read input from the user. |
maruel@chromium.org | 0ff1fab | 2009-05-22 13:08:15 +0000 | [diff] [blame] | 1026 | default_presubmit: A default presubmit script to execute in any case. |
maruel@chromium.org | b0dfd35 | 2009-06-10 14:12:54 +0000 | [diff] [blame] | 1027 | may_prompt: Enable (y/n) questions on warning or error. |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1028 | tbr: was --tbr specified to skip any reviewer/owner checks? |
| 1029 | host_url: scheme, host, and port of host to use for rietveld-related |
| 1030 | checks |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1031 | |
maruel@chromium.org | ce8e46b | 2009-06-26 22:31:51 +0000 | [diff] [blame] | 1032 | Warning: |
| 1033 | If may_prompt is true, output_stream SHOULD be sys.stdout and input_stream |
| 1034 | SHOULD be sys.stdin. |
| 1035 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1036 | Return: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1037 | A PresubmitOutput object. Use output.should_continue() to figure out |
| 1038 | if there were errors or warnings and the caller should abort. |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1039 | """ |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1040 | output = PresubmitOutput(input_stream, output_stream) |
| 1041 | output.write("Running presubmit hooks...\n") |
jam@chromium.org | 2a891dc | 2009-08-20 20:33:37 +0000 | [diff] [blame] | 1042 | start_time = time.time() |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 1043 | presubmit_files = ListRelevantPresubmitFiles(change.AbsoluteLocalPaths(True), |
| 1044 | change.RepositoryRoot()) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1045 | if not presubmit_files and verbose: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1046 | output.write("Warning, no presubmit.py found.\n") |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1047 | results = [] |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1048 | executer = PresubmitExecuter(change, committing, tbr, host_url) |
maruel@chromium.org | 0ff1fab | 2009-05-22 13:08:15 +0000 | [diff] [blame] | 1049 | if default_presubmit: |
| 1050 | if verbose: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1051 | output.write("Running default presubmit script.\n") |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 1052 | fake_path = os.path.join(change.RepositoryRoot(), 'PRESUBMIT.py') |
maruel@chromium.org | 4661e0c | 2009-06-04 00:45:26 +0000 | [diff] [blame] | 1053 | results += executer.ExecPresubmitScript(default_presubmit, fake_path) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1054 | for filename in presubmit_files: |
maruel@chromium.org | 3d23524 | 2009-05-15 12:40:48 +0000 | [diff] [blame] | 1055 | filename = os.path.abspath(filename) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1056 | if verbose: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1057 | output.write("Running %s\n" % filename) |
maruel@chromium.org | c1675e2 | 2009-04-27 20:30:48 +0000 | [diff] [blame] | 1058 | # Accept CRLF presubmit script. |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 1059 | presubmit_script = gclient_utils.FileRead(filename, 'rU') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1060 | results += executer.ExecPresubmitScript(presubmit_script, filename) |
| 1061 | |
| 1062 | errors = [] |
| 1063 | notifications = [] |
| 1064 | warnings = [] |
| 1065 | for result in results: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1066 | if result.fatal: |
| 1067 | errors.append(result) |
| 1068 | elif result.should_prompt: |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1069 | warnings.append(result) |
| 1070 | else: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1071 | notifications.append(result) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1072 | |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1073 | for name, items in (('Messages', notifications), |
| 1074 | ('Warnings', warnings), |
| 1075 | ('ERRORS', errors)): |
| 1076 | if items: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1077 | output.write('** Presubmit %s **\n' % name) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1078 | for item in items: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1079 | item.handle(output) |
| 1080 | output.write('\n') |
pam@chromium.org | ed9a083 | 2009-09-09 22:48:55 +0000 | [diff] [blame] | 1081 | |
| 1082 | total_time = time.time() - start_time |
| 1083 | if total_time > 1.0: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1084 | output.write("Presubmit checks took %.1fs to calculate.\n" % total_time) |
pam@chromium.org | ed9a083 | 2009-09-09 22:48:55 +0000 | [diff] [blame] | 1085 | |
maruel@chromium.org | 07bbc21 | 2009-06-11 02:08:41 +0000 | [diff] [blame] | 1086 | if not errors and warnings and may_prompt: |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1087 | output.prompt_yes_no('There were presubmit warnings. ' |
| 1088 | 'Are you sure you wish to continue? (y/N): ') |
maruel@chromium.org | ce8e46b | 2009-06-26 22:31:51 +0000 | [diff] [blame] | 1089 | |
| 1090 | global _ASKED_FOR_FEEDBACK |
| 1091 | # Ask for feedback one time out of 5. |
| 1092 | if (len(results) and random.randint(0, 4) == 0 and not _ASKED_FOR_FEEDBACK): |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1093 | output.write("Was the presubmit check useful? Please send feedback " |
| 1094 | "& hate mail to maruel@chromium.org!\n") |
maruel@chromium.org | ce8e46b | 2009-06-26 22:31:51 +0000 | [diff] [blame] | 1095 | _ASKED_FOR_FEEDBACK = True |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1096 | return output |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1097 | |
| 1098 | |
| 1099 | def ScanSubDirs(mask, recursive): |
| 1100 | if not recursive: |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1101 | return [x for x in glob.glob(mask) if '.svn' not in x and '.git' not in x] |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1102 | else: |
| 1103 | results = [] |
| 1104 | for root, dirs, files in os.walk('.'): |
| 1105 | if '.svn' in dirs: |
| 1106 | dirs.remove('.svn') |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1107 | if '.git' in dirs: |
| 1108 | dirs.remove('.git') |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1109 | for name in files: |
| 1110 | if fnmatch.fnmatch(name, mask): |
| 1111 | results.append(os.path.join(root, name)) |
| 1112 | return results |
| 1113 | |
| 1114 | |
| 1115 | def ParseFiles(args, recursive): |
maruel@chromium.org | 7444c50 | 2011-02-09 14:02:11 +0000 | [diff] [blame] | 1116 | logging.debug('Searching for %s' % args) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1117 | files = [] |
| 1118 | for arg in args: |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 1119 | files.extend([('M', f) for f in ScanSubDirs(arg, recursive)]) |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1120 | return files |
| 1121 | |
| 1122 | |
| 1123 | def Main(argv): |
| 1124 | parser = optparse.OptionParser(usage="%prog [options]", |
| 1125 | version="%prog " + str(__version__)) |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1126 | parser.add_option("-c", "--commit", action="store_true", default=False, |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1127 | help="Use commit instead of upload checks") |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1128 | parser.add_option("-u", "--upload", action="store_false", dest='commit', |
| 1129 | help="Use upload instead of commit checks") |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1130 | parser.add_option("-r", "--recursive", action="store_true", |
| 1131 | help="Act recursively") |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1132 | parser.add_option("-v", "--verbose", action="store_true", default=False, |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1133 | help="Verbose output") |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1134 | parser.add_option("--files") |
maruel@chromium.org | 4ff922a | 2009-06-12 20:20:19 +0000 | [diff] [blame] | 1135 | parser.add_option("--name", default='no name') |
| 1136 | parser.add_option("--description", default='') |
| 1137 | parser.add_option("--issue", type='int', default=0) |
| 1138 | parser.add_option("--patchset", type='int', default=0) |
maruel@chromium.org | b1901a6 | 2010-06-16 00:18:47 +0000 | [diff] [blame] | 1139 | parser.add_option("--root", default=os.getcwd(), |
| 1140 | help="Search for PRESUBMIT.py up to this directory. " |
| 1141 | "If inherit-review-settings-ok is present in this " |
| 1142 | "directory, parent directories up to the root file " |
| 1143 | "system directories will also be searched.") |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1144 | parser.add_option("--default_presubmit") |
| 1145 | parser.add_option("--may_prompt", action='store_true', default=False) |
maruel@chromium.org | 82e5f28 | 2011-03-17 14:08:55 +0000 | [diff] [blame^] | 1146 | options, args = parser.parse_args(argv) |
maruel@chromium.org | 7444c50 | 2011-02-09 14:02:11 +0000 | [diff] [blame] | 1147 | if options.verbose: |
| 1148 | logging.basicConfig(level=logging.DEBUG) |
| 1149 | if os.path.isdir(os.path.join(options.root, '.svn')): |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1150 | change_class = SvnChange |
| 1151 | if not options.files: |
| 1152 | if args: |
| 1153 | options.files = ParseFiles(args, options.recursive) |
| 1154 | else: |
| 1155 | # Grab modified files. |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 1156 | options.files = scm.SVN.CaptureStatus([options.root]) |
maruel@chromium.org | c70a220 | 2009-06-17 12:55:10 +0000 | [diff] [blame] | 1157 | else: |
maruel@chromium.org | 7444c50 | 2011-02-09 14:02:11 +0000 | [diff] [blame] | 1158 | is_git = os.path.isdir(os.path.join(options.root, '.git')) |
| 1159 | if not is_git: |
| 1160 | is_git = (0 == subprocess.call( |
| 1161 | ['git', 'rev-parse', '--show-cdup'], |
| 1162 | stdout=subprocess.PIPE, cwd=options.root)) |
| 1163 | if is_git: |
| 1164 | # Only look at the subdirectories below cwd. |
| 1165 | change_class = GitChange |
| 1166 | if not options.files: |
| 1167 | if args: |
| 1168 | options.files = ParseFiles(args, options.recursive) |
| 1169 | else: |
| 1170 | # Grab modified files. |
| 1171 | options.files = scm.GIT.CaptureStatus([options.root]) |
| 1172 | else: |
| 1173 | logging.info('Doesn\'t seem under source control.') |
| 1174 | change_class = Change |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1175 | if options.verbose: |
maruel@chromium.org | 7444c50 | 2011-02-09 14:02:11 +0000 | [diff] [blame] | 1176 | if not options.files: |
| 1177 | print "Found no files." |
| 1178 | elif len(options.files) != 1: |
chase@chromium.org | 8e416c8 | 2009-10-06 04:30:44 +0000 | [diff] [blame] | 1179 | print "Found %d files." % len(options.files) |
| 1180 | else: |
| 1181 | print "Found 1 file." |
dpranke@chromium.org | 5ac2101 | 2011-03-16 02:58:25 +0000 | [diff] [blame] | 1182 | results = DoPresubmitChecks(change_class(options.name, |
| 1183 | options.description, |
| 1184 | options.root, |
| 1185 | options.files, |
| 1186 | options.issue, |
| 1187 | options.patchset), |
| 1188 | options.commit, |
| 1189 | options.verbose, |
| 1190 | sys.stdout, |
| 1191 | sys.stdin, |
| 1192 | options.default_presubmit, |
| 1193 | options.may_prompt) |
| 1194 | return not results.should_continue() |
maruel@google.com | fb2b8eb | 2009-04-23 21:03:42 +0000 | [diff] [blame] | 1195 | |
| 1196 | |
| 1197 | if __name__ == '__main__': |
maruel@chromium.org | 82e5f28 | 2011-03-17 14:08:55 +0000 | [diff] [blame^] | 1198 | sys.exit(Main(None)) |