blob: cefb599b18b102331a2fecd0b15cfd30335c2417 [file] [log] [blame]
[email protected]a18130a2012-01-03 17:52:081# Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ca8d1982009-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]ca8d1982009-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]e07b6ac72013-08-20 00:30:4228 r"^gpu[\\\/]config[\\\/].*_list_json\.cc$",
[email protected]4306417642009-06-11 00:33:4029)
[email protected]ca8d1982009-02-19 16:33:1230
[email protected]06e6d0ff2012-12-11 01:36:4431# Fragment of a regular expression that matches C++ and Objective-C++
32# implementation files.
33_IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
34
35# Regular expression that matches code only used for test binaries
36# (best effort).
37_TEST_CODE_EXCLUDED_PATHS = (
38 r'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
39 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
[email protected]11e06082013-04-26 19:09:0340 r'.+_(api|browser|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
[email protected]e2d7e6f2013-04-23 12:57:1241 _IMPLEMENTATION_EXTENSIONS,
[email protected]06e6d0ff2012-12-11 01:36:4442 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
43 r'.*[/\\](test|tool(s)?)[/\\].*',
[email protected]ef070cc2013-05-03 11:53:0544 # content_shell is used for running layout tests.
45 r'content[/\\]shell[/\\].*',
[email protected]06e6d0ff2012-12-11 01:36:4446 # At request of folks maintaining this folder.
47 r'chrome[/\\]browser[/\\]automation[/\\].*',
48)
[email protected]ca8d1982009-02-19 16:33:1249
[email protected]eea609a2011-11-18 13:10:1250_TEST_ONLY_WARNING = (
51 'You might be calling functions intended only for testing from\n'
52 'production code. It is OK to ignore this warning if you know what\n'
53 'you are doing, as the heuristics used to detect the situation are\n'
54 'not perfect. The commit queue will not block on this warning.\n'
55 'Email [email protected] if you have questions.')
56
57
[email protected]cf9b78f2012-11-14 11:40:2858_INCLUDE_ORDER_WARNING = (
59 'Your #include order seems to be broken. Send mail to\n'
60 '[email protected] if this is not the case.')
61
62
[email protected]127f18ec2012-06-16 05:05:5963_BANNED_OBJC_FUNCTIONS = (
64 (
65 'addTrackingRect:',
[email protected]23e6cbc2012-06-16 18:51:2066 (
67 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
[email protected]127f18ec2012-06-16 05:05:5968 'prohibited. Please use CrTrackingArea instead.',
69 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
70 ),
71 False,
72 ),
73 (
74 'NSTrackingArea',
[email protected]23e6cbc2012-06-16 18:51:2075 (
76 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
[email protected]127f18ec2012-06-16 05:05:5977 'instead.',
78 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
79 ),
80 False,
81 ),
82 (
83 'convertPointFromBase:',
[email protected]23e6cbc2012-06-16 18:51:2084 (
85 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:5986 'Please use |convertPoint:(point) fromView:nil| instead.',
87 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
88 ),
89 True,
90 ),
91 (
92 'convertPointToBase:',
[email protected]23e6cbc2012-06-16 18:51:2093 (
94 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:5995 'Please use |convertPoint:(point) toView:nil| instead.',
96 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
97 ),
98 True,
99 ),
100 (
101 'convertRectFromBase:',
[email protected]23e6cbc2012-06-16 18:51:20102 (
103 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59104 'Please use |convertRect:(point) fromView:nil| instead.',
105 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
106 ),
107 True,
108 ),
109 (
110 'convertRectToBase:',
[email protected]23e6cbc2012-06-16 18:51:20111 (
112 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59113 'Please use |convertRect:(point) toView:nil| instead.',
114 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
115 ),
116 True,
117 ),
118 (
119 'convertSizeFromBase:',
[email protected]23e6cbc2012-06-16 18:51:20120 (
121 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59122 'Please use |convertSize:(point) fromView:nil| instead.',
123 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
124 ),
125 True,
126 ),
127 (
128 'convertSizeToBase:',
[email protected]23e6cbc2012-06-16 18:51:20129 (
130 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59131 'Please use |convertSize:(point) toView:nil| instead.',
132 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
133 ),
134 True,
135 ),
136)
137
138
139_BANNED_CPP_FUNCTIONS = (
[email protected]23e6cbc2012-06-16 18:51:20140 # Make sure that gtest's FRIEND_TEST() macro is not used; the
141 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
[email protected]e00ccc92012-11-01 17:32:30142 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
[email protected]23e6cbc2012-06-16 18:51:20143 (
144 'FRIEND_TEST(',
145 (
[email protected]e3c945502012-06-26 20:01:49146 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
[email protected]23e6cbc2012-06-16 18:51:20147 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
148 ),
149 False,
[email protected]7345da02012-11-27 14:31:49150 (),
[email protected]23e6cbc2012-06-16 18:51:20151 ),
152 (
153 'ScopedAllowIO',
154 (
[email protected]e3c945502012-06-26 20:01:49155 'New code should not use ScopedAllowIO. Post a task to the blocking',
156 'pool or the FILE thread instead.',
[email protected]23e6cbc2012-06-16 18:51:20157 ),
[email protected]e3c945502012-06-26 20:01:49158 True,
[email protected]7345da02012-11-27 14:31:49159 (
[email protected]de7d61ff2013-08-20 11:30:41160 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_browser_main\.cc$",
161 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_message_filter\.cc$",
[email protected]398ad132013-04-02 15:11:01162 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
[email protected]7345da02012-11-27 14:31:49163 ),
[email protected]23e6cbc2012-06-16 18:51:20164 ),
[email protected]52657f62013-05-20 05:30:31165 (
166 'SkRefPtr',
167 (
168 'The use of SkRefPtr is prohibited. ',
169 'Please use skia::RefPtr instead.'
170 ),
171 True,
172 (),
173 ),
174 (
175 'SkAutoRef',
176 (
177 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
178 'Please use skia::RefPtr instead.'
179 ),
180 True,
181 (),
182 ),
183 (
184 'SkAutoTUnref',
185 (
186 'The use of SkAutoTUnref is dangerous because it implicitly ',
187 'converts to a raw pointer. Please use skia::RefPtr instead.'
188 ),
189 True,
190 (),
191 ),
192 (
193 'SkAutoUnref',
194 (
195 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
196 'because it implicitly converts to a raw pointer. ',
197 'Please use skia::RefPtr instead.'
198 ),
199 True,
200 (),
201 ),
[email protected]127f18ec2012-06-16 05:05:59202)
203
204
[email protected]b00342e7f2013-03-26 16:21:54205_VALID_OS_MACROS = (
206 # Please keep sorted.
207 'OS_ANDROID',
208 'OS_BSD',
209 'OS_CAT', # For testing.
210 'OS_CHROMEOS',
211 'OS_FREEBSD',
212 'OS_IOS',
213 'OS_LINUX',
214 'OS_MACOSX',
215 'OS_NACL',
216 'OS_OPENBSD',
217 'OS_POSIX',
218 'OS_SOLARIS',
[email protected]b00342e7f2013-03-26 16:21:54219 'OS_WIN',
220)
221
222
[email protected]55459852011-08-10 15:17:19223def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
224 """Attempts to prevent use of functions intended only for testing in
225 non-testing code. For now this is just a best-effort implementation
226 that ignores header files and may have some false positives. A
227 better implementation would probably need a proper C++ parser.
228 """
229 # We only scan .cc files and the like, as the declaration of
230 # for-testing functions in header files are hard to distinguish from
231 # calls to such functions without a proper C++ parser.
[email protected]06e6d0ff2012-12-11 01:36:44232 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
[email protected]55459852011-08-10 15:17:19233
234 base_function_pattern = r'ForTest(ing)?|for_test(ing)?'
235 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
[email protected]de4f7d22013-05-23 14:27:46236 comment_pattern = input_api.re.compile(r'//.*%s' % base_function_pattern)
[email protected]55459852011-08-10 15:17:19237 exclusion_pattern = input_api.re.compile(
238 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
239 base_function_pattern, base_function_pattern))
240
241 def FilterFile(affected_file):
[email protected]06e6d0ff2012-12-11 01:36:44242 black_list = (_EXCLUDED_PATHS +
243 _TEST_CODE_EXCLUDED_PATHS +
244 input_api.DEFAULT_BLACK_LIST)
[email protected]55459852011-08-10 15:17:19245 return input_api.FilterSourceFile(
246 affected_file,
247 white_list=(file_inclusion_pattern, ),
248 black_list=black_list)
249
250 problems = []
251 for f in input_api.AffectedSourceFiles(FilterFile):
252 local_path = f.LocalPath()
[email protected]2fdd1f362013-01-16 03:56:03253 lines = input_api.ReadFile(f).splitlines()
254 line_number = 0
255 for line in lines:
256 if (inclusion_pattern.search(line) and
[email protected]de4f7d22013-05-23 14:27:46257 not comment_pattern.search(line) and
[email protected]2fdd1f362013-01-16 03:56:03258 not exclusion_pattern.search(line)):
[email protected]55459852011-08-10 15:17:19259 problems.append(
[email protected]2fdd1f362013-01-16 03:56:03260 '%s:%d\n %s' % (local_path, line_number, line.strip()))
261 line_number += 1
[email protected]55459852011-08-10 15:17:19262
263 if problems:
[email protected]f7051d52013-04-02 18:31:42264 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
[email protected]2fdd1f362013-01-16 03:56:03265 else:
266 return []
[email protected]55459852011-08-10 15:17:19267
268
[email protected]10689ca2011-09-02 02:31:54269def _CheckNoIOStreamInHeaders(input_api, output_api):
270 """Checks to make sure no .h files include <iostream>."""
271 files = []
272 pattern = input_api.re.compile(r'^#include\s*<iostream>',
273 input_api.re.MULTILINE)
274 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
275 if not f.LocalPath().endswith('.h'):
276 continue
277 contents = input_api.ReadFile(f)
278 if pattern.search(contents):
279 files.append(f)
280
281 if len(files):
282 return [ output_api.PresubmitError(
[email protected]6c063c62012-07-11 19:11:06283 'Do not #include <iostream> in header files, since it inserts static '
284 'initialization into every file including the header. Instead, '
[email protected]10689ca2011-09-02 02:31:54285 '#include <ostream>. See http://crbug.com/94794',
286 files) ]
287 return []
288
289
[email protected]72df4e782012-06-21 16:28:18290def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
291 """Checks to make sure no source files use UNIT_TEST"""
292 problems = []
293 for f in input_api.AffectedFiles():
294 if (not f.LocalPath().endswith(('.cc', '.mm'))):
295 continue
296
297 for line_num, line in f.ChangedContents():
298 if 'UNIT_TEST' in line:
299 problems.append(' %s:%d' % (f.LocalPath(), line_num))
300
301 if not problems:
302 return []
303 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
304 '\n'.join(problems))]
305
306
[email protected]8ea5d4b2011-09-13 21:49:22307def _CheckNoNewWStrings(input_api, output_api):
308 """Checks to make sure we don't introduce use of wstrings."""
[email protected]55463aa62011-10-12 00:48:27309 problems = []
[email protected]8ea5d4b2011-09-13 21:49:22310 for f in input_api.AffectedFiles():
[email protected]b5c24292011-11-28 14:38:20311 if (not f.LocalPath().endswith(('.cc', '.h')) or
[email protected]24be83c2013-08-29 23:01:23312 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h'))):
[email protected]b5c24292011-11-28 14:38:20313 continue
[email protected]8ea5d4b2011-09-13 21:49:22314
[email protected]a11dbe9b2012-08-07 01:32:58315 allowWString = False
[email protected]b5c24292011-11-28 14:38:20316 for line_num, line in f.ChangedContents():
[email protected]a11dbe9b2012-08-07 01:32:58317 if 'presubmit: allow wstring' in line:
318 allowWString = True
319 elif not allowWString and 'wstring' in line:
[email protected]55463aa62011-10-12 00:48:27320 problems.append(' %s:%d' % (f.LocalPath(), line_num))
[email protected]a11dbe9b2012-08-07 01:32:58321 allowWString = False
322 else:
323 allowWString = False
[email protected]8ea5d4b2011-09-13 21:49:22324
[email protected]55463aa62011-10-12 00:48:27325 if not problems:
326 return []
327 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
[email protected]a11dbe9b2012-08-07 01:32:58328 ' If you are calling a cross-platform API that accepts a wstring, '
329 'fix the API.\n' +
[email protected]55463aa62011-10-12 00:48:27330 '\n'.join(problems))]
[email protected]8ea5d4b2011-09-13 21:49:22331
332
[email protected]2a8ac9c2011-10-19 17:20:44333def _CheckNoDEPSGIT(input_api, output_api):
334 """Make sure .DEPS.git is never modified manually."""
335 if any(f.LocalPath().endswith('.DEPS.git') for f in
336 input_api.AffectedFiles()):
337 return [output_api.PresubmitError(
338 'Never commit changes to .DEPS.git. This file is maintained by an\n'
339 'automated system based on what\'s in DEPS and your changes will be\n'
340 'overwritten.\n'
341 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
342 'for more information')]
343 return []
344
345
[email protected]127f18ec2012-06-16 05:05:59346def _CheckNoBannedFunctions(input_api, output_api):
347 """Make sure that banned functions are not used."""
348 warnings = []
349 errors = []
350
351 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
352 for f in input_api.AffectedFiles(file_filter=file_filter):
353 for line_num, line in f.ChangedContents():
354 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
355 if func_name in line:
356 problems = warnings;
357 if error:
358 problems = errors;
359 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
360 for message_line in message:
361 problems.append(' %s' % message_line)
362
363 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
364 for f in input_api.AffectedFiles(file_filter=file_filter):
365 for line_num, line in f.ChangedContents():
[email protected]7345da02012-11-27 14:31:49366 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
367 def IsBlacklisted(affected_file, blacklist):
368 local_path = affected_file.LocalPath()
369 for item in blacklist:
370 if input_api.re.match(item, local_path):
371 return True
372 return False
373 if IsBlacklisted(f, excluded_paths):
374 continue
[email protected]127f18ec2012-06-16 05:05:59375 if func_name in line:
376 problems = warnings;
377 if error:
378 problems = errors;
379 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
380 for message_line in message:
381 problems.append(' %s' % message_line)
382
383 result = []
384 if (warnings):
385 result.append(output_api.PresubmitPromptWarning(
386 'Banned functions were used.\n' + '\n'.join(warnings)))
387 if (errors):
388 result.append(output_api.PresubmitError(
389 'Banned functions were used.\n' + '\n'.join(errors)))
390 return result
391
392
[email protected]6c063c62012-07-11 19:11:06393def _CheckNoPragmaOnce(input_api, output_api):
394 """Make sure that banned functions are not used."""
395 files = []
396 pattern = input_api.re.compile(r'^#pragma\s+once',
397 input_api.re.MULTILINE)
398 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
399 if not f.LocalPath().endswith('.h'):
400 continue
401 contents = input_api.ReadFile(f)
402 if pattern.search(contents):
403 files.append(f)
404
405 if files:
406 return [output_api.PresubmitError(
407 'Do not use #pragma once in header files.\n'
408 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
409 files)]
410 return []
411
[email protected]127f18ec2012-06-16 05:05:59412
[email protected]e7479052012-09-19 00:26:12413def _CheckNoTrinaryTrueFalse(input_api, output_api):
414 """Checks to make sure we don't introduce use of foo ? true : false."""
415 problems = []
416 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
417 for f in input_api.AffectedFiles():
418 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
419 continue
420
421 for line_num, line in f.ChangedContents():
422 if pattern.match(line):
423 problems.append(' %s:%d' % (f.LocalPath(), line_num))
424
425 if not problems:
426 return []
427 return [output_api.PresubmitPromptWarning(
428 'Please consider avoiding the "? true : false" pattern if possible.\n' +
429 '\n'.join(problems))]
430
431
[email protected]55f9f382012-07-31 11:02:18432def _CheckUnwantedDependencies(input_api, output_api):
433 """Runs checkdeps on #include statements added in this
434 change. Breaking - rules is an error, breaking ! rules is a
435 warning.
436 """
437 # We need to wait until we have an input_api object and use this
438 # roundabout construct to import checkdeps because this file is
439 # eval-ed and thus doesn't have __file__.
440 original_sys_path = sys.path
441 try:
442 sys.path = sys.path + [input_api.os_path.join(
443 input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
444 import checkdeps
445 from cpp_checker import CppChecker
446 from rules import Rule
447 finally:
448 # Restore sys.path to what it was before.
449 sys.path = original_sys_path
450
451 added_includes = []
452 for f in input_api.AffectedFiles():
453 if not CppChecker.IsCppFile(f.LocalPath()):
454 continue
455
456 changed_lines = [line for line_num, line in f.ChangedContents()]
457 added_includes.append([f.LocalPath(), changed_lines])
458
[email protected]26385172013-05-09 23:11:35459 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
[email protected]55f9f382012-07-31 11:02:18460
461 error_descriptions = []
462 warning_descriptions = []
463 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
464 added_includes):
465 description_with_path = '%s\n %s' % (path, rule_description)
466 if rule_type == Rule.DISALLOW:
467 error_descriptions.append(description_with_path)
468 else:
469 warning_descriptions.append(description_with_path)
470
471 results = []
472 if error_descriptions:
473 results.append(output_api.PresubmitError(
474 'You added one or more #includes that violate checkdeps rules.',
475 error_descriptions))
476 if warning_descriptions:
[email protected]f7051d52013-04-02 18:31:42477 results.append(output_api.PresubmitPromptOrNotify(
[email protected]55f9f382012-07-31 11:02:18478 'You added one or more #includes of files that are temporarily\n'
479 'allowed but being removed. Can you avoid introducing the\n'
480 '#include? See relevant DEPS file(s) for details and contacts.',
481 warning_descriptions))
482 return results
483
484
[email protected]fbcafe5a2012-08-08 15:31:22485def _CheckFilePermissions(input_api, output_api):
486 """Check that all files have their permissions properly set."""
487 args = [sys.executable, 'tools/checkperms/checkperms.py', '--root',
488 input_api.change.RepositoryRoot()]
489 for f in input_api.AffectedFiles():
490 args += ['--file', f.LocalPath()]
491 errors = []
492 (errors, stderrdata) = subprocess.Popen(args).communicate()
493
494 results = []
495 if errors:
[email protected]c8278b32012-10-30 20:35:49496 results.append(output_api.PresubmitError('checkperms.py failed.',
[email protected]fbcafe5a2012-08-08 15:31:22497 errors))
498 return results
499
500
[email protected]c8278b32012-10-30 20:35:49501def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
502 """Makes sure we don't include ui/aura/window_property.h
503 in header files.
504 """
505 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
506 errors = []
507 for f in input_api.AffectedFiles():
508 if not f.LocalPath().endswith('.h'):
509 continue
510 for line_num, line in f.ChangedContents():
511 if pattern.match(line):
512 errors.append(' %s:%d' % (f.LocalPath(), line_num))
513
514 results = []
515 if errors:
516 results.append(output_api.PresubmitError(
517 'Header files should not include ui/aura/window_property.h', errors))
518 return results
519
520
[email protected]cf9b78f2012-11-14 11:40:28521def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
522 """Checks that the lines in scope occur in the right order.
523
524 1. C system files in alphabetical order
525 2. C++ system files in alphabetical order
526 3. Project's .h files
527 """
528
529 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
530 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
531 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
532
533 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
534
535 state = C_SYSTEM_INCLUDES
536
537 previous_line = ''
[email protected]728b9bb2012-11-14 20:38:57538 previous_line_num = 0
[email protected]cf9b78f2012-11-14 11:40:28539 problem_linenums = []
540 for line_num, line in scope:
541 if c_system_include_pattern.match(line):
542 if state != C_SYSTEM_INCLUDES:
[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 previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57545 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28546 elif cpp_system_include_pattern.match(line):
547 if state == C_SYSTEM_INCLUDES:
548 state = CPP_SYSTEM_INCLUDES
549 elif state == CUSTOM_INCLUDES:
[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 previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57552 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28553 elif custom_include_pattern.match(line):
554 if state != CUSTOM_INCLUDES:
555 state = CUSTOM_INCLUDES
556 elif previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57557 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28558 else:
559 problem_linenums.append(line_num)
560 previous_line = line
[email protected]728b9bb2012-11-14 20:38:57561 previous_line_num = line_num
[email protected]cf9b78f2012-11-14 11:40:28562
563 warnings = []
[email protected]728b9bb2012-11-14 20:38:57564 for (line_num, previous_line_num) in problem_linenums:
565 if line_num in changed_linenums or previous_line_num in changed_linenums:
[email protected]cf9b78f2012-11-14 11:40:28566 warnings.append(' %s:%d' % (file_path, line_num))
567 return warnings
568
569
[email protected]ac294a12012-12-06 16:38:43570def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
[email protected]cf9b78f2012-11-14 11:40:28571 """Checks the #include order for the given file f."""
572
[email protected]2299dcf2012-11-15 19:56:24573 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
[email protected]962f117e2012-11-22 18:11:56574 # Exclude #include <.../...> includes from the check; e.g., <sys/...> includes
575 # often need to appear in a specific order.
576 excluded_include_pattern = input_api.re.compile(r'\s*#include \<.*/.*')
[email protected]2299dcf2012-11-15 19:56:24577 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
[email protected]0e5c1852012-12-18 20:17:11578 if_pattern = input_api.re.compile(
579 r'\s*#\s*(if|elif|else|endif|define|undef).*')
580 # Some files need specialized order of includes; exclude such files from this
581 # check.
582 uncheckable_includes_pattern = input_api.re.compile(
583 r'\s*#include '
584 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
[email protected]cf9b78f2012-11-14 11:40:28585
586 contents = f.NewContents()
587 warnings = []
588 line_num = 0
589
[email protected]ac294a12012-12-06 16:38:43590 # Handle the special first include. If the first include file is
591 # some/path/file.h, the corresponding including file can be some/path/file.cc,
592 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
593 # etc. It's also possible that no special first include exists.
594 for line in contents:
595 line_num += 1
596 if system_include_pattern.match(line):
597 # No special first include -> process the line again along with normal
598 # includes.
599 line_num -= 1
600 break
601 match = custom_include_pattern.match(line)
602 if match:
603 match_dict = match.groupdict()
604 header_basename = input_api.os_path.basename(
605 match_dict['FILE']).replace('.h', '')
606 if header_basename not in input_api.os_path.basename(f.LocalPath()):
[email protected]2299dcf2012-11-15 19:56:24607 # No special first include -> process the line again along with normal
608 # includes.
609 line_num -= 1
[email protected]ac294a12012-12-06 16:38:43610 break
[email protected]cf9b78f2012-11-14 11:40:28611
612 # Split into scopes: Each region between #if and #endif is its own scope.
613 scopes = []
614 current_scope = []
615 for line in contents[line_num:]:
616 line_num += 1
[email protected]0e5c1852012-12-18 20:17:11617 if uncheckable_includes_pattern.match(line):
618 return []
[email protected]2309b0fa02012-11-16 12:18:27619 if if_pattern.match(line):
[email protected]cf9b78f2012-11-14 11:40:28620 scopes.append(current_scope)
621 current_scope = []
[email protected]962f117e2012-11-22 18:11:56622 elif ((system_include_pattern.match(line) or
623 custom_include_pattern.match(line)) and
624 not excluded_include_pattern.match(line)):
[email protected]cf9b78f2012-11-14 11:40:28625 current_scope.append((line_num, line))
626 scopes.append(current_scope)
627
628 for scope in scopes:
629 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
630 changed_linenums))
631 return warnings
632
633
634def _CheckIncludeOrder(input_api, output_api):
635 """Checks that the #include order is correct.
636
637 1. The corresponding header for source files.
638 2. C system files in alphabetical order
639 3. C++ system files in alphabetical order
640 4. Project's .h files in alphabetical order
641
[email protected]ac294a12012-12-06 16:38:43642 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
643 these rules separately.
[email protected]cf9b78f2012-11-14 11:40:28644 """
645
646 warnings = []
647 for f in input_api.AffectedFiles():
[email protected]ac294a12012-12-06 16:38:43648 if f.LocalPath().endswith(('.cc', '.h')):
649 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
650 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
[email protected]cf9b78f2012-11-14 11:40:28651
652 results = []
653 if warnings:
[email protected]f7051d52013-04-02 18:31:42654 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
[email protected]120cf540d2012-12-10 17:55:53655 warnings))
[email protected]cf9b78f2012-11-14 11:40:28656 return results
657
658
[email protected]70ca77752012-11-20 03:45:03659def _CheckForVersionControlConflictsInFile(input_api, f):
660 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
661 errors = []
662 for line_num, line in f.ChangedContents():
663 if pattern.match(line):
664 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
665 return errors
666
667
668def _CheckForVersionControlConflicts(input_api, output_api):
669 """Usually this is not intentional and will cause a compile failure."""
670 errors = []
671 for f in input_api.AffectedFiles():
672 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
673
674 results = []
675 if errors:
676 results.append(output_api.PresubmitError(
677 'Version control conflict markers found, please resolve.', errors))
678 return results
679
680
[email protected]06e6d0ff2012-12-11 01:36:44681def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
682 def FilterFile(affected_file):
683 """Filter function for use with input_api.AffectedSourceFiles,
684 below. This filters out everything except non-test files from
685 top-level directories that generally speaking should not hard-code
686 service URLs (e.g. src/android_webview/, src/content/ and others).
687 """
688 return input_api.FilterSourceFile(
689 affected_file,
[email protected]78bb39d62012-12-11 15:11:56690 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
[email protected]06e6d0ff2012-12-11 01:36:44691 black_list=(_EXCLUDED_PATHS +
692 _TEST_CODE_EXCLUDED_PATHS +
693 input_api.DEFAULT_BLACK_LIST))
694
[email protected]de4f7d22013-05-23 14:27:46695 base_pattern = '"[^"]*google\.com[^"]*"'
696 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
697 pattern = input_api.re.compile(base_pattern)
[email protected]06e6d0ff2012-12-11 01:36:44698 problems = [] # items are (filename, line_number, line)
699 for f in input_api.AffectedSourceFiles(FilterFile):
700 for line_num, line in f.ChangedContents():
[email protected]de4f7d22013-05-23 14:27:46701 if not comment_pattern.search(line) and pattern.search(line):
[email protected]06e6d0ff2012-12-11 01:36:44702 problems.append((f.LocalPath(), line_num, line))
703
704 if problems:
[email protected]f7051d52013-04-02 18:31:42705 return [output_api.PresubmitPromptOrNotify(
[email protected]06e6d0ff2012-12-11 01:36:44706 'Most layers below src/chrome/ should not hardcode service URLs.\n'
707 'Are you sure this is correct? (Contact: [email protected])',
708 [' %s:%d: %s' % (
709 problem[0], problem[1], problem[2]) for problem in problems])]
[email protected]2fdd1f362013-01-16 03:56:03710 else:
711 return []
[email protected]06e6d0ff2012-12-11 01:36:44712
713
[email protected]d2530012013-01-25 16:39:27714def _CheckNoAbbreviationInPngFileName(input_api, output_api):
715 """Makes sure there are no abbreviations in the name of PNG files.
716 """
[email protected]4053a48e2013-01-25 21:43:04717 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
[email protected]d2530012013-01-25 16:39:27718 errors = []
719 for f in input_api.AffectedFiles(include_deletes=False):
720 if pattern.match(f.LocalPath()):
721 errors.append(' %s' % f.LocalPath())
722
723 results = []
724 if errors:
725 results.append(output_api.PresubmitError(
726 'The name of PNG files should not have abbreviations. \n'
727 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
728 'Contact [email protected] if you have questions.', errors))
729 return results
730
731
[email protected]f32e2d1e2013-07-26 21:39:08732def _DepsFilesToCheck(re, changed_lines):
733 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
734 a set of DEPS entries that we should look up."""
735 results = set()
736
737 # This pattern grabs the path without basename in the first
738 # parentheses, and the basename (if present) in the second. It
739 # relies on the simple heuristic that if there is a basename it will
740 # be a header file ending in ".h".
741 pattern = re.compile(
742 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
743 for changed_line in changed_lines:
744 m = pattern.match(changed_line)
745 if m:
746 path = m.group(1)
747 if not (path.startswith('grit/') or path == 'grit'):
748 results.add('%s/DEPS' % m.group(1))
749 return results
750
751
[email protected]e871964c2013-05-13 14:14:55752def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
753 """When a dependency prefixed with + is added to a DEPS file, we
754 want to make sure that the change is reviewed by an OWNER of the
755 target file or directory, to avoid layering violations from being
756 introduced. This check verifies that this happens.
757 """
758 changed_lines = set()
759 for f in input_api.AffectedFiles():
760 filename = input_api.os_path.basename(f.LocalPath())
761 if filename == 'DEPS':
762 changed_lines |= set(line.strip()
763 for line_num, line
764 in f.ChangedContents())
765 if not changed_lines:
766 return []
767
[email protected]f32e2d1e2013-07-26 21:39:08768 virtual_depended_on_files = _DepsFilesToCheck(input_api.re, changed_lines)
[email protected]e871964c2013-05-13 14:14:55769 if not virtual_depended_on_files:
770 return []
771
772 if input_api.is_committing:
773 if input_api.tbr:
774 return [output_api.PresubmitNotifyResult(
775 '--tbr was specified, skipping OWNERS check for DEPS additions')]
776 if not input_api.change.issue:
777 return [output_api.PresubmitError(
778 "DEPS approval by OWNERS check failed: this change has "
779 "no Rietveld issue number, so we can't check it for approvals.")]
780 output = output_api.PresubmitError
781 else:
782 output = output_api.PresubmitNotifyResult
783
784 owners_db = input_api.owners_db
785 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
786 input_api,
787 owners_db.email_regexp,
788 approval_needed=input_api.is_committing)
789
790 owner_email = owner_email or input_api.change.author_email
791
[email protected]de4f7d22013-05-23 14:27:46792 reviewers_plus_owner = set(reviewers)
[email protected]e71c6082013-05-22 02:28:51793 if owner_email:
[email protected]de4f7d22013-05-23 14:27:46794 reviewers_plus_owner.add(owner_email)
[email protected]e871964c2013-05-13 14:14:55795 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
796 reviewers_plus_owner)
797 unapproved_dependencies = ["'+%s'," % path[:-len('/DEPS')]
798 for path in missing_files]
799
800 if unapproved_dependencies:
801 output_list = [
802 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
803 '\n '.join(sorted(unapproved_dependencies)))]
804 if not input_api.is_committing:
805 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
806 output_list.append(output(
807 'Suggested missing target path OWNERS:\n %s' %
808 '\n '.join(suggested_owners or [])))
809 return output_list
810
811 return []
812
813
[email protected]22c9bd72011-03-27 16:47:39814def _CommonChecks(input_api, output_api):
815 """Checks common to both upload and commit."""
816 results = []
817 results.extend(input_api.canned_checks.PanProjectChecks(
818 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
[email protected]66daa702011-05-28 14:41:46819 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
[email protected]55459852011-08-10 15:17:19820 results.extend(
821 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
[email protected]10689ca2011-09-02 02:31:54822 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
[email protected]72df4e782012-06-21 16:28:18823 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
[email protected]8ea5d4b2011-09-13 21:49:22824 results.extend(_CheckNoNewWStrings(input_api, output_api))
[email protected]2a8ac9c2011-10-19 17:20:44825 results.extend(_CheckNoDEPSGIT(input_api, output_api))
[email protected]127f18ec2012-06-16 05:05:59826 results.extend(_CheckNoBannedFunctions(input_api, output_api))
[email protected]6c063c62012-07-11 19:11:06827 results.extend(_CheckNoPragmaOnce(input_api, output_api))
[email protected]e7479052012-09-19 00:26:12828 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
[email protected]55f9f382012-07-31 11:02:18829 results.extend(_CheckUnwantedDependencies(input_api, output_api))
[email protected]fbcafe5a2012-08-08 15:31:22830 results.extend(_CheckFilePermissions(input_api, output_api))
[email protected]c8278b32012-10-30 20:35:49831 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
[email protected]2309b0fa02012-11-16 12:18:27832 results.extend(_CheckIncludeOrder(input_api, output_api))
[email protected]70ca77752012-11-20 03:45:03833 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
[email protected]b8079ae4a2012-12-05 19:56:49834 results.extend(_CheckPatchFiles(input_api, output_api))
[email protected]06e6d0ff2012-12-11 01:36:44835 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
[email protected]d2530012013-01-25 16:39:27836 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
[email protected]b00342e7f2013-03-26 16:21:54837 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
[email protected]e871964c2013-05-13 14:14:55838 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
[email protected]9f919cc2013-07-31 03:04:04839 results.extend(
840 input_api.canned_checks.CheckChangeHasNoTabs(
841 input_api,
842 output_api,
843 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
[email protected]2299dcf2012-11-15 19:56:24844
845 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
846 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
847 input_api, output_api,
848 input_api.PresubmitLocalPath(),
[email protected]6be63382013-01-21 15:42:38849 whitelist=[r'^PRESUBMIT_test\.py$']))
[email protected]22c9bd72011-03-27 16:47:39850 return results
[email protected]1f7b4172010-01-28 01:17:34851
[email protected]b337cb5b2011-01-23 21:24:05852
853def _CheckSubversionConfig(input_api, output_api):
854 """Verifies the subversion config file is correctly setup.
855
856 Checks that autoprops are enabled, returns an error otherwise.
857 """
858 join = input_api.os_path.join
859 if input_api.platform == 'win32':
860 appdata = input_api.environ.get('APPDATA', '')
861 if not appdata:
862 return [output_api.PresubmitError('%APPDATA% is not configured.')]
863 path = join(appdata, 'Subversion', 'config')
864 else:
865 home = input_api.environ.get('HOME', '')
866 if not home:
867 return [output_api.PresubmitError('$HOME is not configured.')]
868 path = join(home, '.subversion', 'config')
869
870 error_msg = (
871 'Please look at http://dev.chromium.org/developers/coding-style to\n'
872 'configure your subversion configuration file. This enables automatic\n'
[email protected]c6a3c10b2011-01-24 16:14:20873 'properties to simplify the project maintenance.\n'
874 'Pro-tip: just download and install\n'
875 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
[email protected]b337cb5b2011-01-23 21:24:05876
877 try:
878 lines = open(path, 'r').read().splitlines()
879 # Make sure auto-props is enabled and check for 2 Chromium standard
880 # auto-prop.
881 if (not '*.cc = svn:eol-style=LF' in lines or
882 not '*.pdf = svn:mime-type=application/pdf' in lines or
883 not 'enable-auto-props = yes' in lines):
884 return [
[email protected]79ed7e62011-02-21 21:08:53885 output_api.PresubmitNotifyResult(
[email protected]b337cb5b2011-01-23 21:24:05886 'It looks like you have not configured your subversion config '
[email protected]b5359c02011-02-01 20:29:56887 'file or it is not up-to-date.\n' + error_msg)
[email protected]b337cb5b2011-01-23 21:24:05888 ]
889 except (OSError, IOError):
890 return [
[email protected]79ed7e62011-02-21 21:08:53891 output_api.PresubmitNotifyResult(
[email protected]b337cb5b2011-01-23 21:24:05892 'Can\'t find your subversion config file.\n' + error_msg)
893 ]
894 return []
895
896
[email protected]66daa702011-05-28 14:41:46897def _CheckAuthorizedAuthor(input_api, output_api):
898 """For non-googler/chromites committers, verify the author's email address is
899 in AUTHORS.
900 """
[email protected]9bb9cb82011-06-13 20:43:01901 # TODO(maruel): Add it to input_api?
902 import fnmatch
903
[email protected]66daa702011-05-28 14:41:46904 author = input_api.change.author_email
[email protected]9bb9cb82011-06-13 20:43:01905 if not author:
906 input_api.logging.info('No author, skipping AUTHOR check')
[email protected]66daa702011-05-28 14:41:46907 return []
[email protected]c99663292011-05-31 19:46:08908 authors_path = input_api.os_path.join(
[email protected]66daa702011-05-28 14:41:46909 input_api.PresubmitLocalPath(), 'AUTHORS')
910 valid_authors = (
911 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
912 for line in open(authors_path))
[email protected]ac54b132011-06-06 18:11:18913 valid_authors = [item.group(1).lower() for item in valid_authors if item]
[email protected]d8b50be2011-06-15 14:19:44914 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
[email protected]5861efb2013-01-07 18:33:23915 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
[email protected]66daa702011-05-28 14:41:46916 return [output_api.PresubmitPromptWarning(
917 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
918 '\n'
919 'http://www.chromium.org/developers/contributing-code and read the '
920 '"Legal" section\n'
921 'If you are a chromite, verify the contributor signed the CLA.') %
922 author)]
923 return []
924
925
[email protected]b8079ae4a2012-12-05 19:56:49926def _CheckPatchFiles(input_api, output_api):
927 problems = [f.LocalPath() for f in input_api.AffectedFiles()
928 if f.LocalPath().endswith(('.orig', '.rej'))]
929 if problems:
930 return [output_api.PresubmitError(
931 "Don't commit .rej and .orig files.", problems)]
[email protected]2fdd1f362013-01-16 03:56:03932 else:
933 return []
[email protected]b8079ae4a2012-12-05 19:56:49934
935
[email protected]b00342e7f2013-03-26 16:21:54936def _DidYouMeanOSMacro(bad_macro):
937 try:
938 return {'A': 'OS_ANDROID',
939 'B': 'OS_BSD',
940 'C': 'OS_CHROMEOS',
941 'F': 'OS_FREEBSD',
942 'L': 'OS_LINUX',
943 'M': 'OS_MACOSX',
944 'N': 'OS_NACL',
945 'O': 'OS_OPENBSD',
946 'P': 'OS_POSIX',
947 'S': 'OS_SOLARIS',
948 'W': 'OS_WIN'}[bad_macro[3].upper()]
949 except KeyError:
950 return ''
951
952
953def _CheckForInvalidOSMacrosInFile(input_api, f):
954 """Check for sensible looking, totally invalid OS macros."""
955 preprocessor_statement = input_api.re.compile(r'^\s*#')
956 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
957 results = []
958 for lnum, line in f.ChangedContents():
959 if preprocessor_statement.search(line):
960 for match in os_macro.finditer(line):
961 if not match.group(1) in _VALID_OS_MACROS:
962 good = _DidYouMeanOSMacro(match.group(1))
963 did_you_mean = ' (did you mean %s?)' % good if good else ''
964 results.append(' %s:%d %s%s' % (f.LocalPath(),
965 lnum,
966 match.group(1),
967 did_you_mean))
968 return results
969
970
971def _CheckForInvalidOSMacros(input_api, output_api):
972 """Check all affected files for invalid OS macros."""
973 bad_macros = []
974 for f in input_api.AffectedFiles():
975 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
976 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
977
978 if not bad_macros:
979 return []
980
981 return [output_api.PresubmitError(
982 'Possibly invalid OS macro[s] found. Please fix your code\n'
983 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
984
985
[email protected]1f7b4172010-01-28 01:17:34986def CheckChangeOnUpload(input_api, output_api):
987 results = []
988 results.extend(_CommonChecks(input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:54989 return results
[email protected]ca8d1982009-02-19 16:33:12990
991
992def CheckChangeOnCommit(input_api, output_api):
[email protected]fe5f57c52009-06-05 14:25:54993 results = []
[email protected]1f7b4172010-01-28 01:17:34994 results.extend(_CommonChecks(input_api, output_api))
[email protected]dd805fe2009-10-01 08:11:51995 # TODO(thestig) temporarily disabled, doesn't work in third_party/
996 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
997 # input_api, output_api, sources))
[email protected]fe5f57c52009-06-05 14:25:54998 # Make sure the tree is 'open'.
[email protected]806e98e2010-03-19 17:49:27999 results.extend(input_api.canned_checks.CheckTreeIsOpen(
[email protected]7f238152009-08-12 19:00:341000 input_api,
1001 output_api,
[email protected]2fdd1f362013-01-16 03:56:031002 json_url='http://chromium-status.appspot.com/current?format=json'))
[email protected]806e98e2010-03-19 17:49:271003 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
[email protected]2fdd1f362013-01-16 03:56:031004 output_api, 'http://codereview.chromium.org',
[email protected]c1ba4c52012-03-09 14:23:281005 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
1006 '[email protected]'))
[email protected]806e98e2010-03-19 17:49:271007
[email protected]3e4eb112011-01-18 03:29:541008 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1009 input_api, output_api))
[email protected]c4b47562011-12-05 23:39:411010 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1011 input_api, output_api))
[email protected]b337cb5b2011-01-23 21:24:051012 results.extend(_CheckSubversionConfig(input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:541013 return results
[email protected]ca8d1982009-02-19 16:33:121014
1015
[email protected]5efb2a822011-09-27 23:06:131016def GetPreferredTrySlaves(project, change):
[email protected]4ce995ea2012-06-27 02:13:101017 files = change.LocalPaths()
1018
[email protected]751b05f2013-01-10 23:12:171019 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
[email protected]3019c902012-06-29 00:09:031020 return []
1021
[email protected]d668899a2012-09-06 18:16:591022 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
[email protected]5753cff2013-07-19 23:57:521023 return ['mac_rel', 'mac:compile']
[email protected]d668899a2012-09-06 18:16:591024 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
[email protected]7fab6202013-02-21 17:54:351025 return ['win_rel', 'win7_aura', 'win:compile']
[email protected]d668899a2012-09-06 18:16:591026 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
[email protected]d95948ef2013-07-02 10:51:001027 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
[email protected]356aa542012-09-19 23:31:291028 if all(re.search('^native_client_sdk', f) for f in files):
1029 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
[email protected]de142152012-10-03 23:02:451030 if all(re.search('[/_]ios[/_.]', f) for f in files):
1031 return ['ios_rel_device', 'ios_dbg_simulator']
[email protected]4ce995ea2012-06-27 02:13:101032
[email protected]3e2f0402012-11-02 16:28:011033 trybots = [
1034 'android_clang_dbg',
1035 'android_dbg',
1036 'ios_dbg_simulator',
1037 'ios_rel_device',
1038 'linux_asan',
[email protected]95c989162012-11-29 05:58:251039 'linux_aura',
[email protected]3e2f0402012-11-02 16:28:011040 'linux_chromeos',
1041 'linux_clang:compile',
1042 'linux_rel',
[email protected]3e2f0402012-11-02 16:28:011043 'mac_rel',
[email protected]7fab6202013-02-21 17:54:351044 'mac:compile',
[email protected]aa85c8b2013-01-11 04:20:281045 'win7_aura',
[email protected]3e2f0402012-11-02 16:28:011046 'win_rel',
[email protected]7fab6202013-02-21 17:54:351047 'win:compile',
[email protected]24d870f2013-07-23 00:45:461048 'win_x64_rel:compile',
[email protected]3e2f0402012-11-02 16:28:011049 ]
[email protected]911753b2012-08-02 12:11:541050
1051 # Match things like path/aura/file.cc and path/file_aura.cc.
[email protected]95c989162012-11-29 05:58:251052 # Same for chromeos.
1053 if any(re.search('[/_](aura|chromeos)', f) for f in files):
[email protected]3e2f0402012-11-02 16:28:011054 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
[email protected]4ce995ea2012-06-27 02:13:101055
[email protected]d95948ef2013-07-02 10:51:001056 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1057 # unless they're .gyp(i) files as changes to those files can break the gyp
1058 # step on that bot.
1059 if (not all(re.search('^chrome', f) for f in files) or
1060 any(re.search('\.gypi?$', f) for f in files)):
1061 trybots += ['android_aosp']
1062
[email protected]4ce995ea2012-06-27 02:13:101063 return trybots