Bug 1388721 - [lint] Support --fix in the flake8 linter via autopep8, r=sylvestre

This adds autopep8 to flake8_requirements.txt. When --fix is passed in,
autopep8 will run prior to the normal flake8 run, so only errors that couldn't
be fixed automatically will be shown.

There is one caveat, namely autopep8 will only use the root .flake8 file
(there's no support for passing in subconfigs).

MozReview-Commit-ID: FJ0doey2KVb
This commit is contained in:
Andrew Halberstadt
2018-05-25 17:02:43 -04:00
parent c29f950df5
commit 3aae3c7cfa
5 changed files with 67 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ import json
import os
import platform
import signal
import subprocess
import sys
from collections import defaultdict
@@ -125,6 +126,15 @@ def lint(paths, config, **lintargs):
'"column":%(col)s,"rule":"%(code)s","message":"%(text)s"}',
]
fix_cmdargs = [
os.path.join(bindir, 'autopep8'),
'--global-config', os.path.join(lintargs['root'], '.flake8'),
'--in-place', '--recursive',
]
if 'exclude' in lintargs:
fix_cmdargs.extend(['--exclude', ','.join(lintargs['exclude'])])
# Run any paths with a .flake8 file in the directory separately so
# it gets picked up. This means only .flake8 files that live in
# directories that are explicitly included will be considered.
@@ -135,8 +145,10 @@ def lint(paths, config, **lintargs):
paths_by_config[os.pathsep.join(configs) if configs else 'default'].append(path)
for configs, paths in paths_by_config.items():
cmd = cmdargs[:]
if lintargs.get('fix'):
subprocess.call(fix_cmdargs + paths)
cmd = cmdargs[:]
if configs != 'default':
configs = reversed(configs.split(os.pathsep))
cmd.extend(['--append-config={}'.format(c) for c in configs])