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$",
28 r
"^gpu[\\\/]config[\\\/].*_list_json\.cc$",
31 # Fragment of a regular expression that matches C++ and Objective-C++
32 # implementation files.
33 _IMPLEMENTATION_EXTENSIONS
= r
'\.(cc|cpp|cxx|mm)$'
35 # Regular expression that matches code only used for test binaries
37 _TEST_CODE_EXCLUDED_PATHS
= (
38 r
'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS
,
39 r
'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS
,
40 r
'.+_(api|browser|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
41 _IMPLEMENTATION_EXTENSIONS
,
42 r
'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS
,
43 r
'.*[/\\](test|tool(s)?)[/\\].*',
44 # content_shell is used for running layout tests.
45 r
'content[/\\]shell[/\\].*',
46 # At request of folks maintaining this folder.
47 r
'chrome[/\\]browser[/\\]automation[/\\].*',
50 _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 joi@chromium.org if you have questions.')
58 _INCLUDE_ORDER_WARNING
= (
59 'Your #include order seems to be broken. Send mail to\n'
60 'marja@chromium.org if this is not the case.')
63 _BANNED_OBJC_FUNCTIONS
= (
67 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
68 'prohibited. Please use CrTrackingArea instead.',
69 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
76 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
78 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
83 'convertPointFromBase:',
85 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
86 'Please use |convertPoint:(point) fromView:nil| instead.',
87 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
92 'convertPointToBase:',
94 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
95 'Please use |convertPoint:(point) toView:nil| instead.',
96 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
101 'convertRectFromBase:',
103 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
104 'Please use |convertRect:(point) fromView:nil| instead.',
105 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
110 'convertRectToBase:',
112 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
113 'Please use |convertRect:(point) toView:nil| instead.',
114 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
119 'convertSizeFromBase:',
121 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
122 'Please use |convertSize:(point) fromView:nil| instead.',
123 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
128 'convertSizeToBase:',
130 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
131 'Please use |convertSize:(point) toView:nil| instead.',
132 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
139 _BANNED_CPP_FUNCTIONS
= (
140 # 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
142 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
146 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
147 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
155 'New code should not use ScopedAllowIO. Post a task to the blocking',
156 'pool or the FILE thread instead.',
160 r
"^content[\\\/]shell[\\\/]browser[\\\/]shell_browser_main\.cc$",
161 r
"^content[\\\/]shell[\\\/]browser[\\\/]shell_message_filter\.cc$",
162 r
"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
168 'The use of SkRefPtr is prohibited. ',
169 'Please use skia::RefPtr instead.'
177 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
178 'Please use skia::RefPtr instead.'
186 'The use of SkAutoTUnref is dangerous because it implicitly ',
187 'converts to a raw pointer. Please use skia::RefPtr instead.'
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.'
206 # Please keep sorted.
209 'OS_CAT', # For testing.
223 def _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.
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.
232 file_inclusion_pattern
= r
'.+%s' % _IMPLEMENTATION_EXTENSIONS
234 base_function_pattern
= r
'ForTest(ing)?|for_test(ing)?'
235 inclusion_pattern
= input_api
.re
.compile(r
'(%s)\s*\(' % base_function_pattern
)
236 comment_pattern
= input_api
.re
.compile(r
'//.*%s' % base_function_pattern
)
237 exclusion_pattern
= input_api
.re
.compile(
238 r
'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
239 base_function_pattern
, base_function_pattern
))
241 def FilterFile(affected_file
):
242 black_list
= (_EXCLUDED_PATHS
+
243 _TEST_CODE_EXCLUDED_PATHS
+
244 input_api
.DEFAULT_BLACK_LIST
)
245 return input_api
.FilterSourceFile(
247 white_list
=(file_inclusion_pattern
, ),
248 black_list
=black_list
)
251 for f
in input_api
.AffectedSourceFiles(FilterFile
):
252 local_path
= f
.LocalPath()
253 lines
= input_api
.ReadFile(f
).splitlines()
256 if (inclusion_pattern
.search(line
) and
257 not comment_pattern
.search(line
) and
258 not exclusion_pattern
.search(line
)):
260 '%s:%d\n %s' % (local_path
, line_number
, line
.strip()))
264 return [output_api
.PresubmitPromptOrNotify(_TEST_ONLY_WARNING
, problems
)]
269 def _CheckNoIOStreamInHeaders(input_api
, output_api
):
270 """Checks to make sure no .h files include <iostream>."""
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'):
277 contents
= input_api
.ReadFile(f
)
278 if pattern
.search(contents
):
282 return [ output_api
.PresubmitError(
283 'Do not #include <iostream> in header files, since it inserts static '
284 'initialization into every file including the header. Instead, '
285 '#include <ostream>. See http://crbug.com/94794',
290 def _CheckNoUNIT_TESTInSourceFiles(input_api
, output_api
):
291 """Checks to make sure no source files use UNIT_TEST"""
293 for f
in input_api
.AffectedFiles():
294 if (not f
.LocalPath().endswith(('.cc', '.mm'))):
297 for line_num
, line
in f
.ChangedContents():
298 if 'UNIT_TEST' in line
:
299 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
303 return [output_api
.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
304 '\n'.join(problems
))]
307 def _CheckNoNewWStrings(input_api
, output_api
):
308 """Checks to make sure we don't introduce use of wstrings."""
310 for f
in input_api
.AffectedFiles():
311 if (not f
.LocalPath().endswith(('.cc', '.h')) or
312 f
.LocalPath().endswith(('test.cc', '_win.cc', '_win.h'))):
316 for line_num
, line
in f
.ChangedContents():
317 if 'presubmit: allow wstring' in line
:
319 elif not allowWString
and 'wstring' in line
:
320 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
327 return [output_api
.PresubmitPromptWarning('New code should not use wstrings.'
328 ' If you are calling a cross-platform API that accepts a wstring, '
330 '\n'.join(problems
))]
333 def _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'
341 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
342 'for more information')]
346 def _CheckNoBannedFunctions(input_api
, output_api
):
347 """Make sure that banned functions are not used."""
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
:
359 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
360 for message_line
in message
:
361 problems
.append(' %s' % message_line
)
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():
366 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
):
373 if IsBlacklisted(f
, excluded_paths
):
375 if func_name
in line
:
379 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
380 for message_line
in message
:
381 problems
.append(' %s' % message_line
)
385 result
.append(output_api
.PresubmitPromptWarning(
386 'Banned functions were used.\n' + '\n'.join(warnings
)))
388 result
.append(output_api
.PresubmitError(
389 'Banned functions were used.\n' + '\n'.join(errors
)))
393 def _CheckNoPragmaOnce(input_api
, output_api
):
394 """Make sure that banned functions are not used."""
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'):
401 contents
= input_api
.ReadFile(f
)
402 if pattern
.search(contents
):
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',
413 def _CheckNoTrinaryTrueFalse(input_api
, output_api
):
414 """Checks to make sure we don't introduce use of foo ? true : false."""
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')):
421 for line_num
, line
in f
.ChangedContents():
422 if pattern
.match(line
):
423 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
427 return [output_api
.PresubmitPromptWarning(
428 'Please consider avoiding the "? true : false" pattern if possible.\n' +
429 '\n'.join(problems
))]
432 def _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
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
442 sys
.path
= sys
.path
+ [input_api
.os_path
.join(
443 input_api
.PresubmitLocalPath(), 'tools', 'checkdeps')]
445 from cpp_checker
import CppChecker
446 from rules
import Rule
448 # Restore sys.path to what it was before.
449 sys
.path
= original_sys_path
452 for f
in input_api
.AffectedFiles():
453 if not CppChecker
.IsCppFile(f
.LocalPath()):
456 changed_lines
= [line
for line_num
, line
in f
.ChangedContents()]
457 added_includes
.append([f
.LocalPath(), changed_lines
])
459 deps_checker
= checkdeps
.DepsChecker(input_api
.PresubmitLocalPath())
461 error_descriptions
= []
462 warning_descriptions
= []
463 for path
, rule_type
, rule_description
in deps_checker
.CheckAddedCppIncludes(
465 description_with_path
= '%s\n %s' % (path
, rule_description
)
466 if rule_type
== Rule
.DISALLOW
:
467 error_descriptions
.append(description_with_path
)
469 warning_descriptions
.append(description_with_path
)
472 if error_descriptions
:
473 results
.append(output_api
.PresubmitError(
474 'You added one or more #includes that violate checkdeps rules.',
476 if warning_descriptions
:
477 results
.append(output_api
.PresubmitPromptOrNotify(
478 '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
))
485 def _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()]
492 (errors
, stderrdata
) = subprocess
.Popen(args
).communicate()
496 results
.append(output_api
.PresubmitError('checkperms.py failed.',
501 def _CheckNoAuraWindowPropertyHInHeaders(input_api
, output_api
):
502 """Makes sure we don't include ui/aura/window_property.h
505 pattern
= input_api
.re
.compile(r
'^#include\s*"ui/aura/window_property.h"')
507 for f
in input_api
.AffectedFiles():
508 if not f
.LocalPath().endswith('.h'):
510 for line_num
, line
in f
.ChangedContents():
511 if pattern
.match(line
):
512 errors
.append(' %s:%d' % (f
.LocalPath(), line_num
))
516 results
.append(output_api
.PresubmitError(
517 'Header files should not include ui/aura/window_property.h', errors
))
521 def _CheckIncludeOrderForScope(scope
, input_api
, file_path
, changed_linenums
):
522 """Checks that the lines in scope occur in the right order.
524 1. C system files in alphabetical order
525 2. C++ system files in alphabetical order
526 3. Project's .h files
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 ".*')
533 C_SYSTEM_INCLUDES
, CPP_SYSTEM_INCLUDES
, CUSTOM_INCLUDES
= range(3)
535 state
= C_SYSTEM_INCLUDES
538 previous_line_num
= 0
539 problem_linenums
= []
540 for line_num
, line
in scope
:
541 if c_system_include_pattern
.match(line
):
542 if state
!= C_SYSTEM_INCLUDES
:
543 problem_linenums
.append((line_num
, previous_line_num
))
544 elif previous_line
and previous_line
> line
:
545 problem_linenums
.append((line_num
, previous_line_num
))
546 elif cpp_system_include_pattern
.match(line
):
547 if state
== C_SYSTEM_INCLUDES
:
548 state
= CPP_SYSTEM_INCLUDES
549 elif state
== CUSTOM_INCLUDES
:
550 problem_linenums
.append((line_num
, previous_line_num
))
551 elif previous_line
and previous_line
> line
:
552 problem_linenums
.append((line_num
, previous_line_num
))
553 elif custom_include_pattern
.match(line
):
554 if state
!= CUSTOM_INCLUDES
:
555 state
= CUSTOM_INCLUDES
556 elif previous_line
and previous_line
> line
:
557 problem_linenums
.append((line_num
, previous_line_num
))
559 problem_linenums
.append(line_num
)
561 previous_line_num
= line_num
564 for (line_num
, previous_line_num
) in problem_linenums
:
565 if line_num
in changed_linenums
or previous_line_num
in changed_linenums
:
566 warnings
.append(' %s:%d' % (file_path
, line_num
))
570 def _CheckIncludeOrderInFile(input_api
, f
, changed_linenums
):
571 """Checks the #include order for the given file f."""
573 system_include_pattern
= input_api
.re
.compile(r
'\s*#include \<.*')
574 # Exclude the following includes from the check:
575 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
577 # 2) <atlbase.h>, "build/build_config.h"
578 excluded_include_pattern
= input_api
.re
.compile(
579 r
'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
580 custom_include_pattern
= input_api
.re
.compile(r
'\s*#include "(?P<FILE>.*)"')
581 if_pattern
= input_api
.re
.compile(
582 r
'\s*#\s*(if|elif|else|endif|define|undef).*')
583 # Some files need specialized order of includes; exclude such files from this
585 uncheckable_includes_pattern
= input_api
.re
.compile(
587 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
589 contents
= f
.NewContents()
593 # Handle the special first include. If the first include file is
594 # some/path/file.h, the corresponding including file can be some/path/file.cc,
595 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
596 # etc. It's also possible that no special first include exists.
597 for line
in contents
:
599 if system_include_pattern
.match(line
):
600 # No special first include -> process the line again along with normal
604 match
= custom_include_pattern
.match(line
)
606 match_dict
= match
.groupdict()
607 header_basename
= input_api
.os_path
.basename(
608 match_dict
['FILE']).replace('.h', '')
609 if header_basename
not in input_api
.os_path
.basename(f
.LocalPath()):
610 # No special first include -> process the line again along with normal
615 # Split into scopes: Each region between #if and #endif is its own scope.
618 for line
in contents
[line_num
:]:
620 if uncheckable_includes_pattern
.match(line
):
622 if if_pattern
.match(line
):
623 scopes
.append(current_scope
)
625 elif ((system_include_pattern
.match(line
) or
626 custom_include_pattern
.match(line
)) and
627 not excluded_include_pattern
.match(line
)):
628 current_scope
.append((line_num
, line
))
629 scopes
.append(current_scope
)
632 warnings
.extend(_CheckIncludeOrderForScope(scope
, input_api
, f
.LocalPath(),
637 def _CheckIncludeOrder(input_api
, output_api
):
638 """Checks that the #include order is correct.
640 1. The corresponding header for source files.
641 2. C system files in alphabetical order
642 3. C++ system files in alphabetical order
643 4. Project's .h files in alphabetical order
645 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
646 these rules separately.
650 for f
in input_api
.AffectedFiles():
651 if f
.LocalPath().endswith(('.cc', '.h')):
652 changed_linenums
= set(line_num
for line_num
, _
in f
.ChangedContents())
653 warnings
.extend(_CheckIncludeOrderInFile(input_api
, f
, changed_linenums
))
657 results
.append(output_api
.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING
,
662 def _CheckForVersionControlConflictsInFile(input_api
, f
):
663 pattern
= input_api
.re
.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
665 for line_num
, line
in f
.ChangedContents():
666 if pattern
.match(line
):
667 errors
.append(' %s:%d %s' % (f
.LocalPath(), line_num
, line
))
671 def _CheckForVersionControlConflicts(input_api
, output_api
):
672 """Usually this is not intentional and will cause a compile failure."""
674 for f
in input_api
.AffectedFiles():
675 errors
.extend(_CheckForVersionControlConflictsInFile(input_api
, f
))
679 results
.append(output_api
.PresubmitError(
680 'Version control conflict markers found, please resolve.', errors
))
684 def _CheckHardcodedGoogleHostsInLowerLayers(input_api
, output_api
):
685 def FilterFile(affected_file
):
686 """Filter function for use with input_api.AffectedSourceFiles,
687 below. This filters out everything except non-test files from
688 top-level directories that generally speaking should not hard-code
689 service URLs (e.g. src/android_webview/, src/content/ and others).
691 return input_api
.FilterSourceFile(
693 white_list
=(r
'^(android_webview|base|content|net)[\\\/].*', ),
694 black_list
=(_EXCLUDED_PATHS
+
695 _TEST_CODE_EXCLUDED_PATHS
+
696 input_api
.DEFAULT_BLACK_LIST
))
698 base_pattern
= '"[^"]*google\.com[^"]*"'
699 comment_pattern
= input_api
.re
.compile('//.*%s' % base_pattern
)
700 pattern
= input_api
.re
.compile(base_pattern
)
701 problems
= [] # items are (filename, line_number, line)
702 for f
in input_api
.AffectedSourceFiles(FilterFile
):
703 for line_num
, line
in f
.ChangedContents():
704 if not comment_pattern
.search(line
) and pattern
.search(line
):
705 problems
.append((f
.LocalPath(), line_num
, line
))
708 return [output_api
.PresubmitPromptOrNotify(
709 'Most layers below src/chrome/ should not hardcode service URLs.\n'
710 'Are you sure this is correct? (Contact: joi@chromium.org)',
712 problem
[0], problem
[1], problem
[2]) for problem
in problems
])]
717 def _CheckNoAbbreviationInPngFileName(input_api
, output_api
):
718 """Makes sure there are no abbreviations in the name of PNG files.
720 pattern
= input_api
.re
.compile(r
'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
722 for f
in input_api
.AffectedFiles(include_deletes
=False):
723 if pattern
.match(f
.LocalPath()):
724 errors
.append(' %s' % f
.LocalPath())
728 results
.append(output_api
.PresubmitError(
729 'The name of PNG files should not have abbreviations. \n'
730 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
731 'Contact oshima@chromium.org if you have questions.', errors
))
735 def _DepsFilesToCheck(re
, changed_lines
):
736 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
737 a set of DEPS entries that we should look up."""
740 # This pattern grabs the path without basename in the first
741 # parentheses, and the basename (if present) in the second. It
742 # relies on the simple heuristic that if there is a basename it will
743 # be a header file ending in ".h".
744 pattern
= re
.compile(
745 r
"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
746 for changed_line
in changed_lines
:
747 m
= pattern
.match(changed_line
)
750 if not (path
.startswith('grit/') or path
== 'grit'):
751 results
.add('%s/DEPS' % m
.group(1))
755 def _CheckAddedDepsHaveTargetApprovals(input_api
, output_api
):
756 """When a dependency prefixed with + is added to a DEPS file, we
757 want to make sure that the change is reviewed by an OWNER of the
758 target file or directory, to avoid layering violations from being
759 introduced. This check verifies that this happens.
761 changed_lines
= set()
762 for f
in input_api
.AffectedFiles():
763 filename
= input_api
.os_path
.basename(f
.LocalPath())
764 if filename
== 'DEPS':
765 changed_lines |
= set(line
.strip()
767 in f
.ChangedContents())
768 if not changed_lines
:
771 virtual_depended_on_files
= _DepsFilesToCheck(input_api
.re
, changed_lines
)
772 if not virtual_depended_on_files
:
775 if input_api
.is_committing
:
777 return [output_api
.PresubmitNotifyResult(
778 '--tbr was specified, skipping OWNERS check for DEPS additions')]
779 if not input_api
.change
.issue
:
780 return [output_api
.PresubmitError(
781 "DEPS approval by OWNERS check failed: this change has "
782 "no Rietveld issue number, so we can't check it for approvals.")]
783 output
= output_api
.PresubmitError
785 output
= output_api
.PresubmitNotifyResult
787 owners_db
= input_api
.owners_db
788 owner_email
, reviewers
= input_api
.canned_checks
._RietveldOwnerAndReviewers
(
790 owners_db
.email_regexp
,
791 approval_needed
=input_api
.is_committing
)
793 owner_email
= owner_email
or input_api
.change
.author_email
795 reviewers_plus_owner
= set(reviewers
)
797 reviewers_plus_owner
.add(owner_email
)
798 missing_files
= owners_db
.files_not_covered_by(virtual_depended_on_files
,
799 reviewers_plus_owner
)
800 unapproved_dependencies
= ["'+%s'," % path
[:-len('/DEPS')]
801 for path
in missing_files
]
803 if unapproved_dependencies
:
805 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
806 '\n '.join(sorted(unapproved_dependencies
)))]
807 if not input_api
.is_committing
:
808 suggested_owners
= owners_db
.reviewers_for(missing_files
, owner_email
)
809 output_list
.append(output(
810 'Suggested missing target path OWNERS:\n %s' %
811 '\n '.join(suggested_owners
or [])))
817 def _CommonChecks(input_api
, output_api
):
818 """Checks common to both upload and commit."""
820 results
.extend(input_api
.canned_checks
.PanProjectChecks(
821 input_api
, output_api
, excluded_paths
=_EXCLUDED_PATHS
))
822 results
.extend(_CheckAuthorizedAuthor(input_api
, output_api
))
824 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
))
825 results
.extend(_CheckNoIOStreamInHeaders(input_api
, output_api
))
826 results
.extend(_CheckNoUNIT_TESTInSourceFiles(input_api
, output_api
))
827 results
.extend(_CheckNoNewWStrings(input_api
, output_api
))
828 results
.extend(_CheckNoDEPSGIT(input_api
, output_api
))
829 results
.extend(_CheckNoBannedFunctions(input_api
, output_api
))
830 results
.extend(_CheckNoPragmaOnce(input_api
, output_api
))
831 results
.extend(_CheckNoTrinaryTrueFalse(input_api
, output_api
))
832 results
.extend(_CheckUnwantedDependencies(input_api
, output_api
))
833 results
.extend(_CheckFilePermissions(input_api
, output_api
))
834 results
.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api
, output_api
))
835 results
.extend(_CheckIncludeOrder(input_api
, output_api
))
836 results
.extend(_CheckForVersionControlConflicts(input_api
, output_api
))
837 results
.extend(_CheckPatchFiles(input_api
, output_api
))
838 results
.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api
, output_api
))
839 results
.extend(_CheckNoAbbreviationInPngFileName(input_api
, output_api
))
840 results
.extend(_CheckForInvalidOSMacros(input_api
, output_api
))
841 results
.extend(_CheckAddedDepsHaveTargetApprovals(input_api
, output_api
))
843 input_api
.canned_checks
.CheckChangeHasNoTabs(
846 source_file_filter
=lambda x
: x
.LocalPath().endswith('.grd')))
848 if any('PRESUBMIT.py' == f
.LocalPath() for f
in input_api
.AffectedFiles()):
849 results
.extend(input_api
.canned_checks
.RunUnitTestsInDirectory(
850 input_api
, output_api
,
851 input_api
.PresubmitLocalPath(),
852 whitelist
=[r
'^PRESUBMIT_test\.py$']))
856 def _CheckSubversionConfig(input_api
, output_api
):
857 """Verifies the subversion config file is correctly setup.
859 Checks that autoprops are enabled, returns an error otherwise.
861 join
= input_api
.os_path
.join
862 if input_api
.platform
== 'win32':
863 appdata
= input_api
.environ
.get('APPDATA', '')
865 return [output_api
.PresubmitError('%APPDATA% is not configured.')]
866 path
= join(appdata
, 'Subversion', 'config')
868 home
= input_api
.environ
.get('HOME', '')
870 return [output_api
.PresubmitError('$HOME is not configured.')]
871 path
= join(home
, '.subversion', 'config')
874 'Please look at http://dev.chromium.org/developers/coding-style to\n'
875 'configure your subversion configuration file. This enables automatic\n'
876 'properties to simplify the project maintenance.\n'
877 'Pro-tip: just download and install\n'
878 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
881 lines
= open(path
, 'r').read().splitlines()
882 # Make sure auto-props is enabled and check for 2 Chromium standard
884 if (not '*.cc = svn:eol-style=LF' in lines
or
885 not '*.pdf = svn:mime-type=application/pdf' in lines
or
886 not 'enable-auto-props = yes' in lines
):
888 output_api
.PresubmitNotifyResult(
889 'It looks like you have not configured your subversion config '
890 'file or it is not up-to-date.\n' + error_msg
)
892 except (OSError, IOError):
894 output_api
.PresubmitNotifyResult(
895 'Can\'t find your subversion config file.\n' + error_msg
)
900 def _CheckAuthorizedAuthor(input_api
, output_api
):
901 """For non-googler/chromites committers, verify the author's email address is
904 # TODO(maruel): Add it to input_api?
907 author
= input_api
.change
.author_email
909 input_api
.logging
.info('No author, skipping AUTHOR check')
911 authors_path
= input_api
.os_path
.join(
912 input_api
.PresubmitLocalPath(), 'AUTHORS')
914 input_api
.re
.match(r
'[^#]+\s+\<(.+?)\>\s*$', line
)
915 for line
in open(authors_path
))
916 valid_authors
= [item
.group(1).lower() for item
in valid_authors
if item
]
917 if not any(fnmatch
.fnmatch(author
.lower(), valid
) for valid
in valid_authors
):
918 input_api
.logging
.info('Valid authors are %s', ', '.join(valid_authors
))
919 return [output_api
.PresubmitPromptWarning(
920 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
922 'http://www.chromium.org/developers/contributing-code and read the '
924 'If you are a chromite, verify the contributor signed the CLA.') %
929 def _CheckPatchFiles(input_api
, output_api
):
930 problems
= [f
.LocalPath() for f
in input_api
.AffectedFiles()
931 if f
.LocalPath().endswith(('.orig', '.rej'))]
933 return [output_api
.PresubmitError(
934 "Don't commit .rej and .orig files.", problems
)]
939 def _DidYouMeanOSMacro(bad_macro
):
941 return {'A': 'OS_ANDROID',
951 'W': 'OS_WIN'}[bad_macro
[3].upper()]
956 def _CheckForInvalidOSMacrosInFile(input_api
, f
):
957 """Check for sensible looking, totally invalid OS macros."""
958 preprocessor_statement
= input_api
.re
.compile(r
'^\s*#')
959 os_macro
= input_api
.re
.compile(r
'defined\((OS_[^)]+)\)')
961 for lnum
, line
in f
.ChangedContents():
962 if preprocessor_statement
.search(line
):
963 for match
in os_macro
.finditer(line
):
964 if not match
.group(1) in _VALID_OS_MACROS
:
965 good
= _DidYouMeanOSMacro(match
.group(1))
966 did_you_mean
= ' (did you mean %s?)' % good
if good
else ''
967 results
.append(' %s:%d %s%s' % (f
.LocalPath(),
974 def _CheckForInvalidOSMacros(input_api
, output_api
):
975 """Check all affected files for invalid OS macros."""
977 for f
in input_api
.AffectedFiles():
978 if not f
.LocalPath().endswith(('.py', '.js', '.html', '.css')):
979 bad_macros
.extend(_CheckForInvalidOSMacrosInFile(input_api
, f
))
984 return [output_api
.PresubmitError(
985 'Possibly invalid OS macro[s] found. Please fix your code\n'
986 'or add your macro to src/PRESUBMIT.py.', bad_macros
)]
989 def CheckChangeOnUpload(input_api
, output_api
):
991 results
.extend(_CommonChecks(input_api
, output_api
))
995 def CheckChangeOnCommit(input_api
, output_api
):
997 results
.extend(_CommonChecks(input_api
, output_api
))
998 # TODO(thestig) temporarily disabled, doesn't work in third_party/
999 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1000 # input_api, output_api, sources))
1001 # Make sure the tree is 'open'.
1002 results
.extend(input_api
.canned_checks
.CheckTreeIsOpen(
1005 json_url
='http://chromium-status.appspot.com/current?format=json'))
1006 results
.extend(input_api
.canned_checks
.CheckRietveldTryJobExecution(input_api
,
1007 output_api
, 'http://codereview.chromium.org',
1008 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
1009 'tryserver@chromium.org'))
1011 results
.extend(input_api
.canned_checks
.CheckChangeHasBugField(
1012 input_api
, output_api
))
1013 results
.extend(input_api
.canned_checks
.CheckChangeHasDescription(
1014 input_api
, output_api
))
1015 results
.extend(_CheckSubversionConfig(input_api
, output_api
))
1019 def GetPreferredTrySlaves(project
, change
):
1020 files
= change
.LocalPaths()
1022 if not files
or all(re
.search(r
'[\\/]OWNERS$', f
) for f
in files
):
1025 if all(re
.search('\.(m|mm)$|(^|[/_])mac[/_.]', f
) for f
in files
):
1026 return ['mac_rel', 'mac:compile']
1027 if all(re
.search('(^|[/_])win[/_.]', f
) for f
in files
):
1028 return ['win_rel', 'win7_aura', 'win:compile']
1029 if all(re
.search('(^|[/_])android[/_.]', f
) for f
in files
):
1030 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
1031 if all(re
.search('^native_client_sdk', f
) for f
in files
):
1032 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
1033 if all(re
.search('[/_]ios[/_.]', f
) for f
in files
):
1034 return ['ios_rel_device', 'ios_dbg_simulator']
1037 'android_clang_dbg',
1039 'ios_dbg_simulator',
1044 'linux_clang:compile',
1051 'win_x64_rel:compile',
1054 # Match things like path/aura/file.cc and path/file_aura.cc.
1055 # Same for chromeos.
1056 if any(re
.search('[/_](aura|chromeos)', f
) for f
in files
):
1057 trybots
+= ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
1059 # If there are gyp changes to base, build, or chromeos, run a full cros build
1060 # in addition to the shorter linux_chromeos build. Changes to high level gyp
1061 # files have a much higher chance of breaking the cros build, which is
1062 # differnt from the linux_chromeos build that most chrome developers test
1064 if any(re
.search('^(base|build|chromeos).*\.gypi?$', f
) for f
in files
):
1065 trybots
+= ['cros_x86']
1067 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1068 # unless they're .gyp(i) files as changes to those files can break the gyp
1070 if (not all(re
.search('^chrome', f
) for f
in files
) or
1071 any(re
.search('\.gypi?$', f
) for f
in files
)):
1072 trybots
+= ['android_aosp']