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 | 10 |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 def CheckLongLines(input_api, output_api, maxlen, source_file_filter=None): | 314 def CheckLongLines(input_api, output_api, maxlen, source_file_filter=None): |
315 """Checks that there aren't any lines longer than maxlen characters in any of | 315 """Checks that there aren't any lines longer than maxlen characters in any of |
316 the text files to be submitted. | 316 the text files to be submitted. |
317 """ | 317 """ |
318 maxlens = { | 318 maxlens = { |
319 'java': 100, | 319 'java': 100, |
320 # This is specifically for Android's handwritten makefiles (Android.mk). | 320 # This is specifically for Android's handwritten makefiles (Android.mk). |
321 'mk': 200, | 321 'mk': 200, |
322 '': maxlen, | 322 '': maxlen, |
323 } | 323 } |
324 # Note: these are C++ specific but processed on all languages. :( | |
325 MACROS = ('#define', '#include', '#import', '#pragma', '#if', '#endif') | |
326 | 324 |
327 # Special java statements. | 325 # Language specific exceptions to max line length. |
328 SPECIAL_JAVA_STARTS = ('package ', 'import ') | 326 # '.h' files count as obj-c, since OBJC_EXCPTS are a superset of CPP_EXCPTS. |
327 CPP_FILE_EXTS = ('c', 'cc') | |
328 CPP_EXCPTS = ('#define', '#include', '#import', '#pragma', '#if', '#endif') | |
329 JAVA_FILE_EXTS = ('java') | |
M-A Ruel
2013/12/02 23:56:27
This was not doing what you thought it was doing.
erikchen2
2013/12/03 00:20:09
ack, thanks
| |
330 JAVA_EXCPTS = ('package ', 'import ') | |
M-A Ruel
2013/12/02 23:56:27
sort
erikchen2
2013/12/03 00:20:09
Done.
| |
331 OBJC_FILE_EXTS = ('h', 'm', 'mm') | |
332 OBJC_EXCPTS = ('#define', '#include', '#import', '#pragma', '#if', | |
333 '#endif', '#import') | |
M-A Ruel
2013/12/02 23:56:27
duplicate of #import
erikchen2
2013/12/03 00:20:09
Done.
| |
334 | |
335 LANGUAGE_EXCEPTIONS = [ | |
M-A Ruel
2013/12/02 23:56:27
I'd used a list of tuple instead.
LANGUAGE_EXCEPTI
erikchen2
2013/12/03 00:20:09
Done.
| |
336 {'extensions' : CPP_FILE_EXTS, 'excpts' : CPP_EXCPTS}, | |
337 {'extensions' : JAVA_FILE_EXTS, 'excpts' : JAVA_EXCPTS}, | |
338 {'extensions' : OBJC_FILE_EXTS, 'excpts' : OBJC_EXCPTS}, | |
339 ] | |
329 | 340 |
330 def no_long_lines(file_extension, line): | 341 def no_long_lines(file_extension, line): |
331 # Allow special java statements to be as long as necessary. | 342 # Check for language specific exceptions. |
332 if file_extension == 'java' and line.startswith(SPECIAL_JAVA_STARTS): | 343 for language in LANGUAGE_EXCEPTIONS: |
M-A Ruel
2013/12/02 23:56:27
if any(
file_extensions in ext and line.starts
erikchen2
2013/12/03 00:20:09
Done.
| |
333 return True | 344 if (file_extension in language['extensions'] and |
345 line.startswith(language['excpts'])): | |
346 return True | |
334 | 347 |
335 file_maxlen = maxlens.get(file_extension, maxlens['']) | 348 file_maxlen = maxlens.get(file_extension, maxlens['']) |
336 # Stupidly long symbols that needs to be worked around if takes 66% of line. | 349 # Stupidly long symbols that needs to be worked around if takes 66% of line. |
337 long_symbol = file_maxlen * 2 / 3 | 350 long_symbol = file_maxlen * 2 / 3 |
338 # Hard line length limit at 50% more. | 351 # Hard line length limit at 50% more. |
339 extra_maxlen = file_maxlen * 3 / 2 | 352 extra_maxlen = file_maxlen * 3 / 2 |
340 | 353 |
341 line_len = len(line) | 354 line_len = len(line) |
342 if line_len <= file_maxlen: | 355 if line_len <= file_maxlen: |
343 return True | 356 return True |
344 | 357 |
345 if line_len > extra_maxlen: | 358 if line_len > extra_maxlen: |
346 return False | 359 return False |
347 | 360 |
348 return ( | 361 return ( |
349 line.startswith(MACROS) or | |
350 any((url in line) for url in ('http://', 'https://')) or | 362 any((url in line) for url in ('http://', 'https://')) or |
351 input_api.re.match( | 363 input_api.re.match( |
352 r'.*[A-Za-z][A-Za-z_0-9]{%d,}.*' % long_symbol, line)) | 364 r'.*[A-Za-z][A-Za-z_0-9]{%d,}.*' % long_symbol, line)) |
353 | 365 |
354 def format_error(filename, line_num, line): | 366 def format_error(filename, line_num, line): |
355 return '%s, line %s, %s chars' % (filename, line_num, len(line)) | 367 return '%s, line %s, %s chars' % (filename, line_num, len(line)) |
356 | 368 |
357 errors = _FindNewViolationsOfRule(no_long_lines, input_api, | 369 errors = _FindNewViolationsOfRule(no_long_lines, input_api, |
358 source_file_filter, | 370 source_file_filter, |
359 error_formatter=format_error) | 371 error_formatter=format_error) |
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1023 snapshot("checking description") | 1035 snapshot("checking description") |
1024 results.extend(input_api.canned_checks.CheckChangeHasDescription( | 1036 results.extend(input_api.canned_checks.CheckChangeHasDescription( |
1025 input_api, output_api)) | 1037 input_api, output_api)) |
1026 results.extend(input_api.canned_checks.CheckDoNotSubmitInDescription( | 1038 results.extend(input_api.canned_checks.CheckDoNotSubmitInDescription( |
1027 input_api, output_api)) | 1039 input_api, output_api)) |
1028 snapshot("checking do not submit in files") | 1040 snapshot("checking do not submit in files") |
1029 results.extend(input_api.canned_checks.CheckDoNotSubmitInFiles( | 1041 results.extend(input_api.canned_checks.CheckDoNotSubmitInFiles( |
1030 input_api, output_api)) | 1042 input_api, output_api)) |
1031 snapshot("done") | 1043 snapshot("done") |
1032 return results | 1044 return results |
OLD | NEW |