Skip to content

Commit

Permalink
Fixes oppia#5529: Print python lint errors at the end of the check (o…
Browse files Browse the repository at this point in the history
…ppia#6005)

* Fixes oppia#5529: Print python lint errors at the end of the check

* Review changes
  • Loading branch information
apb7 authored Dec 22, 2018
1 parent 238f8d5 commit 5481b28
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions scripts/pre_commit_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
# Pylint has issues with the import order of argparse.
# pylint: disable=wrong-import-order
import HTMLParser
import StringIO
import argparse
import ast
import contextlib
import fnmatch
import multiprocessing
import os
Expand Down Expand Up @@ -384,6 +386,16 @@ def _get_all_files_in_directory(dir_path, excluded_glob_patterns):
return files_in_directory


@contextlib.contextmanager
def _redirect_stdout(new_target):
old_target = sys.stdout
sys.stdout = new_target
try:
yield new_target
finally:
sys.stdout = old_target


def _lint_css_files(
node_path, stylelint_path, config_path, files_to_lint, stdout, result):
"""Prints a list of lint errors in the given list of CSS files.
Expand Down Expand Up @@ -524,18 +536,21 @@ def _lint_py_files(config_pylint, config_pycodestyle, files_to_lint, result):
print 'Linting Python files %s to %s...' % (
current_batch_start_index + 1, current_batch_end_index)

# This line invokes Pylint and prints its output to the console.
pylinter = lint.Run(
current_files_to_lint + [config_pylint],
exit=False).linter
# These lines invoke Pycodestyle.
style_guide = pycodestyle.StyleGuide(config_file=config_pycodestyle)
pycodestyle_report = style_guide.check_files(
paths=current_files_to_lint)
# This line prints Pycodestyle's output to the console.
pycodestyle_report.print_statistics()
target_stdout = StringIO.StringIO()
with _redirect_stdout(target_stdout):
# This line invokes Pylint and prints its output
# to the target stdout.
pylinter = lint.Run(
current_files_to_lint + [config_pylint],
exit=False).linter
# These lines invoke Pycodestyle and print its output
# to the target stdout.
style_guide = pycodestyle.StyleGuide(config_file=config_pycodestyle)
pycodestyle_report = style_guide.check_files(
paths=current_files_to_lint)

if pylinter.msg_status != 0 or pycodestyle_report.get_count() != 0:
result.put(target_stdout.getvalue())
are_there_errors = True

current_batch_start_index = current_batch_end_index
Expand Down

0 comments on commit 5481b28

Please sign in to comment.