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 pool'
126 'or the FILE thread instead.',
131 'FilePathWatcher::Delegate',
133 'New code should not use FilePathWatcher::Delegate. Use the callback'
134 'interface instead.',
142 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
):
143 """Attempts to prevent use of functions intended only for testing in
144 non-testing code. For now this is just a best-effort implementation
145 that ignores header files and may have some false positives. A
146 better implementation would probably need a proper C++ parser.
148 # We only scan .cc files and the like, as the declaration of
149 # for-testing functions in header files are hard to distinguish from
150 # calls to such functions without a proper C++ parser.
151 platform_specifiers
= r
'(_(android|chromeos|gtk|mac|posix|win))?'
152 source_extensions
= r
'\.(cc|cpp|cxx|mm)$'
153 file_inclusion_pattern
= r
'.+%s' % source_extensions
154 file_exclusion_patterns
= (
155 r
'.*[/\\](test_|mock_).+%s' % source_extensions
,
156 r
'.+_test_(base|support|util)%s' % source_extensions
,
157 r
'.+_(api|browser|perf|unit|ui)?test%s%s' % (platform_specifiers
,
159 r
'.+profile_sync_service_harness%s' % source_extensions
,
161 path_exclusion_patterns
= (
162 r
'.*[/\\](test|tool(s)?)[/\\].*',
163 # At request of folks maintaining this folder.
164 r
'chrome[/\\]browser[/\\]automation[/\\].*',
167 base_function_pattern
= r
'ForTest(ing)?|for_test(ing)?'
168 inclusion_pattern
= input_api
.re
.compile(r
'(%s)\s*\(' % base_function_pattern
)
169 exclusion_pattern
= input_api
.re
.compile(
170 r
'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
171 base_function_pattern
, base_function_pattern
))
173 def FilterFile(affected_file
):
174 black_list
= (file_exclusion_patterns
+ path_exclusion_patterns
+
175 _EXCLUDED_PATHS
+ input_api
.DEFAULT_BLACK_LIST
)
176 return input_api
.FilterSourceFile(
178 white_list
=(file_inclusion_pattern
, ),
179 black_list
=black_list
)
182 for f
in input_api
.AffectedSourceFiles(FilterFile
):
183 local_path
= f
.LocalPath()
184 lines
= input_api
.ReadFile(f
).splitlines()
187 if (inclusion_pattern
.search(line
) and
188 not exclusion_pattern
.search(line
)):
190 '%s:%d\n %s' % (local_path
, line_number
, line
.strip()))
194 if not input_api
.is_committing
:
195 return [output_api
.PresubmitPromptWarning(_TEST_ONLY_WARNING
, problems
)]
197 # We don't warn on commit, to avoid stopping commits going through CQ.
198 return [output_api
.PresubmitNotifyResult(_TEST_ONLY_WARNING
, problems
)]
203 def _CheckNoIOStreamInHeaders(input_api
, output_api
):
204 """Checks to make sure no .h files include <iostream>."""
206 pattern
= input_api
.re
.compile(r
'^#include\s*<iostream>',
207 input_api
.re
.MULTILINE
)
208 for f
in input_api
.AffectedSourceFiles(input_api
.FilterSourceFile
):
209 if not f
.LocalPath().endswith('.h'):
211 contents
= input_api
.ReadFile(f
)
212 if pattern
.search(contents
):
216 return [ output_api
.PresubmitError(
217 'Do not #include <iostream> in header files, since it inserts static ' +
218 'initialization into every file including the header. Instead, ' +
219 '#include <ostream>. See http://crbug.com/94794',
224 def _CheckNoNewWStrings(input_api
, output_api
):
225 """Checks to make sure we don't introduce use of wstrings."""
227 for f
in input_api
.AffectedFiles():
228 if (not f
.LocalPath().endswith(('.cc', '.h')) or
229 f
.LocalPath().endswith('test.cc')):
232 for line_num
, line
in f
.ChangedContents():
233 if 'wstring' in line
:
234 problems
.append(' %s:%d' % (f
.LocalPath(), line_num
))
238 return [output_api
.PresubmitPromptWarning('New code should not use wstrings.'
239 ' If you are calling an API that accepts a wstring, fix the API.\n' +
240 '\n'.join(problems
))]
243 def _CheckNoDEPSGIT(input_api
, output_api
):
244 """Make sure .DEPS.git is never modified manually."""
245 if any(f
.LocalPath().endswith('.DEPS.git') for f
in
246 input_api
.AffectedFiles()):
247 return [output_api
.PresubmitError(
248 'Never commit changes to .DEPS.git. This file is maintained by an\n'
249 'automated system based on what\'s in DEPS and your changes will be\n'
251 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
252 'for more information')]
256 def _CheckNoBannedFunctions(input_api
, output_api
):
257 """Make sure that banned functions are not used."""
261 file_filter
= lambda f
: f
.LocalPath().endswith(('.mm', '.m', '.h'))
262 for f
in input_api
.AffectedFiles(file_filter
=file_filter
):
263 for line_num
, line
in f
.ChangedContents():
264 for func_name
, message
, error
in _BANNED_OBJC_FUNCTIONS
:
265 if func_name
in line
:
269 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
270 for message_line
in message
:
271 problems
.append(' %s' % message_line
)
273 file_filter
= lambda f
: f
.LocalPath().endswith(('.cc', '.mm', '.h'))
274 for f
in input_api
.AffectedFiles(file_filter
=file_filter
):
275 for line_num
, line
in f
.ChangedContents():
276 for func_name
, message
, error
in _BANNED_CPP_FUNCTIONS
:
277 if func_name
in line
:
281 problems
.append(' %s:%d:' % (f
.LocalPath(), line_num
))
282 for message_line
in message
:
283 problems
.append(' %s' % message_line
)
287 result
.append(output_api
.PresubmitPromptWarning(
288 'Banned functions were used.\n' + '\n'.join(warnings
)))
290 result
.append(output_api
.PresubmitError(
291 'Banned functions were used.\n' + '\n'.join(errors
)))
296 def _CommonChecks(input_api
, output_api
):
297 """Checks common to both upload and commit."""
299 results
.extend(input_api
.canned_checks
.PanProjectChecks(
300 input_api
, output_api
, excluded_paths
=_EXCLUDED_PATHS
))
301 results
.extend(_CheckAuthorizedAuthor(input_api
, output_api
))
303 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api
, output_api
))
304 results
.extend(_CheckNoIOStreamInHeaders(input_api
, output_api
))
305 results
.extend(_CheckNoNewWStrings(input_api
, output_api
))
306 results
.extend(_CheckNoDEPSGIT(input_api
, output_api
))
307 results
.extend(_CheckNoBannedFunctions(input_api
, output_api
))
311 def _CheckSubversionConfig(input_api
, output_api
):
312 """Verifies the subversion config file is correctly setup.
314 Checks that autoprops are enabled, returns an error otherwise.
316 join
= input_api
.os_path
.join
317 if input_api
.platform
== 'win32':
318 appdata
= input_api
.environ
.get('APPDATA', '')
320 return [output_api
.PresubmitError('%APPDATA% is not configured.')]
321 path
= join(appdata
, 'Subversion', 'config')
323 home
= input_api
.environ
.get('HOME', '')
325 return [output_api
.PresubmitError('$HOME is not configured.')]
326 path
= join(home
, '.subversion', 'config')
329 'Please look at http://dev.chromium.org/developers/coding-style to\n'
330 'configure your subversion configuration file. This enables automatic\n'
331 'properties to simplify the project maintenance.\n'
332 'Pro-tip: just download and install\n'
333 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
336 lines
= open(path
, 'r').read().splitlines()
337 # Make sure auto-props is enabled and check for 2 Chromium standard
339 if (not '*.cc = svn:eol-style=LF' in lines
or
340 not '*.pdf = svn:mime-type=application/pdf' in lines
or
341 not 'enable-auto-props = yes' in lines
):
343 output_api
.PresubmitNotifyResult(
344 'It looks like you have not configured your subversion config '
345 'file or it is not up-to-date.\n' + error_msg
)
347 except (OSError, IOError):
349 output_api
.PresubmitNotifyResult(
350 'Can\'t find your subversion config file.\n' + error_msg
)
355 def _CheckAuthorizedAuthor(input_api
, output_api
):
356 """For non-googler/chromites committers, verify the author's email address is
359 # TODO(maruel): Add it to input_api?
362 author
= input_api
.change
.author_email
364 input_api
.logging
.info('No author, skipping AUTHOR check')
366 authors_path
= input_api
.os_path
.join(
367 input_api
.PresubmitLocalPath(), 'AUTHORS')
369 input_api
.re
.match(r
'[^#]+\s+\<(.+?)\>\s*$', line
)
370 for line
in open(authors_path
))
371 valid_authors
= [item
.group(1).lower() for item
in valid_authors
if item
]
372 if input_api
.verbose
:
373 print 'Valid authors are %s' % ', '.join(valid_authors
)
374 if not any(fnmatch
.fnmatch(author
.lower(), valid
) for valid
in valid_authors
):
375 return [output_api
.PresubmitPromptWarning(
376 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
378 'http://www.chromium.org/developers/contributing-code and read the '
380 'If you are a chromite, verify the contributor signed the CLA.') %
385 def CheckChangeOnUpload(input_api
, output_api
):
387 results
.extend(_CommonChecks(input_api
, output_api
))
391 def CheckChangeOnCommit(input_api
, output_api
):
393 results
.extend(_CommonChecks(input_api
, output_api
))
394 # TODO(thestig) temporarily disabled, doesn't work in third_party/
395 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
396 # input_api, output_api, sources))
397 # Make sure the tree is 'open'.
398 results
.extend(input_api
.canned_checks
.CheckTreeIsOpen(
401 json_url
='http://chromium-status.appspot.com/current?format=json'))
402 results
.extend(input_api
.canned_checks
.CheckRietveldTryJobExecution(input_api
,
403 output_api
, 'http://codereview.chromium.org',
404 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
405 'tryserver@chromium.org'))
407 results
.extend(input_api
.canned_checks
.CheckChangeHasBugField(
408 input_api
, output_api
))
409 results
.extend(input_api
.canned_checks
.CheckChangeHasTestField(
410 input_api
, output_api
))
411 results
.extend(input_api
.canned_checks
.CheckChangeHasDescription(
412 input_api
, output_api
))
413 results
.extend(_CheckSubversionConfig(input_api
, output_api
))
417 def GetPreferredTrySlaves(project
, change
):
418 affected_files
= change
.LocalPaths()
419 only_objc_files
= all(f
.endswith(('.mm', '.m')) for f
in affected_files
)
422 preferred
= ['win_rel', 'linux_rel', 'mac_rel', 'linux_clang:compile']
423 aura_re
= '_aura[^/]*[.][^/]*'
424 if any(re
.search(aura_re
, f
) for f
in affected_files
):
425 preferred
.append('linux_chromeos')
427 android_re_list
= ('^base/',
428 '^build/common.gypi$',
435 # Nothing that looks like win-only or aura-only
436 win_re
= '_win\.(cc|h)$'
437 possibly_android
= True
438 for non_android_re
in (aura_re
, win_re
):
439 if all(re
.search(non_android_re
, f
) for f
in affected_files
):
440 possibly_android
= False
443 for f
in change
.AffectedFiles():
444 if any(re
.search(r
, f
.LocalPath()) for r
in android_re_list
):
445 preferred
.append('android')