blob: efb665acff959892a21214708d31d8b5c6902684 [file] [log] [blame]
[email protected]a18130a2012-01-03 17:52:081# Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ca8d19842009-02-19 16:33:122# 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]f1293792009-07-31 18:09:567See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
[email protected]50d7d721e2009-11-15 17:56:188for more details about the presubmit API built into gcl.
[email protected]ca8d19842009-02-19 16:33:129"""
10
[email protected]eea609a2011-11-18 13:10:1211
[email protected]9d16ad12011-12-14 20:49:4712import re
[email protected]fbcafe5a2012-08-08 15:31:2213import subprocess
[email protected]55f9f382012-07-31 11:02:1814import sys
[email protected]9d16ad12011-12-14 20:49:4715
16
[email protected]379e7dd2010-01-28 17:39:2117_EXCLUDED_PATHS = (
[email protected]3e4eb112011-01-18 03:29:5418 r"^breakpad[\\\/].*",
[email protected]40d1dbb2012-10-26 07:18:0019 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py",
20 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py",
[email protected]8886ffcb2013-02-12 04:56:2821 r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk",
[email protected]a18130a2012-01-03 17:52:0822 r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
[email protected]3e4eb112011-01-18 03:29:5423 r"^skia[\\\/].*",
24 r"^v8[\\\/].*",
25 r".*MakeFile$",
[email protected]1084ccc2012-03-14 03:22:5326 r".+_autogen\.h$",
[email protected]ce145c02012-09-06 09:49:3427 r".+[\\\/]pnacl_shim\.c$",
[email protected]4306417642009-06-11 00:33:4028)
[email protected]ca8d19842009-02-19 16:33:1229
[email protected]06e6d0ff2012-12-11 01:36:4430# Fragment of a regular expression that matches C++ and Objective-C++
31# implementation files.
32_IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
33
34# Regular expression that matches code only used for test binaries
35# (best effort).
36_TEST_CODE_EXCLUDED_PATHS = (
37 r'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
38 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
[email protected]11e06082013-04-26 19:09:0339 r'.+_(api|browser|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
[email protected]e2d7e6f2013-04-23 12:57:1240 _IMPLEMENTATION_EXTENSIONS,
[email protected]06e6d0ff2012-12-11 01:36:4441 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
42 r'.*[/\\](test|tool(s)?)[/\\].*',
[email protected]ef070cc2013-05-03 11:53:0543 # content_shell is used for running layout tests.
44 r'content[/\\]shell[/\\].*',
[email protected]06e6d0ff2012-12-11 01:36:4445 # At request of folks maintaining this folder.
46 r'chrome[/\\]browser[/\\]automation[/\\].*',
47)
[email protected]ca8d19842009-02-19 16:33:1248
[email protected]eea609a2011-11-18 13:10:1249_TEST_ONLY_WARNING = (
50 'You might be calling functions intended only for testing from\n'
51 'production code. It is OK to ignore this warning if you know what\n'
52 'you are doing, as the heuristics used to detect the situation are\n'
53 'not perfect. The commit queue will not block on this warning.\n'
54 'Email [email protected] if you have questions.')
55
56
[email protected]cf9b78f2012-11-14 11:40:2857_INCLUDE_ORDER_WARNING = (
58 'Your #include order seems to be broken. Send mail to\n'
59 '[email protected] if this is not the case.')
60
61
[email protected]127f18ec2012-06-16 05:05:5962_BANNED_OBJC_FUNCTIONS = (
63 (
64 'addTrackingRect:',
[email protected]23e6cbc2012-06-16 18:51:2065 (
66 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
[email protected]127f18ec2012-06-16 05:05:5967 'prohibited. Please use CrTrackingArea instead.',
68 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
69 ),
70 False,
71 ),
72 (
73 'NSTrackingArea',
[email protected]23e6cbc2012-06-16 18:51:2074 (
75 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
[email protected]127f18ec2012-06-16 05:05:5976 'instead.',
77 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
78 ),
79 False,
80 ),
81 (
82 'convertPointFromBase:',
[email protected]23e6cbc2012-06-16 18:51:2083 (
84 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:5985 'Please use |convertPoint:(point) fromView:nil| instead.',
86 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
87 ),
88 True,
89 ),
90 (
91 'convertPointToBase:',
[email protected]23e6cbc2012-06-16 18:51:2092 (
93 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:5994 'Please use |convertPoint:(point) toView:nil| instead.',
95 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
96 ),
97 True,
98 ),
99 (
100 'convertRectFromBase:',
[email protected]23e6cbc2012-06-16 18:51:20101 (
102 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59103 'Please use |convertRect:(point) fromView:nil| instead.',
104 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
105 ),
106 True,
107 ),
108 (
109 'convertRectToBase:',
[email protected]23e6cbc2012-06-16 18:51:20110 (
111 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59112 'Please use |convertRect:(point) toView:nil| instead.',
113 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
114 ),
115 True,
116 ),
117 (
118 'convertSizeFromBase:',
[email protected]23e6cbc2012-06-16 18:51:20119 (
120 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59121 'Please use |convertSize:(point) fromView:nil| instead.',
122 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
123 ),
124 True,
125 ),
126 (
127 'convertSizeToBase:',
[email protected]23e6cbc2012-06-16 18:51:20128 (
129 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59130 'Please use |convertSize:(point) toView:nil| instead.',
131 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
132 ),
133 True,
134 ),
135)
136
137
138_BANNED_CPP_FUNCTIONS = (
[email protected]23e6cbc2012-06-16 18:51:20139 # Make sure that gtest's FRIEND_TEST() macro is not used; the
140 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
[email protected]e00ccc92012-11-01 17:32:30141 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
[email protected]23e6cbc2012-06-16 18:51:20142 (
143 'FRIEND_TEST(',
144 (
[email protected]e3c945502012-06-26 20:01:49145 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
[email protected]23e6cbc2012-06-16 18:51:20146 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
147 ),
148 False,
[email protected]7345da02012-11-27 14:31:49149 (),
[email protected]23e6cbc2012-06-16 18:51:20150 ),
151 (
152 'ScopedAllowIO',
153 (
[email protected]e3c945502012-06-26 20:01:49154 'New code should not use ScopedAllowIO. Post a task to the blocking',
155 'pool or the FILE thread instead.',
[email protected]23e6cbc2012-06-16 18:51:20156 ),
[email protected]e3c945502012-06-26 20:01:49157 True,
[email protected]7345da02012-11-27 14:31:49158 (
159 r"^content[\\\/]shell[\\\/]shell_browser_main\.cc$",
[email protected]398ad132013-04-02 15:11:01160 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
[email protected]7345da02012-11-27 14:31:49161 ),
[email protected]23e6cbc2012-06-16 18:51:20162 ),
[email protected]52657f62013-05-20 05:30:31163 (
164 'SkRefPtr',
165 (
166 'The use of SkRefPtr is prohibited. ',
167 'Please use skia::RefPtr instead.'
168 ),
169 True,
170 (),
171 ),
172 (
173 'SkAutoRef',
174 (
175 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
176 'Please use skia::RefPtr instead.'
177 ),
178 True,
179 (),
180 ),
181 (
182 'SkAutoTUnref',
183 (
184 'The use of SkAutoTUnref is dangerous because it implicitly ',
185 'converts to a raw pointer. Please use skia::RefPtr instead.'
186 ),
187 True,
188 (),
189 ),
190 (
191 'SkAutoUnref',
192 (
193 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
194 'because it implicitly converts to a raw pointer. ',
195 'Please use skia::RefPtr instead.'
196 ),
197 True,
198 (),
199 ),
[email protected]127f18ec2012-06-16 05:05:59200)
201
202
[email protected]b00342e7f2013-03-26 16:21:54203_VALID_OS_MACROS = (
204 # Please keep sorted.
205 'OS_ANDROID',
206 'OS_BSD',
207 'OS_CAT', # For testing.
208 'OS_CHROMEOS',
209 'OS_FREEBSD',
210 'OS_IOS',
211 'OS_LINUX',
212 'OS_MACOSX',
213 'OS_NACL',
214 'OS_OPENBSD',
215 'OS_POSIX',
216 'OS_SOLARIS',
[email protected]b00342e7f2013-03-26 16:21:54217 'OS_WIN',
218)
219
220
[email protected]55459852011-08-10 15:17:19221def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
222 """Attempts to prevent use of functions intended only for testing in
223 non-testing code. For now this is just a best-effort implementation
224 that ignores header files and may have some false positives. A
225 better implementation would probably need a proper C++ parser.
226 """
227 # We only scan .cc files and the like, as the declaration of
228 # for-testing functions in header files are hard to distinguish from
229 # calls to such functions without a proper C++ parser.
[email protected]06e6d0ff2012-12-11 01:36:44230 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
[email protected]55459852011-08-10 15:17:19231
232 base_function_pattern = r'ForTest(ing)?|for_test(ing)?'
233 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
[email protected]de4f7d22013-05-23 14:27:46234 comment_pattern = input_api.re.compile(r'//.*%s' % base_function_pattern)
[email protected]55459852011-08-10 15:17:19235 exclusion_pattern = input_api.re.compile(
236 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
237 base_function_pattern, base_function_pattern))
238
239 def FilterFile(affected_file):
[email protected]06e6d0ff2012-12-11 01:36:44240 black_list = (_EXCLUDED_PATHS +
241 _TEST_CODE_EXCLUDED_PATHS +
242 input_api.DEFAULT_BLACK_LIST)
[email protected]55459852011-08-10 15:17:19243 return input_api.FilterSourceFile(
244 affected_file,
245 white_list=(file_inclusion_pattern, ),
246 black_list=black_list)
247
248 problems = []
249 for f in input_api.AffectedSourceFiles(FilterFile):
250 local_path = f.LocalPath()
[email protected]2fdd1f362013-01-16 03:56:03251 lines = input_api.ReadFile(f).splitlines()
252 line_number = 0
253 for line in lines:
254 if (inclusion_pattern.search(line) and
[email protected]de4f7d22013-05-23 14:27:46255 not comment_pattern.search(line) and
[email protected]2fdd1f362013-01-16 03:56:03256 not exclusion_pattern.search(line)):
[email protected]55459852011-08-10 15:17:19257 problems.append(
[email protected]2fdd1f362013-01-16 03:56:03258 '%s:%d\n %s' % (local_path, line_number, line.strip()))
259 line_number += 1
[email protected]55459852011-08-10 15:17:19260
261 if problems:
[email protected]f7051d52013-04-02 18:31:42262 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
[email protected]2fdd1f362013-01-16 03:56:03263 else:
264 return []
[email protected]55459852011-08-10 15:17:19265
266
[email protected]10689ca2011-09-02 02:31:54267def _CheckNoIOStreamInHeaders(input_api, output_api):
268 """Checks to make sure no .h files include <iostream>."""
269 files = []
270 pattern = input_api.re.compile(r'^#include\s*<iostream>',
271 input_api.re.MULTILINE)
272 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
273 if not f.LocalPath().endswith('.h'):
274 continue
275 contents = input_api.ReadFile(f)
276 if pattern.search(contents):
277 files.append(f)
278
279 if len(files):
280 return [ output_api.PresubmitError(
[email protected]6c063c62012-07-11 19:11:06281 'Do not #include <iostream> in header files, since it inserts static '
282 'initialization into every file including the header. Instead, '
[email protected]10689ca2011-09-02 02:31:54283 '#include <ostream>. See http://crbug.com/94794',
284 files) ]
285 return []
286
287
[email protected]72df4e782012-06-21 16:28:18288def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
289 """Checks to make sure no source files use UNIT_TEST"""
290 problems = []
291 for f in input_api.AffectedFiles():
292 if (not f.LocalPath().endswith(('.cc', '.mm'))):
293 continue
294
295 for line_num, line in f.ChangedContents():
296 if 'UNIT_TEST' in line:
297 problems.append(' %s:%d' % (f.LocalPath(), line_num))
298
299 if not problems:
300 return []
301 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
302 '\n'.join(problems))]
303
304
[email protected]8ea5d4b2011-09-13 21:49:22305def _CheckNoNewWStrings(input_api, output_api):
306 """Checks to make sure we don't introduce use of wstrings."""
[email protected]55463aa62011-10-12 00:48:27307 problems = []
[email protected]8ea5d4b2011-09-13 21:49:22308 for f in input_api.AffectedFiles():
[email protected]b5c24292011-11-28 14:38:20309 if (not f.LocalPath().endswith(('.cc', '.h')) or
310 f.LocalPath().endswith('test.cc')):
311 continue
[email protected]8ea5d4b2011-09-13 21:49:22312
[email protected]a11dbe9b2012-08-07 01:32:58313 allowWString = False
[email protected]b5c24292011-11-28 14:38:20314 for line_num, line in f.ChangedContents():
[email protected]a11dbe9b2012-08-07 01:32:58315 if 'presubmit: allow wstring' in line:
316 allowWString = True
317 elif not allowWString and 'wstring' in line:
[email protected]55463aa62011-10-12 00:48:27318 problems.append(' %s:%d' % (f.LocalPath(), line_num))
[email protected]a11dbe9b2012-08-07 01:32:58319 allowWString = False
320 else:
321 allowWString = False
[email protected]8ea5d4b2011-09-13 21:49:22322
[email protected]55463aa62011-10-12 00:48:27323 if not problems:
324 return []
325 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
[email protected]a11dbe9b2012-08-07 01:32:58326 ' If you are calling a cross-platform API that accepts a wstring, '
327 'fix the API.\n' +
[email protected]55463aa62011-10-12 00:48:27328 '\n'.join(problems))]
[email protected]8ea5d4b2011-09-13 21:49:22329
330
[email protected]2a8ac9c2011-10-19 17:20:44331def _CheckNoDEPSGIT(input_api, output_api):
332 """Make sure .DEPS.git is never modified manually."""
333 if any(f.LocalPath().endswith('.DEPS.git') for f in
334 input_api.AffectedFiles()):
335 return [output_api.PresubmitError(
336 'Never commit changes to .DEPS.git. This file is maintained by an\n'
337 'automated system based on what\'s in DEPS and your changes will be\n'
338 'overwritten.\n'
339 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
340 'for more information')]
341 return []
342
343
[email protected]127f18ec2012-06-16 05:05:59344def _CheckNoBannedFunctions(input_api, output_api):
345 """Make sure that banned functions are not used."""
346 warnings = []
347 errors = []
348
349 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
350 for f in input_api.AffectedFiles(file_filter=file_filter):
351 for line_num, line in f.ChangedContents():
352 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
353 if func_name in line:
354 problems = warnings;
355 if error:
356 problems = errors;
357 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
358 for message_line in message:
359 problems.append(' %s' % message_line)
360
361 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
362 for f in input_api.AffectedFiles(file_filter=file_filter):
363 for line_num, line in f.ChangedContents():
[email protected]7345da02012-11-27 14:31:49364 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
365 def IsBlacklisted(affected_file, blacklist):
366 local_path = affected_file.LocalPath()
367 for item in blacklist:
368 if input_api.re.match(item, local_path):
369 return True
370 return False
371 if IsBlacklisted(f, excluded_paths):
372 continue
[email protected]127f18ec2012-06-16 05:05:59373 if func_name in line:
374 problems = warnings;
375 if error:
376 problems = errors;
377 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
378 for message_line in message:
379 problems.append(' %s' % message_line)
380
381 result = []
382 if (warnings):
383 result.append(output_api.PresubmitPromptWarning(
384 'Banned functions were used.\n' + '\n'.join(warnings)))
385 if (errors):
386 result.append(output_api.PresubmitError(
387 'Banned functions were used.\n' + '\n'.join(errors)))
388 return result
389
390
[email protected]6c063c62012-07-11 19:11:06391def _CheckNoPragmaOnce(input_api, output_api):
392 """Make sure that banned functions are not used."""
393 files = []
394 pattern = input_api.re.compile(r'^#pragma\s+once',
395 input_api.re.MULTILINE)
396 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
397 if not f.LocalPath().endswith('.h'):
398 continue
399 contents = input_api.ReadFile(f)
400 if pattern.search(contents):
401 files.append(f)
402
403 if files:
404 return [output_api.PresubmitError(
405 'Do not use #pragma once in header files.\n'
406 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
407 files)]
408 return []
409
[email protected]127f18ec2012-06-16 05:05:59410
[email protected]e7479052012-09-19 00:26:12411def _CheckNoTrinaryTrueFalse(input_api, output_api):
412 """Checks to make sure we don't introduce use of foo ? true : false."""
413 problems = []
414 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
415 for f in input_api.AffectedFiles():
416 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
417 continue
418
419 for line_num, line in f.ChangedContents():
420 if pattern.match(line):
421 problems.append(' %s:%d' % (f.LocalPath(), line_num))
422
423 if not problems:
424 return []
425 return [output_api.PresubmitPromptWarning(
426 'Please consider avoiding the "? true : false" pattern if possible.\n' +
427 '\n'.join(problems))]
428
429
[email protected]55f9f382012-07-31 11:02:18430def _CheckUnwantedDependencies(input_api, output_api):
431 """Runs checkdeps on #include statements added in this
432 change. Breaking - rules is an error, breaking ! rules is a
433 warning.
434 """
435 # We need to wait until we have an input_api object and use this
436 # roundabout construct to import checkdeps because this file is
437 # eval-ed and thus doesn't have __file__.
438 original_sys_path = sys.path
439 try:
440 sys.path = sys.path + [input_api.os_path.join(
441 input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
442 import checkdeps
443 from cpp_checker import CppChecker
444 from rules import Rule
445 finally:
446 # Restore sys.path to what it was before.
447 sys.path = original_sys_path
448
449 added_includes = []
450 for f in input_api.AffectedFiles():
451 if not CppChecker.IsCppFile(f.LocalPath()):
452 continue
453
454 changed_lines = [line for line_num, line in f.ChangedContents()]
455 added_includes.append([f.LocalPath(), changed_lines])
456
[email protected]26385172013-05-09 23:11:35457 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
[email protected]55f9f382012-07-31 11:02:18458
459 error_descriptions = []
460 warning_descriptions = []
461 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
462 added_includes):
463 description_with_path = '%s\n %s' % (path, rule_description)
464 if rule_type == Rule.DISALLOW:
465 error_descriptions.append(description_with_path)
466 else:
467 warning_descriptions.append(description_with_path)
468
469 results = []
470 if error_descriptions:
471 results.append(output_api.PresubmitError(
472 'You added one or more #includes that violate checkdeps rules.',
473 error_descriptions))
474 if warning_descriptions:
[email protected]f7051d52013-04-02 18:31:42475 results.append(output_api.PresubmitPromptOrNotify(
[email protected]55f9f382012-07-31 11:02:18476 'You added one or more #includes of files that are temporarily\n'
477 'allowed but being removed. Can you avoid introducing the\n'
478 '#include? See relevant DEPS file(s) for details and contacts.',
479 warning_descriptions))
480 return results
481
482
[email protected]fbcafe5a2012-08-08 15:31:22483def _CheckFilePermissions(input_api, output_api):
484 """Check that all files have their permissions properly set."""
485 args = [sys.executable, 'tools/checkperms/checkperms.py', '--root',
486 input_api.change.RepositoryRoot()]
487 for f in input_api.AffectedFiles():
488 args += ['--file', f.LocalPath()]
489 errors = []
490 (errors, stderrdata) = subprocess.Popen(args).communicate()
491
492 results = []
493 if errors:
[email protected]c8278b32012-10-30 20:35:49494 results.append(output_api.PresubmitError('checkperms.py failed.',
[email protected]fbcafe5a2012-08-08 15:31:22495 errors))
496 return results
497
498
[email protected]c8278b32012-10-30 20:35:49499def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
500 """Makes sure we don't include ui/aura/window_property.h
501 in header files.
502 """
503 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
504 errors = []
505 for f in input_api.AffectedFiles():
506 if not f.LocalPath().endswith('.h'):
507 continue
508 for line_num, line in f.ChangedContents():
509 if pattern.match(line):
510 errors.append(' %s:%d' % (f.LocalPath(), line_num))
511
512 results = []
513 if errors:
514 results.append(output_api.PresubmitError(
515 'Header files should not include ui/aura/window_property.h', errors))
516 return results
517
518
[email protected]cf9b78f2012-11-14 11:40:28519def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
520 """Checks that the lines in scope occur in the right order.
521
522 1. C system files in alphabetical order
523 2. C++ system files in alphabetical order
524 3. Project's .h files
525 """
526
527 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
528 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
529 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
530
531 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
532
533 state = C_SYSTEM_INCLUDES
534
535 previous_line = ''
[email protected]728b9bb2012-11-14 20:38:57536 previous_line_num = 0
[email protected]cf9b78f2012-11-14 11:40:28537 problem_linenums = []
538 for line_num, line in scope:
539 if c_system_include_pattern.match(line):
540 if state != C_SYSTEM_INCLUDES:
[email protected]728b9bb2012-11-14 20:38:57541 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28542 elif previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57543 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28544 elif cpp_system_include_pattern.match(line):
545 if state == C_SYSTEM_INCLUDES:
546 state = CPP_SYSTEM_INCLUDES
547 elif state == CUSTOM_INCLUDES:
[email protected]728b9bb2012-11-14 20:38:57548 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28549 elif previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57550 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28551 elif custom_include_pattern.match(line):
552 if state != CUSTOM_INCLUDES:
553 state = CUSTOM_INCLUDES
554 elif previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57555 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28556 else:
557 problem_linenums.append(line_num)
558 previous_line = line
[email protected]728b9bb2012-11-14 20:38:57559 previous_line_num = line_num
[email protected]cf9b78f2012-11-14 11:40:28560
561 warnings = []
[email protected]728b9bb2012-11-14 20:38:57562 for (line_num, previous_line_num) in problem_linenums:
563 if line_num in changed_linenums or previous_line_num in changed_linenums:
[email protected]cf9b78f2012-11-14 11:40:28564 warnings.append(' %s:%d' % (file_path, line_num))
565 return warnings
566
567
[email protected]ac294a12012-12-06 16:38:43568def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
[email protected]cf9b78f2012-11-14 11:40:28569 """Checks the #include order for the given file f."""
570
[email protected]2299dcf2012-11-15 19:56:24571 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
[email protected]962f117e2012-11-22 18:11:56572 # Exclude #include <.../...> includes from the check; e.g., <sys/...> includes
573 # often need to appear in a specific order.
574 excluded_include_pattern = input_api.re.compile(r'\s*#include \<.*/.*')
[email protected]2299dcf2012-11-15 19:56:24575 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
[email protected]0e5c1852012-12-18 20:17:11576 if_pattern = input_api.re.compile(
577 r'\s*#\s*(if|elif|else|endif|define|undef).*')
578 # Some files need specialized order of includes; exclude such files from this
579 # check.
580 uncheckable_includes_pattern = input_api.re.compile(
581 r'\s*#include '
582 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
[email protected]cf9b78f2012-11-14 11:40:28583
584 contents = f.NewContents()
585 warnings = []
586 line_num = 0
587
[email protected]ac294a12012-12-06 16:38:43588 # Handle the special first include. If the first include file is
589 # some/path/file.h, the corresponding including file can be some/path/file.cc,
590 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
591 # etc. It's also possible that no special first include exists.
592 for line in contents:
593 line_num += 1
594 if system_include_pattern.match(line):
595 # No special first include -> process the line again along with normal
596 # includes.
597 line_num -= 1
598 break
599 match = custom_include_pattern.match(line)
600 if match:
601 match_dict = match.groupdict()
602 header_basename = input_api.os_path.basename(
603 match_dict['FILE']).replace('.h', '')
604 if header_basename not in input_api.os_path.basename(f.LocalPath()):
[email protected]2299dcf2012-11-15 19:56:24605 # No special first include -> process the line again along with normal
606 # includes.
607 line_num -= 1
[email protected]ac294a12012-12-06 16:38:43608 break
[email protected]cf9b78f2012-11-14 11:40:28609
610 # Split into scopes: Each region between #if and #endif is its own scope.
611 scopes = []
612 current_scope = []
613 for line in contents[line_num:]:
614 line_num += 1
[email protected]0e5c1852012-12-18 20:17:11615 if uncheckable_includes_pattern.match(line):
616 return []
[email protected]2309b0fa02012-11-16 12:18:27617 if if_pattern.match(line):
[email protected]cf9b78f2012-11-14 11:40:28618 scopes.append(current_scope)
619 current_scope = []
[email protected]962f117e2012-11-22 18:11:56620 elif ((system_include_pattern.match(line) or
621 custom_include_pattern.match(line)) and
622 not excluded_include_pattern.match(line)):
[email protected]cf9b78f2012-11-14 11:40:28623 current_scope.append((line_num, line))
624 scopes.append(current_scope)
625
626 for scope in scopes:
627 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
628 changed_linenums))
629 return warnings
630
631
632def _CheckIncludeOrder(input_api, output_api):
633 """Checks that the #include order is correct.
634
635 1. The corresponding header for source files.
636 2. C system files in alphabetical order
637 3. C++ system files in alphabetical order
638 4. Project's .h files in alphabetical order
639
[email protected]ac294a12012-12-06 16:38:43640 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
641 these rules separately.
[email protected]cf9b78f2012-11-14 11:40:28642 """
643
644 warnings = []
645 for f in input_api.AffectedFiles():
[email protected]ac294a12012-12-06 16:38:43646 if f.LocalPath().endswith(('.cc', '.h')):
647 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
648 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
[email protected]cf9b78f2012-11-14 11:40:28649
650 results = []
651 if warnings:
[email protected]f7051d52013-04-02 18:31:42652 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
[email protected]120cf540d2012-12-10 17:55:53653 warnings))
[email protected]cf9b78f2012-11-14 11:40:28654 return results
655
656
[email protected]70ca77752012-11-20 03:45:03657def _CheckForVersionControlConflictsInFile(input_api, f):
658 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
659 errors = []
660 for line_num, line in f.ChangedContents():
661 if pattern.match(line):
662 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
663 return errors
664
665
666def _CheckForVersionControlConflicts(input_api, output_api):
667 """Usually this is not intentional and will cause a compile failure."""
668 errors = []
669 for f in input_api.AffectedFiles():
670 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
671
672 results = []
673 if errors:
674 results.append(output_api.PresubmitError(
675 'Version control conflict markers found, please resolve.', errors))
676 return results
677
678
[email protected]06e6d0ff2012-12-11 01:36:44679def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
680 def FilterFile(affected_file):
681 """Filter function for use with input_api.AffectedSourceFiles,
682 below. This filters out everything except non-test files from
683 top-level directories that generally speaking should not hard-code
684 service URLs (e.g. src/android_webview/, src/content/ and others).
685 """
686 return input_api.FilterSourceFile(
687 affected_file,
[email protected]78bb39d62012-12-11 15:11:56688 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
[email protected]06e6d0ff2012-12-11 01:36:44689 black_list=(_EXCLUDED_PATHS +
690 _TEST_CODE_EXCLUDED_PATHS +
691 input_api.DEFAULT_BLACK_LIST))
692
[email protected]de4f7d22013-05-23 14:27:46693 base_pattern = '"[^"]*google\.com[^"]*"'
694 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
695 pattern = input_api.re.compile(base_pattern)
[email protected]06e6d0ff2012-12-11 01:36:44696 problems = [] # items are (filename, line_number, line)
697 for f in input_api.AffectedSourceFiles(FilterFile):
698 for line_num, line in f.ChangedContents():
[email protected]de4f7d22013-05-23 14:27:46699 if not comment_pattern.search(line) and pattern.search(line):
[email protected]06e6d0ff2012-12-11 01:36:44700 problems.append((f.LocalPath(), line_num, line))
701
702 if problems:
[email protected]f7051d52013-04-02 18:31:42703 return [output_api.PresubmitPromptOrNotify(
[email protected]06e6d0ff2012-12-11 01:36:44704 'Most layers below src/chrome/ should not hardcode service URLs.\n'
705 'Are you sure this is correct? (Contact: [email protected])',
706 [' %s:%d: %s' % (
707 problem[0], problem[1], problem[2]) for problem in problems])]
[email protected]2fdd1f362013-01-16 03:56:03708 else:
709 return []
[email protected]06e6d0ff2012-12-11 01:36:44710
711
[email protected]d2530012013-01-25 16:39:27712def _CheckNoAbbreviationInPngFileName(input_api, output_api):
713 """Makes sure there are no abbreviations in the name of PNG files.
714 """
[email protected]4053a48e2013-01-25 21:43:04715 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
[email protected]d2530012013-01-25 16:39:27716 errors = []
717 for f in input_api.AffectedFiles(include_deletes=False):
718 if pattern.match(f.LocalPath()):
719 errors.append(' %s' % f.LocalPath())
720
721 results = []
722 if errors:
723 results.append(output_api.PresubmitError(
724 'The name of PNG files should not have abbreviations. \n'
725 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
726 'Contact [email protected] if you have questions.', errors))
727 return results
728
729
[email protected]f32e2d1e2013-07-26 21:39:08730def _DepsFilesToCheck(re, changed_lines):
731 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
732 a set of DEPS entries that we should look up."""
733 results = set()
734
735 # This pattern grabs the path without basename in the first
736 # parentheses, and the basename (if present) in the second. It
737 # relies on the simple heuristic that if there is a basename it will
738 # be a header file ending in ".h".
739 pattern = re.compile(
740 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
741 for changed_line in changed_lines:
742 m = pattern.match(changed_line)
743 if m:
744 path = m.group(1)
745 if not (path.startswith('grit/') or path == 'grit'):
746 results.add('%s/DEPS' % m.group(1))
747 return results
748
749
[email protected]e871964c2013-05-13 14:14:55750def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
751 """When a dependency prefixed with + is added to a DEPS file, we
752 want to make sure that the change is reviewed by an OWNER of the
753 target file or directory, to avoid layering violations from being
754 introduced. This check verifies that this happens.
755 """
756 changed_lines = set()
757 for f in input_api.AffectedFiles():
758 filename = input_api.os_path.basename(f.LocalPath())
759 if filename == 'DEPS':
760 changed_lines |= set(line.strip()
761 for line_num, line
762 in f.ChangedContents())
763 if not changed_lines:
764 return []
765
[email protected]f32e2d1e2013-07-26 21:39:08766 virtual_depended_on_files = _DepsFilesToCheck(input_api.re, changed_lines)
[email protected]e871964c2013-05-13 14:14:55767 if not virtual_depended_on_files:
768 return []
769
770 if input_api.is_committing:
771 if input_api.tbr:
772 return [output_api.PresubmitNotifyResult(
773 '--tbr was specified, skipping OWNERS check for DEPS additions')]
774 if not input_api.change.issue:
775 return [output_api.PresubmitError(
776 "DEPS approval by OWNERS check failed: this change has "
777 "no Rietveld issue number, so we can't check it for approvals.")]
778 output = output_api.PresubmitError
779 else:
780 output = output_api.PresubmitNotifyResult
781
782 owners_db = input_api.owners_db
783 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
784 input_api,
785 owners_db.email_regexp,
786 approval_needed=input_api.is_committing)
787
788 owner_email = owner_email or input_api.change.author_email
789
[email protected]de4f7d22013-05-23 14:27:46790 reviewers_plus_owner = set(reviewers)
[email protected]e71c6082013-05-22 02:28:51791 if owner_email:
[email protected]de4f7d22013-05-23 14:27:46792 reviewers_plus_owner.add(owner_email)
[email protected]e871964c2013-05-13 14:14:55793 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
794 reviewers_plus_owner)
795 unapproved_dependencies = ["'+%s'," % path[:-len('/DEPS')]
796 for path in missing_files]
797
798 if unapproved_dependencies:
799 output_list = [
800 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
801 '\n '.join(sorted(unapproved_dependencies)))]
802 if not input_api.is_committing:
803 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
804 output_list.append(output(
805 'Suggested missing target path OWNERS:\n %s' %
806 '\n '.join(suggested_owners or [])))
807 return output_list
808
809 return []
810
811
[email protected]22c9bd72011-03-27 16:47:39812def _CommonChecks(input_api, output_api):
813 """Checks common to both upload and commit."""
814 results = []
815 results.extend(input_api.canned_checks.PanProjectChecks(
816 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
[email protected]66daa702011-05-28 14:41:46817 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
[email protected]55459852011-08-10 15:17:19818 results.extend(
819 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
[email protected]10689ca2011-09-02 02:31:54820 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
[email protected]72df4e782012-06-21 16:28:18821 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
[email protected]8ea5d4b2011-09-13 21:49:22822 results.extend(_CheckNoNewWStrings(input_api, output_api))
[email protected]2a8ac9c2011-10-19 17:20:44823 results.extend(_CheckNoDEPSGIT(input_api, output_api))
[email protected]127f18ec2012-06-16 05:05:59824 results.extend(_CheckNoBannedFunctions(input_api, output_api))
[email protected]6c063c62012-07-11 19:11:06825 results.extend(_CheckNoPragmaOnce(input_api, output_api))
[email protected]e7479052012-09-19 00:26:12826 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
[email protected]55f9f382012-07-31 11:02:18827 results.extend(_CheckUnwantedDependencies(input_api, output_api))
[email protected]fbcafe5a2012-08-08 15:31:22828 results.extend(_CheckFilePermissions(input_api, output_api))
[email protected]c8278b32012-10-30 20:35:49829 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
[email protected]2309b0fa02012-11-16 12:18:27830 results.extend(_CheckIncludeOrder(input_api, output_api))
[email protected]70ca77752012-11-20 03:45:03831 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
[email protected]b8079ae4a2012-12-05 19:56:49832 results.extend(_CheckPatchFiles(input_api, output_api))
[email protected]06e6d0ff2012-12-11 01:36:44833 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
[email protected]d2530012013-01-25 16:39:27834 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
[email protected]b00342e7f2013-03-26 16:21:54835 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
[email protected]e871964c2013-05-13 14:14:55836 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
[email protected]2299dcf2012-11-15 19:56:24837
838 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
839 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
840 input_api, output_api,
841 input_api.PresubmitLocalPath(),
[email protected]6be63382013-01-21 15:42:38842 whitelist=[r'^PRESUBMIT_test\.py$']))
[email protected]22c9bd72011-03-27 16:47:39843 return results
[email protected]1f7b4172010-01-28 01:17:34844
[email protected]b337cb5b2011-01-23 21:24:05845
846def _CheckSubversionConfig(input_api, output_api):
847 """Verifies the subversion config file is correctly setup.
848
849 Checks that autoprops are enabled, returns an error otherwise.
850 """
851 join = input_api.os_path.join
852 if input_api.platform == 'win32':
853 appdata = input_api.environ.get('APPDATA', '')
854 if not appdata:
855 return [output_api.PresubmitError('%APPDATA% is not configured.')]
856 path = join(appdata, 'Subversion', 'config')
857 else:
858 home = input_api.environ.get('HOME', '')
859 if not home:
860 return [output_api.PresubmitError('$HOME is not configured.')]
861 path = join(home, '.subversion', 'config')
862
863 error_msg = (
864 'Please look at http://dev.chromium.org/developers/coding-style to\n'
865 'configure your subversion configuration file. This enables automatic\n'
[email protected]c6a3c10b2011-01-24 16:14:20866 'properties to simplify the project maintenance.\n'
867 'Pro-tip: just download and install\n'
868 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
[email protected]b337cb5b2011-01-23 21:24:05869
870 try:
871 lines = open(path, 'r').read().splitlines()
872 # Make sure auto-props is enabled and check for 2 Chromium standard
873 # auto-prop.
874 if (not '*.cc = svn:eol-style=LF' in lines or
875 not '*.pdf = svn:mime-type=application/pdf' in lines or
876 not 'enable-auto-props = yes' in lines):
877 return [
[email protected]79ed7e62011-02-21 21:08:53878 output_api.PresubmitNotifyResult(
[email protected]b337cb5b2011-01-23 21:24:05879 'It looks like you have not configured your subversion config '
[email protected]b5359c02011-02-01 20:29:56880 'file or it is not up-to-date.\n' + error_msg)
[email protected]b337cb5b2011-01-23 21:24:05881 ]
882 except (OSError, IOError):
883 return [
[email protected]79ed7e62011-02-21 21:08:53884 output_api.PresubmitNotifyResult(
[email protected]b337cb5b2011-01-23 21:24:05885 'Can\'t find your subversion config file.\n' + error_msg)
886 ]
887 return []
888
889
[email protected]66daa702011-05-28 14:41:46890def _CheckAuthorizedAuthor(input_api, output_api):
891 """For non-googler/chromites committers, verify the author's email address is
892 in AUTHORS.
893 """
[email protected]9bb9cb82011-06-13 20:43:01894 # TODO(maruel): Add it to input_api?
895 import fnmatch
896
[email protected]66daa702011-05-28 14:41:46897 author = input_api.change.author_email
[email protected]9bb9cb82011-06-13 20:43:01898 if not author:
899 input_api.logging.info('No author, skipping AUTHOR check')
[email protected]66daa702011-05-28 14:41:46900 return []
[email protected]c99663292011-05-31 19:46:08901 authors_path = input_api.os_path.join(
[email protected]66daa702011-05-28 14:41:46902 input_api.PresubmitLocalPath(), 'AUTHORS')
903 valid_authors = (
904 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
905 for line in open(authors_path))
[email protected]ac54b132011-06-06 18:11:18906 valid_authors = [item.group(1).lower() for item in valid_authors if item]
[email protected]d8b50be2011-06-15 14:19:44907 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
[email protected]5861efb2013-01-07 18:33:23908 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
[email protected]66daa702011-05-28 14:41:46909 return [output_api.PresubmitPromptWarning(
910 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
911 '\n'
912 'http://www.chromium.org/developers/contributing-code and read the '
913 '"Legal" section\n'
914 'If you are a chromite, verify the contributor signed the CLA.') %
915 author)]
916 return []
917
918
[email protected]b8079ae4a2012-12-05 19:56:49919def _CheckPatchFiles(input_api, output_api):
920 problems = [f.LocalPath() for f in input_api.AffectedFiles()
921 if f.LocalPath().endswith(('.orig', '.rej'))]
922 if problems:
923 return [output_api.PresubmitError(
924 "Don't commit .rej and .orig files.", problems)]
[email protected]2fdd1f362013-01-16 03:56:03925 else:
926 return []
[email protected]b8079ae4a2012-12-05 19:56:49927
928
[email protected]b00342e7f2013-03-26 16:21:54929def _DidYouMeanOSMacro(bad_macro):
930 try:
931 return {'A': 'OS_ANDROID',
932 'B': 'OS_BSD',
933 'C': 'OS_CHROMEOS',
934 'F': 'OS_FREEBSD',
935 'L': 'OS_LINUX',
936 'M': 'OS_MACOSX',
937 'N': 'OS_NACL',
938 'O': 'OS_OPENBSD',
939 'P': 'OS_POSIX',
940 'S': 'OS_SOLARIS',
941 'W': 'OS_WIN'}[bad_macro[3].upper()]
942 except KeyError:
943 return ''
944
945
946def _CheckForInvalidOSMacrosInFile(input_api, f):
947 """Check for sensible looking, totally invalid OS macros."""
948 preprocessor_statement = input_api.re.compile(r'^\s*#')
949 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
950 results = []
951 for lnum, line in f.ChangedContents():
952 if preprocessor_statement.search(line):
953 for match in os_macro.finditer(line):
954 if not match.group(1) in _VALID_OS_MACROS:
955 good = _DidYouMeanOSMacro(match.group(1))
956 did_you_mean = ' (did you mean %s?)' % good if good else ''
957 results.append(' %s:%d %s%s' % (f.LocalPath(),
958 lnum,
959 match.group(1),
960 did_you_mean))
961 return results
962
963
964def _CheckForInvalidOSMacros(input_api, output_api):
965 """Check all affected files for invalid OS macros."""
966 bad_macros = []
967 for f in input_api.AffectedFiles():
968 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
969 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
970
971 if not bad_macros:
972 return []
973
974 return [output_api.PresubmitError(
975 'Possibly invalid OS macro[s] found. Please fix your code\n'
976 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
977
978
[email protected]1f7b4172010-01-28 01:17:34979def CheckChangeOnUpload(input_api, output_api):
980 results = []
981 results.extend(_CommonChecks(input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:54982 return results
[email protected]ca8d19842009-02-19 16:33:12983
984
985def CheckChangeOnCommit(input_api, output_api):
[email protected]fe5f57c52009-06-05 14:25:54986 results = []
[email protected]1f7b4172010-01-28 01:17:34987 results.extend(_CommonChecks(input_api, output_api))
[email protected]dd805fe2009-10-01 08:11:51988 # TODO(thestig) temporarily disabled, doesn't work in third_party/
989 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
990 # input_api, output_api, sources))
[email protected]fe5f57c52009-06-05 14:25:54991 # Make sure the tree is 'open'.
[email protected]806e98e2010-03-19 17:49:27992 results.extend(input_api.canned_checks.CheckTreeIsOpen(
[email protected]7f238152009-08-12 19:00:34993 input_api,
994 output_api,
[email protected]2fdd1f362013-01-16 03:56:03995 json_url='http://chromium-status.appspot.com/current?format=json'))
[email protected]806e98e2010-03-19 17:49:27996 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
[email protected]2fdd1f362013-01-16 03:56:03997 output_api, 'http://codereview.chromium.org',
[email protected]c1ba4c52012-03-09 14:23:28998 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
999 '[email protected]'))
[email protected]806e98e2010-03-19 17:49:271000
[email protected]3e4eb112011-01-18 03:29:541001 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1002 input_api, output_api))
[email protected]c4b47562011-12-05 23:39:411003 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1004 input_api, output_api))
[email protected]b337cb5b2011-01-23 21:24:051005 results.extend(_CheckSubversionConfig(input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:541006 return results
[email protected]ca8d19842009-02-19 16:33:121007
1008
[email protected]5efb2a822011-09-27 23:06:131009def GetPreferredTrySlaves(project, change):
[email protected]4ce995ea2012-06-27 02:13:101010 files = change.LocalPaths()
1011
[email protected]751b05f2013-01-10 23:12:171012 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
[email protected]3019c902012-06-29 00:09:031013 return []
1014
[email protected]d668899a2012-09-06 18:16:591015 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
[email protected]5753cff2013-07-19 23:57:521016 return ['mac_rel', 'mac:compile']
[email protected]d668899a2012-09-06 18:16:591017 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
[email protected]7fab6202013-02-21 17:54:351018 return ['win_rel', 'win7_aura', 'win:compile']
[email protected]d668899a2012-09-06 18:16:591019 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
[email protected]d95948ef2013-07-02 10:51:001020 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
[email protected]356aa542012-09-19 23:31:291021 if all(re.search('^native_client_sdk', f) for f in files):
1022 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
[email protected]de142152012-10-03 23:02:451023 if all(re.search('[/_]ios[/_.]', f) for f in files):
1024 return ['ios_rel_device', 'ios_dbg_simulator']
[email protected]4ce995ea2012-06-27 02:13:101025
[email protected]3e2f0402012-11-02 16:28:011026 trybots = [
1027 'android_clang_dbg',
1028 'android_dbg',
1029 'ios_dbg_simulator',
1030 'ios_rel_device',
1031 'linux_asan',
[email protected]95c989162012-11-29 05:58:251032 'linux_aura',
[email protected]3e2f0402012-11-02 16:28:011033 'linux_chromeos',
1034 'linux_clang:compile',
1035 'linux_rel',
[email protected]3e2f0402012-11-02 16:28:011036 'mac_rel',
[email protected]7fab6202013-02-21 17:54:351037 'mac:compile',
[email protected]aa85c8b2013-01-11 04:20:281038 'win7_aura',
[email protected]3e2f0402012-11-02 16:28:011039 'win_rel',
[email protected]7fab6202013-02-21 17:54:351040 'win:compile',
[email protected]24d870f2013-07-23 00:45:461041 'win_x64_rel:compile',
[email protected]3e2f0402012-11-02 16:28:011042 ]
[email protected]911753b2012-08-02 12:11:541043
1044 # Match things like path/aura/file.cc and path/file_aura.cc.
[email protected]95c989162012-11-29 05:58:251045 # Same for chromeos.
1046 if any(re.search('[/_](aura|chromeos)', f) for f in files):
[email protected]3e2f0402012-11-02 16:28:011047 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
[email protected]4ce995ea2012-06-27 02:13:101048
[email protected]d95948ef2013-07-02 10:51:001049 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1050 # unless they're .gyp(i) files as changes to those files can break the gyp
1051 # step on that bot.
1052 if (not all(re.search('^chrome', f) for f in files) or
1053 any(re.search('\.gypi?$', f) for f in files)):
1054 trybots += ['android_aosp']
1055
[email protected]4ce995ea2012-06-27 02:13:101056 return trybots