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