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 """Generic presubmit checks that can be reused by other presubmit checks.""" | 5 """Generic presubmit checks that can be reused by other presubmit checks.""" |
6 | 6 |
7 import os as _os | 7 import os as _os |
8 _HERE = _os.path.dirname(_os.path.abspath(__file__)) | 8 _HERE = _os.path.dirname(_os.path.abspath(__file__)) |
9 | 9 |
10 # Justifications for each filter: | 10 # Justifications for each filter: |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 # Language specific exceptions to max line length. | 338 # Language specific exceptions to max line length. |
339 # '.h' is considered an obj-c file extension, since OBJC_EXCEPTIONS are a | 339 # '.h' is considered an obj-c file extension, since OBJC_EXCEPTIONS are a |
340 # superset of CPP_EXCEPTIONS. | 340 # superset of CPP_EXCEPTIONS. |
341 CPP_FILE_EXTS = ('c', 'cc') | 341 CPP_FILE_EXTS = ('c', 'cc') |
342 CPP_EXCEPTIONS = ('#define', '#endif', '#if', '#include', '#pragma') | 342 CPP_EXCEPTIONS = ('#define', '#endif', '#if', '#include', '#pragma') |
343 JAVA_FILE_EXTS = ('java',) | 343 JAVA_FILE_EXTS = ('java',) |
344 JAVA_EXCEPTIONS = ('import ', 'package ') | 344 JAVA_EXCEPTIONS = ('import ', 'package ') |
345 OBJC_FILE_EXTS = ('h', 'm', 'mm') | 345 OBJC_FILE_EXTS = ('h', 'm', 'mm') |
346 OBJC_EXCEPTIONS = ('#define', '#endif', '#if', '#import', '#include', | 346 OBJC_EXCEPTIONS = ('#define', '#endif', '#if', '#import', '#include', |
347 '#pragma') | 347 '#pragma') |
| 348 PY_FILE_EXTS = ('py') |
| 349 PY_EXCEPTIONS = ('import', 'from') |
348 | 350 |
349 LANGUAGE_EXCEPTIONS = [ | 351 LANGUAGE_EXCEPTIONS = [ |
350 (CPP_FILE_EXTS, CPP_EXCEPTIONS), | 352 (CPP_FILE_EXTS, CPP_EXCEPTIONS), |
351 (JAVA_FILE_EXTS, JAVA_EXCEPTIONS), | 353 (JAVA_FILE_EXTS, JAVA_EXCEPTIONS), |
352 (OBJC_FILE_EXTS, OBJC_EXCEPTIONS), | 354 (OBJC_FILE_EXTS, OBJC_EXCEPTIONS), |
| 355 (PY_FILE_EXTS, PY_EXCEPTIONS), |
353 ] | 356 ] |
354 | 357 |
355 def no_long_lines(file_extension, line): | 358 def no_long_lines(file_extension, line): |
356 # Check for language specific exceptions. | 359 # Check for language specific exceptions. |
357 if any(file_extension in exts and line.startswith(exceptions) | 360 if any(file_extension in exts and line.startswith(exceptions) |
358 for exts, exceptions in LANGUAGE_EXCEPTIONS): | 361 for exts, exceptions in LANGUAGE_EXCEPTIONS): |
359 return True | 362 return True |
360 | 363 |
361 file_maxlen = maxlens.get(file_extension, maxlens['']) | 364 file_maxlen = maxlens.get(file_extension, maxlens['']) |
362 # Stupidly long symbols that needs to be worked around if takes 66% of line. | 365 # Stupidly long symbols that needs to be worked around if takes 66% of line. |
(...skipping 11 matching lines...) Expand all Loading... |
374 | 377 |
375 if line_len > extra_maxlen: | 378 if line_len > extra_maxlen: |
376 return False | 379 return False |
377 | 380 |
378 if 'url(' in line and file_extension == 'css': | 381 if 'url(' in line and file_extension == 'css': |
379 return True | 382 return True |
380 | 383 |
381 if '<include' in line and file_extension in ('css', 'html', 'js'): | 384 if '<include' in line and file_extension in ('css', 'html', 'js'): |
382 return True | 385 return True |
383 | 386 |
| 387 if 'pylint: disable=line-too-long' in line and file_extension == 'py': |
| 388 return True |
| 389 |
384 return input_api.re.match( | 390 return input_api.re.match( |
385 r'.*[A-Za-z][A-Za-z_0-9]{%d,}.*' % long_symbol, line) | 391 r'.*[A-Za-z][A-Za-z_0-9]{%d,}.*' % long_symbol, line) |
386 | 392 |
387 def format_error(filename, line_num, line): | 393 def format_error(filename, line_num, line): |
388 return '%s, line %s, %s chars' % (filename, line_num, len(line)) | 394 return '%s, line %s, %s chars' % (filename, line_num, len(line)) |
389 | 395 |
390 errors = _FindNewViolationsOfRule(no_long_lines, input_api, | 396 errors = _FindNewViolationsOfRule(no_long_lines, input_api, |
391 source_file_filter, | 397 source_file_filter, |
392 error_formatter=format_error) | 398 error_formatter=format_error) |
393 if errors: | 399 if errors: |
(...skipping 755 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1149 for f in affected_files: | 1155 for f in affected_files: |
1150 cmd = ['gn', 'format', '--dry-run', f.AbsoluteLocalPath()] | 1156 cmd = ['gn', 'format', '--dry-run', f.AbsoluteLocalPath()] |
1151 rc = gn.main(cmd) | 1157 rc = gn.main(cmd) |
1152 if rc == 2: | 1158 if rc == 2: |
1153 warnings.append(output_api.PresubmitPromptWarning( | 1159 warnings.append(output_api.PresubmitPromptWarning( |
1154 '%s requires formatting. Please run `gn format --in-place %s`.' % ( | 1160 '%s requires formatting. Please run `gn format --in-place %s`.' % ( |
1155 f.AbsoluteLocalPath(), f.LocalPath()))) | 1161 f.AbsoluteLocalPath(), f.LocalPath()))) |
1156 # It's just a warning, so ignore other types of failures assuming they'll be | 1162 # It's just a warning, so ignore other types of failures assuming they'll be |
1157 # caught elsewhere. | 1163 # caught elsewhere. |
1158 return warnings | 1164 return warnings |
OLD | NEW |