Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _flake8(self):
try:
from flake8.engine import get_style_guide
except ImportError:
from flake8.api.legacy import get_style_guide
ignore = [
'W503', # line break before binary op
'W504', # line break after binary op
'E126', # continuation line over-indented
]
flake8_style = get_style_guide(paths=self._git_staged_or_all_py_files(), max_line_length=120, ignore=ignore)
print("running flake8...")
report = flake8_style.check_files()
if report.total_errors > 0:
print(str(report.total_errors) + " flake8 errors, see above to fix them")
self.failed.append('flake8')
else:
print("flake8 passed!")
def run_flake8():
# flake8
flake8 = get_style_guide(exclude=['.tox', 'build'], max_complexity=10)
report = flake8.check_files([BASE_DIR])
return print_report(report, flake8)
def check_all(self, paths):
"""Run flake8 on all paths."""
try:
# pylint: disable=import-error
# We can't assume the user has this.
from flake8 import engine, main
except Exception:
return
config_file = self.get_flake8_config()
if config_file:
style = engine.get_style_guide(parse_argv=False, config_file=config_file)
report = style.check_files(paths=paths)
# Set statistics here, instead of from the command line.
options = style.options
options.statistics = True
options.total_errors = True
# options.benchmark = True
main.print_report(report, style)
#@+node:ekr.20160517133049.3: *3* flake8.find
def py_lint(file_list):
from flake8.main import DEFAULT_CONFIG
from flake8.engine import get_style_guide
if file_list is None:
file_list = ['src/sentry', 'tests']
file_list = get_files_for_list(file_list)
# remove non-py files and files which no longer exist
file_list = filter(lambda x: x.endswith('.py'), file_list)
flake8_style = get_style_guide(parse_argv=True, config_file=DEFAULT_CONFIG)
report = flake8_style.check_files(file_list)
return report.total_errors != 0
def check_file(path, ignore=(), complexity=-1):
"""Checks a file using pep8 and pyflakes by default and mccabe
optionally.
:param str path: path to the file to be checked
:param tuple ignore: (optional), error and warning codes to be ignored
:param int complexity: (optional), enables the mccabe check for values > 0
"""
ignore = set(ignore).union(EXTRA_IGNORE)
flake8_style = get_style_guide(ignore=ignore, max_complexity=complexity)
return flake8_style.input_file(path)
def hg_hook(ui, repo, **kwargs):
"""This is the function executed directly by Mercurial as part of the
hook. This is never called directly by the user, so the parameters are
undocumented. If you would like to learn more about them, please feel free
to read the official Mercurial documentation.
"""
complexity = ui.config('flake8', 'complexity', default=-1)
strict = ui.configbool('flake8', 'strict', default=True)
config = ui.config('flake8', 'config', default=True)
if config is True:
config = DEFAULT_CONFIG
paths = _get_files(repo, **kwargs)
flake8_style = get_style_guide(
config_file=config, max_complexity=complexity)
report = flake8_style.check_files(paths)
if strict:
return report.total_errors
return 0
def main(files):
"""Call run on all tables in tables_table."""
try:
from flake8 import engine
except Exception:
print(f'{g.shortFileName(__file__)}: can not import flake8')
return
config_file = get_flake8_config()
if config_file:
style = engine.get_style_guide(parse_argv=False, config_file=config_file)
t1 = time.time()
check_all(files, style)
t2 = time.time()
n = len(files)
print('%s file%s, time: %5.2f sec.' % (n, g.plural(n), t2 - t1))
#@+node:ekr.20160517222900.1: *3* get_home
complexity = ui.config('flake8', 'complexity', default=-1)
strict = ui.configbool('flake8', 'strict', default=True)
ignore = ui.config('flake8', 'ignore', default=None)
config = ui.config('flake8', 'config', default=None)
paths = _get_files(repo, **kwargs)
# We only want to pass ignore and max_complexity if they differ from the
# defaults so that we don't override a local configuration file
options = {}
if ignore:
options['ignore'] = ignore
if complexity > -1:
options['max_complexity'] = complexity
flake8_style = get_style_guide(config_file=config, paths=['.'],
**options)
report = flake8_style.check_files(paths)
if strict:
return report.total_errors
return 0
def teardown_test_environment(self, **kwargs):
class JenkinsReport(pep8.BaseReport):
def error(instance, line_number, offset, text, check):
code = super(JenkinsReport, instance).error(
line_number, offset, text, check,
)
if not code:
return
sourceline = instance.line_offset + line_number
self.output.write(
'%s:%s:%s: %s\n' %
(instance.filename, sourceline, offset + 1, text),
)
flake8style = get_style_guide(
parse_argv=False,
config_file=self.pep8_rcfile,
reporter=JenkinsReport,
**self.pep8_options)
# Jenkins pep8 validator requires relative paths
project_root = os.path.abspath(os.path.dirname(__name__))
for location in map(lambda x: os.path.relpath(x, project_root), get_app_locations()):
flake8style.input_dir(location)
self.output.close()