Add reset button for each preference in gesture config UI
[chromium-blink-merge.git] / PRESUBMIT.py
blobcf74399c7d94b66dd48eafe43019e2aff50b64cc
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 C++ and Objective-C++
31 # implementation files.
32 _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
34 # Regular expression that matches code only used for test binaries
35 # (best effort).
36 _TEST_CODE_EXCLUDED_PATHS = (
37 r'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
38 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
39 r'.+_(api|browser|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
40 _IMPLEMENTATION_EXTENSIONS,
41 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
42 r'.*[/\\](test|tool(s)?)[/\\].*',
43 # content_shell is used for running layout tests.
44 r'content[/\\]shell[/\\].*',
45 # At request of folks maintaining this folder.
46 r'chrome[/\\]browser[/\\]automation[/\\].*',
49 _TEST_ONLY_WARNING = (
50 'You might be calling functions intended only for testing from\n'
51 'production code. It is OK to ignore this warning if you know what\n'
52 'you are doing, as the heuristics used to detect the situation are\n'
53 'not perfect. The commit queue will not block on this warning.\n'
54 'Email joi@chromium.org if you have questions.')
57 _INCLUDE_ORDER_WARNING = (
58 'Your #include order seems to be broken. Send mail to\n'
59 'marja@chromium.org if this is not the case.')
62 _BANNED_OBJC_FUNCTIONS = (
64 'addTrackingRect:',
66 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
67 'prohibited. Please use CrTrackingArea instead.',
68 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
70 False,
73 'NSTrackingArea',
75 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
76 'instead.',
77 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
79 False,
82 'convertPointFromBase:',
84 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
85 'Please use |convertPoint:(point) fromView:nil| instead.',
86 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
88 True,
91 'convertPointToBase:',
93 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
94 'Please use |convertPoint:(point) toView:nil| instead.',
95 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
97 True,
100 'convertRectFromBase:',
102 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
103 'Please use |convertRect:(point) fromView:nil| instead.',
104 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
106 True,
109 'convertRectToBase:',
111 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
112 'Please use |convertRect:(point) toView:nil| instead.',
113 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
115 True,
118 'convertSizeFromBase:',
120 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
121 'Please use |convertSize:(point) fromView:nil| instead.',
122 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
124 True,
127 'convertSizeToBase:',
129 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
130 'Please use |convertSize:(point) toView:nil| instead.',
131 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
133 True,
138 _BANNED_CPP_FUNCTIONS = (
139 # Make sure that gtest's FRIEND_TEST() macro is not used; the
140 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
141 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
143 'FRIEND_TEST(',
145 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
146 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
148 False,
152 'ScopedAllowIO',
154 'New code should not use ScopedAllowIO. Post a task to the blocking',
155 'pool or the FILE thread instead.',
157 True,
159 r"^content[\\\/]shell[\\\/]shell_browser_main\.cc$",
160 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
164 'SkRefPtr',
166 'The use of SkRefPtr is prohibited. ',
167 'Please use skia::RefPtr instead.'
169 True,
173 'SkAutoRef',
175 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
176 'Please use skia::RefPtr instead.'
178 True,
182 'SkAutoTUnref',
184 'The use of SkAutoTUnref is dangerous because it implicitly ',
185 'converts to a raw pointer. Please use skia::RefPtr instead.'
187 True,
191 'SkAutoUnref',
193 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
194 'because it implicitly converts to a raw pointer. ',
195 'Please use skia::RefPtr instead.'
197 True,
203 _VALID_OS_MACROS = (
204 # Please keep sorted.
205 'OS_ANDROID',
206 'OS_BSD',
207 'OS_CAT', # For testing.
208 'OS_CHROMEOS',
209 'OS_FREEBSD',
210 'OS_IOS',
211 'OS_LINUX',
212 'OS_MACOSX',
213 'OS_NACL',
214 'OS_OPENBSD',
215 'OS_POSIX',
216 'OS_SOLARIS',
217 'OS_WIN',
221 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
222 """Attempts to prevent use of functions intended only for testing in
223 non-testing code. For now this is just a best-effort implementation
224 that ignores header files and may have some false positives. A
225 better implementation would probably need a proper C++ parser.
227 # We only scan .cc files and the like, as the declaration of
228 # for-testing functions in header files are hard to distinguish from
229 # calls to such functions without a proper C++ parser.
230 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
232 base_function_pattern = r'ForTest(ing)?|for_test(ing)?'
233 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
234 comment_pattern = input_api.re.compile(r'//.*%s' % base_function_pattern)
235 exclusion_pattern = input_api.re.compile(
236 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
237 base_function_pattern, base_function_pattern))
239 def FilterFile(affected_file):
240 black_list = (_EXCLUDED_PATHS +
241 _TEST_CODE_EXCLUDED_PATHS +
242 input_api.DEFAULT_BLACK_LIST)
243 return input_api.FilterSourceFile(
244 affected_file,
245 white_list=(file_inclusion_pattern, ),
246 black_list=black_list)
248 problems = []
249 for f in input_api.AffectedSourceFiles(FilterFile):
250 local_path = f.LocalPath()
251 lines = input_api.ReadFile(f).splitlines()
252 line_number = 0
253 for line in lines:
254 if (inclusion_pattern.search(line) and
255 not comment_pattern.search(line) and
256 not exclusion_pattern.search(line)):
257 problems.append(
258 '%s:%d\n %s' % (local_path, line_number, line.strip()))
259 line_number += 1
261 if problems:
262 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
263 else:
264 return []
267 def _CheckNoIOStreamInHeaders(input_api, output_api):
268 """Checks to make sure no .h files include <iostream>."""
269 files = []
270 pattern = input_api.re.compile(r'^#include\s*<iostream>',
271 input_api.re.MULTILINE)
272 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
273 if not f.LocalPath().endswith('.h'):
274 continue
275 contents = input_api.ReadFile(f)
276 if pattern.search(contents):
277 files.append(f)
279 if len(files):
280 return [ output_api.PresubmitError(
281 'Do not #include <iostream> in header files, since it inserts static '
282 'initialization into every file including the header. Instead, '
283 '#include <ostream>. See http://crbug.com/94794',
284 files) ]
285 return []
288 def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
289 """Checks to make sure no source files use UNIT_TEST"""
290 problems = []
291 for f in input_api.AffectedFiles():
292 if (not f.LocalPath().endswith(('.cc', '.mm'))):
293 continue
295 for line_num, line in f.ChangedContents():
296 if 'UNIT_TEST' in line:
297 problems.append(' %s:%d' % (f.LocalPath(), line_num))
299 if not problems:
300 return []
301 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
302 '\n'.join(problems))]
305 def _CheckNoNewWStrings(input_api, output_api):
306 """Checks to make sure we don't introduce use of wstrings."""
307 problems = []
308 for f in input_api.AffectedFiles():
309 if (not f.LocalPath().endswith(('.cc', '.h')) or
310 f.LocalPath().endswith('test.cc')):
311 continue
313 allowWString = False
314 for line_num, line in f.ChangedContents():
315 if 'presubmit: allow wstring' in line:
316 allowWString = True
317 elif not allowWString and 'wstring' in line:
318 problems.append(' %s:%d' % (f.LocalPath(), line_num))
319 allowWString = False
320 else:
321 allowWString = False
323 if not problems:
324 return []
325 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
326 ' If you are calling a cross-platform API that accepts a wstring, '
327 'fix the API.\n' +
328 '\n'.join(problems))]
331 def _CheckNoDEPSGIT(input_api, output_api):
332 """Make sure .DEPS.git is never modified manually."""
333 if any(f.LocalPath().endswith('.DEPS.git') for f in
334 input_api.AffectedFiles()):
335 return [output_api.PresubmitError(
336 'Never commit changes to .DEPS.git. This file is maintained by an\n'
337 'automated system based on what\'s in DEPS and your changes will be\n'
338 'overwritten.\n'
339 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
340 'for more information')]
341 return []
344 def _CheckNoBannedFunctions(input_api, output_api):
345 """Make sure that banned functions are not used."""
346 warnings = []
347 errors = []
349 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
350 for f in input_api.AffectedFiles(file_filter=file_filter):
351 for line_num, line in f.ChangedContents():
352 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
353 if func_name in line:
354 problems = warnings;
355 if error:
356 problems = errors;
357 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
358 for message_line in message:
359 problems.append(' %s' % message_line)
361 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
362 for f in input_api.AffectedFiles(file_filter=file_filter):
363 for line_num, line in f.ChangedContents():
364 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
365 def IsBlacklisted(affected_file, blacklist):
366 local_path = affected_file.LocalPath()
367 for item in blacklist:
368 if input_api.re.match(item, local_path):
369 return True
370 return False
371 if IsBlacklisted(f, excluded_paths):
372 continue
373 if func_name in line:
374 problems = warnings;
375 if error:
376 problems = errors;
377 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
378 for message_line in message:
379 problems.append(' %s' % message_line)
381 result = []
382 if (warnings):
383 result.append(output_api.PresubmitPromptWarning(
384 'Banned functions were used.\n' + '\n'.join(warnings)))
385 if (errors):
386 result.append(output_api.PresubmitError(
387 'Banned functions were used.\n' + '\n'.join(errors)))
388 return result
391 def _CheckNoPragmaOnce(input_api, output_api):
392 """Make sure that banned functions are not used."""
393 files = []
394 pattern = input_api.re.compile(r'^#pragma\s+once',
395 input_api.re.MULTILINE)
396 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
397 if not f.LocalPath().endswith('.h'):
398 continue
399 contents = input_api.ReadFile(f)
400 if pattern.search(contents):
401 files.append(f)
403 if files:
404 return [output_api.PresubmitError(
405 'Do not use #pragma once in header files.\n'
406 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
407 files)]
408 return []
411 def _CheckNoTrinaryTrueFalse(input_api, output_api):
412 """Checks to make sure we don't introduce use of foo ? true : false."""
413 problems = []
414 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
415 for f in input_api.AffectedFiles():
416 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
417 continue
419 for line_num, line in f.ChangedContents():
420 if pattern.match(line):
421 problems.append(' %s:%d' % (f.LocalPath(), line_num))
423 if not problems:
424 return []
425 return [output_api.PresubmitPromptWarning(
426 'Please consider avoiding the "? true : false" pattern if possible.\n' +
427 '\n'.join(problems))]
430 def _CheckUnwantedDependencies(input_api, output_api):
431 """Runs checkdeps on #include statements added in this
432 change. Breaking - rules is an error, breaking ! rules is a
433 warning.
435 # We need to wait until we have an input_api object and use this
436 # roundabout construct to import checkdeps because this file is
437 # eval-ed and thus doesn't have __file__.
438 original_sys_path = sys.path
439 try:
440 sys.path = sys.path + [input_api.os_path.join(
441 input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
442 import checkdeps
443 from cpp_checker import CppChecker
444 from rules import Rule
445 finally:
446 # Restore sys.path to what it was before.
447 sys.path = original_sys_path
449 added_includes = []
450 for f in input_api.AffectedFiles():
451 if not CppChecker.IsCppFile(f.LocalPath()):
452 continue
454 changed_lines = [line for line_num, line in f.ChangedContents()]
455 added_includes.append([f.LocalPath(), changed_lines])
457 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
459 error_descriptions = []
460 warning_descriptions = []
461 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
462 added_includes):
463 description_with_path = '%s\n %s' % (path, rule_description)
464 if rule_type == Rule.DISALLOW:
465 error_descriptions.append(description_with_path)
466 else:
467 warning_descriptions.append(description_with_path)
469 results = []
470 if error_descriptions:
471 results.append(output_api.PresubmitError(
472 'You added one or more #includes that violate checkdeps rules.',
473 error_descriptions))
474 if warning_descriptions:
475 results.append(output_api.PresubmitPromptOrNotify(
476 'You added one or more #includes of files that are temporarily\n'
477 'allowed but being removed. Can you avoid introducing the\n'
478 '#include? See relevant DEPS file(s) for details and contacts.',
479 warning_descriptions))
480 return results
483 def _CheckFilePermissions(input_api, output_api):
484 """Check that all files have their permissions properly set."""
485 args = [sys.executable, 'tools/checkperms/checkperms.py', '--root',
486 input_api.change.RepositoryRoot()]
487 for f in input_api.AffectedFiles():
488 args += ['--file', f.LocalPath()]
489 errors = []
490 (errors, stderrdata) = subprocess.Popen(args).communicate()
492 results = []
493 if errors:
494 results.append(output_api.PresubmitError('checkperms.py failed.',
495 errors))
496 return results
499 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
500 """Makes sure we don't include ui/aura/window_property.h
501 in header files.
503 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
504 errors = []
505 for f in input_api.AffectedFiles():
506 if not f.LocalPath().endswith('.h'):
507 continue
508 for line_num, line in f.ChangedContents():
509 if pattern.match(line):
510 errors.append(' %s:%d' % (f.LocalPath(), line_num))
512 results = []
513 if errors:
514 results.append(output_api.PresubmitError(
515 'Header files should not include ui/aura/window_property.h', errors))
516 return results
519 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
520 """Checks that the lines in scope occur in the right order.
522 1. C system files in alphabetical order
523 2. C++ system files in alphabetical order
524 3. Project's .h files
527 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
528 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
529 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
531 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
533 state = C_SYSTEM_INCLUDES
535 previous_line = ''
536 previous_line_num = 0
537 problem_linenums = []
538 for line_num, line in scope:
539 if c_system_include_pattern.match(line):
540 if state != C_SYSTEM_INCLUDES:
541 problem_linenums.append((line_num, previous_line_num))
542 elif previous_line and previous_line > line:
543 problem_linenums.append((line_num, previous_line_num))
544 elif cpp_system_include_pattern.match(line):
545 if state == C_SYSTEM_INCLUDES:
546 state = CPP_SYSTEM_INCLUDES
547 elif state == CUSTOM_INCLUDES:
548 problem_linenums.append((line_num, previous_line_num))
549 elif previous_line and previous_line > line:
550 problem_linenums.append((line_num, previous_line_num))
551 elif custom_include_pattern.match(line):
552 if state != CUSTOM_INCLUDES:
553 state = CUSTOM_INCLUDES
554 elif previous_line and previous_line > line:
555 problem_linenums.append((line_num, previous_line_num))
556 else:
557 problem_linenums.append(line_num)
558 previous_line = line
559 previous_line_num = line_num
561 warnings = []
562 for (line_num, previous_line_num) in problem_linenums:
563 if line_num in changed_linenums or previous_line_num in changed_linenums:
564 warnings.append(' %s:%d' % (file_path, line_num))
565 return warnings
568 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
569 """Checks the #include order for the given file f."""
571 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
572 # Exclude #include <.../...> includes from the check; e.g., <sys/...> includes
573 # often need to appear in a specific order.
574 excluded_include_pattern = input_api.re.compile(r'\s*#include \<.*/.*')
575 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
576 if_pattern = input_api.re.compile(
577 r'\s*#\s*(if|elif|else|endif|define|undef).*')
578 # Some files need specialized order of includes; exclude such files from this
579 # check.
580 uncheckable_includes_pattern = input_api.re.compile(
581 r'\s*#include '
582 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
584 contents = f.NewContents()
585 warnings = []
586 line_num = 0
588 # Handle the special first include. If the first include file is
589 # some/path/file.h, the corresponding including file can be some/path/file.cc,
590 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
591 # etc. It's also possible that no special first include exists.
592 for line in contents:
593 line_num += 1
594 if system_include_pattern.match(line):
595 # No special first include -> process the line again along with normal
596 # includes.
597 line_num -= 1
598 break
599 match = custom_include_pattern.match(line)
600 if match:
601 match_dict = match.groupdict()
602 header_basename = input_api.os_path.basename(
603 match_dict['FILE']).replace('.h', '')
604 if header_basename not in input_api.os_path.basename(f.LocalPath()):
605 # No special first include -> process the line again along with normal
606 # includes.
607 line_num -= 1
608 break
610 # Split into scopes: Each region between #if and #endif is its own scope.
611 scopes = []
612 current_scope = []
613 for line in contents[line_num:]:
614 line_num += 1
615 if uncheckable_includes_pattern.match(line):
616 return []
617 if if_pattern.match(line):
618 scopes.append(current_scope)
619 current_scope = []
620 elif ((system_include_pattern.match(line) or
621 custom_include_pattern.match(line)) and
622 not excluded_include_pattern.match(line)):
623 current_scope.append((line_num, line))
624 scopes.append(current_scope)
626 for scope in scopes:
627 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
628 changed_linenums))
629 return warnings
632 def _CheckIncludeOrder(input_api, output_api):
633 """Checks that the #include order is correct.
635 1. The corresponding header for source files.
636 2. C system files in alphabetical order
637 3. C++ system files in alphabetical order
638 4. Project's .h files in alphabetical order
640 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
641 these rules separately.
644 warnings = []
645 for f in input_api.AffectedFiles():
646 if f.LocalPath().endswith(('.cc', '.h')):
647 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
648 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
650 results = []
651 if warnings:
652 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
653 warnings))
654 return results
657 def _CheckForVersionControlConflictsInFile(input_api, f):
658 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
659 errors = []
660 for line_num, line in f.ChangedContents():
661 if pattern.match(line):
662 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
663 return errors
666 def _CheckForVersionControlConflicts(input_api, output_api):
667 """Usually this is not intentional and will cause a compile failure."""
668 errors = []
669 for f in input_api.AffectedFiles():
670 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
672 results = []
673 if errors:
674 results.append(output_api.PresubmitError(
675 'Version control conflict markers found, please resolve.', errors))
676 return results
679 def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
680 def FilterFile(affected_file):
681 """Filter function for use with input_api.AffectedSourceFiles,
682 below. This filters out everything except non-test files from
683 top-level directories that generally speaking should not hard-code
684 service URLs (e.g. src/android_webview/, src/content/ and others).
686 return input_api.FilterSourceFile(
687 affected_file,
688 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
689 black_list=(_EXCLUDED_PATHS +
690 _TEST_CODE_EXCLUDED_PATHS +
691 input_api.DEFAULT_BLACK_LIST))
693 base_pattern = '"[^"]*google\.com[^"]*"'
694 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
695 pattern = input_api.re.compile(base_pattern)
696 problems = [] # items are (filename, line_number, line)
697 for f in input_api.AffectedSourceFiles(FilterFile):
698 for line_num, line in f.ChangedContents():
699 if not comment_pattern.search(line) and pattern.search(line):
700 problems.append((f.LocalPath(), line_num, line))
702 if problems:
703 return [output_api.PresubmitPromptOrNotify(
704 'Most layers below src/chrome/ should not hardcode service URLs.\n'
705 'Are you sure this is correct? (Contact: joi@chromium.org)',
706 [' %s:%d: %s' % (
707 problem[0], problem[1], problem[2]) for problem in problems])]
708 else:
709 return []
712 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
713 """Makes sure there are no abbreviations in the name of PNG files.
715 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
716 errors = []
717 for f in input_api.AffectedFiles(include_deletes=False):
718 if pattern.match(f.LocalPath()):
719 errors.append(' %s' % f.LocalPath())
721 results = []
722 if errors:
723 results.append(output_api.PresubmitError(
724 'The name of PNG files should not have abbreviations. \n'
725 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
726 'Contact oshima@chromium.org if you have questions.', errors))
727 return results
730 def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
731 """When a dependency prefixed with + is added to a DEPS file, we
732 want to make sure that the change is reviewed by an OWNER of the
733 target file or directory, to avoid layering violations from being
734 introduced. This check verifies that this happens.
736 changed_lines = set()
737 for f in input_api.AffectedFiles():
738 filename = input_api.os_path.basename(f.LocalPath())
739 if filename == 'DEPS':
740 changed_lines |= set(line.strip()
741 for line_num, line
742 in f.ChangedContents())
743 if not changed_lines:
744 return []
746 virtual_depended_on_files = set()
747 # This pattern grabs the path without basename in the first
748 # parentheses, and the basename (if present) in the second. It
749 # relies on the simple heuristic that if there is a basename it will
750 # be a header file ending in ".h".
751 pattern = input_api.re.compile(
752 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
753 for changed_line in changed_lines:
754 m = pattern.match(changed_line)
755 if m:
756 path = m.group(1)
757 if not path.startswith('grit/'):
758 virtual_depended_on_files.add('%s/DEPS' % m.group(1))
760 if not virtual_depended_on_files:
761 return []
763 if input_api.is_committing:
764 if input_api.tbr:
765 return [output_api.PresubmitNotifyResult(
766 '--tbr was specified, skipping OWNERS check for DEPS additions')]
767 if not input_api.change.issue:
768 return [output_api.PresubmitError(
769 "DEPS approval by OWNERS check failed: this change has "
770 "no Rietveld issue number, so we can't check it for approvals.")]
771 output = output_api.PresubmitError
772 else:
773 output = output_api.PresubmitNotifyResult
775 owners_db = input_api.owners_db
776 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
777 input_api,
778 owners_db.email_regexp,
779 approval_needed=input_api.is_committing)
781 owner_email = owner_email or input_api.change.author_email
783 reviewers_plus_owner = set(reviewers)
784 if owner_email:
785 reviewers_plus_owner.add(owner_email)
786 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
787 reviewers_plus_owner)
788 unapproved_dependencies = ["'+%s'," % path[:-len('/DEPS')]
789 for path in missing_files]
791 if unapproved_dependencies:
792 output_list = [
793 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
794 '\n '.join(sorted(unapproved_dependencies)))]
795 if not input_api.is_committing:
796 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
797 output_list.append(output(
798 'Suggested missing target path OWNERS:\n %s' %
799 '\n '.join(suggested_owners or [])))
800 return output_list
802 return []
805 def _CommonChecks(input_api, output_api):
806 """Checks common to both upload and commit."""
807 results = []
808 results.extend(input_api.canned_checks.PanProjectChecks(
809 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
810 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
811 results.extend(
812 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
813 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
814 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
815 results.extend(_CheckNoNewWStrings(input_api, output_api))
816 results.extend(_CheckNoDEPSGIT(input_api, output_api))
817 results.extend(_CheckNoBannedFunctions(input_api, output_api))
818 results.extend(_CheckNoPragmaOnce(input_api, output_api))
819 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
820 results.extend(_CheckUnwantedDependencies(input_api, output_api))
821 results.extend(_CheckFilePermissions(input_api, output_api))
822 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
823 results.extend(_CheckIncludeOrder(input_api, output_api))
824 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
825 results.extend(_CheckPatchFiles(input_api, output_api))
826 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
827 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
828 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
829 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
831 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
832 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
833 input_api, output_api,
834 input_api.PresubmitLocalPath(),
835 whitelist=[r'^PRESUBMIT_test\.py$']))
836 return results
839 def _CheckSubversionConfig(input_api, output_api):
840 """Verifies the subversion config file is correctly setup.
842 Checks that autoprops are enabled, returns an error otherwise.
844 join = input_api.os_path.join
845 if input_api.platform == 'win32':
846 appdata = input_api.environ.get('APPDATA', '')
847 if not appdata:
848 return [output_api.PresubmitError('%APPDATA% is not configured.')]
849 path = join(appdata, 'Subversion', 'config')
850 else:
851 home = input_api.environ.get('HOME', '')
852 if not home:
853 return [output_api.PresubmitError('$HOME is not configured.')]
854 path = join(home, '.subversion', 'config')
856 error_msg = (
857 'Please look at http://dev.chromium.org/developers/coding-style to\n'
858 'configure your subversion configuration file. This enables automatic\n'
859 'properties to simplify the project maintenance.\n'
860 'Pro-tip: just download and install\n'
861 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
863 try:
864 lines = open(path, 'r').read().splitlines()
865 # Make sure auto-props is enabled and check for 2 Chromium standard
866 # auto-prop.
867 if (not '*.cc = svn:eol-style=LF' in lines or
868 not '*.pdf = svn:mime-type=application/pdf' in lines or
869 not 'enable-auto-props = yes' in lines):
870 return [
871 output_api.PresubmitNotifyResult(
872 'It looks like you have not configured your subversion config '
873 'file or it is not up-to-date.\n' + error_msg)
875 except (OSError, IOError):
876 return [
877 output_api.PresubmitNotifyResult(
878 'Can\'t find your subversion config file.\n' + error_msg)
880 return []
883 def _CheckAuthorizedAuthor(input_api, output_api):
884 """For non-googler/chromites committers, verify the author's email address is
885 in AUTHORS.
887 # TODO(maruel): Add it to input_api?
888 import fnmatch
890 author = input_api.change.author_email
891 if not author:
892 input_api.logging.info('No author, skipping AUTHOR check')
893 return []
894 authors_path = input_api.os_path.join(
895 input_api.PresubmitLocalPath(), 'AUTHORS')
896 valid_authors = (
897 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
898 for line in open(authors_path))
899 valid_authors = [item.group(1).lower() for item in valid_authors if item]
900 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
901 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
902 return [output_api.PresubmitPromptWarning(
903 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
904 '\n'
905 'http://www.chromium.org/developers/contributing-code and read the '
906 '"Legal" section\n'
907 'If you are a chromite, verify the contributor signed the CLA.') %
908 author)]
909 return []
912 def _CheckPatchFiles(input_api, output_api):
913 problems = [f.LocalPath() for f in input_api.AffectedFiles()
914 if f.LocalPath().endswith(('.orig', '.rej'))]
915 if problems:
916 return [output_api.PresubmitError(
917 "Don't commit .rej and .orig files.", problems)]
918 else:
919 return []
922 def _DidYouMeanOSMacro(bad_macro):
923 try:
924 return {'A': 'OS_ANDROID',
925 'B': 'OS_BSD',
926 'C': 'OS_CHROMEOS',
927 'F': 'OS_FREEBSD',
928 'L': 'OS_LINUX',
929 'M': 'OS_MACOSX',
930 'N': 'OS_NACL',
931 'O': 'OS_OPENBSD',
932 'P': 'OS_POSIX',
933 'S': 'OS_SOLARIS',
934 'W': 'OS_WIN'}[bad_macro[3].upper()]
935 except KeyError:
936 return ''
939 def _CheckForInvalidOSMacrosInFile(input_api, f):
940 """Check for sensible looking, totally invalid OS macros."""
941 preprocessor_statement = input_api.re.compile(r'^\s*#')
942 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
943 results = []
944 for lnum, line in f.ChangedContents():
945 if preprocessor_statement.search(line):
946 for match in os_macro.finditer(line):
947 if not match.group(1) in _VALID_OS_MACROS:
948 good = _DidYouMeanOSMacro(match.group(1))
949 did_you_mean = ' (did you mean %s?)' % good if good else ''
950 results.append(' %s:%d %s%s' % (f.LocalPath(),
951 lnum,
952 match.group(1),
953 did_you_mean))
954 return results
957 def _CheckForInvalidOSMacros(input_api, output_api):
958 """Check all affected files for invalid OS macros."""
959 bad_macros = []
960 for f in input_api.AffectedFiles():
961 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
962 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
964 if not bad_macros:
965 return []
967 return [output_api.PresubmitError(
968 'Possibly invalid OS macro[s] found. Please fix your code\n'
969 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
972 def CheckChangeOnUpload(input_api, output_api):
973 results = []
974 results.extend(_CommonChecks(input_api, output_api))
975 return results
978 def CheckChangeOnCommit(input_api, output_api):
979 results = []
980 results.extend(_CommonChecks(input_api, output_api))
981 # TODO(thestig) temporarily disabled, doesn't work in third_party/
982 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
983 # input_api, output_api, sources))
984 # Make sure the tree is 'open'.
985 results.extend(input_api.canned_checks.CheckTreeIsOpen(
986 input_api,
987 output_api,
988 json_url='http://chromium-status.appspot.com/current?format=json'))
989 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
990 output_api, 'http://codereview.chromium.org',
991 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
992 'tryserver@chromium.org'))
994 results.extend(input_api.canned_checks.CheckChangeHasBugField(
995 input_api, output_api))
996 results.extend(input_api.canned_checks.CheckChangeHasDescription(
997 input_api, output_api))
998 results.extend(_CheckSubversionConfig(input_api, output_api))
999 return results
1002 def GetPreferredTrySlaves(project, change):
1003 files = change.LocalPaths()
1005 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
1006 return []
1008 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
1009 return ['mac_rel', 'mac_asan', 'mac:compile']
1010 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
1011 return ['win_rel', 'win7_aura', 'win:compile']
1012 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
1013 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
1014 if all(re.search('^native_client_sdk', f) for f in files):
1015 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
1016 if all(re.search('[/_]ios[/_.]', f) for f in files):
1017 return ['ios_rel_device', 'ios_dbg_simulator']
1019 trybots = [
1020 'android_clang_dbg',
1021 'android_dbg',
1022 'ios_dbg_simulator',
1023 'ios_rel_device',
1024 'linux_asan',
1025 'linux_aura',
1026 'linux_chromeos',
1027 'linux_clang:compile',
1028 'linux_rel',
1029 'mac_asan',
1030 'mac_rel',
1031 'mac:compile',
1032 'win7_aura',
1033 'win_rel',
1034 'win:compile',
1037 # Match things like path/aura/file.cc and path/file_aura.cc.
1038 # Same for chromeos.
1039 if any(re.search('[/_](aura|chromeos)', f) for f in files):
1040 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
1042 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1043 # unless they're .gyp(i) files as changes to those files can break the gyp
1044 # step on that bot.
1045 if (not all(re.search('^chrome', f) for f in files) or
1046 any(re.search('\.gypi?$', f) for f in files)):
1047 trybots += ['android_aosp']
1049 return trybots