diff options
| author | Ian Cordasco <graffatcolmingov@gmail.com> | 2016-06-16 10:59:16 -0500 |
|---|---|---|
| committer | Ian Cordasco <graffatcolmingov@gmail.com> | 2016-06-16 10:59:16 -0500 |
| commit | 9a9bcdfb5252aef2fb4985a7769fd263c6d2a651 (patch) | |
| tree | 23812b210a81e8ea3f1657c265d09ad5b5f2eb0e | |
| parent | 1de71fbe89e4e3cd9cb4b2b43a1dfd57e7bc4da2 (diff) | |
| download | flake8-9a9bcdfb5252aef2fb4985a7769fd263c6d2a651.tar.gz | |
Add setuptools integration
This proved simpler because I realized something important: Most of the
code that the old integration was using was in fact doing nothing of
value. Since we can't meaningfully allow users to use command-line
options as parameters to `python setup.py flake8`, we just remove the
work that we do to set up those attributes and parse them.
| -rw-r--r-- | flake8/main/setuptools_command.py | 77 | ||||
| -rw-r--r-- | setup.py | 8 |
2 files changed, 83 insertions, 2 deletions
diff --git a/flake8/main/setuptools_command.py b/flake8/main/setuptools_command.py new file mode 100644 index 0000000..1c27bf6 --- /dev/null +++ b/flake8/main/setuptools_command.py @@ -0,0 +1,77 @@ +"""The logic for Flake8's integration with setuptools.""" +import os + +import setuptools + +from flake8.main import application as app + + +class Flake8(setuptools.Command): + """Run Flake8 via setuptools/distutils for registered modules.""" + + description = 'Run Flake8 on modules registered in setup.py' + # NOTE(sigmavirus24): If we populated this with a list of tuples, users + # could do something like ``python setup.py flake8 --ignore=E123,E234`` + # but we would have to redefine it and we can't define it dynamically. + # Since I refuse to copy-and-paste the options here or maintain two lists + # of options, and since this will break when users use plugins that + # provide command-line options, we are leaving this empty. If users want + # to configure this command, they can do so through config files. + user_options = [] + + def initialize_options(self): + """Override this method to initialize our application.""" + pass + + def finalize_options(self): + """Override this to parse the parameters.""" + pass + + def package_files(self): + """Collect the files/dirs included in the registered modules.""" + seen_package_directories = () + directories = self.distribution.package_dir or {} + empty_directory_exists = '' in directories + packages = self.distribution.packages or [] + for package in packages: + package_directory = package + if package in directories: + package_directory = directories[package] + elif empty_directory_exists: + package_directory = os.path.join(directories[''], + package_directory) + + # NOTE(sigmavirus24): Do not collect submodules, e.g., + # if we have: + # - flake8/ + # - flake8/plugins/ + # Flake8 only needs ``flake8/`` to be provided. It will + # recurse on its own. + if package_directory.startswith(seen_package_directories): + continue + + seen_package_directories += (package_directory,) + yield package_directory + + def module_files(self): + """Collect the files listed as py_modules.""" + modules = self.distribution.py_modules or [] + filename_from = '{0}.py'.format + for module in modules: + yield filename_from(module) + + def distribution_files(self): + """Collect package and module files.""" + for package in self.package_files(): + yield package + + for module in self.module_files(): + yield module + + yield 'setup.py' + + def run(self): + """Run the Flake8 application.""" + flake8 = app.Application() + flake8.run(list(self.distribution_files())) + flake8.exit() @@ -76,8 +76,12 @@ setuptools.setup( ], install_requires=requires, entry_points={ - 'distutils.commands': ['flake8 = flake8.main:Flake8Command'], - 'console_scripts': ['flake8 = flake8.main.cli:main'], + 'distutils.commands': [ + 'flake8 = flake8.main.setuptools_command:Flake8' + ], + 'console_scripts': [ + 'flake8 = flake8.main.cli:main' + ], 'flake8.extension': [ 'F = flake8.plugins.pyflakes:FlakesChecker', # PEP-0008 checks provied by PyCQA/pycodestyle |
