| OLD | NEW |
| 1 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2010 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 | 7 |
| 8 ### Description checks | 8 ### Description checks |
| 9 | 9 |
| 10 def CheckChangeHasTestField(input_api, output_api): | 10 def CheckChangeHasTestField(input_api, output_api): |
| (...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 if Find(filepath, white_list) and not Find(filepath, black_list): | 537 if Find(filepath, white_list) and not Find(filepath, black_list): |
| 538 files.append(filepath) | 538 files.append(filepath) |
| 539 return files | 539 return files |
| 540 | 540 |
| 541 | 541 |
| 542 def RunPylint(input_api, output_api, white_list=None, black_list=None): | 542 def RunPylint(input_api, output_api, white_list=None, black_list=None): |
| 543 """Run pylint on python files. | 543 """Run pylint on python files. |
| 544 | 544 |
| 545 The default white_list enforces looking only a *.py files. | 545 The default white_list enforces looking only a *.py files. |
| 546 """ | 546 """ |
| 547 verbose = False | |
| 548 white_list = white_list or ['.*\.py$'] | 547 white_list = white_list or ['.*\.py$'] |
| 549 black_list = black_list or input_api.DEFAULT_BLACK_LIST | 548 black_list = black_list or input_api.DEFAULT_BLACK_LIST |
| 550 if input_api.is_committing: | |
| 551 error_type = output_api.PresubmitError | |
| 552 else: | |
| 553 error_type = output_api.PresubmitPromptWarning | |
| 554 | 549 |
| 555 # Only trigger if there is at least one python file affected. | 550 # Only trigger if there is at least one python file affected. |
| 556 src_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list) | 551 src_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list) |
| 557 if not input_api.AffectedSourceFiles(src_filter): | 552 if not input_api.AffectedSourceFiles(src_filter): |
| 558 return [] | 553 return [] |
| 559 | 554 |
| 560 # On certain pylint/python version combination, running pylint throws a lot of | 555 # On certain pylint/python version combination, running pylint throws a lot of |
| 561 # warning messages. | 556 # warning messages. |
| 562 import warnings | 557 import warnings |
| 563 warnings.filterwarnings('ignore', category=DeprecationWarning) | 558 warnings.filterwarnings('ignore', category=DeprecationWarning) |
| 564 try: | 559 try: |
| 565 files = _FetchAllFiles(input_api, white_list, black_list) | 560 files = _FetchAllFiles(input_api, white_list, black_list) |
| 566 if not files: | 561 if not files: |
| 567 return [] | 562 return [] |
| 568 # Now that at least one python file was modified and all the python files | 563 # Now that at least one python file was modified and all the python files |
| 569 # were listed, try to run pylint. | 564 # were listed, try to run pylint. |
| 570 try: | 565 try: |
| 571 from pylint import lint | 566 from pylint import lint |
| 567 result = lint.Run(sorted(files)) |
| 568 except SystemExit, e: |
| 569 # pylint has the bad habit of calling sys.exit(), trap it here. |
| 570 result = e.code |
| 572 except ImportError: | 571 except ImportError: |
| 573 if input_api.platform == 'win32': | 572 if input_api.platform == 'win32': |
| 574 return [output_api.PresubmitNotifyResult( | 573 return [output_api.PresubmitNotifyResult( |
| 575 'Warning: Can\'t run pylint because it is not installed. Please ' | 574 'Warning: Can\'t run pylint because it is not installed. Please ' |
| 576 'install manually\n' | 575 'install manually\n' |
| 577 'Cannot do static analysis of python files.')] | 576 'Cannot do static analysis of python files.')] |
| 578 return [output_api.PresubmitError( | 577 return [output_api.PresubmitError( |
| 579 'Please install pylint with "sudo apt-get install python-setuptools; ' | 578 'Please install pylint with "sudo apt-get install python-setuptools; ' |
| 580 'sudo easy_install pylint"\n' | 579 'sudo easy_install pylint"\n' |
| 581 'or visit http://pypi.python.org/pypi/setuptools.\n' | 580 'or visit http://pypi.python.org/pypi/setuptools.\n' |
| 582 'Cannot do static analysis of python files.')] | 581 'Cannot do static analysis of python files.')] |
| 583 | |
| 584 def run_lint(files): | |
| 585 try: | |
| 586 lint.Run(files) | |
| 587 assert False | |
| 588 except SystemExit, e: | |
| 589 # pylint has the bad habit of calling sys.exit(), trap it here. | |
| 590 return e.code | |
| 591 | |
| 592 result = None | |
| 593 if not verbose: | |
| 594 result = run_lint(sorted(files)) | |
| 595 else: | |
| 596 for filename in sorted(files): | |
| 597 print('Running pylint on %s' % filename) | |
| 598 out = run_lint([filename]) | |
| 599 if out: | |
| 600 result = out | |
| 601 if result: | 582 if result: |
| 583 if input_api.is_committing: |
| 584 error_type = output_api.PresubmitError |
| 585 else: |
| 586 error_type = output_api.PresubmitPromptWarning |
| 602 return [error_type('Fix pylint errors first.')] | 587 return [error_type('Fix pylint errors first.')] |
| 603 return [] | 588 return [] |
| 604 finally: | 589 finally: |
| 605 warnings.filterwarnings('default', category=DeprecationWarning) | 590 warnings.filterwarnings('default', category=DeprecationWarning) |
| 606 | 591 |
| 607 | |
| 608 # TODO(dpranke): Get the host_url from the input_api instead | 592 # TODO(dpranke): Get the host_url from the input_api instead |
| 609 def CheckRietveldTryJobExecution(input_api, output_api, host_url, platforms, | 593 def CheckRietveldTryJobExecution(input_api, output_api, host_url, platforms, |
| 610 owner): | 594 owner): |
| 611 if not input_api.is_committing: | 595 if not input_api.is_committing: |
| 612 return [] | 596 return [] |
| 613 if not input_api.change.issue or not input_api.change.patchset: | 597 if not input_api.change.issue or not input_api.change.patchset: |
| 614 return [] | 598 return [] |
| 615 url = '%s/%d/get_build_results/%d' % ( | 599 url = '%s/%d/get_build_results/%d' % ( |
| 616 host_url, input_api.change.issue, input_api.change.patchset) | 600 host_url, input_api.change.issue, input_api.change.patchset) |
| 617 try: | 601 try: |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 input_api, output_api, source_file_filter=text_files)) | 879 input_api, output_api, source_file_filter=text_files)) |
| 896 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( | 880 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( |
| 897 input_api, output_api)) | 881 input_api, output_api)) |
| 898 results.extend(input_api.canned_checks.CheckLicense( | 882 results.extend(input_api.canned_checks.CheckLicense( |
| 899 input_api, output_api, license_header, source_file_filter=sources)) | 883 input_api, output_api, license_header, source_file_filter=sources)) |
| 900 results.extend(_CheckConstNSObject( | 884 results.extend(_CheckConstNSObject( |
| 901 input_api, output_api, source_file_filter=sources)) | 885 input_api, output_api, source_file_filter=sources)) |
| 902 results.extend(_CheckSingletonInHeaders( | 886 results.extend(_CheckSingletonInHeaders( |
| 903 input_api, output_api, source_file_filter=sources)) | 887 input_api, output_api, source_file_filter=sources)) |
| 904 return results | 888 return results |
| OLD | NEW |