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