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.
17 r
"^native_client_sdk[\\\/].*",
18 r
"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
26 _TEST_ONLY_WARNING
= (
27 'You might be calling functions intended only for testing from\n'
28 'production code. It is OK to ignore this warning if you know what\n'
29 'you are doing, as the heuristics used to detect the situation are\n'
30 'not perfect. The commit queue will not block on this warning.\n'
31 'Email joi@chromium.org if you have questions.')
34 _BANNED_OBJC_FUNCTIONS
= (
38 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
39 'prohibited. Please use CrTrackingArea instead.',
40 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
47 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
49 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
54 'convertPointFromBase:',
56 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
57 'Please use |convertPoint:(point) fromView:nil| instead.',
58 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
63 'convertPointToBase:',
65 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
66 'Please use |convertPoint:(point) toView:nil| instead.',
67 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
72 'convertRectFromBase:',
74 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
75 'Please use |convertRect:(point) fromView:nil| instead.',
76 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
83 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
84 'Please use |convertRect:(point) toView:nil| instead.',
85 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
90 'convertSizeFromBase:',
92 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
93 'Please use |convertSize:(point) fromView:nil| instead.',
94 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
101 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
102 'Please use |convertSize:(point) toView:nil| instead.',
103 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
110 _BANNED_CPP_FUNCTIONS
= (
111 # Make sure that gtest's FRIEND_TEST() macro is not used; the
112 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
113 # used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes.
117 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
118 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
125 'New code should not use ScopedAllowIO. Post a task to the blocking',
126 'pool or the FILE thread instead.',
131 'FilePathWatcher::Delegate',
133 'New code should not use FilePathWatcher::Delegate. Use the callback',
134 'interface instead.',
139 'browser::FindLastActiveWithProfile',
141 'This function is deprecated and we\'re working on removing it. Pass',
142 'more context to get a Browser*, like a WebContents, window, or session',
143 'id. Talk to ben@ or jam@ for more information.',
148 'browser::FindBrowserWithProfile',
150 'This function is deprecated and we\'re working on removing it. Pass',
151 'more context to get a Browser*, like a WebContents, window, or session',
152 'id. Talk to ben@ or jam@ for more information.',
157 'browser::FindAnyBrowser',
159 'This function is deprecated and we\'re working on removing it. Pass',
160 'more context to get a Browser*, like a WebContents, window, or session',
161 'id. Talk to ben@ or jam@ for more information.',
166 'browser::FindOrCreateTabbedBrowser',
168 'This function is deprecated and we\'re working on removing it. Pass',
169 'more context to get a Browser*, like a WebContents, window, or session',
170 'id. Talk to ben@ or jam@ for more information.',
175 'browser::FindTabbedBrowser',
177 'This function is deprecated and we\'re working on removing it. Pass',
178 'more context to get a Browser*, like a WebContents, window, or session',
179 'id. Talk to ben@ or jam@ for more information.',
187 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
):
188 """Attempts to prevent use of functions intended only for testing in
189 non-testing code. For now this is just a best-effort implementation
190 that ignores header files and may have some false positives. A
191 better implementation would probably need a proper C++ parser.
193 # We only scan .cc files and the like, as the declaration of
194 # for-testing functions in header files are hard to distinguish from
195 # calls to such functions without a proper C++ parser.
196 platform_specifiers
= r
'(_(android|chromeos|gtk|mac|posix|win))?'
197 source_extensions
= r
'\.(cc|cpp|cxx|mm)$'
198 file_inclusion_pattern
= r
'.+%s' % source_extensions
199 file_exclusion_patterns
= (
200 r
'.*[/\\](test_|mock_).+%s' % source_extensions
,
201 r
'.+_test_(base|support|util)%s' % source_extensions
,
202 r
'.+_(api|browser|perf|unit|ui)?test%s%s' % (platform_specifiers
,
204 r
'.+profile_sync_service_harness%s' % source_extensions
,
206 path_exclusion_patterns
= (
207 r
'.*[/\\](test|tool(s)?)[/\\].*',
208 # At request of folks maintaining this folder.
209 r
'chrome[/\\]browser[/\\]automation[/\\].*',
212 base_function_pattern
= r
'ForTest(ing)?|for_test(ing)?'
213 inclusion_pattern
= input_api
.re
.compile(r
'(%s)\s*\(' % base_function_pattern
)
214 exclusion_pattern
= input_api
.re
.compile(
215 r
'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
216 base_function_pattern
, base_function_pattern
))
218 def FilterFile(affected_file
):
219 black_list
= (file_exclusion_patterns
+ path_exclusion_patterns
+
220 _EXCLUDED_PATHS
+ input_api
.DEFAULT_BLACK_LIST
)
221 return input_api
.FilterSourceFile(
223 white_list
=(file_inclusion_pattern
, ),
224 black_list
=black_list
)
227 for f
in input_api
.AffectedSourceFiles(FilterFile
):
228 local_path
= f
.LocalPath()
229 lines
= input_api
.ReadFile(f
).splitlines()
232 if (inclusion_pattern
.search(line
) and
233 not exclusion_pattern
.search(line
)):
235 '%s:%d\n %s' % (local_path
, line_number
, line
.strip()))
239 if not input_api
.is_committing
:
240 return [output_api
.PresubmitPromptWarning(_TEST_ONLY_WARNING
, problems
)]
242 # We don't warn on commit, to avoid stopping commits going through CQ.
243 return [output_api
.PresubmitNotifyResult(_TEST_ONLY_WARNING
, problems
)]
248 def _CheckNoIOStreamInHeaders(input_api
, output_api
):
249 """Checks to make sure no .h files include <iostream>."""
251 pattern
= input_api
.re
.compile(r
'^#include\s*<iostream>',
252 input_api
.re
.MULTILINE
)
253 for f
in input_api
.AffectedSourceFiles(input_api
.FilterSourceFile
):
254 if not f
.LocalPath().endswith('.h'):
256 contents
= input_api
.ReadFile(f
)
257 if pattern
.search(contents
):
261 return [ output_api
.PresubmitError(
262 'Do not #include <iostream> in header files, since it inserts static '
263 'initialization into every file including the header. Instead, '
264 '#include <ostream>. See http://crbug.com/94794',
269 def _CheckNoUNIT_TESTInSourceFiles(input_api
, output_api
):
270 """Checks to make sure no source files use UNIT_TEST"""
272 for f
in input_api
.AffectedFiles():
273 if (not f
.LocalPath().endswith(('.cc', '.mm'))):
276 for line_num
, line
in f
.ChangedContents():
277 if 'UNIT_TEST' in line
:
278 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
282 return [output_api
.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
283 '\n'.join(problems
))]
286 def _CheckNoNewWStrings(input_api
, output_api
):
287 """Checks to make sure we don't introduce use of wstrings."""
289 for f
in input_api
.AffectedFiles():
290 if (not f
.LocalPath().endswith(('.cc', '.h')) or
291 f
.LocalPath().endswith('test.cc')):
294 for line_num
, line
in f
.ChangedContents():
295 if 'wstring' in line
:
296 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
300 return [output_api
.PresubmitPromptWarning('New code should not use wstrings.'
301 ' If you are calling an API that accepts a wstring, fix the API.\n' +
302 '\n'.join(problems
))]
305 def _CheckNoDEPSGIT(input_api
, output_api
):
306 """Make sure .DEPS.git is never modified manually."""
307 if any(f
.LocalPath().endswith('.DEPS.git') for f
in
308 input_api
.AffectedFiles()):
309 return [output_api
.PresubmitError(
310 'Never commit changes to .DEPS.git. This file is maintained by an\n'
311 'automated system based on what\'s in DEPS and your changes will be\n'
313 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
314 'for more information')]
318 def _CheckNoBannedFunctions(input_api
, output_api
):
319 """Make sure that banned functions are not used."""
323 file_filter
= lambda f
: f
.LocalPath().endswith(('.mm', '.m', '.h'))
324 for f
in input_api
.AffectedFiles(file_filter
=file_filter
):
325 for line_num
, line
in f
.ChangedContents():
326 for func_name
, message
, error
in _BANNED_OBJC_FUNCTIONS
:
327 if func_name
in line
:
331 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
332 for message_line
in message
:
333 problems
.append(' %s' % message_line
)
335 file_filter
= lambda f
: f
.LocalPath().endswith(('.cc', '.mm', '.h'))
336 for f
in input_api
.AffectedFiles(file_filter
=file_filter
):
337 for line_num
, line
in f
.ChangedContents():
338 for func_name
, message
, error
in _BANNED_CPP_FUNCTIONS
:
339 if func_name
in line
:
343 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
344 for message_line
in message
:
345 problems
.append(' %s' % message_line
)
349 result
.append(output_api
.PresubmitPromptWarning(
350 'Banned functions were used.\n' + '\n'.join(warnings
)))
352 result
.append(output_api
.PresubmitError(
353 'Banned functions were used.\n' + '\n'.join(errors
)))
357 def _CheckNoPragmaOnce(input_api
, output_api
):
358 """Make sure that banned functions are not used."""
360 pattern
= input_api
.re
.compile(r
'^#pragma\s+once',
361 input_api
.re
.MULTILINE
)
362 for f
in input_api
.AffectedSourceFiles(input_api
.FilterSourceFile
):
363 if not f
.LocalPath().endswith('.h'):
365 contents
= input_api
.ReadFile(f
)
366 if pattern
.search(contents
):
370 return [output_api
.PresubmitError(
371 'Do not use #pragma once in header files.\n'
372 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
377 def _CommonChecks(input_api
, output_api
):
378 """Checks common to both upload and commit."""
380 results
.extend(input_api
.canned_checks
.PanProjectChecks(
381 input_api
, output_api
, excluded_paths
=_EXCLUDED_PATHS
))
382 results
.extend(_CheckAuthorizedAuthor(input_api
, output_api
))
384 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
))
385 results
.extend(_CheckNoIOStreamInHeaders(input_api
, output_api
))
386 results
.extend(_CheckNoUNIT_TESTInSourceFiles(input_api
, output_api
))
387 results
.extend(_CheckNoNewWStrings(input_api
, output_api
))
388 results
.extend(_CheckNoDEPSGIT(input_api
, output_api
))
389 results
.extend(_CheckNoBannedFunctions(input_api
, output_api
))
390 results
.extend(_CheckNoPragmaOnce(input_api
, output_api
))
394 def _CheckSubversionConfig(input_api
, output_api
):
395 """Verifies the subversion config file is correctly setup.
397 Checks that autoprops are enabled, returns an error otherwise.
399 join
= input_api
.os_path
.join
400 if input_api
.platform
== 'win32':
401 appdata
= input_api
.environ
.get('APPDATA', '')
403 return [output_api
.PresubmitError('%APPDATA% is not configured.')]
404 path
= join(appdata
, 'Subversion', 'config')
406 home
= input_api
.environ
.get('HOME', '')
408 return [output_api
.PresubmitError('$HOME is not configured.')]
409 path
= join(home
, '.subversion', 'config')
412 'Please look at http://dev.chromium.org/developers/coding-style to\n'
413 'configure your subversion configuration file. This enables automatic\n'
414 'properties to simplify the project maintenance.\n'
415 'Pro-tip: just download and install\n'
416 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
419 lines
= open(path
, 'r').read().splitlines()
420 # Make sure auto-props is enabled and check for 2 Chromium standard
422 if (not '*.cc = svn:eol-style=LF' in lines
or
423 not '*.pdf = svn:mime-type=application/pdf' in lines
or
424 not 'enable-auto-props = yes' in lines
):
426 output_api
.PresubmitNotifyResult(
427 'It looks like you have not configured your subversion config '
428 'file or it is not up-to-date.\n' + error_msg
)
430 except (OSError, IOError):
432 output_api
.PresubmitNotifyResult(
433 'Can\'t find your subversion config file.\n' + error_msg
)
438 def _CheckAuthorizedAuthor(input_api
, output_api
):
439 """For non-googler/chromites committers, verify the author's email address is
442 # TODO(maruel): Add it to input_api?
445 author
= input_api
.change
.author_email
447 input_api
.logging
.info('No author, skipping AUTHOR check')
449 authors_path
= input_api
.os_path
.join(
450 input_api
.PresubmitLocalPath(), 'AUTHORS')
452 input_api
.re
.match(r
'[^#]+\s+\<(.+?)\>\s*$', line
)
453 for line
in open(authors_path
))
454 valid_authors
= [item
.group(1).lower() for item
in valid_authors
if item
]
455 if input_api
.verbose
:
456 print 'Valid authors are %s' % ', '.join(valid_authors
)
457 if not any(fnmatch
.fnmatch(author
.lower(), valid
) for valid
in valid_authors
):
458 return [output_api
.PresubmitPromptWarning(
459 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
461 'http://www.chromium.org/developers/contributing-code and read the '
463 'If you are a chromite, verify the contributor signed the CLA.') %
468 def CheckChangeOnUpload(input_api
, output_api
):
470 results
.extend(_CommonChecks(input_api
, output_api
))
474 def CheckChangeOnCommit(input_api
, output_api
):
476 results
.extend(_CommonChecks(input_api
, output_api
))
477 # TODO(thestig) temporarily disabled, doesn't work in third_party/
478 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
479 # input_api, output_api, sources))
480 # Make sure the tree is 'open'.
481 results
.extend(input_api
.canned_checks
.CheckTreeIsOpen(
484 json_url
='http://chromium-status.appspot.com/current?format=json'))
485 results
.extend(input_api
.canned_checks
.CheckRietveldTryJobExecution(input_api
,
486 output_api
, 'http://codereview.chromium.org',
487 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
488 'tryserver@chromium.org'))
490 results
.extend(input_api
.canned_checks
.CheckChangeHasBugField(
491 input_api
, output_api
))
492 results
.extend(input_api
.canned_checks
.CheckChangeHasTestField(
493 input_api
, output_api
))
494 results
.extend(input_api
.canned_checks
.CheckChangeHasDescription(
495 input_api
, output_api
))
496 results
.extend(_CheckSubversionConfig(input_api
, output_api
))
500 def GetPreferredTrySlaves(project
, change
):
501 files
= change
.LocalPaths()
506 if all(re
.search('\.(m|mm)$|[/_]mac[/_.]', f
) for f
in files
):
508 if all(re
.search('[/_]win[/_.]', f
) for f
in files
):
510 if all(re
.search('[/_]android[/_.]', f
) for f
in files
):
513 trybots
= ['win_rel', 'linux_rel', 'mac_rel', 'linux_clang:compile']
514 # match things like aurax11.cc or aura_oak.cc
515 if any(re
.search('[/_]aura', f
) for f
in files
):
516 trybots
.append('linux_chromeos')
518 if not all(f
.startswith('chrome/') for f
in files
):
519 trybots
.append('android')