1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Top-level presubmit script for Chromium.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into gcl.
19 r
"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py",
20 r
"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py",
21 r
"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk",
22 r
"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
27 r
".+[\\\/]pnacl_shim\.c$",
30 # Fragment of a regular expression that matches file name suffixes
31 # used to indicate different platforms.
32 _PLATFORM_SPECIFIERS
= r
'(_(android|chromeos|gtk|mac|posix|win))?'
34 # Fragment of a regular expression that matches C++ and Objective-C++
35 # implementation files.
36 _IMPLEMENTATION_EXTENSIONS
= r
'\.(cc|cpp|cxx|mm)$'
38 # Regular expression that matches code only used for test binaries
40 _TEST_CODE_EXCLUDED_PATHS
= (
41 r
'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS
,
42 r
'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS
,
43 r
'.+_(api|browser|perf|unit|ui)?test%s%s' % (_PLATFORM_SPECIFIERS
,
44 _IMPLEMENTATION_EXTENSIONS
),
45 r
'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS
,
46 r
'.*[/\\](test|tool(s)?)[/\\].*',
47 # At request of folks maintaining this folder.
48 r
'chrome[/\\]browser[/\\]automation[/\\].*',
51 _TEST_ONLY_WARNING
= (
52 'You might be calling functions intended only for testing from\n'
53 'production code. It is OK to ignore this warning if you know what\n'
54 'you are doing, as the heuristics used to detect the situation are\n'
55 'not perfect. The commit queue will not block on this warning.\n'
56 'Email joi@chromium.org if you have questions.')
59 _INCLUDE_ORDER_WARNING
= (
60 'Your #include order seems to be broken. Send mail to\n'
61 'marja@chromium.org if this is not the case.')
64 _BANNED_OBJC_FUNCTIONS
= (
68 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
69 'prohibited. Please use CrTrackingArea instead.',
70 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
77 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
79 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
84 'convertPointFromBase:',
86 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
87 'Please use |convertPoint:(point) fromView:nil| instead.',
88 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
93 'convertPointToBase:',
95 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
96 'Please use |convertPoint:(point) toView:nil| instead.',
97 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
102 'convertRectFromBase:',
104 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
105 'Please use |convertRect:(point) fromView:nil| instead.',
106 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
111 'convertRectToBase:',
113 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
114 'Please use |convertRect:(point) toView:nil| instead.',
115 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
120 'convertSizeFromBase:',
122 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
123 'Please use |convertSize:(point) fromView:nil| instead.',
124 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
129 'convertSizeToBase:',
131 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
132 'Please use |convertSize:(point) toView:nil| instead.',
133 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
140 _BANNED_CPP_FUNCTIONS
= (
141 # Make sure that gtest's FRIEND_TEST() macro is not used; the
142 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
143 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
147 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
148 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
156 'New code should not use ScopedAllowIO. Post a task to the blocking',
157 'pool or the FILE thread instead.',
161 r
"^content[\\\/]shell[\\\/]shell_browser_main\.cc$",
162 r
"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
169 # Please keep sorted.
172 'OS_CAT', # For testing.
182 'OS_SUN', # Not in build/build_config.h but in skia.
187 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
):
188 """Attempts to prevent use of functions intended only for testing in
189 non-testing code. For now this is just a best-effort implementation
190 that ignores header files and may have some false positives. A
191 better implementation would probably need a proper C++ parser.
193 # We only scan .cc files and the like, as the declaration of
194 # for-testing functions in header files are hard to distinguish from
195 # calls to such functions without a proper C++ parser.
196 file_inclusion_pattern
= r
'.+%s' % _IMPLEMENTATION_EXTENSIONS
198 base_function_pattern
= r
'ForTest(ing)?|for_test(ing)?'
199 inclusion_pattern
= input_api
.re
.compile(r
'(%s)\s*\(' % base_function_pattern
)
200 exclusion_pattern
= input_api
.re
.compile(
201 r
'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
202 base_function_pattern
, base_function_pattern
))
204 def FilterFile(affected_file
):
205 black_list
= (_EXCLUDED_PATHS
+
206 _TEST_CODE_EXCLUDED_PATHS
+
207 input_api
.DEFAULT_BLACK_LIST
)
208 return input_api
.FilterSourceFile(
210 white_list
=(file_inclusion_pattern
, ),
211 black_list
=black_list
)
214 for f
in input_api
.AffectedSourceFiles(FilterFile
):
215 local_path
= f
.LocalPath()
216 lines
= input_api
.ReadFile(f
).splitlines()
219 if (inclusion_pattern
.search(line
) and
220 not exclusion_pattern
.search(line
)):
222 '%s:%d\n %s' % (local_path
, line_number
, line
.strip()))
226 return [output_api
.PresubmitPromptOrNotify(_TEST_ONLY_WARNING
, problems
)]
231 def _CheckNoIOStreamInHeaders(input_api
, output_api
):
232 """Checks to make sure no .h files include <iostream>."""
234 pattern
= input_api
.re
.compile(r
'^#include\s*<iostream>',
235 input_api
.re
.MULTILINE
)
236 for f
in input_api
.AffectedSourceFiles(input_api
.FilterSourceFile
):
237 if not f
.LocalPath().endswith('.h'):
239 contents
= input_api
.ReadFile(f
)
240 if pattern
.search(contents
):
244 return [ output_api
.PresubmitError(
245 'Do not #include <iostream> in header files, since it inserts static '
246 'initialization into every file including the header. Instead, '
247 '#include <ostream>. See http://crbug.com/94794',
252 def _CheckNoUNIT_TESTInSourceFiles(input_api
, output_api
):
253 """Checks to make sure no source files use UNIT_TEST"""
255 for f
in input_api
.AffectedFiles():
256 if (not f
.LocalPath().endswith(('.cc', '.mm'))):
259 for line_num
, line
in f
.ChangedContents():
260 if 'UNIT_TEST' in line
:
261 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
265 return [output_api
.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
266 '\n'.join(problems
))]
269 def _CheckNoNewWStrings(input_api
, output_api
):
270 """Checks to make sure we don't introduce use of wstrings."""
272 for f
in input_api
.AffectedFiles():
273 if (not f
.LocalPath().endswith(('.cc', '.h')) or
274 f
.LocalPath().endswith('test.cc')):
278 for line_num
, line
in f
.ChangedContents():
279 if 'presubmit: allow wstring' in line
:
281 elif not allowWString
and 'wstring' in line
:
282 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
289 return [output_api
.PresubmitPromptWarning('New code should not use wstrings.'
290 ' If you are calling a cross-platform API that accepts a wstring, '
292 '\n'.join(problems
))]
295 def _CheckNoDEPSGIT(input_api
, output_api
):
296 """Make sure .DEPS.git is never modified manually."""
297 if any(f
.LocalPath().endswith('.DEPS.git') for f
in
298 input_api
.AffectedFiles()):
299 return [output_api
.PresubmitError(
300 'Never commit changes to .DEPS.git. This file is maintained by an\n'
301 'automated system based on what\'s in DEPS and your changes will be\n'
303 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
304 'for more information')]
308 def _CheckNoBannedFunctions(input_api
, output_api
):
309 """Make sure that banned functions are not used."""
313 file_filter
= lambda f
: f
.LocalPath().endswith(('.mm', '.m', '.h'))
314 for f
in input_api
.AffectedFiles(file_filter
=file_filter
):
315 for line_num
, line
in f
.ChangedContents():
316 for func_name
, message
, error
in _BANNED_OBJC_FUNCTIONS
:
317 if func_name
in line
:
321 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
322 for message_line
in message
:
323 problems
.append(' %s' % message_line
)
325 file_filter
= lambda f
: f
.LocalPath().endswith(('.cc', '.mm', '.h'))
326 for f
in input_api
.AffectedFiles(file_filter
=file_filter
):
327 for line_num
, line
in f
.ChangedContents():
328 for func_name
, message
, error
, excluded_paths
in _BANNED_CPP_FUNCTIONS
:
329 def IsBlacklisted(affected_file
, blacklist
):
330 local_path
= affected_file
.LocalPath()
331 for item
in blacklist
:
332 if input_api
.re
.match(item
, local_path
):
335 if IsBlacklisted(f
, excluded_paths
):
337 if func_name
in line
:
341 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
342 for message_line
in message
:
343 problems
.append(' %s' % message_line
)
347 result
.append(output_api
.PresubmitPromptWarning(
348 'Banned functions were used.\n' + '\n'.join(warnings
)))
350 result
.append(output_api
.PresubmitError(
351 'Banned functions were used.\n' + '\n'.join(errors
)))
355 def _CheckNoPragmaOnce(input_api
, output_api
):
356 """Make sure that banned functions are not used."""
358 pattern
= input_api
.re
.compile(r
'^#pragma\s+once',
359 input_api
.re
.MULTILINE
)
360 for f
in input_api
.AffectedSourceFiles(input_api
.FilterSourceFile
):
361 if not f
.LocalPath().endswith('.h'):
363 contents
= input_api
.ReadFile(f
)
364 if pattern
.search(contents
):
368 return [output_api
.PresubmitError(
369 'Do not use #pragma once in header files.\n'
370 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
375 def _CheckNoTrinaryTrueFalse(input_api
, output_api
):
376 """Checks to make sure we don't introduce use of foo ? true : false."""
378 pattern
= input_api
.re
.compile(r
'\?\s*(true|false)\s*:\s*(true|false)')
379 for f
in input_api
.AffectedFiles():
380 if not f
.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
383 for line_num
, line
in f
.ChangedContents():
384 if pattern
.match(line
):
385 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
389 return [output_api
.PresubmitPromptWarning(
390 'Please consider avoiding the "? true : false" pattern if possible.\n' +
391 '\n'.join(problems
))]
394 def _CheckUnwantedDependencies(input_api
, output_api
):
395 """Runs checkdeps on #include statements added in this
396 change. Breaking - rules is an error, breaking ! rules is a
399 # We need to wait until we have an input_api object and use this
400 # roundabout construct to import checkdeps because this file is
401 # eval-ed and thus doesn't have __file__.
402 original_sys_path
= sys
.path
404 sys
.path
= sys
.path
+ [input_api
.os_path
.join(
405 input_api
.PresubmitLocalPath(), 'tools', 'checkdeps')]
407 from cpp_checker
import CppChecker
408 from rules
import Rule
410 # Restore sys.path to what it was before.
411 sys
.path
= original_sys_path
414 for f
in input_api
.AffectedFiles():
415 if not CppChecker
.IsCppFile(f
.LocalPath()):
418 changed_lines
= [line
for line_num
, line
in f
.ChangedContents()]
419 added_includes
.append([f
.LocalPath(), changed_lines
])
421 deps_checker
= checkdeps
.DepsChecker()
423 error_descriptions
= []
424 warning_descriptions
= []
425 for path
, rule_type
, rule_description
in deps_checker
.CheckAddedCppIncludes(
427 description_with_path
= '%s\n %s' % (path
, rule_description
)
428 if rule_type
== Rule
.DISALLOW
:
429 error_descriptions
.append(description_with_path
)
431 warning_descriptions
.append(description_with_path
)
434 if error_descriptions
:
435 results
.append(output_api
.PresubmitError(
436 'You added one or more #includes that violate checkdeps rules.',
438 if warning_descriptions
:
439 results
.append(output_api
.PresubmitPromptOrNotify(
440 'You added one or more #includes of files that are temporarily\n'
441 'allowed but being removed. Can you avoid introducing the\n'
442 '#include? See relevant DEPS file(s) for details and contacts.',
443 warning_descriptions
))
447 def _CheckFilePermissions(input_api
, output_api
):
448 """Check that all files have their permissions properly set."""
449 args
= [sys
.executable
, 'tools/checkperms/checkperms.py', '--root',
450 input_api
.change
.RepositoryRoot()]
451 for f
in input_api
.AffectedFiles():
452 args
+= ['--file', f
.LocalPath()]
454 (errors
, stderrdata
) = subprocess
.Popen(args
).communicate()
458 results
.append(output_api
.PresubmitError('checkperms.py failed.',
463 def _CheckNoAuraWindowPropertyHInHeaders(input_api
, output_api
):
464 """Makes sure we don't include ui/aura/window_property.h
467 pattern
= input_api
.re
.compile(r
'^#include\s*"ui/aura/window_property.h"')
469 for f
in input_api
.AffectedFiles():
470 if not f
.LocalPath().endswith('.h'):
472 for line_num
, line
in f
.ChangedContents():
473 if pattern
.match(line
):
474 errors
.append(' %s:%d' % (f
.LocalPath(), line_num
))
478 results
.append(output_api
.PresubmitError(
479 'Header files should not include ui/aura/window_property.h', errors
))
483 def _CheckIncludeOrderForScope(scope
, input_api
, file_path
, changed_linenums
):
484 """Checks that the lines in scope occur in the right order.
486 1. C system files in alphabetical order
487 2. C++ system files in alphabetical order
488 3. Project's .h files
491 c_system_include_pattern
= input_api
.re
.compile(r
'\s*#include <.*\.h>')
492 cpp_system_include_pattern
= input_api
.re
.compile(r
'\s*#include <.*>')
493 custom_include_pattern
= input_api
.re
.compile(r
'\s*#include ".*')
495 C_SYSTEM_INCLUDES
, CPP_SYSTEM_INCLUDES
, CUSTOM_INCLUDES
= range(3)
497 state
= C_SYSTEM_INCLUDES
500 previous_line_num
= 0
501 problem_linenums
= []
502 for line_num
, line
in scope
:
503 if c_system_include_pattern
.match(line
):
504 if state
!= C_SYSTEM_INCLUDES
:
505 problem_linenums
.append((line_num
, previous_line_num
))
506 elif previous_line
and previous_line
> line
:
507 problem_linenums
.append((line_num
, previous_line_num
))
508 elif cpp_system_include_pattern
.match(line
):
509 if state
== C_SYSTEM_INCLUDES
:
510 state
= CPP_SYSTEM_INCLUDES
511 elif state
== CUSTOM_INCLUDES
:
512 problem_linenums
.append((line_num
, previous_line_num
))
513 elif previous_line
and previous_line
> line
:
514 problem_linenums
.append((line_num
, previous_line_num
))
515 elif custom_include_pattern
.match(line
):
516 if state
!= CUSTOM_INCLUDES
:
517 state
= CUSTOM_INCLUDES
518 elif previous_line
and previous_line
> line
:
519 problem_linenums
.append((line_num
, previous_line_num
))
521 problem_linenums
.append(line_num
)
523 previous_line_num
= line_num
526 for (line_num
, previous_line_num
) in problem_linenums
:
527 if line_num
in changed_linenums
or previous_line_num
in changed_linenums
:
528 warnings
.append(' %s:%d' % (file_path
, line_num
))
532 def _CheckIncludeOrderInFile(input_api
, f
, changed_linenums
):
533 """Checks the #include order for the given file f."""
535 system_include_pattern
= input_api
.re
.compile(r
'\s*#include \<.*')
536 # Exclude #include <.../...> includes from the check; e.g., <sys/...> includes
537 # often need to appear in a specific order.
538 excluded_include_pattern
= input_api
.re
.compile(r
'\s*#include \<.*/.*')
539 custom_include_pattern
= input_api
.re
.compile(r
'\s*#include "(?P<FILE>.*)"')
540 if_pattern
= input_api
.re
.compile(
541 r
'\s*#\s*(if|elif|else|endif|define|undef).*')
542 # Some files need specialized order of includes; exclude such files from this
544 uncheckable_includes_pattern
= input_api
.re
.compile(
546 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
548 contents
= f
.NewContents()
552 # Handle the special first include. If the first include file is
553 # some/path/file.h, the corresponding including file can be some/path/file.cc,
554 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
555 # etc. It's also possible that no special first include exists.
556 for line
in contents
:
558 if system_include_pattern
.match(line
):
559 # No special first include -> process the line again along with normal
563 match
= custom_include_pattern
.match(line
)
565 match_dict
= match
.groupdict()
566 header_basename
= input_api
.os_path
.basename(
567 match_dict
['FILE']).replace('.h', '')
568 if header_basename
not in input_api
.os_path
.basename(f
.LocalPath()):
569 # No special first include -> process the line again along with normal
574 # Split into scopes: Each region between #if and #endif is its own scope.
577 for line
in contents
[line_num
:]:
579 if uncheckable_includes_pattern
.match(line
):
581 if if_pattern
.match(line
):
582 scopes
.append(current_scope
)
584 elif ((system_include_pattern
.match(line
) or
585 custom_include_pattern
.match(line
)) and
586 not excluded_include_pattern
.match(line
)):
587 current_scope
.append((line_num
, line
))
588 scopes
.append(current_scope
)
591 warnings
.extend(_CheckIncludeOrderForScope(scope
, input_api
, f
.LocalPath(),
596 def _CheckIncludeOrder(input_api
, output_api
):
597 """Checks that the #include order is correct.
599 1. The corresponding header for source files.
600 2. C system files in alphabetical order
601 3. C++ system files in alphabetical order
602 4. Project's .h files in alphabetical order
604 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
605 these rules separately.
609 for f
in input_api
.AffectedFiles():
610 if f
.LocalPath().endswith(('.cc', '.h')):
611 changed_linenums
= set(line_num
for line_num
, _
in f
.ChangedContents())
612 warnings
.extend(_CheckIncludeOrderInFile(input_api
, f
, changed_linenums
))
616 results
.append(output_api
.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING
,
621 def _CheckForVersionControlConflictsInFile(input_api
, f
):
622 pattern
= input_api
.re
.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
624 for line_num
, line
in f
.ChangedContents():
625 if pattern
.match(line
):
626 errors
.append(' %s:%d %s' % (f
.LocalPath(), line_num
, line
))
630 def _CheckForVersionControlConflicts(input_api
, output_api
):
631 """Usually this is not intentional and will cause a compile failure."""
633 for f
in input_api
.AffectedFiles():
634 errors
.extend(_CheckForVersionControlConflictsInFile(input_api
, f
))
638 results
.append(output_api
.PresubmitError(
639 'Version control conflict markers found, please resolve.', errors
))
643 def _CheckHardcodedGoogleHostsInLowerLayers(input_api
, output_api
):
644 def FilterFile(affected_file
):
645 """Filter function for use with input_api.AffectedSourceFiles,
646 below. This filters out everything except non-test files from
647 top-level directories that generally speaking should not hard-code
648 service URLs (e.g. src/android_webview/, src/content/ and others).
650 return input_api
.FilterSourceFile(
652 white_list
=(r
'^(android_webview|base|content|net)[\\\/].*', ),
653 black_list
=(_EXCLUDED_PATHS
+
654 _TEST_CODE_EXCLUDED_PATHS
+
655 input_api
.DEFAULT_BLACK_LIST
))
657 pattern
= input_api
.re
.compile('"[^"]*google\.com[^"]*"')
658 problems
= [] # items are (filename, line_number, line)
659 for f
in input_api
.AffectedSourceFiles(FilterFile
):
660 for line_num
, line
in f
.ChangedContents():
661 if pattern
.search(line
):
662 problems
.append((f
.LocalPath(), line_num
, line
))
665 return [output_api
.PresubmitPromptOrNotify(
666 'Most layers below src/chrome/ should not hardcode service URLs.\n'
667 'Are you sure this is correct? (Contact: joi@chromium.org)',
669 problem
[0], problem
[1], problem
[2]) for problem
in problems
])]
674 def _CheckNoAbbreviationInPngFileName(input_api
, output_api
):
675 """Makes sure there are no abbreviations in the name of PNG files.
677 pattern
= input_api
.re
.compile(r
'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
679 for f
in input_api
.AffectedFiles(include_deletes
=False):
680 if pattern
.match(f
.LocalPath()):
681 errors
.append(' %s' % f
.LocalPath())
685 results
.append(output_api
.PresubmitError(
686 'The name of PNG files should not have abbreviations. \n'
687 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
688 'Contact oshima@chromium.org if you have questions.', errors
))
692 def _CommonChecks(input_api
, output_api
):
693 """Checks common to both upload and commit."""
695 results
.extend(input_api
.canned_checks
.PanProjectChecks(
696 input_api
, output_api
, excluded_paths
=_EXCLUDED_PATHS
))
697 results
.extend(_CheckAuthorizedAuthor(input_api
, output_api
))
699 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
))
700 results
.extend(_CheckNoIOStreamInHeaders(input_api
, output_api
))
701 results
.extend(_CheckNoUNIT_TESTInSourceFiles(input_api
, output_api
))
702 results
.extend(_CheckNoNewWStrings(input_api
, output_api
))
703 results
.extend(_CheckNoDEPSGIT(input_api
, output_api
))
704 results
.extend(_CheckNoBannedFunctions(input_api
, output_api
))
705 results
.extend(_CheckNoPragmaOnce(input_api
, output_api
))
706 results
.extend(_CheckNoTrinaryTrueFalse(input_api
, output_api
))
707 results
.extend(_CheckUnwantedDependencies(input_api
, output_api
))
708 results
.extend(_CheckFilePermissions(input_api
, output_api
))
709 results
.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api
, output_api
))
710 results
.extend(_CheckIncludeOrder(input_api
, output_api
))
711 results
.extend(_CheckForVersionControlConflicts(input_api
, output_api
))
712 results
.extend(_CheckPatchFiles(input_api
, output_api
))
713 results
.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api
, output_api
))
714 results
.extend(_CheckNoAbbreviationInPngFileName(input_api
, output_api
))
715 results
.extend(_CheckForInvalidOSMacros(input_api
, output_api
))
717 if any('PRESUBMIT.py' == f
.LocalPath() for f
in input_api
.AffectedFiles()):
718 results
.extend(input_api
.canned_checks
.RunUnitTestsInDirectory(
719 input_api
, output_api
,
720 input_api
.PresubmitLocalPath(),
721 whitelist
=[r
'^PRESUBMIT_test\.py$']))
725 def _CheckSubversionConfig(input_api
, output_api
):
726 """Verifies the subversion config file is correctly setup.
728 Checks that autoprops are enabled, returns an error otherwise.
730 join
= input_api
.os_path
.join
731 if input_api
.platform
== 'win32':
732 appdata
= input_api
.environ
.get('APPDATA', '')
734 return [output_api
.PresubmitError('%APPDATA% is not configured.')]
735 path
= join(appdata
, 'Subversion', 'config')
737 home
= input_api
.environ
.get('HOME', '')
739 return [output_api
.PresubmitError('$HOME is not configured.')]
740 path
= join(home
, '.subversion', 'config')
743 'Please look at http://dev.chromium.org/developers/coding-style to\n'
744 'configure your subversion configuration file. This enables automatic\n'
745 'properties to simplify the project maintenance.\n'
746 'Pro-tip: just download and install\n'
747 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
750 lines
= open(path
, 'r').read().splitlines()
751 # Make sure auto-props is enabled and check for 2 Chromium standard
753 if (not '*.cc = svn:eol-style=LF' in lines
or
754 not '*.pdf = svn:mime-type=application/pdf' in lines
or
755 not 'enable-auto-props = yes' in lines
):
757 output_api
.PresubmitNotifyResult(
758 'It looks like you have not configured your subversion config '
759 'file or it is not up-to-date.\n' + error_msg
)
761 except (OSError, IOError):
763 output_api
.PresubmitNotifyResult(
764 'Can\'t find your subversion config file.\n' + error_msg
)
769 def _CheckAuthorizedAuthor(input_api
, output_api
):
770 """For non-googler/chromites committers, verify the author's email address is
773 # TODO(maruel): Add it to input_api?
776 author
= input_api
.change
.author_email
778 input_api
.logging
.info('No author, skipping AUTHOR check')
780 authors_path
= input_api
.os_path
.join(
781 input_api
.PresubmitLocalPath(), 'AUTHORS')
783 input_api
.re
.match(r
'[^#]+\s+\<(.+?)\>\s*$', line
)
784 for line
in open(authors_path
))
785 valid_authors
= [item
.group(1).lower() for item
in valid_authors
if item
]
786 if not any(fnmatch
.fnmatch(author
.lower(), valid
) for valid
in valid_authors
):
787 input_api
.logging
.info('Valid authors are %s', ', '.join(valid_authors
))
788 return [output_api
.PresubmitPromptWarning(
789 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
791 'http://www.chromium.org/developers/contributing-code and read the '
793 'If you are a chromite, verify the contributor signed the CLA.') %
798 def _CheckPatchFiles(input_api
, output_api
):
799 problems
= [f
.LocalPath() for f
in input_api
.AffectedFiles()
800 if f
.LocalPath().endswith(('.orig', '.rej'))]
802 return [output_api
.PresubmitError(
803 "Don't commit .rej and .orig files.", problems
)]
808 def _DidYouMeanOSMacro(bad_macro
):
810 return {'A': 'OS_ANDROID',
820 'W': 'OS_WIN'}[bad_macro
[3].upper()]
825 def _CheckForInvalidOSMacrosInFile(input_api
, f
):
826 """Check for sensible looking, totally invalid OS macros."""
827 preprocessor_statement
= input_api
.re
.compile(r
'^\s*#')
828 os_macro
= input_api
.re
.compile(r
'defined\((OS_[^)]+)\)')
830 for lnum
, line
in f
.ChangedContents():
831 if preprocessor_statement
.search(line
):
832 for match
in os_macro
.finditer(line
):
833 if not match
.group(1) in _VALID_OS_MACROS
:
834 good
= _DidYouMeanOSMacro(match
.group(1))
835 did_you_mean
= ' (did you mean %s?)' % good
if good
else ''
836 results
.append(' %s:%d %s%s' % (f
.LocalPath(),
843 def _CheckForInvalidOSMacros(input_api
, output_api
):
844 """Check all affected files for invalid OS macros."""
846 for f
in input_api
.AffectedFiles():
847 if not f
.LocalPath().endswith(('.py', '.js', '.html', '.css')):
848 bad_macros
.extend(_CheckForInvalidOSMacrosInFile(input_api
, f
))
853 return [output_api
.PresubmitError(
854 'Possibly invalid OS macro[s] found. Please fix your code\n'
855 'or add your macro to src/PRESUBMIT.py.', bad_macros
)]
858 def CheckChangeOnUpload(input_api
, output_api
):
860 results
.extend(_CommonChecks(input_api
, output_api
))
864 def CheckChangeOnCommit(input_api
, output_api
):
866 results
.extend(_CommonChecks(input_api
, output_api
))
867 # TODO(thestig) temporarily disabled, doesn't work in third_party/
868 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
869 # input_api, output_api, sources))
870 # Make sure the tree is 'open'.
871 results
.extend(input_api
.canned_checks
.CheckTreeIsOpen(
874 json_url
='http://chromium-status.appspot.com/current?format=json'))
875 results
.extend(input_api
.canned_checks
.CheckRietveldTryJobExecution(input_api
,
876 output_api
, 'http://codereview.chromium.org',
877 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
878 'tryserver@chromium.org'))
880 results
.extend(input_api
.canned_checks
.CheckChangeHasBugField(
881 input_api
, output_api
))
882 results
.extend(input_api
.canned_checks
.CheckChangeHasDescription(
883 input_api
, output_api
))
884 results
.extend(_CheckSubversionConfig(input_api
, output_api
))
888 def GetPreferredTrySlaves(project
, change
):
889 files
= change
.LocalPaths()
891 if not files
or all(re
.search(r
'[\\/]OWNERS$', f
) for f
in files
):
894 if all(re
.search('\.(m|mm)$|(^|[/_])mac[/_.]', f
) for f
in files
):
895 return ['mac_rel', 'mac_asan', 'mac:compile']
896 if all(re
.search('(^|[/_])win[/_.]', f
) for f
in files
):
897 return ['win_rel', 'win7_aura', 'win:compile']
898 if all(re
.search('(^|[/_])android[/_.]', f
) for f
in files
):
899 return ['android_dbg', 'android_clang_dbg']
900 if all(re
.search('^native_client_sdk', f
) for f
in files
):
901 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
902 if all(re
.search('[/_]ios[/_.]', f
) for f
in files
):
903 return ['ios_rel_device', 'ios_dbg_simulator']
913 'linux_clang:compile',
923 # Match things like path/aura/file.cc and path/file_aura.cc.
925 if any(re
.search('[/_](aura|chromeos)', f
) for f
in files
):
926 trybots
+= ['linux_chromeos_clang:compile', 'linux_chromeos_asan']