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 608 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
619 if Find(filepath, black_list): | 619 if Find(filepath, black_list): |
620 dirnames.remove(item) | 620 dirnames.remove(item) |
621 for item in filenames: | 621 for item in filenames: |
622 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] | 622 filepath = input_api.os_path.join(dirpath, item)[path_len + 1:] |
623 if Find(filepath, white_list) and not Find(filepath, black_list): | 623 if Find(filepath, white_list) and not Find(filepath, black_list): |
624 files.append(filepath) | 624 files.append(filepath) |
625 return files | 625 return files |
626 | 626 |
627 | 627 |
628 def RunPylint(input_api, output_api, white_list=None, black_list=None, | 628 def RunPylint(input_api, output_api, white_list=None, black_list=None, |
629 disabled_warnings=None): | 629 disabled_warnings=None, extra_paths_list=None): |
630 """Run pylint on python files. | 630 """Run pylint on python files. |
631 | 631 |
632 The default white_list enforces looking only at *.py files. | 632 The default white_list enforces looking only at *.py files. |
633 """ | 633 """ |
634 white_list = tuple(white_list or ('.*\.py$',)) | 634 white_list = tuple(white_list or ('.*\.py$',)) |
635 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) | 635 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
636 extra_paths_list = extra_paths_list or [] | |
637 | |
636 if input_api.is_committing: | 638 if input_api.is_committing: |
637 error_type = output_api.PresubmitError | 639 error_type = output_api.PresubmitError |
638 else: | 640 else: |
639 error_type = output_api.PresubmitPromptWarning | 641 error_type = output_api.PresubmitPromptWarning |
640 | 642 |
641 # Only trigger if there is at least one python file affected. | 643 # Only trigger if there is at least one python file affected. |
642 src_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list) | 644 rel_path = lambda x : input_api.os_path.join(input_api.os_path.relpath( |
645 input_api.PresubmitLocalPath(), input_api.change.RepositoryRoot()), x) | |
646 src_filter = lambda x: input_api.FilterSourceFile( | |
647 x, map(rel_path, white_list), map(rel_path, black_list)) | |
643 if not input_api.AffectedSourceFiles(src_filter): | 648 if not input_api.AffectedSourceFiles(src_filter): |
649 input_api.logging.info('Skipping pylint: no matching changes.') | |
644 return [] | 650 return [] |
645 | 651 |
646 extra_args = ['--rcfile=%s' % input_api.os_path.join(_HERE, 'pylintrc')] | 652 extra_args = ['--rcfile=%s' % input_api.os_path.join(_HERE, 'pylintrc')] |
647 if disabled_warnings: | 653 if disabled_warnings: |
648 extra_args.extend(['-d', ','.join(disabled_warnings)]) | 654 extra_args.extend(['-d', ','.join(disabled_warnings)]) |
649 | 655 |
650 files = _FetchAllFiles(input_api, white_list, black_list) | 656 files = _FetchAllFiles(input_api, white_list, black_list) |
657 input_api.logging.info('Running pylint on: %s', files) | |
M-A Ruel
2013/01/07 15:32:46
Put this comment after line 659?
Isaac (away)
2013/01/07 18:46:28
Sure, done.
| |
651 if not files: | 658 if not files: |
652 return [] | 659 return [] |
653 | 660 |
654 # Copy the system path to the environment so pylint can find the right | 661 # Copy the system path to the environment so pylint can find the right |
655 # imports. | 662 # imports. |
656 env = input_api.environ.copy() | 663 env = input_api.environ.copy() |
657 import sys | 664 import sys |
658 env['PYTHONPATH'] = input_api.os_path.pathsep.join(sys.path) | 665 env['PYTHONPATH'] = input_api.os_path.pathsep.join( |
666 extra_paths_list + sys.path) | |
M-A Ruel
2013/01/07 15:32:46
That's a good idea. Should have done that before.
Isaac (away)
2013/01/07 18:46:28
Yeah it lets us avoid a try/finally loop and impor
| |
659 | 667 |
660 def run_lint(files): | 668 def run_lint(files): |
661 # We can't import pylint directly due to licensing issues, so we run | 669 # We can't import pylint directly due to licensing issues, so we run |
662 # it in another process. Windows needs help running python files so we | 670 # it in another process. Windows needs help running python files so we |
663 # explicitly specify the interpreter to use. It also has limitations on | 671 # explicitly specify the interpreter to use. It also has limitations on |
664 # the size of the command-line, so we pass arguments via a pipe. | 672 # the size of the command-line, so we pass arguments via a pipe. |
665 command = [input_api.python_executable, | 673 command = [input_api.python_executable, |
666 input_api.os_path.join(_HERE, 'third_party', 'pylint.py'), | 674 input_api.os_path.join(_HERE, 'third_party', 'pylint.py'), |
667 '--args-on-stdin'] | 675 '--args-on-stdin'] |
668 try: | 676 try: |
(...skipping 11 matching lines...) Expand all Loading... | |
680 return child.returncode | 688 return child.returncode |
681 except OSError: | 689 except OSError: |
682 return 'Pylint failed!' | 690 return 'Pylint failed!' |
683 | 691 |
684 result = None | 692 result = None |
685 # Always run pylint and pass it all the py files at once. | 693 # Always run pylint and pass it all the py files at once. |
686 # Passing py files one at time is slower and can produce | 694 # Passing py files one at time is slower and can produce |
687 # different results. input_api.verbose used to be used | 695 # different results. input_api.verbose used to be used |
688 # to enable this behaviour but differing behaviour in | 696 # to enable this behaviour but differing behaviour in |
689 # verbose mode is not desirable. | 697 # verbose mode is not desirable. |
690 if True: | 698 print('Running pylint on %d files.' % len(files)) |
M-A Ruel
2013/01/07 15:32:46
While it's odd, leave it there. Because of issues
Isaac (away)
2013/01/07 18:46:28
OK. I added a comment explaining the unreachable c
| |
691 result = run_lint(sorted(files)) | 699 result = run_lint(sorted(files)) |
692 else: | |
693 for filename in sorted(files): | |
694 print('Running pylint on %s' % filename) | |
695 result = run_lint([filename]) or result | |
696 if isinstance(result, basestring): | 700 if isinstance(result, basestring): |
697 return [error_type(result)] | 701 return [error_type(result)] |
698 elif result: | 702 elif result: |
699 return [error_type('Fix pylint errors first.')] | 703 return [error_type('Fix pylint errors first.')] |
700 return [] | 704 return [] |
701 | 705 |
702 | 706 |
703 # TODO(dpranke): Get the host_url from the input_api instead | 707 # TODO(dpranke): Get the host_url from the input_api instead |
704 def CheckRietveldTryJobExecution(dummy_input_api, dummy_output_api, | 708 def CheckRietveldTryJobExecution(dummy_input_api, dummy_output_api, |
705 dummy_host_url, dummy_platforms, | 709 dummy_host_url, dummy_platforms, |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
981 snapshot("checking description") | 985 snapshot("checking description") |
982 results.extend(input_api.canned_checks.CheckChangeHasDescription( | 986 results.extend(input_api.canned_checks.CheckChangeHasDescription( |
983 input_api, output_api)) | 987 input_api, output_api)) |
984 results.extend(input_api.canned_checks.CheckDoNotSubmitInDescription( | 988 results.extend(input_api.canned_checks.CheckDoNotSubmitInDescription( |
985 input_api, output_api)) | 989 input_api, output_api)) |
986 snapshot("checking do not submit in files") | 990 snapshot("checking do not submit in files") |
987 results.extend(input_api.canned_checks.CheckDoNotSubmitInFiles( | 991 results.extend(input_api.canned_checks.CheckDoNotSubmitInFiles( |
988 input_api, output_api)) | 992 input_api, output_api)) |
989 snapshot("done") | 993 snapshot("done") |
990 return results | 994 return results |
OLD | NEW |