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