1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Top-level presubmit script for Chromium.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into gcl.
19 r
"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py",
20 r
"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py",
21 r
"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk",
22 r
"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
27 r
".+[\\\/]pnacl_shim\.c$",
30 # Fragment of a regular expression that matches C++ and Objective-C++
31 # implementation files.
32 _IMPLEMENTATION_EXTENSIONS
= r
'\.(cc|cpp|cxx|mm)$'
34 # Regular expression that matches code only used for test binaries
36 _TEST_CODE_EXCLUDED_PATHS
= (
37 r
'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS
,
38 r
'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS
,
39 r
'.+_(api|browser|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
40 _IMPLEMENTATION_EXTENSIONS
,
41 r
'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS
,
42 r
'.*[/\\](test|tool(s)?)[/\\].*',
43 # At request of folks maintaining this folder.
44 r
'chrome[/\\]browser[/\\]automation[/\\].*',
47 _TEST_ONLY_WARNING
= (
48 'You might be calling functions intended only for testing from\n'
49 'production code. It is OK to ignore this warning if you know what\n'
50 'you are doing, as the heuristics used to detect the situation are\n'
51 'not perfect. The commit queue will not block on this warning.\n'
52 'Email joi@chromium.org if you have questions.')
55 _INCLUDE_ORDER_WARNING
= (
56 'Your #include order seems to be broken. Send mail to\n'
57 'marja@chromium.org if this is not the case.')
60 _BANNED_OBJC_FUNCTIONS
= (
64 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
65 'prohibited. Please use CrTrackingArea instead.',
66 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
73 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
75 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
80 'convertPointFromBase:',
82 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
83 'Please use |convertPoint:(point) fromView:nil| instead.',
84 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
89 'convertPointToBase:',
91 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
92 'Please use |convertPoint:(point) toView:nil| instead.',
93 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
98 'convertRectFromBase:',
100 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
101 'Please use |convertRect:(point) fromView:nil| instead.',
102 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
107 'convertRectToBase:',
109 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
110 'Please use |convertRect:(point) toView:nil| instead.',
111 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
116 'convertSizeFromBase:',
118 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
119 'Please use |convertSize:(point) fromView:nil| instead.',
120 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
125 'convertSizeToBase:',
127 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
128 'Please use |convertSize:(point) toView:nil| instead.',
129 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
136 _BANNED_CPP_FUNCTIONS
= (
137 # Make sure that gtest's FRIEND_TEST() macro is not used; the
138 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
139 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
143 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
144 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
152 'New code should not use ScopedAllowIO. Post a task to the blocking',
153 'pool or the FILE thread instead.',
157 r
"^content[\\\/]shell[\\\/]shell_browser_main\.cc$",
158 r
"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
165 # Please keep sorted.
168 'OS_CAT', # For testing.
182 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
):
183 """Attempts to prevent use of functions intended only for testing in
184 non-testing code. For now this is just a best-effort implementation
185 that ignores header files and may have some false positives. A
186 better implementation would probably need a proper C++ parser.
188 # We only scan .cc files and the like, as the declaration of
189 # for-testing functions in header files are hard to distinguish from
190 # calls to such functions without a proper C++ parser.
191 file_inclusion_pattern
= r
'.+%s' % _IMPLEMENTATION_EXTENSIONS
193 base_function_pattern
= r
'ForTest(ing)?|for_test(ing)?'
194 inclusion_pattern
= input_api
.re
.compile(r
'(%s)\s*\(' % base_function_pattern
)
195 exclusion_pattern
= input_api
.re
.compile(
196 r
'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
197 base_function_pattern
, base_function_pattern
))
199 def FilterFile(affected_file
):
200 black_list
= (_EXCLUDED_PATHS
+
201 _TEST_CODE_EXCLUDED_PATHS
+
202 input_api
.DEFAULT_BLACK_LIST
)
203 return input_api
.FilterSourceFile(
205 white_list
=(file_inclusion_pattern
, ),
206 black_list
=black_list
)
209 for f
in input_api
.AffectedSourceFiles(FilterFile
):
210 local_path
= f
.LocalPath()
211 lines
= input_api
.ReadFile(f
).splitlines()
214 if (inclusion_pattern
.search(line
) and
215 not exclusion_pattern
.search(line
)):
217 '%s:%d\n %s' % (local_path
, line_number
, line
.strip()))
221 return [output_api
.PresubmitPromptOrNotify(_TEST_ONLY_WARNING
, problems
)]
226 def _CheckNoIOStreamInHeaders(input_api
, output_api
):
227 """Checks to make sure no .h files include <iostream>."""
229 pattern
= input_api
.re
.compile(r
'^#include\s*<iostream>',
230 input_api
.re
.MULTILINE
)
231 for f
in input_api
.AffectedSourceFiles(input_api
.FilterSourceFile
):
232 if not f
.LocalPath().endswith('.h'):
234 contents
= input_api
.ReadFile(f
)
235 if pattern
.search(contents
):
239 return [ output_api
.PresubmitError(
240 'Do not #include <iostream> in header files, since it inserts static '
241 'initialization into every file including the header. Instead, '
242 '#include <ostream>. See http://crbug.com/94794',
247 def _CheckNoUNIT_TESTInSourceFiles(input_api
, output_api
):
248 """Checks to make sure no source files use UNIT_TEST"""
250 for f
in input_api
.AffectedFiles():
251 if (not f
.LocalPath().endswith(('.cc', '.mm'))):
254 for line_num
, line
in f
.ChangedContents():
255 if 'UNIT_TEST' in line
:
256 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
260 return [output_api
.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
261 '\n'.join(problems
))]
264 def _CheckNoNewWStrings(input_api
, output_api
):
265 """Checks to make sure we don't introduce use of wstrings."""
267 for f
in input_api
.AffectedFiles():
268 if (not f
.LocalPath().endswith(('.cc', '.h')) or
269 f
.LocalPath().endswith('test.cc')):
273 for line_num
, line
in f
.ChangedContents():
274 if 'presubmit: allow wstring' in line
:
276 elif not allowWString
and 'wstring' in line
:
277 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
284 return [output_api
.PresubmitPromptWarning('New code should not use wstrings.'
285 ' If you are calling a cross-platform API that accepts a wstring, '
287 '\n'.join(problems
))]
290 def _CheckNoDEPSGIT(input_api
, output_api
):
291 """Make sure .DEPS.git is never modified manually."""
292 if any(f
.LocalPath().endswith('.DEPS.git') for f
in
293 input_api
.AffectedFiles()):
294 return [output_api
.PresubmitError(
295 'Never commit changes to .DEPS.git. This file is maintained by an\n'
296 'automated system based on what\'s in DEPS and your changes will be\n'
298 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
299 'for more information')]
303 def _CheckNoBannedFunctions(input_api
, output_api
):
304 """Make sure that banned functions are not used."""
308 file_filter
= lambda f
: f
.LocalPath().endswith(('.mm', '.m', '.h'))
309 for f
in input_api
.AffectedFiles(file_filter
=file_filter
):
310 for line_num
, line
in f
.ChangedContents():
311 for func_name
, message
, error
in _BANNED_OBJC_FUNCTIONS
:
312 if func_name
in line
:
316 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
317 for message_line
in message
:
318 problems
.append(' %s' % message_line
)
320 file_filter
= lambda f
: f
.LocalPath().endswith(('.cc', '.mm', '.h'))
321 for f
in input_api
.AffectedFiles(file_filter
=file_filter
):
322 for line_num
, line
in f
.ChangedContents():
323 for func_name
, message
, error
, excluded_paths
in _BANNED_CPP_FUNCTIONS
:
324 def IsBlacklisted(affected_file
, blacklist
):
325 local_path
= affected_file
.LocalPath()
326 for item
in blacklist
:
327 if input_api
.re
.match(item
, local_path
):
330 if IsBlacklisted(f
, excluded_paths
):
332 if func_name
in line
:
336 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
337 for message_line
in message
:
338 problems
.append(' %s' % message_line
)
342 result
.append(output_api
.PresubmitPromptWarning(
343 'Banned functions were used.\n' + '\n'.join(warnings
)))
345 result
.append(output_api
.PresubmitError(
346 'Banned functions were used.\n' + '\n'.join(errors
)))
350 def _CheckNoPragmaOnce(input_api
, output_api
):
351 """Make sure that banned functions are not used."""
353 pattern
= input_api
.re
.compile(r
'^#pragma\s+once',
354 input_api
.re
.MULTILINE
)
355 for f
in input_api
.AffectedSourceFiles(input_api
.FilterSourceFile
):
356 if not f
.LocalPath().endswith('.h'):
358 contents
= input_api
.ReadFile(f
)
359 if pattern
.search(contents
):
363 return [output_api
.PresubmitError(
364 'Do not use #pragma once in header files.\n'
365 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
370 def _CheckNoTrinaryTrueFalse(input_api
, output_api
):
371 """Checks to make sure we don't introduce use of foo ? true : false."""
373 pattern
= input_api
.re
.compile(r
'\?\s*(true|false)\s*:\s*(true|false)')
374 for f
in input_api
.AffectedFiles():
375 if not f
.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
378 for line_num
, line
in f
.ChangedContents():
379 if pattern
.match(line
):
380 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
384 return [output_api
.PresubmitPromptWarning(
385 'Please consider avoiding the "? true : false" pattern if possible.\n' +
386 '\n'.join(problems
))]
389 def _CheckUnwantedDependencies(input_api
, output_api
):
390 """Runs checkdeps on #include statements added in this
391 change. Breaking - rules is an error, breaking ! rules is a
394 # We need to wait until we have an input_api object and use this
395 # roundabout construct to import checkdeps because this file is
396 # eval-ed and thus doesn't have __file__.
397 original_sys_path
= sys
.path
399 sys
.path
= sys
.path
+ [input_api
.os_path
.join(
400 input_api
.PresubmitLocalPath(), 'tools', 'checkdeps')]
402 from cpp_checker
import CppChecker
403 from rules
import Rule
405 # Restore sys.path to what it was before.
406 sys
.path
= original_sys_path
409 for f
in input_api
.AffectedFiles():
410 if not CppChecker
.IsCppFile(f
.LocalPath()):
413 changed_lines
= [line
for line_num
, line
in f
.ChangedContents()]
414 added_includes
.append([f
.LocalPath(), changed_lines
])
416 deps_checker
= checkdeps
.DepsChecker()
418 error_descriptions
= []
419 warning_descriptions
= []
420 for path
, rule_type
, rule_description
in deps_checker
.CheckAddedCppIncludes(
422 description_with_path
= '%s\n %s' % (path
, rule_description
)
423 if rule_type
== Rule
.DISALLOW
:
424 error_descriptions
.append(description_with_path
)
426 warning_descriptions
.append(description_with_path
)
429 if error_descriptions
:
430 results
.append(output_api
.PresubmitError(
431 'You added one or more #includes that violate checkdeps rules.',
433 if warning_descriptions
:
434 results
.append(output_api
.PresubmitPromptOrNotify(
435 'You added one or more #includes of files that are temporarily\n'
436 'allowed but being removed. Can you avoid introducing the\n'
437 '#include? See relevant DEPS file(s) for details and contacts.',
438 warning_descriptions
))
442 def _CheckFilePermissions(input_api
, output_api
):
443 """Check that all files have their permissions properly set."""
444 args
= [sys
.executable
, 'tools/checkperms/checkperms.py', '--root',
445 input_api
.change
.RepositoryRoot()]
446 for f
in input_api
.AffectedFiles():
447 args
+= ['--file', f
.LocalPath()]
449 (errors
, stderrdata
) = subprocess
.Popen(args
).communicate()
453 results
.append(output_api
.PresubmitError('checkperms.py failed.',
458 def _CheckNoAuraWindowPropertyHInHeaders(input_api
, output_api
):
459 """Makes sure we don't include ui/aura/window_property.h
462 pattern
= input_api
.re
.compile(r
'^#include\s*"ui/aura/window_property.h"')
464 for f
in input_api
.AffectedFiles():
465 if not f
.LocalPath().endswith('.h'):
467 for line_num
, line
in f
.ChangedContents():
468 if pattern
.match(line
):
469 errors
.append(' %s:%d' % (f
.LocalPath(), line_num
))
473 results
.append(output_api
.PresubmitError(
474 'Header files should not include ui/aura/window_property.h', errors
))
478 def _CheckIncludeOrderForScope(scope
, input_api
, file_path
, changed_linenums
):
479 """Checks that the lines in scope occur in the right order.
481 1. C system files in alphabetical order
482 2. C++ system files in alphabetical order
483 3. Project's .h files
486 c_system_include_pattern
= input_api
.re
.compile(r
'\s*#include <.*\.h>')
487 cpp_system_include_pattern
= input_api
.re
.compile(r
'\s*#include <.*>')
488 custom_include_pattern
= input_api
.re
.compile(r
'\s*#include ".*')
490 C_SYSTEM_INCLUDES
, CPP_SYSTEM_INCLUDES
, CUSTOM_INCLUDES
= range(3)
492 state
= C_SYSTEM_INCLUDES
495 previous_line_num
= 0
496 problem_linenums
= []
497 for line_num
, line
in scope
:
498 if c_system_include_pattern
.match(line
):
499 if state
!= C_SYSTEM_INCLUDES
:
500 problem_linenums
.append((line_num
, previous_line_num
))
501 elif previous_line
and previous_line
> line
:
502 problem_linenums
.append((line_num
, previous_line_num
))
503 elif cpp_system_include_pattern
.match(line
):
504 if state
== C_SYSTEM_INCLUDES
:
505 state
= CPP_SYSTEM_INCLUDES
506 elif state
== CUSTOM_INCLUDES
:
507 problem_linenums
.append((line_num
, previous_line_num
))
508 elif previous_line
and previous_line
> line
:
509 problem_linenums
.append((line_num
, previous_line_num
))
510 elif custom_include_pattern
.match(line
):
511 if state
!= CUSTOM_INCLUDES
:
512 state
= CUSTOM_INCLUDES
513 elif previous_line
and previous_line
> line
:
514 problem_linenums
.append((line_num
, previous_line_num
))
516 problem_linenums
.append(line_num
)
518 previous_line_num
= line_num
521 for (line_num
, previous_line_num
) in problem_linenums
:
522 if line_num
in changed_linenums
or previous_line_num
in changed_linenums
:
523 warnings
.append(' %s:%d' % (file_path
, line_num
))
527 def _CheckIncludeOrderInFile(input_api
, f
, changed_linenums
):
528 """Checks the #include order for the given file f."""
530 system_include_pattern
= input_api
.re
.compile(r
'\s*#include \<.*')
531 # Exclude #include <.../...> includes from the check; e.g., <sys/...> includes
532 # often need to appear in a specific order.
533 excluded_include_pattern
= input_api
.re
.compile(r
'\s*#include \<.*/.*')
534 custom_include_pattern
= input_api
.re
.compile(r
'\s*#include "(?P<FILE>.*)"')
535 if_pattern
= input_api
.re
.compile(
536 r
'\s*#\s*(if|elif|else|endif|define|undef).*')
537 # Some files need specialized order of includes; exclude such files from this
539 uncheckable_includes_pattern
= input_api
.re
.compile(
541 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
543 contents
= f
.NewContents()
547 # Handle the special first include. If the first include file is
548 # some/path/file.h, the corresponding including file can be some/path/file.cc,
549 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
550 # etc. It's also possible that no special first include exists.
551 for line
in contents
:
553 if system_include_pattern
.match(line
):
554 # No special first include -> process the line again along with normal
558 match
= custom_include_pattern
.match(line
)
560 match_dict
= match
.groupdict()
561 header_basename
= input_api
.os_path
.basename(
562 match_dict
['FILE']).replace('.h', '')
563 if header_basename
not in input_api
.os_path
.basename(f
.LocalPath()):
564 # No special first include -> process the line again along with normal
569 # Split into scopes: Each region between #if and #endif is its own scope.
572 for line
in contents
[line_num
:]:
574 if uncheckable_includes_pattern
.match(line
):
576 if if_pattern
.match(line
):
577 scopes
.append(current_scope
)
579 elif ((system_include_pattern
.match(line
) or
580 custom_include_pattern
.match(line
)) and
581 not excluded_include_pattern
.match(line
)):
582 current_scope
.append((line_num
, line
))
583 scopes
.append(current_scope
)
586 warnings
.extend(_CheckIncludeOrderForScope(scope
, input_api
, f
.LocalPath(),
591 def _CheckIncludeOrder(input_api
, output_api
):
592 """Checks that the #include order is correct.
594 1. The corresponding header for source files.
595 2. C system files in alphabetical order
596 3. C++ system files in alphabetical order
597 4. Project's .h files in alphabetical order
599 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
600 these rules separately.
604 for f
in input_api
.AffectedFiles():
605 if f
.LocalPath().endswith(('.cc', '.h')):
606 changed_linenums
= set(line_num
for line_num
, _
in f
.ChangedContents())
607 warnings
.extend(_CheckIncludeOrderInFile(input_api
, f
, changed_linenums
))
611 results
.append(output_api
.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING
,
616 def _CheckForVersionControlConflictsInFile(input_api
, f
):
617 pattern
= input_api
.re
.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
619 for line_num
, line
in f
.ChangedContents():
620 if pattern
.match(line
):
621 errors
.append(' %s:%d %s' % (f
.LocalPath(), line_num
, line
))
625 def _CheckForVersionControlConflicts(input_api
, output_api
):
626 """Usually this is not intentional and will cause a compile failure."""
628 for f
in input_api
.AffectedFiles():
629 errors
.extend(_CheckForVersionControlConflictsInFile(input_api
, f
))
633 results
.append(output_api
.PresubmitError(
634 'Version control conflict markers found, please resolve.', errors
))
638 def _CheckHardcodedGoogleHostsInLowerLayers(input_api
, output_api
):
639 def FilterFile(affected_file
):
640 """Filter function for use with input_api.AffectedSourceFiles,
641 below. This filters out everything except non-test files from
642 top-level directories that generally speaking should not hard-code
643 service URLs (e.g. src/android_webview/, src/content/ and others).
645 return input_api
.FilterSourceFile(
647 white_list
=(r
'^(android_webview|base|content|net)[\\\/].*', ),
648 black_list
=(_EXCLUDED_PATHS
+
649 _TEST_CODE_EXCLUDED_PATHS
+
650 input_api
.DEFAULT_BLACK_LIST
))
652 pattern
= input_api
.re
.compile('"[^"]*google\.com[^"]*"')
653 problems
= [] # items are (filename, line_number, line)
654 for f
in input_api
.AffectedSourceFiles(FilterFile
):
655 for line_num
, line
in f
.ChangedContents():
656 if pattern
.search(line
):
657 problems
.append((f
.LocalPath(), line_num
, line
))
660 return [output_api
.PresubmitPromptOrNotify(
661 'Most layers below src/chrome/ should not hardcode service URLs.\n'
662 'Are you sure this is correct? (Contact: joi@chromium.org)',
664 problem
[0], problem
[1], problem
[2]) for problem
in problems
])]
669 def _CheckNoAbbreviationInPngFileName(input_api
, output_api
):
670 """Makes sure there are no abbreviations in the name of PNG files.
672 pattern
= input_api
.re
.compile(r
'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
674 for f
in input_api
.AffectedFiles(include_deletes
=False):
675 if pattern
.match(f
.LocalPath()):
676 errors
.append(' %s' % f
.LocalPath())
680 results
.append(output_api
.PresubmitError(
681 'The name of PNG files should not have abbreviations. \n'
682 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
683 'Contact oshima@chromium.org if you have questions.', errors
))
687 def _CommonChecks(input_api
, output_api
):
688 """Checks common to both upload and commit."""
690 results
.extend(input_api
.canned_checks
.PanProjectChecks(
691 input_api
, output_api
, excluded_paths
=_EXCLUDED_PATHS
))
692 results
.extend(_CheckAuthorizedAuthor(input_api
, output_api
))
694 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
))
695 results
.extend(_CheckNoIOStreamInHeaders(input_api
, output_api
))
696 results
.extend(_CheckNoUNIT_TESTInSourceFiles(input_api
, output_api
))
697 results
.extend(_CheckNoNewWStrings(input_api
, output_api
))
698 results
.extend(_CheckNoDEPSGIT(input_api
, output_api
))
699 results
.extend(_CheckNoBannedFunctions(input_api
, output_api
))
700 results
.extend(_CheckNoPragmaOnce(input_api
, output_api
))
701 results
.extend(_CheckNoTrinaryTrueFalse(input_api
, output_api
))
702 results
.extend(_CheckUnwantedDependencies(input_api
, output_api
))
703 results
.extend(_CheckFilePermissions(input_api
, output_api
))
704 results
.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api
, output_api
))
705 results
.extend(_CheckIncludeOrder(input_api
, output_api
))
706 results
.extend(_CheckForVersionControlConflicts(input_api
, output_api
))
707 results
.extend(_CheckPatchFiles(input_api
, output_api
))
708 results
.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api
, output_api
))
709 results
.extend(_CheckNoAbbreviationInPngFileName(input_api
, output_api
))
710 results
.extend(_CheckForInvalidOSMacros(input_api
, output_api
))
712 if any('PRESUBMIT.py' == f
.LocalPath() for f
in input_api
.AffectedFiles()):
713 results
.extend(input_api
.canned_checks
.RunUnitTestsInDirectory(
714 input_api
, output_api
,
715 input_api
.PresubmitLocalPath(),
716 whitelist
=[r
'^PRESUBMIT_test\.py$']))
720 def _CheckSubversionConfig(input_api
, output_api
):
721 """Verifies the subversion config file is correctly setup.
723 Checks that autoprops are enabled, returns an error otherwise.
725 join
= input_api
.os_path
.join
726 if input_api
.platform
== 'win32':
727 appdata
= input_api
.environ
.get('APPDATA', '')
729 return [output_api
.PresubmitError('%APPDATA% is not configured.')]
730 path
= join(appdata
, 'Subversion', 'config')
732 home
= input_api
.environ
.get('HOME', '')
734 return [output_api
.PresubmitError('$HOME is not configured.')]
735 path
= join(home
, '.subversion', 'config')
738 'Please look at http://dev.chromium.org/developers/coding-style to\n'
739 'configure your subversion configuration file. This enables automatic\n'
740 'properties to simplify the project maintenance.\n'
741 'Pro-tip: just download and install\n'
742 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
745 lines
= open(path
, 'r').read().splitlines()
746 # Make sure auto-props is enabled and check for 2 Chromium standard
748 if (not '*.cc = svn:eol-style=LF' in lines
or
749 not '*.pdf = svn:mime-type=application/pdf' in lines
or
750 not 'enable-auto-props = yes' in lines
):
752 output_api
.PresubmitNotifyResult(
753 'It looks like you have not configured your subversion config '
754 'file or it is not up-to-date.\n' + error_msg
)
756 except (OSError, IOError):
758 output_api
.PresubmitNotifyResult(
759 'Can\'t find your subversion config file.\n' + error_msg
)
764 def _CheckAuthorizedAuthor(input_api
, output_api
):
765 """For non-googler/chromites committers, verify the author's email address is
768 # TODO(maruel): Add it to input_api?
771 author
= input_api
.change
.author_email
773 input_api
.logging
.info('No author, skipping AUTHOR check')
775 authors_path
= input_api
.os_path
.join(
776 input_api
.PresubmitLocalPath(), 'AUTHORS')
778 input_api
.re
.match(r
'[^#]+\s+\<(.+?)\>\s*$', line
)
779 for line
in open(authors_path
))
780 valid_authors
= [item
.group(1).lower() for item
in valid_authors
if item
]
781 if not any(fnmatch
.fnmatch(author
.lower(), valid
) for valid
in valid_authors
):
782 input_api
.logging
.info('Valid authors are %s', ', '.join(valid_authors
))
783 return [output_api
.PresubmitPromptWarning(
784 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
786 'http://www.chromium.org/developers/contributing-code and read the '
788 'If you are a chromite, verify the contributor signed the CLA.') %
793 def _CheckPatchFiles(input_api
, output_api
):
794 problems
= [f
.LocalPath() for f
in input_api
.AffectedFiles()
795 if f
.LocalPath().endswith(('.orig', '.rej'))]
797 return [output_api
.PresubmitError(
798 "Don't commit .rej and .orig files.", problems
)]
803 def _DidYouMeanOSMacro(bad_macro
):
805 return {'A': 'OS_ANDROID',
815 'W': 'OS_WIN'}[bad_macro
[3].upper()]
820 def _CheckForInvalidOSMacrosInFile(input_api
, f
):
821 """Check for sensible looking, totally invalid OS macros."""
822 preprocessor_statement
= input_api
.re
.compile(r
'^\s*#')
823 os_macro
= input_api
.re
.compile(r
'defined\((OS_[^)]+)\)')
825 for lnum
, line
in f
.ChangedContents():
826 if preprocessor_statement
.search(line
):
827 for match
in os_macro
.finditer(line
):
828 if not match
.group(1) in _VALID_OS_MACROS
:
829 good
= _DidYouMeanOSMacro(match
.group(1))
830 did_you_mean
= ' (did you mean %s?)' % good
if good
else ''
831 results
.append(' %s:%d %s%s' % (f
.LocalPath(),
838 def _CheckForInvalidOSMacros(input_api
, output_api
):
839 """Check all affected files for invalid OS macros."""
841 for f
in input_api
.AffectedFiles():
842 if not f
.LocalPath().endswith(('.py', '.js', '.html', '.css')):
843 bad_macros
.extend(_CheckForInvalidOSMacrosInFile(input_api
, f
))
848 return [output_api
.PresubmitError(
849 'Possibly invalid OS macro[s] found. Please fix your code\n'
850 'or add your macro to src/PRESUBMIT.py.', bad_macros
)]
853 def CheckChangeOnUpload(input_api
, output_api
):
855 results
.extend(_CommonChecks(input_api
, output_api
))
859 def CheckChangeOnCommit(input_api
, output_api
):
861 results
.extend(_CommonChecks(input_api
, output_api
))
862 # TODO(thestig) temporarily disabled, doesn't work in third_party/
863 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
864 # input_api, output_api, sources))
865 # Make sure the tree is 'open'.
866 results
.extend(input_api
.canned_checks
.CheckTreeIsOpen(
869 json_url
='http://chromium-status.appspot.com/current?format=json'))
870 results
.extend(input_api
.canned_checks
.CheckRietveldTryJobExecution(input_api
,
871 output_api
, 'http://codereview.chromium.org',
872 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
873 'tryserver@chromium.org'))
875 results
.extend(input_api
.canned_checks
.CheckChangeHasBugField(
876 input_api
, output_api
))
877 results
.extend(input_api
.canned_checks
.CheckChangeHasDescription(
878 input_api
, output_api
))
879 results
.extend(_CheckSubversionConfig(input_api
, output_api
))
883 def GetPreferredTrySlaves(project
, change
):
884 files
= change
.LocalPaths()
886 if not files
or all(re
.search(r
'[\\/]OWNERS$', f
) for f
in files
):
889 if all(re
.search('\.(m|mm)$|(^|[/_])mac[/_.]', f
) for f
in files
):
890 return ['mac_rel', 'mac_asan', 'mac:compile']
891 if all(re
.search('(^|[/_])win[/_.]', f
) for f
in files
):
892 return ['win_rel', 'win7_aura', 'win:compile']
893 if all(re
.search('(^|[/_])android[/_.]', f
) for f
in files
):
894 return ['android_dbg', 'android_clang_dbg']
895 if all(re
.search('^native_client_sdk', f
) for f
in files
):
896 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
897 if all(re
.search('[/_]ios[/_.]', f
) for f
in files
):
898 return ['ios_rel_device', 'ios_dbg_simulator']
908 'linux_clang:compile',
918 # Match things like path/aura/file.cc and path/file_aura.cc.
920 if any(re
.search('[/_](aura|chromeos)', f
) for f
in files
):
921 trybots
+= ['linux_chromeos_clang:compile', 'linux_chromeos_asan']