1
"""The logic for Flake8's integration with setuptools."""
6
from flake8.main import application as app
11
class Flake8(setuptools.Command):
12
"""Run Flake8 via setuptools/distutils for registered modules."""
14
description = 'Run Flake8 on modules registered in setup.py'
15
# NOTE(sigmavirus24): If we populated this with a list of tuples, users
16
# could do something like ``python setup.py flake8 --ignore=E123,E234``
17
# but we would have to redefine it and we can't define it dynamically.
18
# Since I refuse to copy-and-paste the options here or maintain two lists
19
# of options, and since this will break when users use plugins that
20
# provide command-line options, we are leaving this empty. If users want
21
# to configure this command, they can do so through config files.
24
def initialize_options(self):
25
"""Override this method to initialize our application."""
26
self.flake8 = app.Application()
27
self.flake8.initialize([])
28
options = self.flake8.option_manager.options
29
for option in options:
30
if option.parse_from_config:
31
setattr(self, option.config_name, UNSET)
33
def finalize_options(self):
34
"""Override this to parse the parameters."""
35
options = self.flake8.option_manager.options
36
for option in options:
37
if option.parse_from_config:
38
name = option.config_name
39
value = getattr(self, name, UNSET)
42
setattr(self.flake8.options,
44
option.normalize_from_setuptools(value))
46
def package_files(self):
47
"""Collect the files/dirs included in the registered modules."""
48
seen_package_directories = ()
49
directories = self.distribution.package_dir or {}
50
empty_directory_exists = '' in directories
51
packages = self.distribution.packages or []
52
for package in packages:
53
package_directory = package
54
if package in directories:
55
package_directory = directories[package]
56
elif empty_directory_exists:
57
package_directory = os.path.join(directories[''],
60
# NOTE(sigmavirus24): Do not collect submodules, e.g.,
64
# Flake8 only needs ``flake8/`` to be provided. It will
66
if package_directory.startswith(seen_package_directories):
69
seen_package_directories += (package_directory,)
70
yield package_directory
72
def module_files(self):
73
"""Collect the files listed as py_modules."""
74
modules = self.distribution.py_modules or []
75
filename_from = '{0}.py'.format
76
for module in modules:
77
yield filename_from(module)
79
def distribution_files(self):
80
"""Collect package and module files."""
81
for package in self.package_files():
84
for module in self.module_files():
90
"""Run the Flake8 application."""
91
self.flake8.run_checks(list(self.distribution_files()))
92
self.flake8.formatter.start()
93
self.flake8.report_errors()
94
self.flake8.report_statistics()
95
self.flake8.report_benchmarks()
96
self.flake8.formatter.stop()