Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Top-level presubmit script for Chromium. | 5 """Top-level presubmit script for Chromium. |
| 6 | 6 |
| 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 8 for more details about the presubmit API built into gcl. | 8 for more details about the presubmit API built into gcl. |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 | |
| 12 import re | |
| 13 | |
| 14 | |
| 15 _EXCLUDED_PATHS = ( | 11 _EXCLUDED_PATHS = ( |
| 16 r"^breakpad[\\\/].*", | 12 r"^breakpad[\\\/].*", |
| 17 r"^native_client_sdk[\\\/].*", | 13 r"^native_client_sdk[\\\/].*", |
| 18 r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*", | 14 r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*", |
| 19 r"^skia[\\\/].*", | 15 r"^skia[\\\/].*", |
| 20 r"^v8[\\\/].*", | 16 r"^v8[\\\/].*", |
| 21 r".*MakeFile$", | 17 r".*MakeFile$", |
| 22 ) | 18 ) |
| 23 | 19 |
| 24 | 20 |
| 25 _TEST_ONLY_WARNING = ( | 21 _TEST_ONLY_WARNING = ( |
| 26 'You might be calling functions intended only for testing from\n' | 22 'You might be calling functions intended only for testing from\n' |
| 27 'production code. It is OK to ignore this warning if you know what\n' | 23 'production code. It is OK to ignore this warning if you know what\n' |
| 28 'you are doing, as the heuristics used to detect the situation are\n' | 24 'you are doing, as the heuristics used to detect the situation are\n' |
| 29 'not perfect. The commit queue will not block on this warning.\n' | 25 'not perfect. The commit queue will not block on this warning.\n' |
| 30 'Email joi@chromium.org if you have questions.') | 26 'Email joi@chromium.org if you have questions.') |
| 31 | 27 |
| 32 | 28 |
| 29 def _CheckJavaScriptStyle(input_api, output_api): | |
| 30 """Check for JavaScript style violations.""" | |
| 31 | |
| 32 import closure_linter.common.errorhandler | |
| 33 | |
| 34 class ErrorHandlerImpl(closure_linter.common.errorhandler.ErrorHandler): | |
| 35 """Implementation of ErrorHandler that collects all errors except those | |
| 36 that don't apply for Chromium JavaScript code. | |
| 37 """ | |
| 38 | |
| 39 def __init__(self): | |
| 40 self._errors = [] | |
| 41 | |
| 42 def HandleFile(self, filename, first_token): | |
| 43 self._filename = filename | |
| 44 | |
| 45 def HandleError(self, error): | |
| 46 if (self._valid(error)): | |
| 47 error.filename = self._filename | |
| 48 self._errors.append(error) | |
| 49 | |
| 50 def GetErrors(self): | |
| 51 return self._errors | |
| 52 | |
| 53 def HasErrors(self): | |
| 54 return bool(self._errors) | |
| 55 | |
| 56 def _valid(self, error): | |
| 57 """Check whether an error is valid. Most errors are valid, with a few | |
| 58 exceptions which are listed here. | |
| 59 """ | |
| 60 | |
| 61 import closure_linter.errors | |
| 62 | |
| 63 return error.code not in [ | |
| 64 closure_linter.errors.COMMA_AT_END_OF_LITERAL, | |
| 65 closure_linter.errors.JSDOC_ILLEGAL_QUESTION_WITH_PIPE, | |
| 66 closure_linter.errors.JSDOC_TAG_DESCRIPTION_ENDS_WITH_INVALID_CHARACTE R | |
|
M-A Ruel
2012/02/07 00:34:09
the presubmit check will be fine since there's a l
Tyler Breisacher (Chromium)
2012/02/07 01:15:59
Done.
| |
| 67 ] | |
| 68 | |
| 69 import closure_linter.checker | |
| 70 import os.path | |
|
M-A Ruel
2012/02/07 00:34:09
use input_api.os_path
Tyler Breisacher (Chromium)
2012/02/07 01:15:59
Done.
| |
| 71 | |
| 72 # Only check the following folders. OWNERS of folders containing JavaScript | |
| 73 # code can opt-in to this check by adding the folder here. | |
| 74 join = input_api.os_path.join | |
| 75 checked_folders = [ | |
| 76 join('chrome', 'browser', 'resources', 'ntp4'), | |
| 77 join('chrome', 'browser', 'resources', 'options2'), | |
| 78 ] | |
| 79 | |
| 80 def inCheckedFolder(affected_file): | |
| 81 return any(affected_file.LocalPath().startswith(cf) | |
| 82 for cf in checked_folders) | |
| 83 | |
| 84 def jsOrHtml(affected_file): | |
| 85 return input_api.re.search('\.(js|html?)$', affected_file.LocalPath()) | |
| 86 | |
| 87 def fileFilter(affected_file): | |
| 88 return jsOrHtml(affected_file) and inCheckedFolder(affected_file) | |
| 89 | |
| 90 results = [] | |
| 91 | |
| 92 for f in input_api.change.AffectedFiles(file_filter=fileFilter): | |
| 93 errorLines = [] | |
| 94 | |
| 95 # check for getElementById() | |
| 96 for i, line in enumerate(f.NewContents(), start=1): | |
| 97 if 'getElementById' in line: | |
| 98 errorLines.append(' line %d: %s\n%s' % ( | |
| 99 i, | |
| 100 'Use $() instead of document.getElementById()', | |
| 101 line)) | |
| 102 | |
| 103 const_re = input_api.re.compile(r'\bconst\b') | |
| 104 if const_re.search(line): | |
|
M-A Ruel
2012/02/07 00:34:09
if input_api.re.search(r'\bconst\b', line):
Tyler Breisacher (Chromium)
2012/02/07 01:15:59
Done.
| |
| 105 errorLines.append(' line %d: %s\n%s' % ( | |
| 106 i, | |
| 107 'Use |var| instead of |const|. See http://crbug.com/80149', | |
| 108 line)) | |
| 109 | |
| 110 # Use closure_linter to check for several different errors | |
| 111 error_handler = ErrorHandlerImpl() | |
| 112 checker = closure_linter.checker.JavaScriptStyleChecker(error_handler) | |
| 113 checker.Check(f.LocalPath()) | |
| 114 | |
| 115 for error in error_handler.GetErrors(): | |
| 116 errorMsg = ' line %d: E%04d: %s\n%s' % ( | |
| 117 error.token.line_number, | |
| 118 error.code, | |
| 119 error.message, | |
| 120 error.token.line) | |
| 121 errorLines.append(errorMsg) | |
| 122 | |
| 123 if errorLines: | |
| 124 errorLines = [ | |
| 125 'Found JavaScript style violations in %s:' % | |
| 126 f.LocalPath()] + errorLines | |
| 127 results.append(output_api.PresubmitError('\n'.join(errorLines))) | |
| 128 | |
| 129 if results: | |
| 130 results.append(output_api.PresubmitNotifyResult( | |
| 131 'See the JavaScript style guide at ' | |
| 132 'http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml' | |
| 133 ' and contact tbreisacher@chromium.org for feedback on this' | |
| 134 ' PRESUBMIT check.')) | |
| 135 | |
| 136 return results | |
| 137 | |
| 33 | 138 |
| 34 def _CheckNoInterfacesInBase(input_api, output_api): | 139 def _CheckNoInterfacesInBase(input_api, output_api): |
| 35 """Checks to make sure no files in libbase.a have |@interface|.""" | 140 """Checks to make sure no files in libbase.a have |@interface|.""" |
| 36 pattern = input_api.re.compile(r'^\s*@interface', input_api.re.MULTILINE) | 141 pattern = input_api.re.compile(r'^\s*@interface', input_api.re.MULTILINE) |
| 37 files = [] | 142 files = [] |
| 38 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): | 143 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 39 if (f.LocalPath().startswith('base/') and | 144 if (f.LocalPath().startswith('base/') and |
| 40 not f.LocalPath().endswith('_unittest.mm')): | 145 not f.LocalPath().endswith('_unittest.mm')): |
| 41 contents = input_api.ReadFile(f) | 146 contents = input_api.ReadFile(f) |
| 42 if pattern.search(contents): | 147 if pattern.search(contents): |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 if not problems: | 312 if not problems: |
| 208 return [] | 313 return [] |
| 209 return [output_api.PresubmitPromptWarning('The old callback system is ' | 314 return [output_api.PresubmitPromptWarning('The old callback system is ' |
| 210 'deprecated. If possible, use base::Bind and base::Callback instead.\n' + | 315 'deprecated. If possible, use base::Bind and base::Callback instead.\n' + |
| 211 '\n'.join(problems))] | 316 '\n'.join(problems))] |
| 212 | 317 |
| 213 | 318 |
| 214 def _CommonChecks(input_api, output_api): | 319 def _CommonChecks(input_api, output_api): |
| 215 """Checks common to both upload and commit.""" | 320 """Checks common to both upload and commit.""" |
| 216 results = [] | 321 results = [] |
| 322 results.extend(_CheckJavaScriptStyle(input_api, output_api)) | |
| 217 results.extend(input_api.canned_checks.PanProjectChecks( | 323 results.extend(input_api.canned_checks.PanProjectChecks( |
| 218 input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) | 324 input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) |
| 219 results.extend(_CheckNoInterfacesInBase(input_api, output_api)) | 325 results.extend(_CheckNoInterfacesInBase(input_api, output_api)) |
| 220 results.extend(_CheckAuthorizedAuthor(input_api, output_api)) | 326 results.extend(_CheckAuthorizedAuthor(input_api, output_api)) |
| 221 results.extend( | 327 results.extend( |
| 222 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) | 328 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) |
| 223 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api)) | 329 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api)) |
| 224 results.extend(_CheckNoNewWStrings(input_api, output_api)) | 330 results.extend(_CheckNoNewWStrings(input_api, output_api)) |
| 225 results.extend(_CheckNoDEPSGIT(input_api, output_api)) | 331 results.extend(_CheckNoDEPSGIT(input_api, output_api)) |
| 226 results.extend(_CheckNoFRIEND_TEST(input_api, output_api)) | 332 results.extend(_CheckNoFRIEND_TEST(input_api, output_api)) |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 return results | 439 return results |
| 334 | 440 |
| 335 | 441 |
| 336 def GetPreferredTrySlaves(project, change): | 442 def GetPreferredTrySlaves(project, change): |
| 337 only_objc_files = all( | 443 only_objc_files = all( |
| 338 f.LocalPath().endswith(('.mm', '.m')) for f in change.AffectedFiles()) | 444 f.LocalPath().endswith(('.mm', '.m')) for f in change.AffectedFiles()) |
| 339 if only_objc_files: | 445 if only_objc_files: |
| 340 return ['mac_rel'] | 446 return ['mac_rel'] |
| 341 preferred = ['win_rel', 'linux_rel', 'mac_rel'] | 447 preferred = ['win_rel', 'linux_rel', 'mac_rel'] |
| 342 aura_re = '_aura[^/]*[.][^/]*' | 448 aura_re = '_aura[^/]*[.][^/]*' |
| 343 if any(re.search(aura_re, f.LocalPath()) for f in change.AffectedFiles()): | 449 if any(input_api.re.search(aura_re, f.LocalPath()) |
| 450 for f in change.AffectedFiles()): | |
| 344 preferred.append('linux_chromeos') | 451 preferred.append('linux_chromeos') |
| 345 return preferred | 452 return preferred |
| OLD | NEW |