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[\\\/]shell_browser_main\.cc$",
161 r
"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
167 'The use of SkRefPtr is prohibited. ',
168 'Please use skia::RefPtr instead.'
176 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
177 'Please use skia::RefPtr instead.'
185 'The use of SkAutoTUnref is dangerous because it implicitly ',
186 'converts to a raw pointer. Please use skia::RefPtr instead.'
194 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
195 'because it implicitly converts to a raw pointer. ',
196 'Please use skia::RefPtr instead.'
205 # Please keep sorted.
208 'OS_CAT', # For testing.
222 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
):
223 """Attempts to prevent use of functions intended only for testing in
224 non-testing code. For now this is just a best-effort implementation
225 that ignores header files and may have some false positives. A
226 better implementation would probably need a proper C++ parser.
228 # We only scan .cc files and the like, as the declaration of
229 # for-testing functions in header files are hard to distinguish from
230 # calls to such functions without a proper C++ parser.
231 file_inclusion_pattern
= r
'.+%s' % _IMPLEMENTATION_EXTENSIONS
233 base_function_pattern
= r
'ForTest(ing)?|for_test(ing)?'
234 inclusion_pattern
= input_api
.re
.compile(r
'(%s)\s*\(' % base_function_pattern
)
235 comment_pattern
= input_api
.re
.compile(r
'//.*%s' % base_function_pattern
)
236 exclusion_pattern
= input_api
.re
.compile(
237 r
'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
238 base_function_pattern
, base_function_pattern
))
240 def FilterFile(affected_file
):
241 black_list
= (_EXCLUDED_PATHS
+
242 _TEST_CODE_EXCLUDED_PATHS
+
243 input_api
.DEFAULT_BLACK_LIST
)
244 return input_api
.FilterSourceFile(
246 white_list
=(file_inclusion_pattern
, ),
247 black_list
=black_list
)
250 for f
in input_api
.AffectedSourceFiles(FilterFile
):
251 local_path
= f
.LocalPath()
252 lines
= input_api
.ReadFile(f
).splitlines()
255 if (inclusion_pattern
.search(line
) and
256 not comment_pattern
.search(line
) and
257 not exclusion_pattern
.search(line
)):
259 '%s:%d\n %s' % (local_path
, line_number
, line
.strip()))
263 return [output_api
.PresubmitPromptOrNotify(_TEST_ONLY_WARNING
, problems
)]
268 def _CheckNoIOStreamInHeaders(input_api
, output_api
):
269 """Checks to make sure no .h files include <iostream>."""
271 pattern
= input_api
.re
.compile(r
'^#include\s*<iostream>',
272 input_api
.re
.MULTILINE
)
273 for f
in input_api
.AffectedSourceFiles(input_api
.FilterSourceFile
):
274 if not f
.LocalPath().endswith('.h'):
276 contents
= input_api
.ReadFile(f
)
277 if pattern
.search(contents
):
281 return [ output_api
.PresubmitError(
282 'Do not #include <iostream> in header files, since it inserts static '
283 'initialization into every file including the header. Instead, '
284 '#include <ostream>. See http://crbug.com/94794',
289 def _CheckNoUNIT_TESTInSourceFiles(input_api
, output_api
):
290 """Checks to make sure no source files use UNIT_TEST"""
292 for f
in input_api
.AffectedFiles():
293 if (not f
.LocalPath().endswith(('.cc', '.mm'))):
296 for line_num
, line
in f
.ChangedContents():
297 if 'UNIT_TEST' in line
:
298 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
302 return [output_api
.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
303 '\n'.join(problems
))]
306 def _CheckNoNewWStrings(input_api
, output_api
):
307 """Checks to make sure we don't introduce use of wstrings."""
309 for f
in input_api
.AffectedFiles():
310 if (not f
.LocalPath().endswith(('.cc', '.h')) or
311 f
.LocalPath().endswith('test.cc')):
315 for line_num
, line
in f
.ChangedContents():
316 if 'presubmit: allow wstring' in line
:
318 elif not allowWString
and 'wstring' in line
:
319 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
326 return [output_api
.PresubmitPromptWarning('New code should not use wstrings.'
327 ' If you are calling a cross-platform API that accepts a wstring, '
329 '\n'.join(problems
))]
332 def _CheckNoDEPSGIT(input_api
, output_api
):
333 """Make sure .DEPS.git is never modified manually."""
334 if any(f
.LocalPath().endswith('.DEPS.git') for f
in
335 input_api
.AffectedFiles()):
336 return [output_api
.PresubmitError(
337 'Never commit changes to .DEPS.git. This file is maintained by an\n'
338 'automated system based on what\'s in DEPS and your changes will be\n'
340 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
341 'for more information')]
345 def _CheckNoBannedFunctions(input_api
, output_api
):
346 """Make sure that banned functions are not used."""
350 file_filter
= lambda f
: f
.LocalPath().endswith(('.mm', '.m', '.h'))
351 for f
in input_api
.AffectedFiles(file_filter
=file_filter
):
352 for line_num
, line
in f
.ChangedContents():
353 for func_name
, message
, error
in _BANNED_OBJC_FUNCTIONS
:
354 if func_name
in line
:
358 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
359 for message_line
in message
:
360 problems
.append(' %s' % message_line
)
362 file_filter
= lambda f
: f
.LocalPath().endswith(('.cc', '.mm', '.h'))
363 for f
in input_api
.AffectedFiles(file_filter
=file_filter
):
364 for line_num
, line
in f
.ChangedContents():
365 for func_name
, message
, error
, excluded_paths
in _BANNED_CPP_FUNCTIONS
:
366 def IsBlacklisted(affected_file
, blacklist
):
367 local_path
= affected_file
.LocalPath()
368 for item
in blacklist
:
369 if input_api
.re
.match(item
, local_path
):
372 if IsBlacklisted(f
, excluded_paths
):
374 if func_name
in line
:
378 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
379 for message_line
in message
:
380 problems
.append(' %s' % message_line
)
384 result
.append(output_api
.PresubmitPromptWarning(
385 'Banned functions were used.\n' + '\n'.join(warnings
)))
387 result
.append(output_api
.PresubmitError(
388 'Banned functions were used.\n' + '\n'.join(errors
)))
392 def _CheckNoPragmaOnce(input_api
, output_api
):
393 """Make sure that banned functions are not used."""
395 pattern
= input_api
.re
.compile(r
'^#pragma\s+once',
396 input_api
.re
.MULTILINE
)
397 for f
in input_api
.AffectedSourceFiles(input_api
.FilterSourceFile
):
398 if not f
.LocalPath().endswith('.h'):
400 contents
= input_api
.ReadFile(f
)
401 if pattern
.search(contents
):
405 return [output_api
.PresubmitError(
406 'Do not use #pragma once in header files.\n'
407 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
412 def _CheckNoTrinaryTrueFalse(input_api
, output_api
):
413 """Checks to make sure we don't introduce use of foo ? true : false."""
415 pattern
= input_api
.re
.compile(r
'\?\s*(true|false)\s*:\s*(true|false)')
416 for f
in input_api
.AffectedFiles():
417 if not f
.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
420 for line_num
, line
in f
.ChangedContents():
421 if pattern
.match(line
):
422 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
426 return [output_api
.PresubmitPromptWarning(
427 'Please consider avoiding the "? true : false" pattern if possible.\n' +
428 '\n'.join(problems
))]
431 def _CheckUnwantedDependencies(input_api
, output_api
):
432 """Runs checkdeps on #include statements added in this
433 change. Breaking - rules is an error, breaking ! rules is a
436 # We need to wait until we have an input_api object and use this
437 # roundabout construct to import checkdeps because this file is
438 # eval-ed and thus doesn't have __file__.
439 original_sys_path
= sys
.path
441 sys
.path
= sys
.path
+ [input_api
.os_path
.join(
442 input_api
.PresubmitLocalPath(), 'tools', 'checkdeps')]
444 from cpp_checker
import CppChecker
445 from rules
import Rule
447 # Restore sys.path to what it was before.
448 sys
.path
= original_sys_path
451 for f
in input_api
.AffectedFiles():
452 if not CppChecker
.IsCppFile(f
.LocalPath()):
455 changed_lines
= [line
for line_num
, line
in f
.ChangedContents()]
456 added_includes
.append([f
.LocalPath(), changed_lines
])
458 deps_checker
= checkdeps
.DepsChecker(input_api
.PresubmitLocalPath())
460 error_descriptions
= []
461 warning_descriptions
= []
462 for path
, rule_type
, rule_description
in deps_checker
.CheckAddedCppIncludes(
464 description_with_path
= '%s\n %s' % (path
, rule_description
)
465 if rule_type
== Rule
.DISALLOW
:
466 error_descriptions
.append(description_with_path
)
468 warning_descriptions
.append(description_with_path
)
471 if error_descriptions
:
472 results
.append(output_api
.PresubmitError(
473 'You added one or more #includes that violate checkdeps rules.',
475 if warning_descriptions
:
476 results
.append(output_api
.PresubmitPromptOrNotify(
477 'You added one or more #includes of files that are temporarily\n'
478 'allowed but being removed. Can you avoid introducing the\n'
479 '#include? See relevant DEPS file(s) for details and contacts.',
480 warning_descriptions
))
484 def _CheckFilePermissions(input_api
, output_api
):
485 """Check that all files have their permissions properly set."""
486 args
= [sys
.executable
, 'tools/checkperms/checkperms.py', '--root',
487 input_api
.change
.RepositoryRoot()]
488 for f
in input_api
.AffectedFiles():
489 args
+= ['--file', f
.LocalPath()]
491 (errors
, stderrdata
) = subprocess
.Popen(args
).communicate()
495 results
.append(output_api
.PresubmitError('checkperms.py failed.',
500 def _CheckNoAuraWindowPropertyHInHeaders(input_api
, output_api
):
501 """Makes sure we don't include ui/aura/window_property.h
504 pattern
= input_api
.re
.compile(r
'^#include\s*"ui/aura/window_property.h"')
506 for f
in input_api
.AffectedFiles():
507 if not f
.LocalPath().endswith('.h'):
509 for line_num
, line
in f
.ChangedContents():
510 if pattern
.match(line
):
511 errors
.append(' %s:%d' % (f
.LocalPath(), line_num
))
515 results
.append(output_api
.PresubmitError(
516 'Header files should not include ui/aura/window_property.h', errors
))
520 def _CheckIncludeOrderForScope(scope
, input_api
, file_path
, changed_linenums
):
521 """Checks that the lines in scope occur in the right order.
523 1. C system files in alphabetical order
524 2. C++ system files in alphabetical order
525 3. Project's .h files
528 c_system_include_pattern
= input_api
.re
.compile(r
'\s*#include <.*\.h>')
529 cpp_system_include_pattern
= input_api
.re
.compile(r
'\s*#include <.*>')
530 custom_include_pattern
= input_api
.re
.compile(r
'\s*#include ".*')
532 C_SYSTEM_INCLUDES
, CPP_SYSTEM_INCLUDES
, CUSTOM_INCLUDES
= range(3)
534 state
= C_SYSTEM_INCLUDES
537 previous_line_num
= 0
538 problem_linenums
= []
539 for line_num
, line
in scope
:
540 if c_system_include_pattern
.match(line
):
541 if state
!= C_SYSTEM_INCLUDES
:
542 problem_linenums
.append((line_num
, previous_line_num
))
543 elif previous_line
and previous_line
> line
:
544 problem_linenums
.append((line_num
, previous_line_num
))
545 elif cpp_system_include_pattern
.match(line
):
546 if state
== C_SYSTEM_INCLUDES
:
547 state
= CPP_SYSTEM_INCLUDES
548 elif state
== CUSTOM_INCLUDES
:
549 problem_linenums
.append((line_num
, previous_line_num
))
550 elif previous_line
and previous_line
> line
:
551 problem_linenums
.append((line_num
, previous_line_num
))
552 elif custom_include_pattern
.match(line
):
553 if state
!= CUSTOM_INCLUDES
:
554 state
= CUSTOM_INCLUDES
555 elif previous_line
and previous_line
> line
:
556 problem_linenums
.append((line_num
, previous_line_num
))
558 problem_linenums
.append(line_num
)
560 previous_line_num
= line_num
563 for (line_num
, previous_line_num
) in problem_linenums
:
564 if line_num
in changed_linenums
or previous_line_num
in changed_linenums
:
565 warnings
.append(' %s:%d' % (file_path
, line_num
))
569 def _CheckIncludeOrderInFile(input_api
, f
, changed_linenums
):
570 """Checks the #include order for the given file f."""
572 system_include_pattern
= input_api
.re
.compile(r
'\s*#include \<.*')
573 # Exclude #include <.../...> includes from the check; e.g., <sys/...> includes
574 # often need to appear in a specific order.
575 excluded_include_pattern
= input_api
.re
.compile(r
'\s*#include \<.*/.*')
576 custom_include_pattern
= input_api
.re
.compile(r
'\s*#include "(?P<FILE>.*)"')
577 if_pattern
= input_api
.re
.compile(
578 r
'\s*#\s*(if|elif|else|endif|define|undef).*')
579 # Some files need specialized order of includes; exclude such files from this
581 uncheckable_includes_pattern
= input_api
.re
.compile(
583 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
585 contents
= f
.NewContents()
589 # Handle the special first include. If the first include file is
590 # some/path/file.h, the corresponding including file can be some/path/file.cc,
591 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
592 # etc. It's also possible that no special first include exists.
593 for line
in contents
:
595 if system_include_pattern
.match(line
):
596 # No special first include -> process the line again along with normal
600 match
= custom_include_pattern
.match(line
)
602 match_dict
= match
.groupdict()
603 header_basename
= input_api
.os_path
.basename(
604 match_dict
['FILE']).replace('.h', '')
605 if header_basename
not in input_api
.os_path
.basename(f
.LocalPath()):
606 # No special first include -> process the line again along with normal
611 # Split into scopes: Each region between #if and #endif is its own scope.
614 for line
in contents
[line_num
:]:
616 if uncheckable_includes_pattern
.match(line
):
618 if if_pattern
.match(line
):
619 scopes
.append(current_scope
)
621 elif ((system_include_pattern
.match(line
) or
622 custom_include_pattern
.match(line
)) and
623 not excluded_include_pattern
.match(line
)):
624 current_scope
.append((line_num
, line
))
625 scopes
.append(current_scope
)
628 warnings
.extend(_CheckIncludeOrderForScope(scope
, input_api
, f
.LocalPath(),
633 def _CheckIncludeOrder(input_api
, output_api
):
634 """Checks that the #include order is correct.
636 1. The corresponding header for source files.
637 2. C system files in alphabetical order
638 3. C++ system files in alphabetical order
639 4. Project's .h files in alphabetical order
641 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
642 these rules separately.
646 for f
in input_api
.AffectedFiles():
647 if f
.LocalPath().endswith(('.cc', '.h')):
648 changed_linenums
= set(line_num
for line_num
, _
in f
.ChangedContents())
649 warnings
.extend(_CheckIncludeOrderInFile(input_api
, f
, changed_linenums
))
653 results
.append(output_api
.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING
,
658 def _CheckForVersionControlConflictsInFile(input_api
, f
):
659 pattern
= input_api
.re
.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
661 for line_num
, line
in f
.ChangedContents():
662 if pattern
.match(line
):
663 errors
.append(' %s:%d %s' % (f
.LocalPath(), line_num
, line
))
667 def _CheckForVersionControlConflicts(input_api
, output_api
):
668 """Usually this is not intentional and will cause a compile failure."""
670 for f
in input_api
.AffectedFiles():
671 errors
.extend(_CheckForVersionControlConflictsInFile(input_api
, f
))
675 results
.append(output_api
.PresubmitError(
676 'Version control conflict markers found, please resolve.', errors
))
680 def _CheckHardcodedGoogleHostsInLowerLayers(input_api
, output_api
):
681 def FilterFile(affected_file
):
682 """Filter function for use with input_api.AffectedSourceFiles,
683 below. This filters out everything except non-test files from
684 top-level directories that generally speaking should not hard-code
685 service URLs (e.g. src/android_webview/, src/content/ and others).
687 return input_api
.FilterSourceFile(
689 white_list
=(r
'^(android_webview|base|content|net)[\\\/].*', ),
690 black_list
=(_EXCLUDED_PATHS
+
691 _TEST_CODE_EXCLUDED_PATHS
+
692 input_api
.DEFAULT_BLACK_LIST
))
694 base_pattern
= '"[^"]*google\.com[^"]*"'
695 comment_pattern
= input_api
.re
.compile('//.*%s' % base_pattern
)
696 pattern
= input_api
.re
.compile(base_pattern
)
697 problems
= [] # items are (filename, line_number, line)
698 for f
in input_api
.AffectedSourceFiles(FilterFile
):
699 for line_num
, line
in f
.ChangedContents():
700 if not comment_pattern
.search(line
) and pattern
.search(line
):
701 problems
.append((f
.LocalPath(), line_num
, line
))
704 return [output_api
.PresubmitPromptOrNotify(
705 'Most layers below src/chrome/ should not hardcode service URLs.\n'
706 'Are you sure this is correct? (Contact: joi@chromium.org)',
708 problem
[0], problem
[1], problem
[2]) for problem
in problems
])]
713 def _CheckNoAbbreviationInPngFileName(input_api
, output_api
):
714 """Makes sure there are no abbreviations in the name of PNG files.
716 pattern
= input_api
.re
.compile(r
'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
718 for f
in input_api
.AffectedFiles(include_deletes
=False):
719 if pattern
.match(f
.LocalPath()):
720 errors
.append(' %s' % f
.LocalPath())
724 results
.append(output_api
.PresubmitError(
725 'The name of PNG files should not have abbreviations. \n'
726 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
727 'Contact oshima@chromium.org if you have questions.', errors
))
731 def _DepsFilesToCheck(re
, changed_lines
):
732 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
733 a set of DEPS entries that we should look up."""
736 # This pattern grabs the path without basename in the first
737 # parentheses, and the basename (if present) in the second. It
738 # relies on the simple heuristic that if there is a basename it will
739 # be a header file ending in ".h".
740 pattern
= re
.compile(
741 r
"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
742 for changed_line
in changed_lines
:
743 m
= pattern
.match(changed_line
)
746 if not (path
.startswith('grit/') or path
== 'grit'):
747 results
.add('%s/DEPS' % m
.group(1))
751 def _CheckAddedDepsHaveTargetApprovals(input_api
, output_api
):
752 """When a dependency prefixed with + is added to a DEPS file, we
753 want to make sure that the change is reviewed by an OWNER of the
754 target file or directory, to avoid layering violations from being
755 introduced. This check verifies that this happens.
757 changed_lines
= set()
758 for f
in input_api
.AffectedFiles():
759 filename
= input_api
.os_path
.basename(f
.LocalPath())
760 if filename
== 'DEPS':
761 changed_lines |
= set(line
.strip()
763 in f
.ChangedContents())
764 if not changed_lines
:
767 virtual_depended_on_files
= _DepsFilesToCheck(input_api
.re
, changed_lines
)
768 if not virtual_depended_on_files
:
771 if input_api
.is_committing
:
773 return [output_api
.PresubmitNotifyResult(
774 '--tbr was specified, skipping OWNERS check for DEPS additions')]
775 if not input_api
.change
.issue
:
776 return [output_api
.PresubmitError(
777 "DEPS approval by OWNERS check failed: this change has "
778 "no Rietveld issue number, so we can't check it for approvals.")]
779 output
= output_api
.PresubmitError
781 output
= output_api
.PresubmitNotifyResult
783 owners_db
= input_api
.owners_db
784 owner_email
, reviewers
= input_api
.canned_checks
._RietveldOwnerAndReviewers
(
786 owners_db
.email_regexp
,
787 approval_needed
=input_api
.is_committing
)
789 owner_email
= owner_email
or input_api
.change
.author_email
791 reviewers_plus_owner
= set(reviewers
)
793 reviewers_plus_owner
.add(owner_email
)
794 missing_files
= owners_db
.files_not_covered_by(virtual_depended_on_files
,
795 reviewers_plus_owner
)
796 unapproved_dependencies
= ["'+%s'," % path
[:-len('/DEPS')]
797 for path
in missing_files
]
799 if unapproved_dependencies
:
801 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
802 '\n '.join(sorted(unapproved_dependencies
)))]
803 if not input_api
.is_committing
:
804 suggested_owners
= owners_db
.reviewers_for(missing_files
, owner_email
)
805 output_list
.append(output(
806 'Suggested missing target path OWNERS:\n %s' %
807 '\n '.join(suggested_owners
or [])))
813 def _CommonChecks(input_api
, output_api
):
814 """Checks common to both upload and commit."""
816 results
.extend(input_api
.canned_checks
.PanProjectChecks(
817 input_api
, output_api
, excluded_paths
=_EXCLUDED_PATHS
))
818 results
.extend(_CheckAuthorizedAuthor(input_api
, output_api
))
820 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
))
821 results
.extend(_CheckNoIOStreamInHeaders(input_api
, output_api
))
822 results
.extend(_CheckNoUNIT_TESTInSourceFiles(input_api
, output_api
))
823 results
.extend(_CheckNoNewWStrings(input_api
, output_api
))
824 results
.extend(_CheckNoDEPSGIT(input_api
, output_api
))
825 results
.extend(_CheckNoBannedFunctions(input_api
, output_api
))
826 results
.extend(_CheckNoPragmaOnce(input_api
, output_api
))
827 results
.extend(_CheckNoTrinaryTrueFalse(input_api
, output_api
))
828 results
.extend(_CheckUnwantedDependencies(input_api
, output_api
))
829 results
.extend(_CheckFilePermissions(input_api
, output_api
))
830 results
.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api
, output_api
))
831 results
.extend(_CheckIncludeOrder(input_api
, output_api
))
832 results
.extend(_CheckForVersionControlConflicts(input_api
, output_api
))
833 results
.extend(_CheckPatchFiles(input_api
, output_api
))
834 results
.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api
, output_api
))
835 results
.extend(_CheckNoAbbreviationInPngFileName(input_api
, output_api
))
836 results
.extend(_CheckForInvalidOSMacros(input_api
, output_api
))
837 results
.extend(_CheckAddedDepsHaveTargetApprovals(input_api
, output_api
))
839 input_api
.canned_checks
.CheckChangeHasNoTabs(
842 source_file_filter
=lambda x
: x
.LocalPath().endswith('.grd')))
844 if any('PRESUBMIT.py' == f
.LocalPath() for f
in input_api
.AffectedFiles()):
845 results
.extend(input_api
.canned_checks
.RunUnitTestsInDirectory(
846 input_api
, output_api
,
847 input_api
.PresubmitLocalPath(),
848 whitelist
=[r
'^PRESUBMIT_test\.py$']))
852 def _CheckSubversionConfig(input_api
, output_api
):
853 """Verifies the subversion config file is correctly setup.
855 Checks that autoprops are enabled, returns an error otherwise.
857 join
= input_api
.os_path
.join
858 if input_api
.platform
== 'win32':
859 appdata
= input_api
.environ
.get('APPDATA', '')
861 return [output_api
.PresubmitError('%APPDATA% is not configured.')]
862 path
= join(appdata
, 'Subversion', 'config')
864 home
= input_api
.environ
.get('HOME', '')
866 return [output_api
.PresubmitError('$HOME is not configured.')]
867 path
= join(home
, '.subversion', 'config')
870 'Please look at http://dev.chromium.org/developers/coding-style to\n'
871 'configure your subversion configuration file. This enables automatic\n'
872 'properties to simplify the project maintenance.\n'
873 'Pro-tip: just download and install\n'
874 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
877 lines
= open(path
, 'r').read().splitlines()
878 # Make sure auto-props is enabled and check for 2 Chromium standard
880 if (not '*.cc = svn:eol-style=LF' in lines
or
881 not '*.pdf = svn:mime-type=application/pdf' in lines
or
882 not 'enable-auto-props = yes' in lines
):
884 output_api
.PresubmitNotifyResult(
885 'It looks like you have not configured your subversion config '
886 'file or it is not up-to-date.\n' + error_msg
)
888 except (OSError, IOError):
890 output_api
.PresubmitNotifyResult(
891 'Can\'t find your subversion config file.\n' + error_msg
)
896 def _CheckAuthorizedAuthor(input_api
, output_api
):
897 """For non-googler/chromites committers, verify the author's email address is
900 # TODO(maruel): Add it to input_api?
903 author
= input_api
.change
.author_email
905 input_api
.logging
.info('No author, skipping AUTHOR check')
907 authors_path
= input_api
.os_path
.join(
908 input_api
.PresubmitLocalPath(), 'AUTHORS')
910 input_api
.re
.match(r
'[^#]+\s+\<(.+?)\>\s*$', line
)
911 for line
in open(authors_path
))
912 valid_authors
= [item
.group(1).lower() for item
in valid_authors
if item
]
913 if not any(fnmatch
.fnmatch(author
.lower(), valid
) for valid
in valid_authors
):
914 input_api
.logging
.info('Valid authors are %s', ', '.join(valid_authors
))
915 return [output_api
.PresubmitPromptWarning(
916 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
918 'http://www.chromium.org/developers/contributing-code and read the '
920 'If you are a chromite, verify the contributor signed the CLA.') %
925 def _CheckPatchFiles(input_api
, output_api
):
926 problems
= [f
.LocalPath() for f
in input_api
.AffectedFiles()
927 if f
.LocalPath().endswith(('.orig', '.rej'))]
929 return [output_api
.PresubmitError(
930 "Don't commit .rej and .orig files.", problems
)]
935 def _DidYouMeanOSMacro(bad_macro
):
937 return {'A': 'OS_ANDROID',
947 'W': 'OS_WIN'}[bad_macro
[3].upper()]
952 def _CheckForInvalidOSMacrosInFile(input_api
, f
):
953 """Check for sensible looking, totally invalid OS macros."""
954 preprocessor_statement
= input_api
.re
.compile(r
'^\s*#')
955 os_macro
= input_api
.re
.compile(r
'defined\((OS_[^)]+)\)')
957 for lnum
, line
in f
.ChangedContents():
958 if preprocessor_statement
.search(line
):
959 for match
in os_macro
.finditer(line
):
960 if not match
.group(1) in _VALID_OS_MACROS
:
961 good
= _DidYouMeanOSMacro(match
.group(1))
962 did_you_mean
= ' (did you mean %s?)' % good
if good
else ''
963 results
.append(' %s:%d %s%s' % (f
.LocalPath(),
970 def _CheckForInvalidOSMacros(input_api
, output_api
):
971 """Check all affected files for invalid OS macros."""
973 for f
in input_api
.AffectedFiles():
974 if not f
.LocalPath().endswith(('.py', '.js', '.html', '.css')):
975 bad_macros
.extend(_CheckForInvalidOSMacrosInFile(input_api
, f
))
980 return [output_api
.PresubmitError(
981 'Possibly invalid OS macro[s] found. Please fix your code\n'
982 'or add your macro to src/PRESUBMIT.py.', bad_macros
)]
985 def CheckChangeOnUpload(input_api
, output_api
):
987 results
.extend(_CommonChecks(input_api
, output_api
))
991 def CheckChangeOnCommit(input_api
, output_api
):
993 results
.extend(_CommonChecks(input_api
, output_api
))
994 # TODO(thestig) temporarily disabled, doesn't work in third_party/
995 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
996 # input_api, output_api, sources))
997 # Make sure the tree is 'open'.
998 results
.extend(input_api
.canned_checks
.CheckTreeIsOpen(
1001 json_url
='http://chromium-status.appspot.com/current?format=json'))
1002 results
.extend(input_api
.canned_checks
.CheckRietveldTryJobExecution(input_api
,
1003 output_api
, 'http://codereview.chromium.org',
1004 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
1005 'tryserver@chromium.org'))
1007 results
.extend(input_api
.canned_checks
.CheckChangeHasBugField(
1008 input_api
, output_api
))
1009 results
.extend(input_api
.canned_checks
.CheckChangeHasDescription(
1010 input_api
, output_api
))
1011 results
.extend(_CheckSubversionConfig(input_api
, output_api
))
1015 def GetPreferredTrySlaves(project
, change
):
1016 files
= change
.LocalPaths()
1018 if not files
or all(re
.search(r
'[\\/]OWNERS$', f
) for f
in files
):
1021 if all(re
.search('\.(m|mm)$|(^|[/_])mac[/_.]', f
) for f
in files
):
1022 return ['mac_rel', 'mac:compile']
1023 if all(re
.search('(^|[/_])win[/_.]', f
) for f
in files
):
1024 return ['win_rel', 'win7_aura', 'win:compile']
1025 if all(re
.search('(^|[/_])android[/_.]', f
) for f
in files
):
1026 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
1027 if all(re
.search('^native_client_sdk', f
) for f
in files
):
1028 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
1029 if all(re
.search('[/_]ios[/_.]', f
) for f
in files
):
1030 return ['ios_rel_device', 'ios_dbg_simulator']
1033 'android_clang_dbg',
1035 'ios_dbg_simulator',
1040 'linux_clang:compile',
1047 'win_x64_rel:compile',
1050 # Match things like path/aura/file.cc and path/file_aura.cc.
1051 # Same for chromeos.
1052 if any(re
.search('[/_](aura|chromeos)', f
) for f
in files
):
1053 trybots
+= ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
1055 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1056 # unless they're .gyp(i) files as changes to those files can break the gyp
1058 if (not all(re
.search('^chrome', f
) for f
in files
) or
1059 any(re
.search('\.gypi?$', f
) for f
in files
)):
1060 trybots
+= ['android_aosp']