Sanity check base::Time::Now() when recording data reduction stats
[chromium-blink-merge.git] / PRESUBMIT.py
blobcefb599b18b102331a2fecd0b15cfd30335c2417
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$",
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
36 # (best effort).
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 = (
65 'addTrackingRect:',
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',
71 False,
74 'NSTrackingArea',
76 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
77 'instead.',
78 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
80 False,
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',
89 True,
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',
98 True,
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',
107 True,
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',
116 True,
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',
125 True,
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',
134 True,
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.
144 'FRIEND_TEST(',
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.',
149 False,
153 'ScopedAllowIO',
155 'New code should not use ScopedAllowIO. Post a task to the blocking',
156 'pool or the FILE thread instead.',
158 True,
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$",
166 'SkRefPtr',
168 'The use of SkRefPtr is prohibited. ',
169 'Please use skia::RefPtr instead.'
171 True,
175 'SkAutoRef',
177 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
178 'Please use skia::RefPtr instead.'
180 True,
184 'SkAutoTUnref',
186 'The use of SkAutoTUnref is dangerous because it implicitly ',
187 'converts to a raw pointer. Please use skia::RefPtr instead.'
189 True,
193 'SkAutoUnref',
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.'
199 True,
205 _VALID_OS_MACROS = (
206 # Please keep sorted.
207 'OS_ANDROID',
208 'OS_BSD',
209 'OS_CAT', # For testing.
210 'OS_CHROMEOS',
211 'OS_FREEBSD',
212 'OS_IOS',
213 'OS_LINUX',
214 'OS_MACOSX',
215 'OS_NACL',
216 'OS_OPENBSD',
217 'OS_POSIX',
218 'OS_SOLARIS',
219 'OS_WIN',
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(
246 affected_file,
247 white_list=(file_inclusion_pattern, ),
248 black_list=black_list)
250 problems = []
251 for f in input_api.AffectedSourceFiles(FilterFile):
252 local_path = f.LocalPath()
253 lines = input_api.ReadFile(f).splitlines()
254 line_number = 0
255 for line in lines:
256 if (inclusion_pattern.search(line) and
257 not comment_pattern.search(line) and
258 not exclusion_pattern.search(line)):
259 problems.append(
260 '%s:%d\n %s' % (local_path, line_number, line.strip()))
261 line_number += 1
263 if problems:
264 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
265 else:
266 return []
269 def _CheckNoIOStreamInHeaders(input_api, output_api):
270 """Checks to make sure no .h files include <iostream>."""
271 files = []
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'):
276 continue
277 contents = input_api.ReadFile(f)
278 if pattern.search(contents):
279 files.append(f)
281 if len(files):
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',
286 files) ]
287 return []
290 def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
291 """Checks to make sure no source files use UNIT_TEST"""
292 problems = []
293 for f in input_api.AffectedFiles():
294 if (not f.LocalPath().endswith(('.cc', '.mm'))):
295 continue
297 for line_num, line in f.ChangedContents():
298 if 'UNIT_TEST' in line:
299 problems.append(' %s:%d' % (f.LocalPath(), line_num))
301 if not problems:
302 return []
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."""
309 problems = []
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'))):
313 continue
315 allowWString = False
316 for line_num, line in f.ChangedContents():
317 if 'presubmit: allow wstring' in line:
318 allowWString = True
319 elif not allowWString and 'wstring' in line:
320 problems.append(' %s:%d' % (f.LocalPath(), line_num))
321 allowWString = False
322 else:
323 allowWString = False
325 if not problems:
326 return []
327 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
328 ' If you are calling a cross-platform API that accepts a wstring, '
329 'fix the API.\n' +
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'
340 'overwritten.\n'
341 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
342 'for more information')]
343 return []
346 def _CheckNoBannedFunctions(input_api, output_api):
347 """Make sure that banned functions are not used."""
348 warnings = []
349 errors = []
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:
356 problems = warnings;
357 if error:
358 problems = errors;
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):
371 return True
372 return False
373 if IsBlacklisted(f, excluded_paths):
374 continue
375 if func_name in line:
376 problems = warnings;
377 if error:
378 problems = errors;
379 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
380 for message_line in message:
381 problems.append(' %s' % message_line)
383 result = []
384 if (warnings):
385 result.append(output_api.PresubmitPromptWarning(
386 'Banned functions were used.\n' + '\n'.join(warnings)))
387 if (errors):
388 result.append(output_api.PresubmitError(
389 'Banned functions were used.\n' + '\n'.join(errors)))
390 return result
393 def _CheckNoPragmaOnce(input_api, output_api):
394 """Make sure that banned functions are not used."""
395 files = []
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'):
400 continue
401 contents = input_api.ReadFile(f)
402 if pattern.search(contents):
403 files.append(f)
405 if files:
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',
409 files)]
410 return []
413 def _CheckNoTrinaryTrueFalse(input_api, output_api):
414 """Checks to make sure we don't introduce use of foo ? true : false."""
415 problems = []
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')):
419 continue
421 for line_num, line in f.ChangedContents():
422 if pattern.match(line):
423 problems.append(' %s:%d' % (f.LocalPath(), line_num))
425 if not problems:
426 return []
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
435 warning.
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
441 try:
442 sys.path = sys.path + [input_api.os_path.join(
443 input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
444 import checkdeps
445 from cpp_checker import CppChecker
446 from rules import Rule
447 finally:
448 # Restore sys.path to what it was before.
449 sys.path = original_sys_path
451 added_includes = []
452 for f in input_api.AffectedFiles():
453 if not CppChecker.IsCppFile(f.LocalPath()):
454 continue
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(
464 added_includes):
465 description_with_path = '%s\n %s' % (path, rule_description)
466 if rule_type == Rule.DISALLOW:
467 error_descriptions.append(description_with_path)
468 else:
469 warning_descriptions.append(description_with_path)
471 results = []
472 if error_descriptions:
473 results.append(output_api.PresubmitError(
474 'You added one or more #includes that violate checkdeps rules.',
475 error_descriptions))
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))
482 return results
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()]
491 errors = []
492 (errors, stderrdata) = subprocess.Popen(args).communicate()
494 results = []
495 if errors:
496 results.append(output_api.PresubmitError('checkperms.py failed.',
497 errors))
498 return results
501 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
502 """Makes sure we don't include ui/aura/window_property.h
503 in header files.
505 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
506 errors = []
507 for f in input_api.AffectedFiles():
508 if not f.LocalPath().endswith('.h'):
509 continue
510 for line_num, line in f.ChangedContents():
511 if pattern.match(line):
512 errors.append(' %s:%d' % (f.LocalPath(), line_num))
514 results = []
515 if errors:
516 results.append(output_api.PresubmitError(
517 'Header files should not include ui/aura/window_property.h', errors))
518 return results
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
537 previous_line = ''
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))
558 else:
559 problem_linenums.append(line_num)
560 previous_line = line
561 previous_line_num = line_num
563 warnings = []
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))
567 return warnings
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 #include <.../...> includes from the check; e.g., <sys/...> includes
575 # often need to appear in a specific order.
576 excluded_include_pattern = input_api.re.compile(r'\s*#include \<.*/.*')
577 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
578 if_pattern = input_api.re.compile(
579 r'\s*#\s*(if|elif|else|endif|define|undef).*')
580 # Some files need specialized order of includes; exclude such files from this
581 # check.
582 uncheckable_includes_pattern = input_api.re.compile(
583 r'\s*#include '
584 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
586 contents = f.NewContents()
587 warnings = []
588 line_num = 0
590 # Handle the special first include. If the first include file is
591 # some/path/file.h, the corresponding including file can be some/path/file.cc,
592 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
593 # etc. It's also possible that no special first include exists.
594 for line in contents:
595 line_num += 1
596 if system_include_pattern.match(line):
597 # No special first include -> process the line again along with normal
598 # includes.
599 line_num -= 1
600 break
601 match = custom_include_pattern.match(line)
602 if match:
603 match_dict = match.groupdict()
604 header_basename = input_api.os_path.basename(
605 match_dict['FILE']).replace('.h', '')
606 if header_basename not in input_api.os_path.basename(f.LocalPath()):
607 # No special first include -> process the line again along with normal
608 # includes.
609 line_num -= 1
610 break
612 # Split into scopes: Each region between #if and #endif is its own scope.
613 scopes = []
614 current_scope = []
615 for line in contents[line_num:]:
616 line_num += 1
617 if uncheckable_includes_pattern.match(line):
618 return []
619 if if_pattern.match(line):
620 scopes.append(current_scope)
621 current_scope = []
622 elif ((system_include_pattern.match(line) or
623 custom_include_pattern.match(line)) and
624 not excluded_include_pattern.match(line)):
625 current_scope.append((line_num, line))
626 scopes.append(current_scope)
628 for scope in scopes:
629 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
630 changed_linenums))
631 return warnings
634 def _CheckIncludeOrder(input_api, output_api):
635 """Checks that the #include order is correct.
637 1. The corresponding header for source files.
638 2. C system files in alphabetical order
639 3. C++ system files in alphabetical order
640 4. Project's .h files in alphabetical order
642 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
643 these rules separately.
646 warnings = []
647 for f in input_api.AffectedFiles():
648 if f.LocalPath().endswith(('.cc', '.h')):
649 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
650 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
652 results = []
653 if warnings:
654 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
655 warnings))
656 return results
659 def _CheckForVersionControlConflictsInFile(input_api, f):
660 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
661 errors = []
662 for line_num, line in f.ChangedContents():
663 if pattern.match(line):
664 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
665 return errors
668 def _CheckForVersionControlConflicts(input_api, output_api):
669 """Usually this is not intentional and will cause a compile failure."""
670 errors = []
671 for f in input_api.AffectedFiles():
672 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
674 results = []
675 if errors:
676 results.append(output_api.PresubmitError(
677 'Version control conflict markers found, please resolve.', errors))
678 return results
681 def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
682 def FilterFile(affected_file):
683 """Filter function for use with input_api.AffectedSourceFiles,
684 below. This filters out everything except non-test files from
685 top-level directories that generally speaking should not hard-code
686 service URLs (e.g. src/android_webview/, src/content/ and others).
688 return input_api.FilterSourceFile(
689 affected_file,
690 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
691 black_list=(_EXCLUDED_PATHS +
692 _TEST_CODE_EXCLUDED_PATHS +
693 input_api.DEFAULT_BLACK_LIST))
695 base_pattern = '"[^"]*google\.com[^"]*"'
696 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
697 pattern = input_api.re.compile(base_pattern)
698 problems = [] # items are (filename, line_number, line)
699 for f in input_api.AffectedSourceFiles(FilterFile):
700 for line_num, line in f.ChangedContents():
701 if not comment_pattern.search(line) and pattern.search(line):
702 problems.append((f.LocalPath(), line_num, line))
704 if problems:
705 return [output_api.PresubmitPromptOrNotify(
706 'Most layers below src/chrome/ should not hardcode service URLs.\n'
707 'Are you sure this is correct? (Contact: joi@chromium.org)',
708 [' %s:%d: %s' % (
709 problem[0], problem[1], problem[2]) for problem in problems])]
710 else:
711 return []
714 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
715 """Makes sure there are no abbreviations in the name of PNG files.
717 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
718 errors = []
719 for f in input_api.AffectedFiles(include_deletes=False):
720 if pattern.match(f.LocalPath()):
721 errors.append(' %s' % f.LocalPath())
723 results = []
724 if errors:
725 results.append(output_api.PresubmitError(
726 'The name of PNG files should not have abbreviations. \n'
727 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
728 'Contact oshima@chromium.org if you have questions.', errors))
729 return results
732 def _DepsFilesToCheck(re, changed_lines):
733 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
734 a set of DEPS entries that we should look up."""
735 results = set()
737 # This pattern grabs the path without basename in the first
738 # parentheses, and the basename (if present) in the second. It
739 # relies on the simple heuristic that if there is a basename it will
740 # be a header file ending in ".h".
741 pattern = re.compile(
742 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
743 for changed_line in changed_lines:
744 m = pattern.match(changed_line)
745 if m:
746 path = m.group(1)
747 if not (path.startswith('grit/') or path == 'grit'):
748 results.add('%s/DEPS' % m.group(1))
749 return results
752 def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
753 """When a dependency prefixed with + is added to a DEPS file, we
754 want to make sure that the change is reviewed by an OWNER of the
755 target file or directory, to avoid layering violations from being
756 introduced. This check verifies that this happens.
758 changed_lines = set()
759 for f in input_api.AffectedFiles():
760 filename = input_api.os_path.basename(f.LocalPath())
761 if filename == 'DEPS':
762 changed_lines |= set(line.strip()
763 for line_num, line
764 in f.ChangedContents())
765 if not changed_lines:
766 return []
768 virtual_depended_on_files = _DepsFilesToCheck(input_api.re, changed_lines)
769 if not virtual_depended_on_files:
770 return []
772 if input_api.is_committing:
773 if input_api.tbr:
774 return [output_api.PresubmitNotifyResult(
775 '--tbr was specified, skipping OWNERS check for DEPS additions')]
776 if not input_api.change.issue:
777 return [output_api.PresubmitError(
778 "DEPS approval by OWNERS check failed: this change has "
779 "no Rietveld issue number, so we can't check it for approvals.")]
780 output = output_api.PresubmitError
781 else:
782 output = output_api.PresubmitNotifyResult
784 owners_db = input_api.owners_db
785 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
786 input_api,
787 owners_db.email_regexp,
788 approval_needed=input_api.is_committing)
790 owner_email = owner_email or input_api.change.author_email
792 reviewers_plus_owner = set(reviewers)
793 if owner_email:
794 reviewers_plus_owner.add(owner_email)
795 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
796 reviewers_plus_owner)
797 unapproved_dependencies = ["'+%s'," % path[:-len('/DEPS')]
798 for path in missing_files]
800 if unapproved_dependencies:
801 output_list = [
802 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
803 '\n '.join(sorted(unapproved_dependencies)))]
804 if not input_api.is_committing:
805 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
806 output_list.append(output(
807 'Suggested missing target path OWNERS:\n %s' %
808 '\n '.join(suggested_owners or [])))
809 return output_list
811 return []
814 def _CommonChecks(input_api, output_api):
815 """Checks common to both upload and commit."""
816 results = []
817 results.extend(input_api.canned_checks.PanProjectChecks(
818 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
819 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
820 results.extend(
821 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
822 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
823 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
824 results.extend(_CheckNoNewWStrings(input_api, output_api))
825 results.extend(_CheckNoDEPSGIT(input_api, output_api))
826 results.extend(_CheckNoBannedFunctions(input_api, output_api))
827 results.extend(_CheckNoPragmaOnce(input_api, output_api))
828 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
829 results.extend(_CheckUnwantedDependencies(input_api, output_api))
830 results.extend(_CheckFilePermissions(input_api, output_api))
831 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
832 results.extend(_CheckIncludeOrder(input_api, output_api))
833 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
834 results.extend(_CheckPatchFiles(input_api, output_api))
835 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
836 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
837 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
838 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
839 results.extend(
840 input_api.canned_checks.CheckChangeHasNoTabs(
841 input_api,
842 output_api,
843 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
845 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
846 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
847 input_api, output_api,
848 input_api.PresubmitLocalPath(),
849 whitelist=[r'^PRESUBMIT_test\.py$']))
850 return results
853 def _CheckSubversionConfig(input_api, output_api):
854 """Verifies the subversion config file is correctly setup.
856 Checks that autoprops are enabled, returns an error otherwise.
858 join = input_api.os_path.join
859 if input_api.platform == 'win32':
860 appdata = input_api.environ.get('APPDATA', '')
861 if not appdata:
862 return [output_api.PresubmitError('%APPDATA% is not configured.')]
863 path = join(appdata, 'Subversion', 'config')
864 else:
865 home = input_api.environ.get('HOME', '')
866 if not home:
867 return [output_api.PresubmitError('$HOME is not configured.')]
868 path = join(home, '.subversion', 'config')
870 error_msg = (
871 'Please look at http://dev.chromium.org/developers/coding-style to\n'
872 'configure your subversion configuration file. This enables automatic\n'
873 'properties to simplify the project maintenance.\n'
874 'Pro-tip: just download and install\n'
875 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
877 try:
878 lines = open(path, 'r').read().splitlines()
879 # Make sure auto-props is enabled and check for 2 Chromium standard
880 # auto-prop.
881 if (not '*.cc = svn:eol-style=LF' in lines or
882 not '*.pdf = svn:mime-type=application/pdf' in lines or
883 not 'enable-auto-props = yes' in lines):
884 return [
885 output_api.PresubmitNotifyResult(
886 'It looks like you have not configured your subversion config '
887 'file or it is not up-to-date.\n' + error_msg)
889 except (OSError, IOError):
890 return [
891 output_api.PresubmitNotifyResult(
892 'Can\'t find your subversion config file.\n' + error_msg)
894 return []
897 def _CheckAuthorizedAuthor(input_api, output_api):
898 """For non-googler/chromites committers, verify the author's email address is
899 in AUTHORS.
901 # TODO(maruel): Add it to input_api?
902 import fnmatch
904 author = input_api.change.author_email
905 if not author:
906 input_api.logging.info('No author, skipping AUTHOR check')
907 return []
908 authors_path = input_api.os_path.join(
909 input_api.PresubmitLocalPath(), 'AUTHORS')
910 valid_authors = (
911 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
912 for line in open(authors_path))
913 valid_authors = [item.group(1).lower() for item in valid_authors if item]
914 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
915 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
916 return [output_api.PresubmitPromptWarning(
917 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
918 '\n'
919 'http://www.chromium.org/developers/contributing-code and read the '
920 '"Legal" section\n'
921 'If you are a chromite, verify the contributor signed the CLA.') %
922 author)]
923 return []
926 def _CheckPatchFiles(input_api, output_api):
927 problems = [f.LocalPath() for f in input_api.AffectedFiles()
928 if f.LocalPath().endswith(('.orig', '.rej'))]
929 if problems:
930 return [output_api.PresubmitError(
931 "Don't commit .rej and .orig files.", problems)]
932 else:
933 return []
936 def _DidYouMeanOSMacro(bad_macro):
937 try:
938 return {'A': 'OS_ANDROID',
939 'B': 'OS_BSD',
940 'C': 'OS_CHROMEOS',
941 'F': 'OS_FREEBSD',
942 'L': 'OS_LINUX',
943 'M': 'OS_MACOSX',
944 'N': 'OS_NACL',
945 'O': 'OS_OPENBSD',
946 'P': 'OS_POSIX',
947 'S': 'OS_SOLARIS',
948 'W': 'OS_WIN'}[bad_macro[3].upper()]
949 except KeyError:
950 return ''
953 def _CheckForInvalidOSMacrosInFile(input_api, f):
954 """Check for sensible looking, totally invalid OS macros."""
955 preprocessor_statement = input_api.re.compile(r'^\s*#')
956 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
957 results = []
958 for lnum, line in f.ChangedContents():
959 if preprocessor_statement.search(line):
960 for match in os_macro.finditer(line):
961 if not match.group(1) in _VALID_OS_MACROS:
962 good = _DidYouMeanOSMacro(match.group(1))
963 did_you_mean = ' (did you mean %s?)' % good if good else ''
964 results.append(' %s:%d %s%s' % (f.LocalPath(),
965 lnum,
966 match.group(1),
967 did_you_mean))
968 return results
971 def _CheckForInvalidOSMacros(input_api, output_api):
972 """Check all affected files for invalid OS macros."""
973 bad_macros = []
974 for f in input_api.AffectedFiles():
975 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
976 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
978 if not bad_macros:
979 return []
981 return [output_api.PresubmitError(
982 'Possibly invalid OS macro[s] found. Please fix your code\n'
983 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
986 def CheckChangeOnUpload(input_api, output_api):
987 results = []
988 results.extend(_CommonChecks(input_api, output_api))
989 return results
992 def CheckChangeOnCommit(input_api, output_api):
993 results = []
994 results.extend(_CommonChecks(input_api, output_api))
995 # TODO(thestig) temporarily disabled, doesn't work in third_party/
996 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
997 # input_api, output_api, sources))
998 # Make sure the tree is 'open'.
999 results.extend(input_api.canned_checks.CheckTreeIsOpen(
1000 input_api,
1001 output_api,
1002 json_url='http://chromium-status.appspot.com/current?format=json'))
1003 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
1004 output_api, 'http://codereview.chromium.org',
1005 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
1006 'tryserver@chromium.org'))
1008 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1009 input_api, output_api))
1010 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1011 input_api, output_api))
1012 results.extend(_CheckSubversionConfig(input_api, output_api))
1013 return results
1016 def GetPreferredTrySlaves(project, change):
1017 files = change.LocalPaths()
1019 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
1020 return []
1022 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
1023 return ['mac_rel', 'mac:compile']
1024 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
1025 return ['win_rel', 'win7_aura', 'win:compile']
1026 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
1027 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
1028 if all(re.search('^native_client_sdk', f) for f in files):
1029 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
1030 if all(re.search('[/_]ios[/_.]', f) for f in files):
1031 return ['ios_rel_device', 'ios_dbg_simulator']
1033 trybots = [
1034 'android_clang_dbg',
1035 'android_dbg',
1036 'ios_dbg_simulator',
1037 'ios_rel_device',
1038 'linux_asan',
1039 'linux_aura',
1040 'linux_chromeos',
1041 'linux_clang:compile',
1042 'linux_rel',
1043 'mac_rel',
1044 'mac:compile',
1045 'win7_aura',
1046 'win_rel',
1047 'win:compile',
1048 'win_x64_rel:compile',
1051 # Match things like path/aura/file.cc and path/file_aura.cc.
1052 # Same for chromeos.
1053 if any(re.search('[/_](aura|chromeos)', f) for f in files):
1054 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
1056 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1057 # unless they're .gyp(i) files as changes to those files can break the gyp
1058 # step on that bot.
1059 if (not all(re.search('^chrome', f) for f in files) or
1060 any(re.search('\.gypi?$', f) for f in files)):
1061 trybots += ['android_aosp']
1063 return trybots