From 2f9825a6af8e34288353c553f980b06be6e15f65 Mon Sep 17 00:00:00 2001 From: Peter Teichman Date: Sun, 9 Sep 2012 08:20:18 -0400 Subject: [PATCH 1/5] apply the pep8 default max_line_length in _PEP8Options --- flake8/run.py | 1 + 1 file changed, 1 insertion(+) diff --git a/flake8/run.py b/flake8/run.py index 649f7b9..7d5b137 100644 --- a/flake8/run.py +++ b/flake8/run.py @@ -110,6 +110,7 @@ class _PEP8Options(object): benchmark = False testsuite = '' doctest = False + max_line_length = pep8.MAX_LINE_LENGTH def _initpep8(): From 2e04e30d51206bed517168f1842b43bdc53c95ab Mon Sep 17 00:00:00 2001 From: Peter Teichman Date: Sun, 9 Sep 2012 08:24:04 -0400 Subject: [PATCH 2/5] add Flake8Command, a setuptools command This runs flake8 with the default options on all Python files referenced by a package's setup.py. It works with both package-based and py_modules-based file lists. --- flake8/run.py | 35 +++++++++++++++++++++++++++++++++++ setup.py | 5 +++++ 2 files changed, 40 insertions(+) diff --git a/flake8/run.py b/flake8/run.py index 7d5b137..bcc3308 100644 --- a/flake8/run.py +++ b/flake8/run.py @@ -164,5 +164,40 @@ 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 + + if self.distribution.py_modules: + for filename in self.distribution.py_modules: + yield "%s.py" % filename + + def run(self): + _initpep8() + + warnings = 0 + for path in _get_python_files(self.distribution_files()): + warnings += check_file(path) + + raise SystemExit(warnings > 0) + + if __name__ == '__main__': main() diff --git a/setup.py b/setup.py index dee5365..1dc5694 100755 --- a/setup.py +++ b/setup.py @@ -24,6 +24,11 @@ setup( url="http://bitbucket.org/tarek/flake8", packages=["flake8", "flake8.tests"], scripts=["flake8/flake8"], + entry_points = { + "distutils.commands": [ + "flake8 = flake8.run:Flake8Command", + ], + }, long_description=README, classifiers=[ "Environment :: Console", From c6ac38cd7053740594a9178346ed07b378f624f0 Mon Sep 17 00:00:00 2001 From: Peter Teichman Date: Wed, 12 Sep 2012 18:44:24 -0400 Subject: [PATCH 3/5] Incorporate patch review on Flake8Command * add a note about the setuptools command to README * don't install the Flake8Command entry point without setuptools * fix a bug in Flake8Command that might cause double checking of files in 'packages' --- README | 25 +++++++++++++++++++++++++ flake8/run.py | 16 ++++++++++++---- setup.py | 12 +++++++----- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/README b/README index 379c174..33a453e 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 bcc3308..b19821e 100644 --- a/flake8/run.py +++ b/flake8/run.py @@ -35,6 +35,7 @@ def check_code(code, complexity=-1): def _get_python_files(paths): + seen = set() for path in paths: if os.path.isdir(path): for dirpath, dirnames, filenames in os.walk(path): @@ -42,11 +43,11 @@ def _get_python_files(paths): if not filename.endswith('.py'): continue fullpath = os.path.join(dirpath, filename) - if not skip_file(fullpath): + if not skip_file(fullpath) and fullpath not in seen: yield fullpath else: - if not skip_file(path): + if not skip_file(path) and path not in seen: yield path @@ -183,7 +184,7 @@ else: def distribution_files(self): if self.distribution.packages: for package in self.distribution.packages: - yield package + yield package.replace(".", os.path.sep) if self.distribution.py_modules: for filename in self.distribution.py_modules: @@ -192,9 +193,16 @@ else: def run(self): _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()): - warnings += check_file(path) + if path not in checked: + warnings += check_file(path) + checked.add(path) raise SystemExit(warnings > 0) diff --git a/setup.py b/setup.py index 1dc5694..74efd37 100755 --- a/setup.py +++ b/setup.py @@ -1,12 +1,14 @@ import sys ispy3 = sys.version_info[0] == 3 +issetuptools = False if ispy3: from distutils.core import setup # NOQA else: try: from setuptools import setup # NOQA + issetuptools = True except ImportError: from distutils.core import setup # NOQA @@ -14,6 +16,10 @@ from flake8 import __version__ README = open('README').read() +entry_points = {} +if issetuptools: + entry_points["distutils.commands"] = ["flake8 = flake8.run:Flake8Command"] + setup( name="flake8", license="MIT", @@ -24,11 +30,7 @@ setup( url="http://bitbucket.org/tarek/flake8", packages=["flake8", "flake8.tests"], scripts=["flake8/flake8"], - entry_points = { - "distutils.commands": [ - "flake8 = flake8.run:Flake8Command", - ], - }, + entry_points=entry_points, long_description=README, classifiers=[ "Environment :: Console", From 3bd3c01e35e750b45192231ff0725e0a4b6a46b1 Mon Sep 17 00:00:00 2001 From: Peter Teichman Date: Mon, 17 Sep 2012 07:32:11 -0400 Subject: [PATCH 4/5] Remove the now-unused set() in _get_python_files This was left over from a previous patch and unused after adding the 'checked' set() to Flake8Command.run() --- flake8/run.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/flake8/run.py b/flake8/run.py index f2c1811..6c89df3 100644 --- a/flake8/run.py +++ b/flake8/run.py @@ -37,7 +37,6 @@ def check_code(code, complexity=-1): def _get_python_files(paths): - seen = set() for path in paths: if os.path.isdir(path): for dirpath, dirnames, filenames in os.walk(path): @@ -45,11 +44,11 @@ def _get_python_files(paths): if not filename.endswith('.py'): continue fullpath = os.path.join(dirpath, filename) - if not skip_file(fullpath) and fullpath not in seen: + if not skip_file(fullpath): yield fullpath else: - if not skip_file(path) and path not in seen: + if not skip_file(path): yield path From 6a13c4622790f4ded72164a4f13ed69412b14c9f Mon Sep 17 00:00:00 2001 From: Peter Teichman Date: Mon, 17 Sep 2012 19:07:05 -0400 Subject: [PATCH 5/5] Add Peter Teichman to CONTRIBUTORS.txt --- CONTRIBUTORS.txt | 1 + 1 file changed, 1 insertion(+) 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