Skip to content

Instantly share code, notes, and snippets.

@neelchauhan
Created October 27, 2016 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neelchauhan/849ef8eb7e15fb8370c653abcc5ab4e2 to your computer and use it in GitHub Desktop.
Save neelchauhan/849ef8eb7e15fb8370c653abcc5ab4e2 to your computer and use it in GitHub Desktop.
Patch for Stem to reference change of pep8 name to pycodestyle (and add fallback)
From 36e244c05468c27d14ac61a5c3f6f7feb5c64413 Mon Sep 17 00:00:00 2001
From: Neel Chauhan <neel@neelc.org>
Date: Wed, 26 Oct 2016 20:35:15 -0400
Subject: [PATCH 1/2] Switch to 'import pycodestyle' and add fallback mechanism
---
stem/util/test_tools.py | 74 ++++++++++++++++++++++++++++++-------------------
1 file changed, 45 insertions(+), 29 deletions(-)
diff --git a/stem/util/test_tools.py b/stem/util/test_tools.py
index 9a9f7f6..991d61d 100644
--- a/stem/util/test_tools.py
+++ b/stem/util/test_tools.py
@@ -11,9 +11,9 @@ Helper functions for testing.
clean_orphaned_pyc - delete *.pyc files without corresponding *.py
is_pyflakes_available - checks if pyflakes is available
- is_pep8_available - checks if pep8 is available
+ is_pycodestyle_available - checks if pycodestyle is available
- stylistic_issues - checks for PEP8 and other stylistic issues
+ stylistic_issues - checks for pycodestyle and other stylistic issues
pyflakes_issues - static checks for problems via pyflakes
"""
@@ -26,7 +26,7 @@ import stem.util.conf
import stem.util.system
CONFIG = stem.util.conf.config_dict('test', {
- 'pep8.ignore': [],
+ 'pycodestyle.ignore': [],
'pyflakes.ignore': [],
'exclude_paths': [],
})
@@ -34,7 +34,7 @@ CONFIG = stem.util.conf.config_dict('test', {
class Issue(collections.namedtuple('Issue', ['line_number', 'message', 'line'])):
"""
- Issue encountered by pyflakes or pep8.
+ Issue encountered by pyflakes or pycodestyle.
:var int line_number: line number the issue occured on
:var str message: description of the issue
@@ -86,6 +86,18 @@ def clean_orphaned_pyc(paths):
return orphaned_pyc
+def module_exists(module_name):
+ """
+ Checks if a module exists
+
+ :returns: **True** if module exists and **False** otherwise
+ """
+ try:
+ mod = __import__(module_name)
+ except ImportError:
+ return False
+ else:
+ return True
def is_pyflakes_available():
"""
@@ -102,44 +114,45 @@ def is_pyflakes_available():
return False
-def is_pep8_available():
+def is_pycodestyle_available():
"""
- Checks if pep8 is availalbe.
+ Checks if pycodestyle is availalbe.
- :returns: **True** if we can use pep8 and **False** otherwise
+ :returns: **True** if we can use pycodestyle and **False** otherwise
"""
- try:
- import pep8
-
- if not hasattr(pep8, 'BaseReport'):
- raise ImportError()
-
- return True
- except ImportError:
+ if module_exists('pycodestyle'):
+ import pycodestyle
+ elif module_exists('pep8'):
+ import pep8 as pycodestyle
+ else:
return False
+ if not hasattr(pycodestyle, 'BaseReport'):
+ return False
+ else:
+ return True
def stylistic_issues(paths, check_newlines = False, check_exception_keyword = False, prefer_single_quotes = False):
"""
- Checks for stylistic issues that are an issue according to the parts of PEP8
- we conform to. You can suppress PEP8 issues by making a 'test' configuration
- that sets 'pep8.ignore'.
+ Checks for stylistic issues that are an issue according to the parts of
+ pycodestyle we conform to. You can suppress pycodestyle issues by making a
+ 'test' configuration that sets 'pycodestyle.ignore'.
For example, with a 'test/settings.cfg' of...
::
- # PEP8 compliance issues that we're ignoreing...
+ # pycodestyle compliance issues that we're ignoreing...
#
# * E111 and E121 four space indentations
# * E501 line is over 79 characters
- pep8.ignore E111
- pep8.ignore E121
- pep8.ignore E501
+ pycodestyle.ignore E111
+ pycodestyle.ignore E121
+ pycodestyle.ignore E501
- pep8.ignore run_tests.py => E402: import stem.util.enum
+ pycodestyle.ignore run_tests.py => E402: import stem.util.enum
... you can then run tests with...
@@ -182,7 +195,7 @@ def stylistic_issues(paths, check_newlines = False, check_exception_keyword = Fa
ignore_rules = []
ignore_for_file = []
- for rule in CONFIG['pep8.ignore']:
+ for rule in CONFIG['pycodestyle.ignore']:
if '=>' in rule:
path, rule_entry = rule.split('=>', 1)
@@ -199,10 +212,13 @@ def stylistic_issues(paths, check_newlines = False, check_exception_keyword = Fa
return False
- if is_pep8_available():
- import pep8
+ if is_pycodestyle_available():
+ if module_exists('pycodestyle'):
+ import pycodestyle
+ elif module_exists('pep8'):
+ import pep8 as pycodestyle
- class StyleReport(pep8.BaseReport):
+ class StyleReport(pycodestyle.BaseReport):
def __init__(self, options):
super(StyleReport, self).__init__(options)
@@ -215,7 +231,7 @@ def stylistic_issues(paths, check_newlines = False, check_exception_keyword = Fa
if not is_ignored(self.filename, code, line):
issues.setdefault(self.filename, []).append(Issue(line_number, text, line))
- style_checker = pep8.StyleGuide(ignore = ignore_rules, reporter = StyleReport)
+ style_checker = pycodestyle.StyleGuide(ignore = ignore_rules, reporter = StyleReport)
style_checker.check_files(list(_python_files(paths)))
if check_newlines or check_exception_keyword:
@@ -269,7 +285,7 @@ def pyflakes_issues(paths):
::
pyflakes.ignore stem/util/test_tools.py => 'pyflakes' imported but unused
- pyflakes.ignore stem/util/test_tools.py => 'pep8' imported but unused
+ pyflakes.ignore stem/util/test_tools.py => 'pycodestyle' imported but unused
If a 'exclude_paths' was set in our test config then we exclude any absolute
paths matching those regexes.
--
2.9.3
From ddea1ca95edb197180636216687eb7e2510b25f1 Mon Sep 17 00:00:00 2001
From: Neel Chauhan <neel@neelc.org>
Date: Thu, 27 Oct 2016 09:49:48 -0400
Subject: [PATCH 2/2] Finish modifying tests to reference pycodestyle as well
as pep8
---
run_tests.py | 20 ++++++++++----------
test/util.py | 27 +++++++++++++++++++++------
2 files changed, 31 insertions(+), 16 deletions(-)
diff --git a/run_tests.py b/run_tests.py
index 2fb93bf..0f98db7 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -80,7 +80,7 @@ PYFLAKES_TASK = Task(
)
PEP8_TASK = Task(
- 'running pep8',
+ 'running pycodestyle',
stem.util.test_tools.stylistic_issues,
args = (SRC_PATHS, True, True, True),
is_required = False,
@@ -140,14 +140,14 @@ def main():
sys.exit(1)
- pyflakes_task, pep8_task = None, None
+ pyflakes_task, pycodestyle_task = None, None
if not args.specific_test:
if stem.util.test_tools.is_pyflakes_available():
pyflakes_task = PYFLAKES_TASK
- if stem.util.test_tools.is_pep8_available():
- pep8_task = PEP8_TASK
+ if stem.util.test_tools.is_pycodestyle_available():
+ pycodestyle_task = PEP8_TASK
test.util.run_tasks(
'INITIALISING',
@@ -156,11 +156,11 @@ def main():
Task('checking pycrypto version', test.util.check_pycrypto_version),
Task('checking mock version', test.util.check_mock_version),
Task('checking pyflakes version', test.util.check_pyflakes_version),
- Task('checking pep8 version', test.util.check_pep8_version),
+ Task('checking pycodestyle version', test.util.check_pycodestyle_version),
Task('checking for orphaned .pyc files', test.util.clean_orphaned_pyc, (SRC_PATHS,)),
Task('checking for unused tests', test.util.check_for_unused_tests, ((os.path.join(STEM_BASE, 'test'),),)),
pyflakes_task,
- pep8_task,
+ pycodestyle_task,
)
# buffer that we log messages into so they can be printed after a test has finished
@@ -277,12 +277,12 @@ def main():
elif not stem.util.test_tools.is_pyflakes_available():
println('Static error checking requires pyflakes version 0.7.3 or later. Please install it from ...\n http://pypi.python.org/pypi/pyflakes\n', ERROR)
- if pep8_task and pep8_task.is_successful:
- for path, issues in pep8_task.result.items():
+ if pycodestyle_task and pycodestyle_task.is_successful:
+ for path, issues in pycodestyle_task.result.items():
for issue in issues:
static_check_issues.setdefault(path, []).append(issue)
- elif not stem.util.test_tools.is_pep8_available():
- println('Style checks require pep8 version 1.4.2 or later. Please install it from...\n http://pypi.python.org/pypi/pep8\n', ERROR)
+ elif not stem.util.test_tools.is_pycodestyle_available():
+ println('Style checks require pycodestyle version 1.4.2 or later. Please install it from...\n http://pypi.python.org/pypi/pycodestyle\n', ERROR)
_print_static_issues(static_check_issues)
diff --git a/test/util.py b/test/util.py
index ef0e377..478c056 100644
--- a/test/util.py
+++ b/test/util.py
@@ -23,7 +23,7 @@ Tasks are...
|- check_python_version - checks our version of python
|- check_pycrypto_version - checks our version of pycrypto
|- check_pyflakes_version - checks our version of pyflakes
- |- check_pep8_version - checks our version of pep8
+ |- check_pycodestyle_version - checks our version of pep8
|- clean_orphaned_pyc - removes any *.pyc without a corresponding *.py
+- check_for_unused_tests - checks to see if any tests are missing from our settings
"""
@@ -84,6 +84,18 @@ STEM_BASE = os.path.sep.join(__file__.split(os.path.sep)[:-2])
NEW_CAPABILITIES = []
+def module_exists(module_name):
+ """
+ Checks if a module exists
+
+ :returns: **True** if module exists and **False** otherwise
+ """
+ try:
+ mod = __import__(module_name)
+ except ImportError:
+ return False
+ else:
+ return True
def get_unit_tests(module_prefix = None):
"""
@@ -226,13 +238,16 @@ def check_pyflakes_version():
return 'missing'
-def check_pep8_version():
- try:
- import pep8
- return pep8.__version__
- except ImportError:
+def check_pycodestyle_version():
+ if module_exists('pycodestyle'):
+ import pycodestyle
+ elif module_exists('pep8'):
+ import pep8 as pycodestyle
+ else:
return 'missing'
+ return pycodestyle.__version__
+
def clean_orphaned_pyc(paths):
"""
--
2.9.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment