Bluetooth: remove BluetoothAdapter::IsScanning
[chromium-blink-merge.git] / PRESUBMIT.py
blobb304285365847e5497af6a192e26eea8ba11bfb9
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.
9 """
12 import re
13 import subprocess
14 import sys
17 _EXCLUDED_PATHS = (
18 r"^breakpad[\\\/].*",
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[\\\/].*",
23 r"^skia[\\\/].*",
24 r"^v8[\\\/].*",
25 r".*MakeFile$",
26 r".+_autogen\.h$",
27 r".+[\\\/]pnacl_shim\.c$",
30 # Fragment of a regular expression that matches file name suffixes
31 # used to indicate different platforms.
32 _PLATFORM_SPECIFIERS = r'(_(android|chromeos|gtk|mac|posix|win))?'
34 # Fragment of a regular expression that matches C++ and Objective-C++
35 # implementation files.
36 _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
38 # Regular expression that matches code only used for test binaries
39 # (best effort).
40 _TEST_CODE_EXCLUDED_PATHS = (
41 r'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
42 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
43 r'.+_(api|browser|perf|unit|ui)?test%s%s' % (_PLATFORM_SPECIFIERS,
44 _IMPLEMENTATION_EXTENSIONS),
45 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
46 r'.*[/\\](test|tool(s)?)[/\\].*',
47 # At request of folks maintaining this folder.
48 r'chrome[/\\]browser[/\\]automation[/\\].*',
51 _TEST_ONLY_WARNING = (
52 'You might be calling functions intended only for testing from\n'
53 'production code. It is OK to ignore this warning if you know what\n'
54 'you are doing, as the heuristics used to detect the situation are\n'
55 'not perfect. The commit queue will not block on this warning.\n'
56 'Email joi@chromium.org if you have questions.')
59 _INCLUDE_ORDER_WARNING = (
60 'Your #include order seems to be broken. Send mail to\n'
61 'marja@chromium.org if this is not the case.')
64 _BANNED_OBJC_FUNCTIONS = (
66 'addTrackingRect:',
68 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
69 'prohibited. Please use CrTrackingArea instead.',
70 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
72 False,
75 'NSTrackingArea',
77 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
78 'instead.',
79 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
81 False,
84 'convertPointFromBase:',
86 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
87 'Please use |convertPoint:(point) fromView:nil| instead.',
88 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
90 True,
93 'convertPointToBase:',
95 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
96 'Please use |convertPoint:(point) toView:nil| instead.',
97 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
99 True,
102 'convertRectFromBase:',
104 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
105 'Please use |convertRect:(point) fromView:nil| instead.',
106 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
108 True,
111 'convertRectToBase:',
113 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
114 'Please use |convertRect:(point) toView:nil| instead.',
115 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
117 True,
120 'convertSizeFromBase:',
122 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
123 'Please use |convertSize:(point) fromView:nil| instead.',
124 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
126 True,
129 'convertSizeToBase:',
131 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
132 'Please use |convertSize:(point) toView:nil| instead.',
133 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
135 True,
140 _BANNED_CPP_FUNCTIONS = (
141 # Make sure that gtest's FRIEND_TEST() macro is not used; the
142 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
143 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
145 'FRIEND_TEST(',
147 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
148 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
150 False,
154 'ScopedAllowIO',
156 'New code should not use ScopedAllowIO. Post a task to the blocking',
157 'pool or the FILE thread instead.',
159 True,
161 r"^content[\\\/]shell[\\\/]shell_browser_main\.cc$",
167 _VALID_OS_MACROS = (
168 # Please keep sorted.
169 'OS_ANDROID',
170 'OS_BSD',
171 'OS_CAT', # For testing.
172 'OS_CHROMEOS',
173 'OS_FREEBSD',
174 'OS_IOS',
175 'OS_LINUX',
176 'OS_MACOSX',
177 'OS_NACL',
178 'OS_OPENBSD',
179 'OS_POSIX',
180 'OS_SOLARIS',
181 'OS_SUN', # Not in build/build_config.h but in skia.
182 'OS_WIN',
186 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
187 """Attempts to prevent use of functions intended only for testing in
188 non-testing code. For now this is just a best-effort implementation
189 that ignores header files and may have some false positives. A
190 better implementation would probably need a proper C++ parser.
192 # We only scan .cc files and the like, as the declaration of
193 # for-testing functions in header files are hard to distinguish from
194 # calls to such functions without a proper C++ parser.
195 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
197 base_function_pattern = r'ForTest(ing)?|for_test(ing)?'
198 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
199 exclusion_pattern = input_api.re.compile(
200 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
201 base_function_pattern, base_function_pattern))
203 def FilterFile(affected_file):
204 black_list = (_EXCLUDED_PATHS +
205 _TEST_CODE_EXCLUDED_PATHS +
206 input_api.DEFAULT_BLACK_LIST)
207 return input_api.FilterSourceFile(
208 affected_file,
209 white_list=(file_inclusion_pattern, ),
210 black_list=black_list)
212 problems = []
213 for f in input_api.AffectedSourceFiles(FilterFile):
214 local_path = f.LocalPath()
215 lines = input_api.ReadFile(f).splitlines()
216 line_number = 0
217 for line in lines:
218 if (inclusion_pattern.search(line) and
219 not exclusion_pattern.search(line)):
220 problems.append(
221 '%s:%d\n %s' % (local_path, line_number, line.strip()))
222 line_number += 1
224 if problems:
225 if not input_api.is_committing:
226 return [output_api.PresubmitPromptWarning(_TEST_ONLY_WARNING, problems)]
227 else:
228 # We don't warn on commit, to avoid stopping commits going through CQ.
229 return [output_api.PresubmitNotifyResult(_TEST_ONLY_WARNING, problems)]
230 else:
231 return []
234 def _CheckNoIOStreamInHeaders(input_api, output_api):
235 """Checks to make sure no .h files include <iostream>."""
236 files = []
237 pattern = input_api.re.compile(r'^#include\s*<iostream>',
238 input_api.re.MULTILINE)
239 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
240 if not f.LocalPath().endswith('.h'):
241 continue
242 contents = input_api.ReadFile(f)
243 if pattern.search(contents):
244 files.append(f)
246 if len(files):
247 return [ output_api.PresubmitError(
248 'Do not #include <iostream> in header files, since it inserts static '
249 'initialization into every file including the header. Instead, '
250 '#include <ostream>. See http://crbug.com/94794',
251 files) ]
252 return []
255 def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
256 """Checks to make sure no source files use UNIT_TEST"""
257 problems = []
258 for f in input_api.AffectedFiles():
259 if (not f.LocalPath().endswith(('.cc', '.mm'))):
260 continue
262 for line_num, line in f.ChangedContents():
263 if 'UNIT_TEST' in line:
264 problems.append(' %s:%d' % (f.LocalPath(), line_num))
266 if not problems:
267 return []
268 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
269 '\n'.join(problems))]
272 def _CheckNoNewWStrings(input_api, output_api):
273 """Checks to make sure we don't introduce use of wstrings."""
274 problems = []
275 for f in input_api.AffectedFiles():
276 if (not f.LocalPath().endswith(('.cc', '.h')) or
277 f.LocalPath().endswith('test.cc')):
278 continue
280 allowWString = False
281 for line_num, line in f.ChangedContents():
282 if 'presubmit: allow wstring' in line:
283 allowWString = True
284 elif not allowWString and 'wstring' in line:
285 problems.append(' %s:%d' % (f.LocalPath(), line_num))
286 allowWString = False
287 else:
288 allowWString = False
290 if not problems:
291 return []
292 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
293 ' If you are calling a cross-platform API that accepts a wstring, '
294 'fix the API.\n' +
295 '\n'.join(problems))]
298 def _CheckNoDEPSGIT(input_api, output_api):
299 """Make sure .DEPS.git is never modified manually."""
300 if any(f.LocalPath().endswith('.DEPS.git') for f in
301 input_api.AffectedFiles()):
302 return [output_api.PresubmitError(
303 'Never commit changes to .DEPS.git. This file is maintained by an\n'
304 'automated system based on what\'s in DEPS and your changes will be\n'
305 'overwritten.\n'
306 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
307 'for more information')]
308 return []
311 def _CheckNoBannedFunctions(input_api, output_api):
312 """Make sure that banned functions are not used."""
313 warnings = []
314 errors = []
316 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
317 for f in input_api.AffectedFiles(file_filter=file_filter):
318 for line_num, line in f.ChangedContents():
319 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
320 if func_name in line:
321 problems = warnings;
322 if error:
323 problems = errors;
324 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
325 for message_line in message:
326 problems.append(' %s' % message_line)
328 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
329 for f in input_api.AffectedFiles(file_filter=file_filter):
330 for line_num, line in f.ChangedContents():
331 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
332 def IsBlacklisted(affected_file, blacklist):
333 local_path = affected_file.LocalPath()
334 for item in blacklist:
335 if input_api.re.match(item, local_path):
336 return True
337 return False
338 if IsBlacklisted(f, excluded_paths):
339 continue
340 if func_name in line:
341 problems = warnings;
342 if error:
343 problems = errors;
344 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
345 for message_line in message:
346 problems.append(' %s' % message_line)
348 result = []
349 if (warnings):
350 result.append(output_api.PresubmitPromptWarning(
351 'Banned functions were used.\n' + '\n'.join(warnings)))
352 if (errors):
353 result.append(output_api.PresubmitError(
354 'Banned functions were used.\n' + '\n'.join(errors)))
355 return result
358 def _CheckNoPragmaOnce(input_api, output_api):
359 """Make sure that banned functions are not used."""
360 files = []
361 pattern = input_api.re.compile(r'^#pragma\s+once',
362 input_api.re.MULTILINE)
363 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
364 if not f.LocalPath().endswith('.h'):
365 continue
366 contents = input_api.ReadFile(f)
367 if pattern.search(contents):
368 files.append(f)
370 if files:
371 return [output_api.PresubmitError(
372 'Do not use #pragma once in header files.\n'
373 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
374 files)]
375 return []
378 def _CheckNoTrinaryTrueFalse(input_api, output_api):
379 """Checks to make sure we don't introduce use of foo ? true : false."""
380 problems = []
381 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
382 for f in input_api.AffectedFiles():
383 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
384 continue
386 for line_num, line in f.ChangedContents():
387 if pattern.match(line):
388 problems.append(' %s:%d' % (f.LocalPath(), line_num))
390 if not problems:
391 return []
392 return [output_api.PresubmitPromptWarning(
393 'Please consider avoiding the "? true : false" pattern if possible.\n' +
394 '\n'.join(problems))]
397 def _CheckUnwantedDependencies(input_api, output_api):
398 """Runs checkdeps on #include statements added in this
399 change. Breaking - rules is an error, breaking ! rules is a
400 warning.
402 # We need to wait until we have an input_api object and use this
403 # roundabout construct to import checkdeps because this file is
404 # eval-ed and thus doesn't have __file__.
405 original_sys_path = sys.path
406 try:
407 sys.path = sys.path + [input_api.os_path.join(
408 input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
409 import checkdeps
410 from cpp_checker import CppChecker
411 from rules import Rule
412 finally:
413 # Restore sys.path to what it was before.
414 sys.path = original_sys_path
416 added_includes = []
417 for f in input_api.AffectedFiles():
418 if not CppChecker.IsCppFile(f.LocalPath()):
419 continue
421 changed_lines = [line for line_num, line in f.ChangedContents()]
422 added_includes.append([f.LocalPath(), changed_lines])
424 deps_checker = checkdeps.DepsChecker()
426 error_descriptions = []
427 warning_descriptions = []
428 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
429 added_includes):
430 description_with_path = '%s\n %s' % (path, rule_description)
431 if rule_type == Rule.DISALLOW:
432 error_descriptions.append(description_with_path)
433 else:
434 warning_descriptions.append(description_with_path)
436 results = []
437 if error_descriptions:
438 results.append(output_api.PresubmitError(
439 'You added one or more #includes that violate checkdeps rules.',
440 error_descriptions))
441 if warning_descriptions:
442 if not input_api.is_committing:
443 warning_factory = output_api.PresubmitPromptWarning
444 else:
445 # We don't want to block use of the CQ when there is a warning
446 # of this kind, so we only show a message when committing.
447 warning_factory = output_api.PresubmitNotifyResult
448 results.append(warning_factory(
449 'You added one or more #includes of files that are temporarily\n'
450 'allowed but being removed. Can you avoid introducing the\n'
451 '#include? See relevant DEPS file(s) for details and contacts.',
452 warning_descriptions))
453 return results
456 def _CheckFilePermissions(input_api, output_api):
457 """Check that all files have their permissions properly set."""
458 args = [sys.executable, 'tools/checkperms/checkperms.py', '--root',
459 input_api.change.RepositoryRoot()]
460 for f in input_api.AffectedFiles():
461 args += ['--file', f.LocalPath()]
462 errors = []
463 (errors, stderrdata) = subprocess.Popen(args).communicate()
465 results = []
466 if errors:
467 results.append(output_api.PresubmitError('checkperms.py failed.',
468 errors))
469 return results
472 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
473 """Makes sure we don't include ui/aura/window_property.h
474 in header files.
476 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
477 errors = []
478 for f in input_api.AffectedFiles():
479 if not f.LocalPath().endswith('.h'):
480 continue
481 for line_num, line in f.ChangedContents():
482 if pattern.match(line):
483 errors.append(' %s:%d' % (f.LocalPath(), line_num))
485 results = []
486 if errors:
487 results.append(output_api.PresubmitError(
488 'Header files should not include ui/aura/window_property.h', errors))
489 return results
492 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
493 """Checks that the lines in scope occur in the right order.
495 1. C system files in alphabetical order
496 2. C++ system files in alphabetical order
497 3. Project's .h files
500 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
501 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
502 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
504 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
506 state = C_SYSTEM_INCLUDES
508 previous_line = ''
509 previous_line_num = 0
510 problem_linenums = []
511 for line_num, line in scope:
512 if c_system_include_pattern.match(line):
513 if state != C_SYSTEM_INCLUDES:
514 problem_linenums.append((line_num, previous_line_num))
515 elif previous_line and previous_line > line:
516 problem_linenums.append((line_num, previous_line_num))
517 elif cpp_system_include_pattern.match(line):
518 if state == C_SYSTEM_INCLUDES:
519 state = CPP_SYSTEM_INCLUDES
520 elif state == CUSTOM_INCLUDES:
521 problem_linenums.append((line_num, previous_line_num))
522 elif previous_line and previous_line > line:
523 problem_linenums.append((line_num, previous_line_num))
524 elif custom_include_pattern.match(line):
525 if state != CUSTOM_INCLUDES:
526 state = CUSTOM_INCLUDES
527 elif previous_line and previous_line > line:
528 problem_linenums.append((line_num, previous_line_num))
529 else:
530 problem_linenums.append(line_num)
531 previous_line = line
532 previous_line_num = line_num
534 warnings = []
535 for (line_num, previous_line_num) in problem_linenums:
536 if line_num in changed_linenums or previous_line_num in changed_linenums:
537 warnings.append(' %s:%d' % (file_path, line_num))
538 return warnings
541 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
542 """Checks the #include order for the given file f."""
544 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
545 # Exclude #include <.../...> includes from the check; e.g., <sys/...> includes
546 # often need to appear in a specific order.
547 excluded_include_pattern = input_api.re.compile(r'\s*#include \<.*/.*')
548 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
549 if_pattern = input_api.re.compile(
550 r'\s*#\s*(if|elif|else|endif|define|undef).*')
551 # Some files need specialized order of includes; exclude such files from this
552 # check.
553 uncheckable_includes_pattern = input_api.re.compile(
554 r'\s*#include '
555 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
557 contents = f.NewContents()
558 warnings = []
559 line_num = 0
561 # Handle the special first include. If the first include file is
562 # some/path/file.h, the corresponding including file can be some/path/file.cc,
563 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
564 # etc. It's also possible that no special first include exists.
565 for line in contents:
566 line_num += 1
567 if system_include_pattern.match(line):
568 # No special first include -> process the line again along with normal
569 # includes.
570 line_num -= 1
571 break
572 match = custom_include_pattern.match(line)
573 if match:
574 match_dict = match.groupdict()
575 header_basename = input_api.os_path.basename(
576 match_dict['FILE']).replace('.h', '')
577 if header_basename not in input_api.os_path.basename(f.LocalPath()):
578 # No special first include -> process the line again along with normal
579 # includes.
580 line_num -= 1
581 break
583 # Split into scopes: Each region between #if and #endif is its own scope.
584 scopes = []
585 current_scope = []
586 for line in contents[line_num:]:
587 line_num += 1
588 if uncheckable_includes_pattern.match(line):
589 return []
590 if if_pattern.match(line):
591 scopes.append(current_scope)
592 current_scope = []
593 elif ((system_include_pattern.match(line) or
594 custom_include_pattern.match(line)) and
595 not excluded_include_pattern.match(line)):
596 current_scope.append((line_num, line))
597 scopes.append(current_scope)
599 for scope in scopes:
600 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
601 changed_linenums))
602 return warnings
605 def _CheckIncludeOrder(input_api, output_api):
606 """Checks that the #include order is correct.
608 1. The corresponding header for source files.
609 2. C system files in alphabetical order
610 3. C++ system files in alphabetical order
611 4. Project's .h files in alphabetical order
613 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
614 these rules separately.
617 warnings = []
618 for f in input_api.AffectedFiles():
619 if f.LocalPath().endswith(('.cc', '.h')):
620 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
621 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
623 results = []
624 if warnings:
625 if not input_api.is_committing:
626 results.append(output_api.PresubmitPromptWarning(_INCLUDE_ORDER_WARNING,
627 warnings))
628 else:
629 # We don't warn on commit, to avoid stopping commits going through CQ.
630 results.append(output_api.PresubmitNotifyResult(_INCLUDE_ORDER_WARNING,
631 warnings))
632 return results
635 def _CheckForVersionControlConflictsInFile(input_api, f):
636 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
637 errors = []
638 for line_num, line in f.ChangedContents():
639 if pattern.match(line):
640 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
641 return errors
644 def _CheckForVersionControlConflicts(input_api, output_api):
645 """Usually this is not intentional and will cause a compile failure."""
646 errors = []
647 for f in input_api.AffectedFiles():
648 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
650 results = []
651 if errors:
652 results.append(output_api.PresubmitError(
653 'Version control conflict markers found, please resolve.', errors))
654 return results
657 def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
658 def FilterFile(affected_file):
659 """Filter function for use with input_api.AffectedSourceFiles,
660 below. This filters out everything except non-test files from
661 top-level directories that generally speaking should not hard-code
662 service URLs (e.g. src/android_webview/, src/content/ and others).
664 return input_api.FilterSourceFile(
665 affected_file,
666 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
667 black_list=(_EXCLUDED_PATHS +
668 _TEST_CODE_EXCLUDED_PATHS +
669 input_api.DEFAULT_BLACK_LIST))
671 pattern = input_api.re.compile('"[^"]*google\.com[^"]*"')
672 problems = [] # items are (filename, line_number, line)
673 for f in input_api.AffectedSourceFiles(FilterFile):
674 for line_num, line in f.ChangedContents():
675 if pattern.search(line):
676 problems.append((f.LocalPath(), line_num, line))
678 if problems:
679 if not input_api.is_committing:
680 warning_factory = output_api.PresubmitPromptWarning
681 else:
682 # We don't want to block use of the CQ when there is a warning
683 # of this kind, so we only show a message when committing.
684 warning_factory = output_api.PresubmitNotifyResult
685 return [warning_factory(
686 'Most layers below src/chrome/ should not hardcode service URLs.\n'
687 'Are you sure this is correct? (Contact: joi@chromium.org)',
688 [' %s:%d: %s' % (
689 problem[0], problem[1], problem[2]) for problem in problems])]
690 else:
691 return []
694 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
695 """Makes sure there are no abbreviations in the name of PNG files.
697 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
698 errors = []
699 for f in input_api.AffectedFiles(include_deletes=False):
700 if pattern.match(f.LocalPath()):
701 errors.append(' %s' % f.LocalPath())
703 results = []
704 if errors:
705 results.append(output_api.PresubmitError(
706 'The name of PNG files should not have abbreviations. \n'
707 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
708 'Contact oshima@chromium.org if you have questions.', errors))
709 return results
712 def _CommonChecks(input_api, output_api):
713 """Checks common to both upload and commit."""
714 results = []
715 results.extend(input_api.canned_checks.PanProjectChecks(
716 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
717 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
718 results.extend(
719 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
720 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
721 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
722 results.extend(_CheckNoNewWStrings(input_api, output_api))
723 results.extend(_CheckNoDEPSGIT(input_api, output_api))
724 results.extend(_CheckNoBannedFunctions(input_api, output_api))
725 results.extend(_CheckNoPragmaOnce(input_api, output_api))
726 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
727 results.extend(_CheckUnwantedDependencies(input_api, output_api))
728 results.extend(_CheckFilePermissions(input_api, output_api))
729 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
730 results.extend(_CheckIncludeOrder(input_api, output_api))
731 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
732 results.extend(_CheckPatchFiles(input_api, output_api))
733 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
734 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
735 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
737 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
738 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
739 input_api, output_api,
740 input_api.PresubmitLocalPath(),
741 whitelist=[r'^PRESUBMIT_test\.py$']))
742 return results
745 def _CheckSubversionConfig(input_api, output_api):
746 """Verifies the subversion config file is correctly setup.
748 Checks that autoprops are enabled, returns an error otherwise.
750 join = input_api.os_path.join
751 if input_api.platform == 'win32':
752 appdata = input_api.environ.get('APPDATA', '')
753 if not appdata:
754 return [output_api.PresubmitError('%APPDATA% is not configured.')]
755 path = join(appdata, 'Subversion', 'config')
756 else:
757 home = input_api.environ.get('HOME', '')
758 if not home:
759 return [output_api.PresubmitError('$HOME is not configured.')]
760 path = join(home, '.subversion', 'config')
762 error_msg = (
763 'Please look at http://dev.chromium.org/developers/coding-style to\n'
764 'configure your subversion configuration file. This enables automatic\n'
765 'properties to simplify the project maintenance.\n'
766 'Pro-tip: just download and install\n'
767 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
769 try:
770 lines = open(path, 'r').read().splitlines()
771 # Make sure auto-props is enabled and check for 2 Chromium standard
772 # auto-prop.
773 if (not '*.cc = svn:eol-style=LF' in lines or
774 not '*.pdf = svn:mime-type=application/pdf' in lines or
775 not 'enable-auto-props = yes' in lines):
776 return [
777 output_api.PresubmitNotifyResult(
778 'It looks like you have not configured your subversion config '
779 'file or it is not up-to-date.\n' + error_msg)
781 except (OSError, IOError):
782 return [
783 output_api.PresubmitNotifyResult(
784 'Can\'t find your subversion config file.\n' + error_msg)
786 return []
789 def _CheckAuthorizedAuthor(input_api, output_api):
790 """For non-googler/chromites committers, verify the author's email address is
791 in AUTHORS.
793 # TODO(maruel): Add it to input_api?
794 import fnmatch
796 author = input_api.change.author_email
797 if not author:
798 input_api.logging.info('No author, skipping AUTHOR check')
799 return []
800 authors_path = input_api.os_path.join(
801 input_api.PresubmitLocalPath(), 'AUTHORS')
802 valid_authors = (
803 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
804 for line in open(authors_path))
805 valid_authors = [item.group(1).lower() for item in valid_authors if item]
806 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
807 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
808 return [output_api.PresubmitPromptWarning(
809 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
810 '\n'
811 'http://www.chromium.org/developers/contributing-code and read the '
812 '"Legal" section\n'
813 'If you are a chromite, verify the contributor signed the CLA.') %
814 author)]
815 return []
818 def _CheckPatchFiles(input_api, output_api):
819 problems = [f.LocalPath() for f in input_api.AffectedFiles()
820 if f.LocalPath().endswith(('.orig', '.rej'))]
821 if problems:
822 return [output_api.PresubmitError(
823 "Don't commit .rej and .orig files.", problems)]
824 else:
825 return []
828 def _DidYouMeanOSMacro(bad_macro):
829 try:
830 return {'A': 'OS_ANDROID',
831 'B': 'OS_BSD',
832 'C': 'OS_CHROMEOS',
833 'F': 'OS_FREEBSD',
834 'L': 'OS_LINUX',
835 'M': 'OS_MACOSX',
836 'N': 'OS_NACL',
837 'O': 'OS_OPENBSD',
838 'P': 'OS_POSIX',
839 'S': 'OS_SOLARIS',
840 'W': 'OS_WIN'}[bad_macro[3].upper()]
841 except KeyError:
842 return ''
845 def _CheckForInvalidOSMacrosInFile(input_api, f):
846 """Check for sensible looking, totally invalid OS macros."""
847 preprocessor_statement = input_api.re.compile(r'^\s*#')
848 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
849 results = []
850 for lnum, line in f.ChangedContents():
851 if preprocessor_statement.search(line):
852 for match in os_macro.finditer(line):
853 if not match.group(1) in _VALID_OS_MACROS:
854 good = _DidYouMeanOSMacro(match.group(1))
855 did_you_mean = ' (did you mean %s?)' % good if good else ''
856 results.append(' %s:%d %s%s' % (f.LocalPath(),
857 lnum,
858 match.group(1),
859 did_you_mean))
860 return results
863 def _CheckForInvalidOSMacros(input_api, output_api):
864 """Check all affected files for invalid OS macros."""
865 bad_macros = []
866 for f in input_api.AffectedFiles():
867 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
868 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
870 if not bad_macros:
871 return []
873 return [output_api.PresubmitError(
874 'Possibly invalid OS macro[s] found. Please fix your code\n'
875 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
878 def CheckChangeOnUpload(input_api, output_api):
879 results = []
880 results.extend(_CommonChecks(input_api, output_api))
881 return results
884 def CheckChangeOnCommit(input_api, output_api):
885 results = []
886 results.extend(_CommonChecks(input_api, output_api))
887 # TODO(thestig) temporarily disabled, doesn't work in third_party/
888 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
889 # input_api, output_api, sources))
890 # Make sure the tree is 'open'.
891 results.extend(input_api.canned_checks.CheckTreeIsOpen(
892 input_api,
893 output_api,
894 json_url='http://chromium-status.appspot.com/current?format=json'))
895 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
896 output_api, 'http://codereview.chromium.org',
897 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
898 'tryserver@chromium.org'))
900 results.extend(input_api.canned_checks.CheckChangeHasBugField(
901 input_api, output_api))
902 results.extend(input_api.canned_checks.CheckChangeHasDescription(
903 input_api, output_api))
904 results.extend(_CheckSubversionConfig(input_api, output_api))
905 return results
908 def GetPreferredTrySlaves(project, change):
909 files = change.LocalPaths()
911 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
912 return []
914 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
915 return ['mac_rel', 'mac_asan', 'mac:compile']
916 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
917 return ['win_rel', 'win7_aura', 'win:compile']
918 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
919 return ['android_dbg', 'android_clang_dbg']
920 if all(re.search('^native_client_sdk', f) for f in files):
921 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
922 if all(re.search('[/_]ios[/_.]', f) for f in files):
923 return ['ios_rel_device', 'ios_dbg_simulator']
925 trybots = [
926 'android_clang_dbg',
927 'android_dbg',
928 'ios_dbg_simulator',
929 'ios_rel_device',
930 'linux_asan',
931 'linux_aura',
932 'linux_chromeos',
933 'linux_clang:compile',
934 'linux_rel',
935 'mac_asan',
936 'mac_rel',
937 'mac:compile',
938 'win7_aura',
939 'win_rel',
940 'win:compile',
943 # Match things like path/aura/file.cc and path/file_aura.cc.
944 # Same for chromeos.
945 if any(re.search('[/_](aura|chromeos)', f) for f in files):
946 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
948 return trybots