diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 6e0ad84..f41e5d4 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -13,3 +13,4 @@ Contributors (by order of appearance) : - Jannis Leidel - Miki Tebeka - David Cramer +- Peter Teichman diff --git a/README b/README index ae235dd..96ab869 100644 --- a/README +++ b/README @@ -123,6 +123,31 @@ In order to use Flake8 inside a buildout, edit your buildout.cfg and add this:: entry-points = flake8=flake8.run:main + +setuptools integration +====================== + +If setuptools is available, Flake8 provides a command that checks the +Python files declared by your project. To use it, add flake8 to your +setup_requires:: + + setup( + name="project", + packages=["project"], + + setup_requires=[ + "flake8" + ] + ) + +Running ``python setup.py flake8`` on the command line will check the +files listed in your ``py_modules`` and ``packages``. If any warnings +are found, the command will exit with an error code:: + + $ python setup.py flake8 + + + Original projects ================= diff --git a/flake8/run.py b/flake8/run.py index 01f60e7..6c89df3 100644 --- a/flake8/run.py +++ b/flake8/run.py @@ -114,6 +114,7 @@ class _PEP8Options(object): benchmark = False testsuite = '' doctest = False + max_line_length = pep8.MAX_LINE_LENGTH def _initpep8(): @@ -171,5 +172,50 @@ def hg_hook(ui, repo, **kwargs): return 0 + +try: + from setuptools import Command +except ImportError: + Flake8Command = None +else: + class Flake8Command(Command): + description = "Run flake8 on modules registered in setuptools" + user_options = [] + + def initialize_options(self): + pass + + def finalize_options(self): + pass + + def distribution_files(self): + if self.distribution.packages: + for package in self.distribution.packages: + yield package.replace(".", os.path.sep) + + if self.distribution.py_modules: + for filename in self.distribution.py_modules: + yield "%s.py" % filename + + def run(self): + global pep8style + pep8style = pep8.StyleGuide(config_file=True) + + _initpep8() + + # _get_python_files can produce the same file several + # times, if one of its paths is a parent of another. Keep + # a set of checked files to de-duplicate. + checked = set() + + warnings = 0 + for path in _get_python_files(self.distribution_files()): + if path not in checked: + warnings += check_file(path) + checked.add(path) + + raise SystemExit(warnings > 0) + + if __name__ == '__main__': main() diff --git a/setup.py b/setup.py index 3927603..0d85ec6 100755 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -import sys +import sys import os ispy3 = sys.version_info[0] == 3 @@ -14,11 +14,11 @@ else: try: from setuptools import setup # NOQA kwargs = { + 'entry_points': + {'distutils.commands': ['flake8 = flake8.run:Flake8Command'], + 'console_scripts': ['flake8 = flake8.run:main']}, 'tests_require': ['nose'], 'test_suite': 'nose.collector', - 'entry_points': { - 'console_scripts': ['flake8 = flake8.run:main'] - }, } except ImportError: from distutils.core import setup # NOQA