[email protected] | a18130a | 2012-01-03 17:52:08 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Top-level presubmit script for Chromium. |
| 6 | |
[email protected] | f129379 | 2009-07-31 18:09:56 | [diff] [blame] | 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
[email protected] | 50d7d721e | 2009-11-15 17:56:18 | [diff] [blame] | 8 | for more details about the presubmit API built into gcl. |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 9 | """ |
| 10 | |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 11 | |
[email protected] | 9d16ad1 | 2011-12-14 20:49:47 | [diff] [blame] | 12 | import re |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 13 | import sys |
[email protected] | 9d16ad1 | 2011-12-14 20:49:47 | [diff] [blame] | 14 | |
| 15 | |
[email protected] | 379e7dd | 2010-01-28 17:39:21 | [diff] [blame] | 16 | _EXCLUDED_PATHS = ( |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 17 | r"^breakpad[\\\/].*", |
[email protected] | a18130a | 2012-01-03 17:52:08 | [diff] [blame] | 18 | r"^native_client_sdk[\\\/].*", |
| 19 | r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*", |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 20 | r"^skia[\\\/].*", |
| 21 | r"^v8[\\\/].*", |
| 22 | r".*MakeFile$", |
[email protected] | 1084ccc | 2012-03-14 03:22:53 | [diff] [blame] | 23 | r".+_autogen\.h$", |
[email protected] | 430641764 | 2009-06-11 00:33:40 | [diff] [blame] | 24 | ) |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 25 | |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 26 | |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 27 | _TEST_ONLY_WARNING = ( |
| 28 | 'You might be calling functions intended only for testing from\n' |
| 29 | 'production code. It is OK to ignore this warning if you know what\n' |
| 30 | 'you are doing, as the heuristics used to detect the situation are\n' |
| 31 | 'not perfect. The commit queue will not block on this warning.\n' |
| 32 | 'Email [email protected] if you have questions.') |
| 33 | |
| 34 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 35 | _BANNED_OBJC_FUNCTIONS = ( |
| 36 | ( |
| 37 | 'addTrackingRect:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 38 | ( |
| 39 | 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is' |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 40 | 'prohibited. Please use CrTrackingArea instead.', |
| 41 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 42 | ), |
| 43 | False, |
| 44 | ), |
| 45 | ( |
| 46 | 'NSTrackingArea', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 47 | ( |
| 48 | 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 49 | 'instead.', |
| 50 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 51 | ), |
| 52 | False, |
| 53 | ), |
| 54 | ( |
| 55 | 'convertPointFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 56 | ( |
| 57 | 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 58 | 'Please use |convertPoint:(point) fromView:nil| instead.', |
| 59 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 60 | ), |
| 61 | True, |
| 62 | ), |
| 63 | ( |
| 64 | 'convertPointToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 65 | ( |
| 66 | 'The use of -[NSView convertPointToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 67 | 'Please use |convertPoint:(point) toView:nil| instead.', |
| 68 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 69 | ), |
| 70 | True, |
| 71 | ), |
| 72 | ( |
| 73 | 'convertRectFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 74 | ( |
| 75 | 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 76 | 'Please use |convertRect:(point) fromView:nil| instead.', |
| 77 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 78 | ), |
| 79 | True, |
| 80 | ), |
| 81 | ( |
| 82 | 'convertRectToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 83 | ( |
| 84 | 'The use of -[NSView convertRectToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 85 | 'Please use |convertRect:(point) toView:nil| instead.', |
| 86 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 87 | ), |
| 88 | True, |
| 89 | ), |
| 90 | ( |
| 91 | 'convertSizeFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 92 | ( |
| 93 | 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 94 | 'Please use |convertSize:(point) fromView:nil| instead.', |
| 95 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 96 | ), |
| 97 | True, |
| 98 | ), |
| 99 | ( |
| 100 | 'convertSizeToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 101 | ( |
| 102 | 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 103 | 'Please use |convertSize:(point) toView:nil| instead.', |
| 104 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 105 | ), |
| 106 | True, |
| 107 | ), |
| 108 | ) |
| 109 | |
| 110 | |
| 111 | _BANNED_CPP_FUNCTIONS = ( |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 112 | # Make sure that gtest's FRIEND_TEST() macro is not used; the |
| 113 | # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be |
| 114 | # used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes. |
| 115 | ( |
| 116 | 'FRIEND_TEST(', |
| 117 | ( |
[email protected] | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 118 | 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 119 | 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.', |
| 120 | ), |
| 121 | False, |
| 122 | ), |
| 123 | ( |
| 124 | 'ScopedAllowIO', |
| 125 | ( |
[email protected] | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 126 | 'New code should not use ScopedAllowIO. Post a task to the blocking', |
| 127 | 'pool or the FILE thread instead.', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 128 | ), |
[email protected] | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 129 | True, |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 130 | ), |
| 131 | ( |
| 132 | 'FilePathWatcher::Delegate', |
| 133 | ( |
[email protected] | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 134 | 'New code should not use FilePathWatcher::Delegate. Use the callback', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 135 | 'interface instead.', |
| 136 | ), |
| 137 | False, |
| 138 | ), |
[email protected] | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 139 | ( |
| 140 | 'browser::FindLastActiveWithProfile', |
| 141 | ( |
| 142 | 'This function is deprecated and we\'re working on removing it. Pass', |
| 143 | 'more context to get a Browser*, like a WebContents, window, or session', |
| 144 | 'id. Talk to ben@ or jam@ for more information.', |
| 145 | ), |
| 146 | True, |
| 147 | ), |
| 148 | ( |
| 149 | 'browser::FindBrowserWithProfile', |
| 150 | ( |
| 151 | 'This function is deprecated and we\'re working on removing it. Pass', |
| 152 | 'more context to get a Browser*, like a WebContents, window, or session', |
| 153 | 'id. Talk to ben@ or jam@ for more information.', |
| 154 | ), |
| 155 | True, |
| 156 | ), |
| 157 | ( |
| 158 | 'browser::FindAnyBrowser', |
| 159 | ( |
| 160 | 'This function is deprecated and we\'re working on removing it. Pass', |
| 161 | 'more context to get a Browser*, like a WebContents, window, or session', |
| 162 | 'id. Talk to ben@ or jam@ for more information.', |
| 163 | ), |
| 164 | True, |
| 165 | ), |
| 166 | ( |
| 167 | 'browser::FindOrCreateTabbedBrowser', |
| 168 | ( |
| 169 | 'This function is deprecated and we\'re working on removing it. Pass', |
| 170 | 'more context to get a Browser*, like a WebContents, window, or session', |
| 171 | 'id. Talk to ben@ or jam@ for more information.', |
| 172 | ), |
| 173 | True, |
| 174 | ), |
| 175 | ( |
| 176 | 'browser::FindTabbedBrowser', |
| 177 | ( |
| 178 | 'This function is deprecated and we\'re working on removing it. Pass', |
| 179 | 'more context to get a Browser*, like a WebContents, window, or session', |
| 180 | 'id. Talk to ben@ or jam@ for more information.', |
| 181 | ), |
| 182 | True, |
| 183 | ), |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 184 | ) |
| 185 | |
| 186 | |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 187 | |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 188 | def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api): |
| 189 | """Attempts to prevent use of functions intended only for testing in |
| 190 | non-testing code. For now this is just a best-effort implementation |
| 191 | that ignores header files and may have some false positives. A |
| 192 | better implementation would probably need a proper C++ parser. |
| 193 | """ |
| 194 | # We only scan .cc files and the like, as the declaration of |
| 195 | # for-testing functions in header files are hard to distinguish from |
| 196 | # calls to such functions without a proper C++ parser. |
[email protected] | 403bfbc9 | 2012-06-11 23:30:09 | [diff] [blame] | 197 | platform_specifiers = r'(_(android|chromeos|gtk|mac|posix|win))?' |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 198 | source_extensions = r'\.(cc|cpp|cxx|mm)$' |
| 199 | file_inclusion_pattern = r'.+%s' % source_extensions |
[email protected] | 19e77fd | 2011-10-20 05:24:05 | [diff] [blame] | 200 | file_exclusion_patterns = ( |
[email protected] | e21ce38 | 2012-01-04 18:48:25 | [diff] [blame] | 201 | r'.*[/\\](test_|mock_).+%s' % source_extensions, |
[email protected] | c762d25 | 2012-02-28 02:07:24 | [diff] [blame] | 202 | r'.+_test_(base|support|util)%s' % source_extensions, |
[email protected] | 403bfbc9 | 2012-06-11 23:30:09 | [diff] [blame] | 203 | r'.+_(api|browser|perf|unit|ui)?test%s%s' % (platform_specifiers, |
| 204 | source_extensions), |
[email protected] | 19e77fd | 2011-10-20 05:24:05 | [diff] [blame] | 205 | r'.+profile_sync_service_harness%s' % source_extensions, |
| 206 | ) |
| 207 | path_exclusion_patterns = ( |
| 208 | r'.*[/\\](test|tool(s)?)[/\\].*', |
| 209 | # At request of folks maintaining this folder. |
| 210 | r'chrome[/\\]browser[/\\]automation[/\\].*', |
| 211 | ) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 212 | |
| 213 | base_function_pattern = r'ForTest(ing)?|for_test(ing)?' |
| 214 | inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern) |
| 215 | exclusion_pattern = input_api.re.compile( |
| 216 | r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % ( |
| 217 | base_function_pattern, base_function_pattern)) |
| 218 | |
| 219 | def FilterFile(affected_file): |
[email protected] | 19e77fd | 2011-10-20 05:24:05 | [diff] [blame] | 220 | black_list = (file_exclusion_patterns + path_exclusion_patterns + |
[email protected] | 3afb12a4 | 2011-08-15 13:48:33 | [diff] [blame] | 221 | _EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 222 | return input_api.FilterSourceFile( |
| 223 | affected_file, |
| 224 | white_list=(file_inclusion_pattern, ), |
| 225 | black_list=black_list) |
| 226 | |
| 227 | problems = [] |
| 228 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 229 | local_path = f.LocalPath() |
| 230 | lines = input_api.ReadFile(f).splitlines() |
| 231 | line_number = 0 |
| 232 | for line in lines: |
| 233 | if (inclusion_pattern.search(line) and |
| 234 | not exclusion_pattern.search(line)): |
| 235 | problems.append( |
| 236 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
| 237 | line_number += 1 |
| 238 | |
| 239 | if problems: |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 240 | if not input_api.is_committing: |
| 241 | return [output_api.PresubmitPromptWarning(_TEST_ONLY_WARNING, problems)] |
| 242 | else: |
| 243 | # We don't warn on commit, to avoid stopping commits going through CQ. |
| 244 | return [output_api.PresubmitNotifyResult(_TEST_ONLY_WARNING, problems)] |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 245 | else: |
| 246 | return [] |
| 247 | |
| 248 | |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 249 | def _CheckNoIOStreamInHeaders(input_api, output_api): |
| 250 | """Checks to make sure no .h files include <iostream>.""" |
| 251 | files = [] |
| 252 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 253 | input_api.re.MULTILINE) |
| 254 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 255 | if not f.LocalPath().endswith('.h'): |
| 256 | continue |
| 257 | contents = input_api.ReadFile(f) |
| 258 | if pattern.search(contents): |
| 259 | files.append(f) |
| 260 | |
| 261 | if len(files): |
| 262 | return [ output_api.PresubmitError( |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 263 | 'Do not #include <iostream> in header files, since it inserts static ' |
| 264 | 'initialization into every file including the header. Instead, ' |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 265 | '#include <ostream>. See http://crbug.com/94794', |
| 266 | files) ] |
| 267 | return [] |
| 268 | |
| 269 | |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 270 | def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api): |
| 271 | """Checks to make sure no source files use UNIT_TEST""" |
| 272 | problems = [] |
| 273 | for f in input_api.AffectedFiles(): |
| 274 | if (not f.LocalPath().endswith(('.cc', '.mm'))): |
| 275 | continue |
| 276 | |
| 277 | for line_num, line in f.ChangedContents(): |
| 278 | if 'UNIT_TEST' in line: |
| 279 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 280 | |
| 281 | if not problems: |
| 282 | return [] |
| 283 | return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' + |
| 284 | '\n'.join(problems))] |
| 285 | |
| 286 | |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 287 | def _CheckNoNewWStrings(input_api, output_api): |
| 288 | """Checks to make sure we don't introduce use of wstrings.""" |
[email protected] | 55463aa6 | 2011-10-12 00:48:27 | [diff] [blame] | 289 | problems = [] |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 290 | for f in input_api.AffectedFiles(): |
[email protected] | b5c2429 | 2011-11-28 14:38:20 | [diff] [blame] | 291 | if (not f.LocalPath().endswith(('.cc', '.h')) or |
| 292 | f.LocalPath().endswith('test.cc')): |
| 293 | continue |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 294 | |
[email protected] | b5c2429 | 2011-11-28 14:38:20 | [diff] [blame] | 295 | for line_num, line in f.ChangedContents(): |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 296 | if 'wstring' in line: |
[email protected] | 55463aa6 | 2011-10-12 00:48:27 | [diff] [blame] | 297 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 298 | |
[email protected] | 55463aa6 | 2011-10-12 00:48:27 | [diff] [blame] | 299 | if not problems: |
| 300 | return [] |
| 301 | return [output_api.PresubmitPromptWarning('New code should not use wstrings.' |
| 302 | ' If you are calling an API that accepts a wstring, fix the API.\n' + |
| 303 | '\n'.join(problems))] |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 304 | |
| 305 | |
[email protected] | 2a8ac9c | 2011-10-19 17:20:44 | [diff] [blame] | 306 | def _CheckNoDEPSGIT(input_api, output_api): |
| 307 | """Make sure .DEPS.git is never modified manually.""" |
| 308 | if any(f.LocalPath().endswith('.DEPS.git') for f in |
| 309 | input_api.AffectedFiles()): |
| 310 | return [output_api.PresubmitError( |
| 311 | 'Never commit changes to .DEPS.git. This file is maintained by an\n' |
| 312 | 'automated system based on what\'s in DEPS and your changes will be\n' |
| 313 | 'overwritten.\n' |
| 314 | 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n' |
| 315 | 'for more information')] |
| 316 | return [] |
| 317 | |
| 318 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 319 | def _CheckNoBannedFunctions(input_api, output_api): |
| 320 | """Make sure that banned functions are not used.""" |
| 321 | warnings = [] |
| 322 | errors = [] |
| 323 | |
| 324 | file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h')) |
| 325 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 326 | for line_num, line in f.ChangedContents(): |
| 327 | for func_name, message, error in _BANNED_OBJC_FUNCTIONS: |
| 328 | if func_name in line: |
| 329 | problems = warnings; |
| 330 | if error: |
| 331 | problems = errors; |
| 332 | problems.append(' %s:%d:' % (f.LocalPath(), line_num)) |
| 333 | for message_line in message: |
| 334 | problems.append(' %s' % message_line) |
| 335 | |
| 336 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h')) |
| 337 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 338 | for line_num, line in f.ChangedContents(): |
| 339 | for func_name, message, error in _BANNED_CPP_FUNCTIONS: |
| 340 | if func_name in line: |
| 341 | problems = warnings; |
| 342 | if error: |
| 343 | problems = errors; |
| 344 | problems.append(' %s:%d:' % (f.LocalPath(), line_num)) |
| 345 | for message_line in message: |
| 346 | problems.append(' %s' % message_line) |
| 347 | |
| 348 | result = [] |
| 349 | if (warnings): |
| 350 | result.append(output_api.PresubmitPromptWarning( |
| 351 | 'Banned functions were used.\n' + '\n'.join(warnings))) |
| 352 | if (errors): |
| 353 | result.append(output_api.PresubmitError( |
| 354 | 'Banned functions were used.\n' + '\n'.join(errors))) |
| 355 | return result |
| 356 | |
| 357 | |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 358 | def _CheckNoPragmaOnce(input_api, output_api): |
| 359 | """Make sure that banned functions are not used.""" |
| 360 | files = [] |
| 361 | pattern = input_api.re.compile(r'^#pragma\s+once', |
| 362 | input_api.re.MULTILINE) |
| 363 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 364 | if not f.LocalPath().endswith('.h'): |
| 365 | continue |
| 366 | contents = input_api.ReadFile(f) |
| 367 | if pattern.search(contents): |
| 368 | files.append(f) |
| 369 | |
| 370 | if files: |
| 371 | return [output_api.PresubmitError( |
| 372 | 'Do not use #pragma once in header files.\n' |
| 373 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 374 | files)] |
| 375 | return [] |
| 376 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 377 | |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 378 | def _CheckUnwantedDependencies(input_api, output_api): |
| 379 | """Runs checkdeps on #include statements added in this |
| 380 | change. Breaking - rules is an error, breaking ! rules is a |
| 381 | warning. |
| 382 | """ |
| 383 | # We need to wait until we have an input_api object and use this |
| 384 | # roundabout construct to import checkdeps because this file is |
| 385 | # eval-ed and thus doesn't have __file__. |
| 386 | original_sys_path = sys.path |
| 387 | try: |
| 388 | sys.path = sys.path + [input_api.os_path.join( |
| 389 | input_api.PresubmitLocalPath(), 'tools', 'checkdeps')] |
| 390 | import checkdeps |
| 391 | from cpp_checker import CppChecker |
| 392 | from rules import Rule |
| 393 | finally: |
| 394 | # Restore sys.path to what it was before. |
| 395 | sys.path = original_sys_path |
| 396 | |
| 397 | added_includes = [] |
| 398 | for f in input_api.AffectedFiles(): |
| 399 | if not CppChecker.IsCppFile(f.LocalPath()): |
| 400 | continue |
| 401 | |
| 402 | changed_lines = [line for line_num, line in f.ChangedContents()] |
| 403 | added_includes.append([f.LocalPath(), changed_lines]) |
| 404 | |
| 405 | deps_checker = checkdeps.DepsChecker() |
| 406 | |
| 407 | error_descriptions = [] |
| 408 | warning_descriptions = [] |
| 409 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 410 | added_includes): |
| 411 | description_with_path = '%s\n %s' % (path, rule_description) |
| 412 | if rule_type == Rule.DISALLOW: |
| 413 | error_descriptions.append(description_with_path) |
| 414 | else: |
| 415 | warning_descriptions.append(description_with_path) |
| 416 | |
| 417 | results = [] |
| 418 | if error_descriptions: |
| 419 | results.append(output_api.PresubmitError( |
| 420 | 'You added one or more #includes that violate checkdeps rules.', |
| 421 | error_descriptions)) |
| 422 | if warning_descriptions: |
| 423 | results.append(output_api.PresubmitPromptWarning( |
| 424 | 'You added one or more #includes of files that are temporarily\n' |
| 425 | 'allowed but being removed. Can you avoid introducing the\n' |
| 426 | '#include? See relevant DEPS file(s) for details and contacts.', |
| 427 | warning_descriptions)) |
| 428 | return results |
| 429 | |
| 430 | |
[email protected] | 22c9bd7 | 2011-03-27 16:47:39 | [diff] [blame] | 431 | def _CommonChecks(input_api, output_api): |
| 432 | """Checks common to both upload and commit.""" |
| 433 | results = [] |
| 434 | results.extend(input_api.canned_checks.PanProjectChecks( |
| 435 | input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 436 | results.extend(_CheckAuthorizedAuthor(input_api, output_api)) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 437 | results.extend( |
| 438 | _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 439 | results.extend(_CheckNoIOStreamInHeaders(input_api, output_api)) |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 440 | results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api)) |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 441 | results.extend(_CheckNoNewWStrings(input_api, output_api)) |
[email protected] | 2a8ac9c | 2011-10-19 17:20:44 | [diff] [blame] | 442 | results.extend(_CheckNoDEPSGIT(input_api, output_api)) |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 443 | results.extend(_CheckNoBannedFunctions(input_api, output_api)) |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 444 | results.extend(_CheckNoPragmaOnce(input_api, output_api)) |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 445 | results.extend(_CheckUnwantedDependencies(input_api, output_api)) |
[email protected] | 22c9bd7 | 2011-03-27 16:47:39 | [diff] [blame] | 446 | return results |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 447 | |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 448 | |
| 449 | def _CheckSubversionConfig(input_api, output_api): |
| 450 | """Verifies the subversion config file is correctly setup. |
| 451 | |
| 452 | Checks that autoprops are enabled, returns an error otherwise. |
| 453 | """ |
| 454 | join = input_api.os_path.join |
| 455 | if input_api.platform == 'win32': |
| 456 | appdata = input_api.environ.get('APPDATA', '') |
| 457 | if not appdata: |
| 458 | return [output_api.PresubmitError('%APPDATA% is not configured.')] |
| 459 | path = join(appdata, 'Subversion', 'config') |
| 460 | else: |
| 461 | home = input_api.environ.get('HOME', '') |
| 462 | if not home: |
| 463 | return [output_api.PresubmitError('$HOME is not configured.')] |
| 464 | path = join(home, '.subversion', 'config') |
| 465 | |
| 466 | error_msg = ( |
| 467 | 'Please look at http://dev.chromium.org/developers/coding-style to\n' |
| 468 | 'configure your subversion configuration file. This enables automatic\n' |
[email protected] | c6a3c10b | 2011-01-24 16:14:20 | [diff] [blame] | 469 | 'properties to simplify the project maintenance.\n' |
| 470 | 'Pro-tip: just download and install\n' |
| 471 | 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n') |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 472 | |
| 473 | try: |
| 474 | lines = open(path, 'r').read().splitlines() |
| 475 | # Make sure auto-props is enabled and check for 2 Chromium standard |
| 476 | # auto-prop. |
| 477 | if (not '*.cc = svn:eol-style=LF' in lines or |
| 478 | not '*.pdf = svn:mime-type=application/pdf' in lines or |
| 479 | not 'enable-auto-props = yes' in lines): |
| 480 | return [ |
[email protected] | 79ed7e6 | 2011-02-21 21:08:53 | [diff] [blame] | 481 | output_api.PresubmitNotifyResult( |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 482 | 'It looks like you have not configured your subversion config ' |
[email protected] | b5359c0 | 2011-02-01 20:29:56 | [diff] [blame] | 483 | 'file or it is not up-to-date.\n' + error_msg) |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 484 | ] |
| 485 | except (OSError, IOError): |
| 486 | return [ |
[email protected] | 79ed7e6 | 2011-02-21 21:08:53 | [diff] [blame] | 487 | output_api.PresubmitNotifyResult( |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 488 | 'Can\'t find your subversion config file.\n' + error_msg) |
| 489 | ] |
| 490 | return [] |
| 491 | |
| 492 | |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 493 | def _CheckAuthorizedAuthor(input_api, output_api): |
| 494 | """For non-googler/chromites committers, verify the author's email address is |
| 495 | in AUTHORS. |
| 496 | """ |
[email protected] | 9bb9cb8 | 2011-06-13 20:43:01 | [diff] [blame] | 497 | # TODO(maruel): Add it to input_api? |
| 498 | import fnmatch |
| 499 | |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 500 | author = input_api.change.author_email |
[email protected] | 9bb9cb8 | 2011-06-13 20:43:01 | [diff] [blame] | 501 | if not author: |
| 502 | input_api.logging.info('No author, skipping AUTHOR check') |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 503 | return [] |
[email protected] | c9966329 | 2011-05-31 19:46:08 | [diff] [blame] | 504 | authors_path = input_api.os_path.join( |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 505 | input_api.PresubmitLocalPath(), 'AUTHORS') |
| 506 | valid_authors = ( |
| 507 | input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line) |
| 508 | for line in open(authors_path)) |
[email protected] | ac54b13 | 2011-06-06 18:11:18 | [diff] [blame] | 509 | valid_authors = [item.group(1).lower() for item in valid_authors if item] |
[email protected] | 9bb9cb8 | 2011-06-13 20:43:01 | [diff] [blame] | 510 | if input_api.verbose: |
| 511 | print 'Valid authors are %s' % ', '.join(valid_authors) |
[email protected] | d8b50be | 2011-06-15 14:19:44 | [diff] [blame] | 512 | if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors): |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 513 | return [output_api.PresubmitPromptWarning( |
| 514 | ('%s is not in AUTHORS file. If you are a new contributor, please visit' |
| 515 | '\n' |
| 516 | 'http://www.chromium.org/developers/contributing-code and read the ' |
| 517 | '"Legal" section\n' |
| 518 | 'If you are a chromite, verify the contributor signed the CLA.') % |
| 519 | author)] |
| 520 | return [] |
| 521 | |
| 522 | |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 523 | def CheckChangeOnUpload(input_api, output_api): |
| 524 | results = [] |
| 525 | results.extend(_CommonChecks(input_api, output_api)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 526 | return results |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 527 | |
| 528 | |
| 529 | def CheckChangeOnCommit(input_api, output_api): |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 530 | results = [] |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 531 | results.extend(_CommonChecks(input_api, output_api)) |
[email protected] | dd805fe | 2009-10-01 08:11:51 | [diff] [blame] | 532 | # TODO(thestig) temporarily disabled, doesn't work in third_party/ |
| 533 | #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories( |
| 534 | # input_api, output_api, sources)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 535 | # Make sure the tree is 'open'. |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 536 | results.extend(input_api.canned_checks.CheckTreeIsOpen( |
[email protected] | 7f23815 | 2009-08-12 19:00:34 | [diff] [blame] | 537 | input_api, |
| 538 | output_api, |
[email protected] | 4efa4214 | 2010-08-26 01:29:26 | [diff] [blame] | 539 | json_url='http://chromium-status.appspot.com/current?format=json')) |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 540 | results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api, |
[email protected] | 4ddc5df | 2011-12-12 03:05:04 | [diff] [blame] | 541 | output_api, 'http://codereview.chromium.org', |
[email protected] | c1ba4c5 | 2012-03-09 14:23:28 | [diff] [blame] | 542 | ('win_rel', 'linux_rel', 'mac_rel, win:compile'), |
| 543 | '[email protected]')) |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 544 | |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 545 | results.extend(input_api.canned_checks.CheckChangeHasBugField( |
| 546 | input_api, output_api)) |
[email protected] | c4b4756 | 2011-12-05 23:39:41 | [diff] [blame] | 547 | results.extend(input_api.canned_checks.CheckChangeHasDescription( |
| 548 | input_api, output_api)) |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 549 | results.extend(_CheckSubversionConfig(input_api, output_api)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 550 | return results |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 551 | |
| 552 | |
[email protected] | 5efb2a82 | 2011-09-27 23:06:13 | [diff] [blame] | 553 | def GetPreferredTrySlaves(project, change): |
[email protected] | 4ce995ea | 2012-06-27 02:13:10 | [diff] [blame] | 554 | files = change.LocalPaths() |
| 555 | |
[email protected] | 3019c90 | 2012-06-29 00:09:03 | [diff] [blame] | 556 | if not files: |
| 557 | return [] |
| 558 | |
[email protected] | 4ce995ea | 2012-06-27 02:13:10 | [diff] [blame] | 559 | if all(re.search('\.(m|mm)$|[/_]mac[/_.]', f) for f in files): |
[email protected] | 4ddc5df | 2011-12-12 03:05:04 | [diff] [blame] | 560 | return ['mac_rel'] |
[email protected] | 4ce995ea | 2012-06-27 02:13:10 | [diff] [blame] | 561 | if all(re.search('[/_]win[/_.]', f) for f in files): |
| 562 | return ['win_rel'] |
| 563 | if all(re.search('[/_]android[/_.]', f) for f in files): |
| 564 | return ['android'] |
| 565 | |
[email protected] | d602b90 | 2012-07-19 16:30:31 | [diff] [blame] | 566 | trybots = ['win_rel', 'linux_rel', 'mac_rel', 'linux_clang:compile', |
| 567 | 'android'] |
[email protected] | 911753b | 2012-08-02 12:11:54 | [diff] [blame^] | 568 | |
| 569 | # Match things like path/aura/file.cc and path/file_aura.cc. |
| 570 | # Same for chromeos. |
| 571 | if any(re.search('[/_](aura|chromeos)', f) for f in files): |
| 572 | trybots += ['linux_chromeos', 'linux_chromeos_clang:compile'] |
[email protected] | 4ce995ea | 2012-06-27 02:13:10 | [diff] [blame] | 573 | |
[email protected] | 4ce995ea | 2012-06-27 02:13:10 | [diff] [blame] | 574 | return trybots |