| 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 |
| 8 _HERE = _os.path.dirname(_os.path.abspath(__file__)) |
| 9 |
| 7 | 10 |
| 8 ### Description checks | 11 ### Description checks |
| 9 | 12 |
| 10 def CheckChangeHasTestField(input_api, output_api): | 13 def CheckChangeHasTestField(input_api, output_api): |
| 11 """Requires that the changelist have a TEST= field.""" | 14 """Requires that the changelist have a TEST= field.""" |
| 12 if input_api.change.TEST: | 15 if input_api.change.TEST: |
| 13 return [] | 16 return [] |
| 14 else: | 17 else: |
| 15 return [output_api.PresubmitNotifyResult( | 18 return [output_api.PresubmitNotifyResult( |
| 16 'If this change requires manual test instructions to QA team, add ' | 19 'If this change requires manual test instructions to QA team, add ' |
| (...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] | 611 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] |
| 609 if Find(filepath, black_list): | 612 if Find(filepath, black_list): |
| 610 dirnames.remove(item) | 613 dirnames.remove(item) |
| 611 for item in filenames: | 614 for item in filenames: |
| 612 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] | 615 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] |
| 613 if Find(filepath, white_list) and not Find(filepath, black_list): | 616 if Find(filepath, white_list) and not Find(filepath, black_list): |
| 614 files.append(filepath) | 617 files.append(filepath) |
| 615 return files | 618 return files |
| 616 | 619 |
| 617 | 620 |
| 618 def RunPylint(input_api, output_api, white_list=None, black_list=None): | 621 def RunPylint(input_api, output_api, white_list=None, black_list=None, |
| 622 disabled_warnings=None): |
| 619 """Run pylint on python files. | 623 """Run pylint on python files. |
| 620 | 624 |
| 621 The default white_list enforces looking only a *.py files. | 625 The default white_list enforces looking only a *.py files. |
| 622 """ | 626 """ |
| 623 white_list = tuple(white_list or ('.*\.py$',)) | 627 white_list = tuple(white_list or ('.*\.py$',)) |
| 624 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) | 628 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
| 625 if input_api.is_committing: | 629 if input_api.is_committing: |
| 626 error_type = output_api.PresubmitError | 630 error_type = output_api.PresubmitError |
| 627 else: | 631 else: |
| 628 error_type = output_api.PresubmitPromptWarning | 632 error_type = output_api.PresubmitPromptWarning |
| 629 | 633 |
| 630 # Only trigger if there is at least one python file affected. | 634 # Only trigger if there is at least one python file affected. |
| 631 src_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list) | 635 src_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list) |
| 632 if not input_api.AffectedSourceFiles(src_filter): | 636 if not input_api.AffectedSourceFiles(src_filter): |
| 633 return [] | 637 return [] |
| 634 | 638 |
| 639 extra_args = ['--rcfile=%s' % input_api.os_path.join(_HERE, 'pylintrc')] |
| 640 if disabled_warnings: |
| 641 extra_args.extend(['-d', ','.join(disabled_warnings)]) |
| 642 |
| 635 # On certain pylint/python version combination, running pylint throws a lot of | 643 # On certain pylint/python version combination, running pylint throws a lot of |
| 636 # warning messages. | 644 # warning messages. |
| 637 import warnings | 645 import warnings |
| 638 warnings.filterwarnings('ignore', category=DeprecationWarning) | 646 warnings.filterwarnings('ignore', category=DeprecationWarning) |
| 639 try: | 647 try: |
| 640 files = _FetchAllFiles(input_api, white_list, black_list) | 648 files = _FetchAllFiles(input_api, white_list, black_list) |
| 641 if not files: | 649 if not files: |
| 642 return [] | 650 return [] |
| 643 # Now that at least one python file was modified and all the python files | 651 # Now that at least one python file was modified and all the python files |
| 644 # were listed, try to run pylint. | 652 # were listed, try to run pylint. |
| 645 try: | 653 try: |
| 646 from pylint import lint | 654 from pylint import lint |
| 647 from pylint.utils import UnknownMessage | 655 from pylint.utils import UnknownMessage |
| 648 input_api.logging.debug( | 656 input_api.logging.debug( |
| 649 'Using pylint v%s from %s' % (lint.version, lint.__file__)) | 657 'Using pylint v%s from %s' % (lint.version, lint.__file__)) |
| 650 except ImportError: | 658 except ImportError: |
| 651 if input_api.platform == 'win32': | 659 if input_api.platform == 'win32': |
| 652 return [output_api.PresubmitNotifyResult( | 660 return [output_api.PresubmitNotifyResult( |
| 653 'Warning: Can\'t run pylint because it is not installed. Please ' | 661 'Warning: Can\'t run pylint because it is not installed. Please ' |
| 654 'install manually\n' | 662 'install manually\n' |
| 655 'Cannot do static analysis of python files.')] | 663 'Cannot do static analysis of python files.')] |
| 656 return [output_api.PresubmitError( | 664 return [output_api.PresubmitError( |
| 657 'Please install pylint with "sudo apt-get install python-setuptools; ' | 665 'Please install pylint with "sudo apt-get install python-setuptools; ' |
| 658 'sudo easy_install pylint"\n' | 666 'sudo easy_install pylint"\n' |
| 659 'or visit http://pypi.python.org/pypi/setuptools.\n' | 667 'or visit http://pypi.python.org/pypi/setuptools.\n' |
| 660 'Cannot do static analysis of python files.')] | 668 'Cannot do static analysis of python files.')] |
| 661 | 669 |
| 662 def run_lint(files): | 670 def run_lint(files): |
| 663 try: | 671 try: |
| 664 lint.Run(files) | 672 lint.Run(files + extra_args) |
| 665 assert False | 673 assert False |
| 666 except SystemExit, e: | 674 except SystemExit, e: |
| 667 # pylint has the bad habit of calling sys.exit(), trap it here. | 675 # pylint has the bad habit of calling sys.exit(), trap it here. |
| 668 return e.code | 676 return e.code |
| 669 except UnknownMessage, e: | 677 except UnknownMessage, e: |
| 670 return 'Please upgrade pylint: %s' % e | 678 return 'Please upgrade pylint: %s' % e |
| 671 | 679 |
| 672 result = None | 680 result = None |
| 673 if not input_api.verbose: | 681 if not input_api.verbose: |
| 674 result = run_lint(sorted(files)) | 682 result = run_lint(sorted(files)) |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 935 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( | 943 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( |
| 936 input_api, output_api)) | 944 input_api, output_api)) |
| 937 snapshot("checking license") | 945 snapshot("checking license") |
| 938 results.extend(input_api.canned_checks.CheckLicense( | 946 results.extend(input_api.canned_checks.CheckLicense( |
| 939 input_api, output_api, license_header, source_file_filter=sources)) | 947 input_api, output_api, license_header, source_file_filter=sources)) |
| 940 snapshot("checking was uploaded") | 948 snapshot("checking was uploaded") |
| 941 results.extend(input_api.canned_checks.CheckChangeWasUploaded( | 949 results.extend(input_api.canned_checks.CheckChangeWasUploaded( |
| 942 input_api, output_api)) | 950 input_api, output_api)) |
| 943 snapshot("done") | 951 snapshot("done") |
| 944 return results | 952 return results |
| OLD | NEW |