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