gecko-dev/tools/lint/flake8.lint

150 строки
4.0 KiB
Plaintext
Исходник Обычный вид История

# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import json
import os
import signal
import which
from mozprocess import ProcessHandler
from mozlint import result
FLAKE8_NOT_FOUND = """
Could not find flake8! Install flake8 and try again.
$ pip install flake8
""".strip()
LINE_OFFSETS = {
# continuation line under-indented for hanging indent
'E121': (-1, 2),
# continuation line missing indentation or outdented
'E122': (-1, 2),
# continuation line over-indented for hanging indent
'E126': (-1, 2),
# continuation line over-indented for visual indent
'E127': (-1, 2),
# continuation line under-indented for visual indent
'E128': (-1, 2),
# continuation line unaligned for hanging indend
'E131': (-1, 2),
# expected 1 blank line, found 0
'E301': (-1, 2),
# expected 2 blank lines, found 1
'E302': (-2, 3),
}
"""Maps a flake8 error to a lineoffset tuple.
The offset is of the form (lineno_offset, num_lines) and is passed
to the lineoffset property of `ResultContainer`.
"""
EXTENSIONS = ['.py', '.lint']
results = []
def process_line(line):
# Escape slashes otherwise JSON conversion will not work
line = line.replace('\\', '\\\\')
try:
res = json.loads(line)
except ValueError:
print('Non JSON output from linter, will not be processed: {}'.format(line))
return
if 'code' in res:
if res['code'].startswith('W'):
res['level'] = 'warning'
if res['code'] in LINE_OFFSETS:
res['lineoffset'] = LINE_OFFSETS[res['code']]
results.append(result.from_linter(LINTER, **res))
def run_process(cmdargs):
# flake8 seems to handle SIGINT poorly. Handle it here instead
# so we can kill the process without a cryptic traceback.
orig = signal.signal(signal.SIGINT, signal.SIG_IGN)
proc = ProcessHandler(cmdargs, env=os.environ,
processOutputLine=process_line)
proc.run()
signal.signal(signal.SIGINT, orig)
try:
proc.wait()
except KeyboardInterrupt:
proc.kill()
def lint(files, **lintargs):
binary = os.environ.get('FLAKE8')
if not binary:
try:
binary = which.which('flake8')
except which.WhichError:
pass
if not binary:
print(FLAKE8_NOT_FOUND)
return []
cmdargs = [
binary,
'--format', '{"path":"%(path)s","lineno":%(row)s,'
'"column":%(col)s,"rule":"%(code)s","message":"%(text)s"}',
]
# 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.
# See bug 1277851
no_config = []
for f in files:
if not os.path.isfile(os.path.join(f, '.flake8')):
no_config.append(f)
continue
run_process(cmdargs+[f])
# XXX For some reason passing in --exclude results in flake8 not using
# the local .flake8 file. So for now only pass in --exclude if there
# is no local config.
exclude = lintargs.get('exclude')
if exclude:
cmdargs += ['--exclude', ','.join(lintargs['exclude'])]
if no_config:
run_process(cmdargs+no_config)
return results
LINTER = {
'name': "flake8",
'description': "Python linter",
'include': [
'python/mozlint',
'taskcluster',
'testing/firefox-ui',
'testing/marionette/client',
'testing/marionette/harness',
Bug 1280573 - Add testing/mozbase to flake8 linter: r=ahal added testing/mozbase to tools/lint/flake8.lint fixed a first batch of PEP8 errors/warnings at first the commad autopep8 -i --max-line-length 99 -r -j 8 . has been used to fix simpler problems, run from testing/mozbase some of the issues can not easily fixed : - undefined 'names' in code for example isLinux - isLinux and isBsd "fixed" with # noqa - undefined 'message' resolved with return fmt.format(... - undefined 'structured' resolved replacing those with mozlog - long comments - some remaining - addressed with # noqa - package level import everything - addressed with # flake8: noqa restored testing/mozbase/mozdevice/mozdevice/Zeroconf.py fixed issues reported on mozreview fixed ')' in testing/mozbase/mozprocess/mozprocess/qijo.py imports finally fixed multiline string at testing/mozbase/manifestparser/tests/test_manifestparser.py:114 ^^^ and again, but now with ./mach python-test --path-only testing/mozbase/manifestparser/tests/test_manifestparser.py passing fixed testing/mozbase/manifestparser/tests/test_convert_directory.py assert fixed this error: 10:15:21 INFO - return lambda line: stack_fixer_module.fixSymbols(line) 10:15:21 INFO - TypeError: fixSymbols() takes exactly 2 arguments (1 given) fixed two spaces lint error even of # noqa comments restored assignement to lambda with # noqa to silence the lint error global noqa for testing/mozbase/manifestparser/tests/test_filters.py stupid is/is not error... MozReview-Commit-ID: 1FpJF54GqIi --HG-- extra : rebase_source : 3cf0277fb36a296e3506aeacc2ff05e1b03f9eac
2016-09-30 17:08:37 +03:00
'testing/mozbase',
'testing/mochitest',
'testing/puppeteer',
'testing/talos/',
'tools/lint',
],
Bug 1280573 - Add testing/mozbase to flake8 linter: r=ahal added testing/mozbase to tools/lint/flake8.lint fixed a first batch of PEP8 errors/warnings at first the commad autopep8 -i --max-line-length 99 -r -j 8 . has been used to fix simpler problems, run from testing/mozbase some of the issues can not easily fixed : - undefined 'names' in code for example isLinux - isLinux and isBsd "fixed" with # noqa - undefined 'message' resolved with return fmt.format(... - undefined 'structured' resolved replacing those with mozlog - long comments - some remaining - addressed with # noqa - package level import everything - addressed with # flake8: noqa restored testing/mozbase/mozdevice/mozdevice/Zeroconf.py fixed issues reported on mozreview fixed ')' in testing/mozbase/mozprocess/mozprocess/qijo.py imports finally fixed multiline string at testing/mozbase/manifestparser/tests/test_manifestparser.py:114 ^^^ and again, but now with ./mach python-test --path-only testing/mozbase/manifestparser/tests/test_manifestparser.py passing fixed testing/mozbase/manifestparser/tests/test_convert_directory.py assert fixed this error: 10:15:21 INFO - return lambda line: stack_fixer_module.fixSymbols(line) 10:15:21 INFO - TypeError: fixSymbols() takes exactly 2 arguments (1 given) fixed two spaces lint error even of # noqa comments restored assignement to lambda with # noqa to silence the lint error global noqa for testing/mozbase/manifestparser/tests/test_filters.py stupid is/is not error... MozReview-Commit-ID: 1FpJF54GqIi --HG-- extra : rebase_source : 3cf0277fb36a296e3506aeacc2ff05e1b03f9eac
2016-09-30 17:08:37 +03:00
'exclude': ["testing/mozbase/mozdevice/mozdevice/Zeroconf.py",
'testing/mochitest/pywebsocket'],
'extensions': EXTENSIONS,
'type': 'external',
'payload': lint,
}