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 # Match the final or penultimate token if it is xxxtest so we can ignore it
582 # when considering the special first include.
583 test_file_tag_pattern
= input_api
.re
.compile(
584 r
'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
585 if_pattern
= input_api
.re
.compile(
586 r
'\s*#\s*(if|elif|else|endif|define|undef).*')
587 # Some files need specialized order of includes; exclude such files from this
589 uncheckable_includes_pattern
= input_api
.re
.compile(
591 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
593 contents
= f
.NewContents()
597 # Handle the special first include. If the first include file is
598 # some/path/file.h, the corresponding including file can be some/path/file.cc,
599 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
600 # etc. It's also possible that no special first include exists.
601 # If the included file is some/path/file_platform.h the including file could
602 # also be some/path/file_xxxtest_platform.h.
603 including_file_base_name
= test_file_tag_pattern
.sub(
604 '', input_api
.os_path
.basename(f
.LocalPath()))
606 for line
in contents
:
608 if system_include_pattern
.match(line
):
609 # No special first include -> process the line again along with normal
613 match
= custom_include_pattern
.match(line
)
615 match_dict
= match
.groupdict()
616 header_basename
= test_file_tag_pattern
.sub(
617 '', input_api
.os_path
.basename(match_dict
['FILE'])).replace('.h', '')
619 if header_basename
not in including_file_base_name
:
620 # No special first include -> process the line again along with normal
625 # Split into scopes: Each region between #if and #endif is its own scope.
628 for line
in contents
[line_num
:]:
630 if uncheckable_includes_pattern
.match(line
):
632 if if_pattern
.match(line
):
633 scopes
.append(current_scope
)
635 elif ((system_include_pattern
.match(line
) or
636 custom_include_pattern
.match(line
)) and
637 not excluded_include_pattern
.match(line
)):
638 current_scope
.append((line_num
, line
))
639 scopes
.append(current_scope
)
642 warnings
.extend(_CheckIncludeOrderForScope(scope
, input_api
, f
.LocalPath(),
647 def _CheckIncludeOrder(input_api
, output_api
):
648 """Checks that the #include order is correct.
650 1. The corresponding header for source files.
651 2. C system files in alphabetical order
652 3. C++ system files in alphabetical order
653 4. Project's .h files in alphabetical order
655 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
656 these rules separately.
660 for f
in input_api
.AffectedFiles():
661 if f
.LocalPath().endswith(('.cc', '.h')):
662 changed_linenums
= set(line_num
for line_num
, _
in f
.ChangedContents())
663 warnings
.extend(_CheckIncludeOrderInFile(input_api
, f
, changed_linenums
))
667 results
.append(output_api
.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING
,
672 def _CheckForVersionControlConflictsInFile(input_api
, f
):
673 pattern
= input_api
.re
.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
675 for line_num
, line
in f
.ChangedContents():
676 if pattern
.match(line
):
677 errors
.append(' %s:%d %s' % (f
.LocalPath(), line_num
, line
))
681 def _CheckForVersionControlConflicts(input_api
, output_api
):
682 """Usually this is not intentional and will cause a compile failure."""
684 for f
in input_api
.AffectedFiles():
685 errors
.extend(_CheckForVersionControlConflictsInFile(input_api
, f
))
689 results
.append(output_api
.PresubmitError(
690 'Version control conflict markers found, please resolve.', errors
))
694 def _CheckHardcodedGoogleHostsInLowerLayers(input_api
, output_api
):
695 def FilterFile(affected_file
):
696 """Filter function for use with input_api.AffectedSourceFiles,
697 below. This filters out everything except non-test files from
698 top-level directories that generally speaking should not hard-code
699 service URLs (e.g. src/android_webview/, src/content/ and others).
701 return input_api
.FilterSourceFile(
703 white_list
=(r
'^(android_webview|base|content|net)[\\\/].*', ),
704 black_list
=(_EXCLUDED_PATHS
+
705 _TEST_CODE_EXCLUDED_PATHS
+
706 input_api
.DEFAULT_BLACK_LIST
))
708 base_pattern
= '"[^"]*google\.com[^"]*"'
709 comment_pattern
= input_api
.re
.compile('//.*%s' % base_pattern
)
710 pattern
= input_api
.re
.compile(base_pattern
)
711 problems
= [] # items are (filename, line_number, line)
712 for f
in input_api
.AffectedSourceFiles(FilterFile
):
713 for line_num
, line
in f
.ChangedContents():
714 if not comment_pattern
.search(line
) and pattern
.search(line
):
715 problems
.append((f
.LocalPath(), line_num
, line
))
718 return [output_api
.PresubmitPromptOrNotify(
719 'Most layers below src/chrome/ should not hardcode service URLs.\n'
720 'Are you sure this is correct? (Contact: joi@chromium.org)',
722 problem
[0], problem
[1], problem
[2]) for problem
in problems
])]
727 def _CheckNoAbbreviationInPngFileName(input_api
, output_api
):
728 """Makes sure there are no abbreviations in the name of PNG files.
730 pattern
= input_api
.re
.compile(r
'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
732 for f
in input_api
.AffectedFiles(include_deletes
=False):
733 if pattern
.match(f
.LocalPath()):
734 errors
.append(' %s' % f
.LocalPath())
738 results
.append(output_api
.PresubmitError(
739 'The name of PNG files should not have abbreviations. \n'
740 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
741 'Contact oshima@chromium.org if you have questions.', errors
))
745 def _DepsFilesToCheck(re
, changed_lines
):
746 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
747 a set of DEPS entries that we should look up."""
750 # This pattern grabs the path without basename in the first
751 # parentheses, and the basename (if present) in the second. It
752 # relies on the simple heuristic that if there is a basename it will
753 # be a header file ending in ".h".
754 pattern
= re
.compile(
755 r
"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
756 for changed_line
in changed_lines
:
757 m
= pattern
.match(changed_line
)
760 if not (path
.startswith('grit/') or path
== 'grit'):
761 results
.add('%s/DEPS' % m
.group(1))
765 def _CheckAddedDepsHaveTargetApprovals(input_api
, output_api
):
766 """When a dependency prefixed with + is added to a DEPS file, we
767 want to make sure that the change is reviewed by an OWNER of the
768 target file or directory, to avoid layering violations from being
769 introduced. This check verifies that this happens.
771 changed_lines
= set()
772 for f
in input_api
.AffectedFiles():
773 filename
= input_api
.os_path
.basename(f
.LocalPath())
774 if filename
== 'DEPS':
775 changed_lines |
= set(line
.strip()
777 in f
.ChangedContents())
778 if not changed_lines
:
781 virtual_depended_on_files
= _DepsFilesToCheck(input_api
.re
, changed_lines
)
782 if not virtual_depended_on_files
:
785 if input_api
.is_committing
:
787 return [output_api
.PresubmitNotifyResult(
788 '--tbr was specified, skipping OWNERS check for DEPS additions')]
789 if not input_api
.change
.issue
:
790 return [output_api
.PresubmitError(
791 "DEPS approval by OWNERS check failed: this change has "
792 "no Rietveld issue number, so we can't check it for approvals.")]
793 output
= output_api
.PresubmitError
795 output
= output_api
.PresubmitNotifyResult
797 owners_db
= input_api
.owners_db
798 owner_email
, reviewers
= input_api
.canned_checks
._RietveldOwnerAndReviewers
(
800 owners_db
.email_regexp
,
801 approval_needed
=input_api
.is_committing
)
803 owner_email
= owner_email
or input_api
.change
.author_email
805 reviewers_plus_owner
= set(reviewers
)
807 reviewers_plus_owner
.add(owner_email
)
808 missing_files
= owners_db
.files_not_covered_by(virtual_depended_on_files
,
809 reviewers_plus_owner
)
810 unapproved_dependencies
= ["'+%s'," % path
[:-len('/DEPS')]
811 for path
in missing_files
]
813 if unapproved_dependencies
:
815 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
816 '\n '.join(sorted(unapproved_dependencies
)))]
817 if not input_api
.is_committing
:
818 suggested_owners
= owners_db
.reviewers_for(missing_files
, owner_email
)
819 output_list
.append(output(
820 'Suggested missing target path OWNERS:\n %s' %
821 '\n '.join(suggested_owners
or [])))
827 def _CommonChecks(input_api
, output_api
):
828 """Checks common to both upload and commit."""
830 results
.extend(input_api
.canned_checks
.PanProjectChecks(
831 input_api
, output_api
, excluded_paths
=_EXCLUDED_PATHS
))
832 results
.extend(_CheckAuthorizedAuthor(input_api
, output_api
))
834 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
))
835 results
.extend(_CheckNoIOStreamInHeaders(input_api
, output_api
))
836 results
.extend(_CheckNoUNIT_TESTInSourceFiles(input_api
, output_api
))
837 results
.extend(_CheckNoNewWStrings(input_api
, output_api
))
838 results
.extend(_CheckNoDEPSGIT(input_api
, output_api
))
839 results
.extend(_CheckNoBannedFunctions(input_api
, output_api
))
840 results
.extend(_CheckNoPragmaOnce(input_api
, output_api
))
841 results
.extend(_CheckNoTrinaryTrueFalse(input_api
, output_api
))
842 results
.extend(_CheckUnwantedDependencies(input_api
, output_api
))
843 results
.extend(_CheckFilePermissions(input_api
, output_api
))
844 results
.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api
, output_api
))
845 results
.extend(_CheckIncludeOrder(input_api
, output_api
))
846 results
.extend(_CheckForVersionControlConflicts(input_api
, output_api
))
847 results
.extend(_CheckPatchFiles(input_api
, output_api
))
848 results
.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api
, output_api
))
849 results
.extend(_CheckNoAbbreviationInPngFileName(input_api
, output_api
))
850 results
.extend(_CheckForInvalidOSMacros(input_api
, output_api
))
851 results
.extend(_CheckAddedDepsHaveTargetApprovals(input_api
, output_api
))
853 input_api
.canned_checks
.CheckChangeHasNoTabs(
856 source_file_filter
=lambda x
: x
.LocalPath().endswith('.grd')))
858 if any('PRESUBMIT.py' == f
.LocalPath() for f
in input_api
.AffectedFiles()):
859 results
.extend(input_api
.canned_checks
.RunUnitTestsInDirectory(
860 input_api
, output_api
,
861 input_api
.PresubmitLocalPath(),
862 whitelist
=[r
'^PRESUBMIT_test\.py$']))
866 def _CheckSubversionConfig(input_api
, output_api
):
867 """Verifies the subversion config file is correctly setup.
869 Checks that autoprops are enabled, returns an error otherwise.
871 join
= input_api
.os_path
.join
872 if input_api
.platform
== 'win32':
873 appdata
= input_api
.environ
.get('APPDATA', '')
875 return [output_api
.PresubmitError('%APPDATA% is not configured.')]
876 path
= join(appdata
, 'Subversion', 'config')
878 home
= input_api
.environ
.get('HOME', '')
880 return [output_api
.PresubmitError('$HOME is not configured.')]
881 path
= join(home
, '.subversion', 'config')
884 'Please look at http://dev.chromium.org/developers/coding-style to\n'
885 'configure your subversion configuration file. This enables automatic\n'
886 'properties to simplify the project maintenance.\n'
887 'Pro-tip: just download and install\n'
888 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
891 lines
= open(path
, 'r').read().splitlines()
892 # Make sure auto-props is enabled and check for 2 Chromium standard
894 if (not '*.cc = svn:eol-style=LF' in lines
or
895 not '*.pdf = svn:mime-type=application/pdf' in lines
or
896 not 'enable-auto-props = yes' in lines
):
898 output_api
.PresubmitNotifyResult(
899 'It looks like you have not configured your subversion config '
900 'file or it is not up-to-date.\n' + error_msg
)
902 except (OSError, IOError):
904 output_api
.PresubmitNotifyResult(
905 'Can\'t find your subversion config file.\n' + error_msg
)
910 def _CheckAuthorizedAuthor(input_api
, output_api
):
911 """For non-googler/chromites committers, verify the author's email address is
914 # TODO(maruel): Add it to input_api?
917 author
= input_api
.change
.author_email
919 input_api
.logging
.info('No author, skipping AUTHOR check')
921 authors_path
= input_api
.os_path
.join(
922 input_api
.PresubmitLocalPath(), 'AUTHORS')
924 input_api
.re
.match(r
'[^#]+\s+\<(.+?)\>\s*$', line
)
925 for line
in open(authors_path
))
926 valid_authors
= [item
.group(1).lower() for item
in valid_authors
if item
]
927 if not any(fnmatch
.fnmatch(author
.lower(), valid
) for valid
in valid_authors
):
928 input_api
.logging
.info('Valid authors are %s', ', '.join(valid_authors
))
929 return [output_api
.PresubmitPromptWarning(
930 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
932 'http://www.chromium.org/developers/contributing-code and read the '
934 'If you are a chromite, verify the contributor signed the CLA.') %
939 def _CheckPatchFiles(input_api
, output_api
):
940 problems
= [f
.LocalPath() for f
in input_api
.AffectedFiles()
941 if f
.LocalPath().endswith(('.orig', '.rej'))]
943 return [output_api
.PresubmitError(
944 "Don't commit .rej and .orig files.", problems
)]
949 def _DidYouMeanOSMacro(bad_macro
):
951 return {'A': 'OS_ANDROID',
961 'W': 'OS_WIN'}[bad_macro
[3].upper()]
966 def _CheckForInvalidOSMacrosInFile(input_api
, f
):
967 """Check for sensible looking, totally invalid OS macros."""
968 preprocessor_statement
= input_api
.re
.compile(r
'^\s*#')
969 os_macro
= input_api
.re
.compile(r
'defined\((OS_[^)]+)\)')
971 for lnum
, line
in f
.ChangedContents():
972 if preprocessor_statement
.search(line
):
973 for match
in os_macro
.finditer(line
):
974 if not match
.group(1) in _VALID_OS_MACROS
:
975 good
= _DidYouMeanOSMacro(match
.group(1))
976 did_you_mean
= ' (did you mean %s?)' % good
if good
else ''
977 results
.append(' %s:%d %s%s' % (f
.LocalPath(),
984 def _CheckForInvalidOSMacros(input_api
, output_api
):
985 """Check all affected files for invalid OS macros."""
987 for f
in input_api
.AffectedFiles():
988 if not f
.LocalPath().endswith(('.py', '.js', '.html', '.css')):
989 bad_macros
.extend(_CheckForInvalidOSMacrosInFile(input_api
, f
))
994 return [output_api
.PresubmitError(
995 'Possibly invalid OS macro[s] found. Please fix your code\n'
996 'or add your macro to src/PRESUBMIT.py.', bad_macros
)]
999 def CheckChangeOnUpload(input_api
, output_api
):
1001 results
.extend(_CommonChecks(input_api
, output_api
))
1005 def CheckChangeOnCommit(input_api
, output_api
):
1007 results
.extend(_CommonChecks(input_api
, output_api
))
1008 # TODO(thestig) temporarily disabled, doesn't work in third_party/
1009 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1010 # input_api, output_api, sources))
1011 # Make sure the tree is 'open'.
1012 results
.extend(input_api
.canned_checks
.CheckTreeIsOpen(
1015 json_url
='http://chromium-status.appspot.com/current?format=json'))
1016 results
.extend(input_api
.canned_checks
.CheckRietveldTryJobExecution(input_api
,
1017 output_api
, 'http://codereview.chromium.org',
1018 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
1019 'tryserver@chromium.org'))
1021 results
.extend(input_api
.canned_checks
.CheckChangeHasBugField(
1022 input_api
, output_api
))
1023 results
.extend(input_api
.canned_checks
.CheckChangeHasDescription(
1024 input_api
, output_api
))
1025 results
.extend(_CheckSubversionConfig(input_api
, output_api
))
1029 def GetPreferredTrySlaves(project
, change
):
1030 files
= change
.LocalPaths()
1032 if not files
or all(re
.search(r
'[\\/]OWNERS$', f
) for f
in files
):
1035 if all(re
.search('\.(m|mm)$|(^|[/_])mac[/_.]', f
) for f
in files
):
1036 return ['mac_rel', 'mac:compile']
1037 if all(re
.search('(^|[/_])win[/_.]', f
) for f
in files
):
1038 return ['win_rel', 'win7_aura', 'win:compile']
1039 if all(re
.search('(^|[/_])android[/_.]', f
) for f
in files
):
1040 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
1041 if all(re
.search('^native_client_sdk', f
) for f
in files
):
1042 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
1043 if all(re
.search('[/_]ios[/_.]', f
) for f
in files
):
1044 return ['ios_rel_device', 'ios_dbg_simulator']
1047 'android_clang_dbg',
1049 'ios_dbg_simulator',
1054 'linux_clang:compile',
1061 'win_x64_rel:compile',
1064 # Match things like path/aura/file.cc and path/file_aura.cc.
1065 # Same for chromeos.
1066 if any(re
.search('[/_](aura|chromeos)', f
) for f
in files
):
1067 trybots
+= ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
1069 # If there are gyp changes to base, build, or chromeos, run a full cros build
1070 # in addition to the shorter linux_chromeos build. Changes to high level gyp
1071 # files have a much higher chance of breaking the cros build, which is
1072 # differnt from the linux_chromeos build that most chrome developers test
1074 if any(re
.search('^(base|build|chromeos).*\.gypi?$', f
) for f
in files
):
1075 trybots
+= ['cros_x86']
1077 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1078 # unless they're .gyp(i) files as changes to those files can break the gyp
1080 if (not all(re
.search('^chrome', f
) for f
in files
) or
1081 any(re
.search('\.gypi?$', f
) for f
in files
)):
1082 trybots
+= ['android_aosp']