From a606628b16d4e6ceb41b0444d02187db6fef8d73 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Mon, 5 Jun 2017 08:49:59 -0600 Subject: [PATCH] Extract tools/linter_lib/pep8.py --- tools/lint | 109 +------------------------------------ tools/linter_lib/pep8.py | 114 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 108 deletions(-) create mode 100644 tools/linter_lib/pep8.py diff --git a/tools/lint b/tools/lint index 8a24ffad93..5f32d7656f 100755 --- a/tools/lint +++ b/tools/lint @@ -28,114 +28,6 @@ def bright_red_output(): sys.stdout.write('\x1B[0m') -def check_pep8(files): - # type: (List[str]) -> bool - - def run_pycodestyle(files, ignored_rules): - # type: (List[str], List[str]) -> bool - failed = False - pep8 = subprocess.Popen( - ['pycodestyle'] + files + ['--ignore={rules}'.format(rules=','.join(ignored_rules))], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) - for pipe in (pep8.stdout, pep8.stderr): - assert(pipe is not None) # convince mypy that pipe cannot be None - for ln in pipe: - sys.stdout.write(ln) - failed = True - return failed - - failed = False - ignored_rules = [ - # Each of these rules are ignored for the explained reason. - - # "multiple spaces before operator" - # There are several typos here, but also several instances that are - # being used for alignment in dict keys/values using the `dict` - # constructor. We could fix the alignment cases by switching to the `{}` - # constructor, but it makes fixing this rule a little less - # straightforward. - 'E221', - - # 'missing whitespace around arithmetic operator' - # This should possibly be cleaned up, though changing some of - # these may make the code less readable. - 'E226', - - # "unexpected spaces around keyword / parameter equals" - # Many of these should be fixed, but many are also being used for - # alignment/making the code easier to read. - 'E251', - - # "block comment should start with '#'" - # These serve to show which lines should be changed in files customized - # by the user. We could probably resolve one of E265 or E266 by - # standardizing on a single style for lines that the user might want to - # change. - 'E265', - - # "too many leading '#' for block comment" - # Most of these are there for valid reasons. - 'E266', - - # "expected 2 blank lines after class or function definition" - # Zulip only uses 1 blank line after class/function - # definitions; the PEP-8 recommendation results in super sparse code. - 'E302', 'E305', - - # "module level import not at top of file" - # Most of these are there for valid reasons, though there might be a - # few that could be eliminated. - 'E402', - - # "line too long" - # Zulip is a bit less strict about line length, and has its - # own check for this (see max_length) - 'E501', - - # "do not assign a lambda expression, use a def" - # Fixing these would probably reduce readability in most cases. - 'E731', - ] - - # TODO: Clear up this list of violations. - IGNORE_FILES_PEPE261 = [ - 'api/zulip/__init__.py', - 'tools/run-dev.py', - 'zerver/lib/bugdown/__init__.py', - 'zerver/models.py', - 'zerver/tests/test_bugdown.py', - 'zerver/tests/test_events.py', - 'zerver/tests/test_messages.py', - 'zerver/tests/test_narrow.py', - 'zerver/tests/test_outgoing_webhook_system.py', - 'zerver/tests/test_realm.py', - 'zerver/tests/test_signup.py', - 'zerver/tests/test_subs.py', - 'zerver/tests/test_upload.py', - 'zerver/tornado/socket.py', - 'zerver/tornado/websocket_client.py', - 'zerver/worker/queue_processors.py', - 'zilencer/management/commands/populate_db.py', - 'zproject/dev_settings.py', - 'zproject/prod_settings_template.py', - 'zproject/settings.py', - ] - - filtered_files = [fn for fn in files if fn not in IGNORE_FILES_PEPE261] - filtered_files_E261 = [fn for fn in files if fn in IGNORE_FILES_PEPE261] - - if len(files) == 0: - return False - if not len(filtered_files) == 0: - failed = run_pycodestyle(filtered_files, ignored_rules) - if not len(filtered_files_E261) == 0: - # Adding an extra ignore rule for these files since they still remain in - # violation of PEP-E261. - failed = run_pycodestyle(filtered_files_E261, ignored_rules + ['E261']) - - return failed - - def run_parallel(lint_functions): # type: (Dict[str, Callable[[], int]]) -> bool pids = [] @@ -187,6 +79,7 @@ def run(): from tools.linter_lib.custom_check import build_custom_checkers from tools.linter_lib.exclude import EXCLUDED_FILES from tools.linter_lib.pyflakes import check_pyflakes + from tools.linter_lib.pep8 import check_pep8 from tools.lib.test_script import ( get_provisioning_status, diff --git a/tools/linter_lib/pep8.py b/tools/linter_lib/pep8.py new file mode 100644 index 0000000000..b7a0123154 --- /dev/null +++ b/tools/linter_lib/pep8.py @@ -0,0 +1,114 @@ +from __future__ import print_function +from __future__ import absolute_import + +import subprocess +import sys + +from typing import List + +def check_pep8(files): + # type: (List[str]) -> bool + + def run_pycodestyle(files, ignored_rules): + # type: (List[str], List[str]) -> bool + failed = False + pep8 = subprocess.Popen( + ['pycodestyle'] + files + ['--ignore={rules}'.format(rules=','.join(ignored_rules))], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) + for pipe in (pep8.stdout, pep8.stderr): + assert(pipe is not None) # convince mypy that pipe cannot be None + for ln in pipe: + sys.stdout.write(ln) + failed = True + return failed + + failed = False + ignored_rules = [ + # Each of these rules are ignored for the explained reason. + + # "multiple spaces before operator" + # There are several typos here, but also several instances that are + # being used for alignment in dict keys/values using the `dict` + # constructor. We could fix the alignment cases by switching to the `{}` + # constructor, but it makes fixing this rule a little less + # straightforward. + 'E221', + + # 'missing whitespace around arithmetic operator' + # This should possibly be cleaned up, though changing some of + # these may make the code less readable. + 'E226', + + # "unexpected spaces around keyword / parameter equals" + # Many of these should be fixed, but many are also being used for + # alignment/making the code easier to read. + 'E251', + + # "block comment should start with '#'" + # These serve to show which lines should be changed in files customized + # by the user. We could probably resolve one of E265 or E266 by + # standardizing on a single style for lines that the user might want to + # change. + 'E265', + + # "too many leading '#' for block comment" + # Most of these are there for valid reasons. + 'E266', + + # "expected 2 blank lines after class or function definition" + # Zulip only uses 1 blank line after class/function + # definitions; the PEP-8 recommendation results in super sparse code. + 'E302', 'E305', + + # "module level import not at top of file" + # Most of these are there for valid reasons, though there might be a + # few that could be eliminated. + 'E402', + + # "line too long" + # Zulip is a bit less strict about line length, and has its + # own check for this (see max_length) + 'E501', + + # "do not assign a lambda expression, use a def" + # Fixing these would probably reduce readability in most cases. + 'E731', + ] + + # TODO: Clear up this list of violations. + IGNORE_FILES_PEPE261 = [ + 'api/zulip/__init__.py', + 'tools/run-dev.py', + 'zerver/lib/bugdown/__init__.py', + 'zerver/models.py', + 'zerver/tests/test_bugdown.py', + 'zerver/tests/test_events.py', + 'zerver/tests/test_messages.py', + 'zerver/tests/test_narrow.py', + 'zerver/tests/test_outgoing_webhook_system.py', + 'zerver/tests/test_realm.py', + 'zerver/tests/test_signup.py', + 'zerver/tests/test_subs.py', + 'zerver/tests/test_upload.py', + 'zerver/tornado/socket.py', + 'zerver/tornado/websocket_client.py', + 'zerver/worker/queue_processors.py', + 'zilencer/management/commands/populate_db.py', + 'zproject/dev_settings.py', + 'zproject/prod_settings_template.py', + 'zproject/settings.py', + ] + + filtered_files = [fn for fn in files if fn not in IGNORE_FILES_PEPE261] + filtered_files_E261 = [fn for fn in files if fn in IGNORE_FILES_PEPE261] + + if len(files) == 0: + return False + if not len(filtered_files) == 0: + failed = run_pycodestyle(filtered_files, ignored_rules) + if not len(filtered_files_E261) == 0: + # Adding an extra ignore rule for these files since they still remain in + # violation of PEP-E261. + failed = run_pycodestyle(filtered_files_E261, ignored_rules + ['E261']) + + return failed