Exclude GPU JSON data files from presubmit checks
[chromium-blink-merge.git] / PRESUBMIT.py
blob49a366598cfab3ac04b213052409fad3b6251ad2
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[\\\/]shell_browser_main\.cc$",
161 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
165 'SkRefPtr',
167 'The use of SkRefPtr is prohibited. ',
168 'Please use skia::RefPtr instead.'
170 True,
174 'SkAutoRef',
176 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
177 'Please use skia::RefPtr instead.'
179 True,
183 'SkAutoTUnref',
185 'The use of SkAutoTUnref is dangerous because it implicitly ',
186 'converts to a raw pointer. Please use skia::RefPtr instead.'
188 True,
192 'SkAutoUnref',
194 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
195 'because it implicitly converts to a raw pointer. ',
196 'Please use skia::RefPtr instead.'
198 True,
204 _VALID_OS_MACROS = (
205 # Please keep sorted.
206 'OS_ANDROID',
207 'OS_BSD',
208 'OS_CAT', # For testing.
209 'OS_CHROMEOS',
210 'OS_FREEBSD',
211 'OS_IOS',
212 'OS_LINUX',
213 'OS_MACOSX',
214 'OS_NACL',
215 'OS_OPENBSD',
216 'OS_POSIX',
217 'OS_SOLARIS',
218 'OS_WIN',
222 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
223 """Attempts to prevent use of functions intended only for testing in
224 non-testing code. For now this is just a best-effort implementation
225 that ignores header files and may have some false positives. A
226 better implementation would probably need a proper C++ parser.
228 # We only scan .cc files and the like, as the declaration of
229 # for-testing functions in header files are hard to distinguish from
230 # calls to such functions without a proper C++ parser.
231 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
233 base_function_pattern = r'ForTest(ing)?|for_test(ing)?'
234 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
235 comment_pattern = input_api.re.compile(r'//.*%s' % base_function_pattern)
236 exclusion_pattern = input_api.re.compile(
237 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
238 base_function_pattern, base_function_pattern))
240 def FilterFile(affected_file):
241 black_list = (_EXCLUDED_PATHS +
242 _TEST_CODE_EXCLUDED_PATHS +
243 input_api.DEFAULT_BLACK_LIST)
244 return input_api.FilterSourceFile(
245 affected_file,
246 white_list=(file_inclusion_pattern, ),
247 black_list=black_list)
249 problems = []
250 for f in input_api.AffectedSourceFiles(FilterFile):
251 local_path = f.LocalPath()
252 lines = input_api.ReadFile(f).splitlines()
253 line_number = 0
254 for line in lines:
255 if (inclusion_pattern.search(line) and
256 not comment_pattern.search(line) and
257 not exclusion_pattern.search(line)):
258 problems.append(
259 '%s:%d\n %s' % (local_path, line_number, line.strip()))
260 line_number += 1
262 if problems:
263 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
264 else:
265 return []
268 def _CheckNoIOStreamInHeaders(input_api, output_api):
269 """Checks to make sure no .h files include <iostream>."""
270 files = []
271 pattern = input_api.re.compile(r'^#include\s*<iostream>',
272 input_api.re.MULTILINE)
273 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
274 if not f.LocalPath().endswith('.h'):
275 continue
276 contents = input_api.ReadFile(f)
277 if pattern.search(contents):
278 files.append(f)
280 if len(files):
281 return [ output_api.PresubmitError(
282 'Do not #include <iostream> in header files, since it inserts static '
283 'initialization into every file including the header. Instead, '
284 '#include <ostream>. See http://crbug.com/94794',
285 files) ]
286 return []
289 def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
290 """Checks to make sure no source files use UNIT_TEST"""
291 problems = []
292 for f in input_api.AffectedFiles():
293 if (not f.LocalPath().endswith(('.cc', '.mm'))):
294 continue
296 for line_num, line in f.ChangedContents():
297 if 'UNIT_TEST' in line:
298 problems.append(' %s:%d' % (f.LocalPath(), line_num))
300 if not problems:
301 return []
302 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
303 '\n'.join(problems))]
306 def _CheckNoNewWStrings(input_api, output_api):
307 """Checks to make sure we don't introduce use of wstrings."""
308 problems = []
309 for f in input_api.AffectedFiles():
310 if (not f.LocalPath().endswith(('.cc', '.h')) or
311 f.LocalPath().endswith('test.cc')):
312 continue
314 allowWString = False
315 for line_num, line in f.ChangedContents():
316 if 'presubmit: allow wstring' in line:
317 allowWString = True
318 elif not allowWString and 'wstring' in line:
319 problems.append(' %s:%d' % (f.LocalPath(), line_num))
320 allowWString = False
321 else:
322 allowWString = False
324 if not problems:
325 return []
326 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
327 ' If you are calling a cross-platform API that accepts a wstring, '
328 'fix the API.\n' +
329 '\n'.join(problems))]
332 def _CheckNoDEPSGIT(input_api, output_api):
333 """Make sure .DEPS.git is never modified manually."""
334 if any(f.LocalPath().endswith('.DEPS.git') for f in
335 input_api.AffectedFiles()):
336 return [output_api.PresubmitError(
337 'Never commit changes to .DEPS.git. This file is maintained by an\n'
338 'automated system based on what\'s in DEPS and your changes will be\n'
339 'overwritten.\n'
340 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
341 'for more information')]
342 return []
345 def _CheckNoBannedFunctions(input_api, output_api):
346 """Make sure that banned functions are not used."""
347 warnings = []
348 errors = []
350 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
351 for f in input_api.AffectedFiles(file_filter=file_filter):
352 for line_num, line in f.ChangedContents():
353 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
354 if func_name in line:
355 problems = warnings;
356 if error:
357 problems = errors;
358 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
359 for message_line in message:
360 problems.append(' %s' % message_line)
362 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
363 for f in input_api.AffectedFiles(file_filter=file_filter):
364 for line_num, line in f.ChangedContents():
365 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
366 def IsBlacklisted(affected_file, blacklist):
367 local_path = affected_file.LocalPath()
368 for item in blacklist:
369 if input_api.re.match(item, local_path):
370 return True
371 return False
372 if IsBlacklisted(f, excluded_paths):
373 continue
374 if func_name in line:
375 problems = warnings;
376 if error:
377 problems = errors;
378 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
379 for message_line in message:
380 problems.append(' %s' % message_line)
382 result = []
383 if (warnings):
384 result.append(output_api.PresubmitPromptWarning(
385 'Banned functions were used.\n' + '\n'.join(warnings)))
386 if (errors):
387 result.append(output_api.PresubmitError(
388 'Banned functions were used.\n' + '\n'.join(errors)))
389 return result
392 def _CheckNoPragmaOnce(input_api, output_api):
393 """Make sure that banned functions are not used."""
394 files = []
395 pattern = input_api.re.compile(r'^#pragma\s+once',
396 input_api.re.MULTILINE)
397 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
398 if not f.LocalPath().endswith('.h'):
399 continue
400 contents = input_api.ReadFile(f)
401 if pattern.search(contents):
402 files.append(f)
404 if files:
405 return [output_api.PresubmitError(
406 'Do not use #pragma once in header files.\n'
407 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
408 files)]
409 return []
412 def _CheckNoTrinaryTrueFalse(input_api, output_api):
413 """Checks to make sure we don't introduce use of foo ? true : false."""
414 problems = []
415 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
416 for f in input_api.AffectedFiles():
417 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
418 continue
420 for line_num, line in f.ChangedContents():
421 if pattern.match(line):
422 problems.append(' %s:%d' % (f.LocalPath(), line_num))
424 if not problems:
425 return []
426 return [output_api.PresubmitPromptWarning(
427 'Please consider avoiding the "? true : false" pattern if possible.\n' +
428 '\n'.join(problems))]
431 def _CheckUnwantedDependencies(input_api, output_api):
432 """Runs checkdeps on #include statements added in this
433 change. Breaking - rules is an error, breaking ! rules is a
434 warning.
436 # We need to wait until we have an input_api object and use this
437 # roundabout construct to import checkdeps because this file is
438 # eval-ed and thus doesn't have __file__.
439 original_sys_path = sys.path
440 try:
441 sys.path = sys.path + [input_api.os_path.join(
442 input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
443 import checkdeps
444 from cpp_checker import CppChecker
445 from rules import Rule
446 finally:
447 # Restore sys.path to what it was before.
448 sys.path = original_sys_path
450 added_includes = []
451 for f in input_api.AffectedFiles():
452 if not CppChecker.IsCppFile(f.LocalPath()):
453 continue
455 changed_lines = [line for line_num, line in f.ChangedContents()]
456 added_includes.append([f.LocalPath(), changed_lines])
458 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
460 error_descriptions = []
461 warning_descriptions = []
462 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
463 added_includes):
464 description_with_path = '%s\n %s' % (path, rule_description)
465 if rule_type == Rule.DISALLOW:
466 error_descriptions.append(description_with_path)
467 else:
468 warning_descriptions.append(description_with_path)
470 results = []
471 if error_descriptions:
472 results.append(output_api.PresubmitError(
473 'You added one or more #includes that violate checkdeps rules.',
474 error_descriptions))
475 if warning_descriptions:
476 results.append(output_api.PresubmitPromptOrNotify(
477 'You added one or more #includes of files that are temporarily\n'
478 'allowed but being removed. Can you avoid introducing the\n'
479 '#include? See relevant DEPS file(s) for details and contacts.',
480 warning_descriptions))
481 return results
484 def _CheckFilePermissions(input_api, output_api):
485 """Check that all files have their permissions properly set."""
486 args = [sys.executable, 'tools/checkperms/checkperms.py', '--root',
487 input_api.change.RepositoryRoot()]
488 for f in input_api.AffectedFiles():
489 args += ['--file', f.LocalPath()]
490 errors = []
491 (errors, stderrdata) = subprocess.Popen(args).communicate()
493 results = []
494 if errors:
495 results.append(output_api.PresubmitError('checkperms.py failed.',
496 errors))
497 return results
500 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
501 """Makes sure we don't include ui/aura/window_property.h
502 in header files.
504 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
505 errors = []
506 for f in input_api.AffectedFiles():
507 if not f.LocalPath().endswith('.h'):
508 continue
509 for line_num, line in f.ChangedContents():
510 if pattern.match(line):
511 errors.append(' %s:%d' % (f.LocalPath(), line_num))
513 results = []
514 if errors:
515 results.append(output_api.PresubmitError(
516 'Header files should not include ui/aura/window_property.h', errors))
517 return results
520 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
521 """Checks that the lines in scope occur in the right order.
523 1. C system files in alphabetical order
524 2. C++ system files in alphabetical order
525 3. Project's .h files
528 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
529 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
530 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
532 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
534 state = C_SYSTEM_INCLUDES
536 previous_line = ''
537 previous_line_num = 0
538 problem_linenums = []
539 for line_num, line in scope:
540 if c_system_include_pattern.match(line):
541 if state != C_SYSTEM_INCLUDES:
542 problem_linenums.append((line_num, previous_line_num))
543 elif previous_line and previous_line > line:
544 problem_linenums.append((line_num, previous_line_num))
545 elif cpp_system_include_pattern.match(line):
546 if state == C_SYSTEM_INCLUDES:
547 state = CPP_SYSTEM_INCLUDES
548 elif state == CUSTOM_INCLUDES:
549 problem_linenums.append((line_num, previous_line_num))
550 elif previous_line and previous_line > line:
551 problem_linenums.append((line_num, previous_line_num))
552 elif custom_include_pattern.match(line):
553 if state != CUSTOM_INCLUDES:
554 state = CUSTOM_INCLUDES
555 elif previous_line and previous_line > line:
556 problem_linenums.append((line_num, previous_line_num))
557 else:
558 problem_linenums.append(line_num)
559 previous_line = line
560 previous_line_num = line_num
562 warnings = []
563 for (line_num, previous_line_num) in problem_linenums:
564 if line_num in changed_linenums or previous_line_num in changed_linenums:
565 warnings.append(' %s:%d' % (file_path, line_num))
566 return warnings
569 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
570 """Checks the #include order for the given file f."""
572 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
573 # Exclude #include <.../...> includes from the check; e.g., <sys/...> includes
574 # often need to appear in a specific order.
575 excluded_include_pattern = input_api.re.compile(r'\s*#include \<.*/.*')
576 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
577 if_pattern = input_api.re.compile(
578 r'\s*#\s*(if|elif|else|endif|define|undef).*')
579 # Some files need specialized order of includes; exclude such files from this
580 # check.
581 uncheckable_includes_pattern = input_api.re.compile(
582 r'\s*#include '
583 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
585 contents = f.NewContents()
586 warnings = []
587 line_num = 0
589 # Handle the special first include. If the first include file is
590 # some/path/file.h, the corresponding including file can be some/path/file.cc,
591 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
592 # etc. It's also possible that no special first include exists.
593 for line in contents:
594 line_num += 1
595 if system_include_pattern.match(line):
596 # No special first include -> process the line again along with normal
597 # includes.
598 line_num -= 1
599 break
600 match = custom_include_pattern.match(line)
601 if match:
602 match_dict = match.groupdict()
603 header_basename = input_api.os_path.basename(
604 match_dict['FILE']).replace('.h', '')
605 if header_basename not in input_api.os_path.basename(f.LocalPath()):
606 # No special first include -> process the line again along with normal
607 # includes.
608 line_num -= 1
609 break
611 # Split into scopes: Each region between #if and #endif is its own scope.
612 scopes = []
613 current_scope = []
614 for line in contents[line_num:]:
615 line_num += 1
616 if uncheckable_includes_pattern.match(line):
617 return []
618 if if_pattern.match(line):
619 scopes.append(current_scope)
620 current_scope = []
621 elif ((system_include_pattern.match(line) or
622 custom_include_pattern.match(line)) and
623 not excluded_include_pattern.match(line)):
624 current_scope.append((line_num, line))
625 scopes.append(current_scope)
627 for scope in scopes:
628 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
629 changed_linenums))
630 return warnings
633 def _CheckIncludeOrder(input_api, output_api):
634 """Checks that the #include order is correct.
636 1. The corresponding header for source files.
637 2. C system files in alphabetical order
638 3. C++ system files in alphabetical order
639 4. Project's .h files in alphabetical order
641 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
642 these rules separately.
645 warnings = []
646 for f in input_api.AffectedFiles():
647 if f.LocalPath().endswith(('.cc', '.h')):
648 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
649 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
651 results = []
652 if warnings:
653 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
654 warnings))
655 return results
658 def _CheckForVersionControlConflictsInFile(input_api, f):
659 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
660 errors = []
661 for line_num, line in f.ChangedContents():
662 if pattern.match(line):
663 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
664 return errors
667 def _CheckForVersionControlConflicts(input_api, output_api):
668 """Usually this is not intentional and will cause a compile failure."""
669 errors = []
670 for f in input_api.AffectedFiles():
671 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
673 results = []
674 if errors:
675 results.append(output_api.PresubmitError(
676 'Version control conflict markers found, please resolve.', errors))
677 return results
680 def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
681 def FilterFile(affected_file):
682 """Filter function for use with input_api.AffectedSourceFiles,
683 below. This filters out everything except non-test files from
684 top-level directories that generally speaking should not hard-code
685 service URLs (e.g. src/android_webview/, src/content/ and others).
687 return input_api.FilterSourceFile(
688 affected_file,
689 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
690 black_list=(_EXCLUDED_PATHS +
691 _TEST_CODE_EXCLUDED_PATHS +
692 input_api.DEFAULT_BLACK_LIST))
694 base_pattern = '"[^"]*google\.com[^"]*"'
695 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
696 pattern = input_api.re.compile(base_pattern)
697 problems = [] # items are (filename, line_number, line)
698 for f in input_api.AffectedSourceFiles(FilterFile):
699 for line_num, line in f.ChangedContents():
700 if not comment_pattern.search(line) and pattern.search(line):
701 problems.append((f.LocalPath(), line_num, line))
703 if problems:
704 return [output_api.PresubmitPromptOrNotify(
705 'Most layers below src/chrome/ should not hardcode service URLs.\n'
706 'Are you sure this is correct? (Contact: joi@chromium.org)',
707 [' %s:%d: %s' % (
708 problem[0], problem[1], problem[2]) for problem in problems])]
709 else:
710 return []
713 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
714 """Makes sure there are no abbreviations in the name of PNG files.
716 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
717 errors = []
718 for f in input_api.AffectedFiles(include_deletes=False):
719 if pattern.match(f.LocalPath()):
720 errors.append(' %s' % f.LocalPath())
722 results = []
723 if errors:
724 results.append(output_api.PresubmitError(
725 'The name of PNG files should not have abbreviations. \n'
726 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
727 'Contact oshima@chromium.org if you have questions.', errors))
728 return results
731 def _DepsFilesToCheck(re, changed_lines):
732 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
733 a set of DEPS entries that we should look up."""
734 results = set()
736 # This pattern grabs the path without basename in the first
737 # parentheses, and the basename (if present) in the second. It
738 # relies on the simple heuristic that if there is a basename it will
739 # be a header file ending in ".h".
740 pattern = re.compile(
741 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
742 for changed_line in changed_lines:
743 m = pattern.match(changed_line)
744 if m:
745 path = m.group(1)
746 if not (path.startswith('grit/') or path == 'grit'):
747 results.add('%s/DEPS' % m.group(1))
748 return results
751 def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
752 """When a dependency prefixed with + is added to a DEPS file, we
753 want to make sure that the change is reviewed by an OWNER of the
754 target file or directory, to avoid layering violations from being
755 introduced. This check verifies that this happens.
757 changed_lines = set()
758 for f in input_api.AffectedFiles():
759 filename = input_api.os_path.basename(f.LocalPath())
760 if filename == 'DEPS':
761 changed_lines |= set(line.strip()
762 for line_num, line
763 in f.ChangedContents())
764 if not changed_lines:
765 return []
767 virtual_depended_on_files = _DepsFilesToCheck(input_api.re, changed_lines)
768 if not virtual_depended_on_files:
769 return []
771 if input_api.is_committing:
772 if input_api.tbr:
773 return [output_api.PresubmitNotifyResult(
774 '--tbr was specified, skipping OWNERS check for DEPS additions')]
775 if not input_api.change.issue:
776 return [output_api.PresubmitError(
777 "DEPS approval by OWNERS check failed: this change has "
778 "no Rietveld issue number, so we can't check it for approvals.")]
779 output = output_api.PresubmitError
780 else:
781 output = output_api.PresubmitNotifyResult
783 owners_db = input_api.owners_db
784 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
785 input_api,
786 owners_db.email_regexp,
787 approval_needed=input_api.is_committing)
789 owner_email = owner_email or input_api.change.author_email
791 reviewers_plus_owner = set(reviewers)
792 if owner_email:
793 reviewers_plus_owner.add(owner_email)
794 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
795 reviewers_plus_owner)
796 unapproved_dependencies = ["'+%s'," % path[:-len('/DEPS')]
797 for path in missing_files]
799 if unapproved_dependencies:
800 output_list = [
801 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
802 '\n '.join(sorted(unapproved_dependencies)))]
803 if not input_api.is_committing:
804 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
805 output_list.append(output(
806 'Suggested missing target path OWNERS:\n %s' %
807 '\n '.join(suggested_owners or [])))
808 return output_list
810 return []
813 def _CommonChecks(input_api, output_api):
814 """Checks common to both upload and commit."""
815 results = []
816 results.extend(input_api.canned_checks.PanProjectChecks(
817 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
818 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
819 results.extend(
820 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
821 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
822 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
823 results.extend(_CheckNoNewWStrings(input_api, output_api))
824 results.extend(_CheckNoDEPSGIT(input_api, output_api))
825 results.extend(_CheckNoBannedFunctions(input_api, output_api))
826 results.extend(_CheckNoPragmaOnce(input_api, output_api))
827 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
828 results.extend(_CheckUnwantedDependencies(input_api, output_api))
829 results.extend(_CheckFilePermissions(input_api, output_api))
830 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
831 results.extend(_CheckIncludeOrder(input_api, output_api))
832 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
833 results.extend(_CheckPatchFiles(input_api, output_api))
834 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
835 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
836 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
837 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
838 results.extend(
839 input_api.canned_checks.CheckChangeHasNoTabs(
840 input_api,
841 output_api,
842 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
844 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
845 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
846 input_api, output_api,
847 input_api.PresubmitLocalPath(),
848 whitelist=[r'^PRESUBMIT_test\.py$']))
849 return results
852 def _CheckSubversionConfig(input_api, output_api):
853 """Verifies the subversion config file is correctly setup.
855 Checks that autoprops are enabled, returns an error otherwise.
857 join = input_api.os_path.join
858 if input_api.platform == 'win32':
859 appdata = input_api.environ.get('APPDATA', '')
860 if not appdata:
861 return [output_api.PresubmitError('%APPDATA% is not configured.')]
862 path = join(appdata, 'Subversion', 'config')
863 else:
864 home = input_api.environ.get('HOME', '')
865 if not home:
866 return [output_api.PresubmitError('$HOME is not configured.')]
867 path = join(home, '.subversion', 'config')
869 error_msg = (
870 'Please look at http://dev.chromium.org/developers/coding-style to\n'
871 'configure your subversion configuration file. This enables automatic\n'
872 'properties to simplify the project maintenance.\n'
873 'Pro-tip: just download and install\n'
874 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
876 try:
877 lines = open(path, 'r').read().splitlines()
878 # Make sure auto-props is enabled and check for 2 Chromium standard
879 # auto-prop.
880 if (not '*.cc = svn:eol-style=LF' in lines or
881 not '*.pdf = svn:mime-type=application/pdf' in lines or
882 not 'enable-auto-props = yes' in lines):
883 return [
884 output_api.PresubmitNotifyResult(
885 'It looks like you have not configured your subversion config '
886 'file or it is not up-to-date.\n' + error_msg)
888 except (OSError, IOError):
889 return [
890 output_api.PresubmitNotifyResult(
891 'Can\'t find your subversion config file.\n' + error_msg)
893 return []
896 def _CheckAuthorizedAuthor(input_api, output_api):
897 """For non-googler/chromites committers, verify the author's email address is
898 in AUTHORS.
900 # TODO(maruel): Add it to input_api?
901 import fnmatch
903 author = input_api.change.author_email
904 if not author:
905 input_api.logging.info('No author, skipping AUTHOR check')
906 return []
907 authors_path = input_api.os_path.join(
908 input_api.PresubmitLocalPath(), 'AUTHORS')
909 valid_authors = (
910 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
911 for line in open(authors_path))
912 valid_authors = [item.group(1).lower() for item in valid_authors if item]
913 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
914 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
915 return [output_api.PresubmitPromptWarning(
916 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
917 '\n'
918 'http://www.chromium.org/developers/contributing-code and read the '
919 '"Legal" section\n'
920 'If you are a chromite, verify the contributor signed the CLA.') %
921 author)]
922 return []
925 def _CheckPatchFiles(input_api, output_api):
926 problems = [f.LocalPath() for f in input_api.AffectedFiles()
927 if f.LocalPath().endswith(('.orig', '.rej'))]
928 if problems:
929 return [output_api.PresubmitError(
930 "Don't commit .rej and .orig files.", problems)]
931 else:
932 return []
935 def _DidYouMeanOSMacro(bad_macro):
936 try:
937 return {'A': 'OS_ANDROID',
938 'B': 'OS_BSD',
939 'C': 'OS_CHROMEOS',
940 'F': 'OS_FREEBSD',
941 'L': 'OS_LINUX',
942 'M': 'OS_MACOSX',
943 'N': 'OS_NACL',
944 'O': 'OS_OPENBSD',
945 'P': 'OS_POSIX',
946 'S': 'OS_SOLARIS',
947 'W': 'OS_WIN'}[bad_macro[3].upper()]
948 except KeyError:
949 return ''
952 def _CheckForInvalidOSMacrosInFile(input_api, f):
953 """Check for sensible looking, totally invalid OS macros."""
954 preprocessor_statement = input_api.re.compile(r'^\s*#')
955 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
956 results = []
957 for lnum, line in f.ChangedContents():
958 if preprocessor_statement.search(line):
959 for match in os_macro.finditer(line):
960 if not match.group(1) in _VALID_OS_MACROS:
961 good = _DidYouMeanOSMacro(match.group(1))
962 did_you_mean = ' (did you mean %s?)' % good if good else ''
963 results.append(' %s:%d %s%s' % (f.LocalPath(),
964 lnum,
965 match.group(1),
966 did_you_mean))
967 return results
970 def _CheckForInvalidOSMacros(input_api, output_api):
971 """Check all affected files for invalid OS macros."""
972 bad_macros = []
973 for f in input_api.AffectedFiles():
974 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
975 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
977 if not bad_macros:
978 return []
980 return [output_api.PresubmitError(
981 'Possibly invalid OS macro[s] found. Please fix your code\n'
982 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
985 def CheckChangeOnUpload(input_api, output_api):
986 results = []
987 results.extend(_CommonChecks(input_api, output_api))
988 return results
991 def CheckChangeOnCommit(input_api, output_api):
992 results = []
993 results.extend(_CommonChecks(input_api, output_api))
994 # TODO(thestig) temporarily disabled, doesn't work in third_party/
995 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
996 # input_api, output_api, sources))
997 # Make sure the tree is 'open'.
998 results.extend(input_api.canned_checks.CheckTreeIsOpen(
999 input_api,
1000 output_api,
1001 json_url='http://chromium-status.appspot.com/current?format=json'))
1002 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
1003 output_api, 'http://codereview.chromium.org',
1004 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
1005 'tryserver@chromium.org'))
1007 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1008 input_api, output_api))
1009 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1010 input_api, output_api))
1011 results.extend(_CheckSubversionConfig(input_api, output_api))
1012 return results
1015 def GetPreferredTrySlaves(project, change):
1016 files = change.LocalPaths()
1018 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
1019 return []
1021 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
1022 return ['mac_rel', 'mac:compile']
1023 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
1024 return ['win_rel', 'win7_aura', 'win:compile']
1025 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
1026 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
1027 if all(re.search('^native_client_sdk', f) for f in files):
1028 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
1029 if all(re.search('[/_]ios[/_.]', f) for f in files):
1030 return ['ios_rel_device', 'ios_dbg_simulator']
1032 trybots = [
1033 'android_clang_dbg',
1034 'android_dbg',
1035 'ios_dbg_simulator',
1036 'ios_rel_device',
1037 'linux_asan',
1038 'linux_aura',
1039 'linux_chromeos',
1040 'linux_clang:compile',
1041 'linux_rel',
1042 'mac_rel',
1043 'mac:compile',
1044 'win7_aura',
1045 'win_rel',
1046 'win:compile',
1047 'win_x64_rel:compile',
1050 # Match things like path/aura/file.cc and path/file_aura.cc.
1051 # Same for chromeos.
1052 if any(re.search('[/_](aura|chromeos)', f) for f in files):
1053 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
1055 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1056 # unless they're .gyp(i) files as changes to those files can break the gyp
1057 # step on that bot.
1058 if (not all(re.search('^chrome', f) for f in files) or
1059 any(re.search('\.gypi?$', f) for f in files)):
1060 trybots += ['android_aosp']
1062 return trybots