2016-05-06 00:21:12 +03:00
|
|
|
# -*- Mode: python; c-basic-offset: 4; 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
|
2016-05-17 19:24:42 +03:00
|
|
|
import signal
|
|
|
|
|
|
|
|
import which
|
|
|
|
from mozprocess import ProcessHandler
|
2016-05-06 00:21:12 +03:00
|
|
|
|
|
|
|
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`.
|
|
|
|
"""
|
|
|
|
|
2016-05-17 19:24:42 +03:00
|
|
|
results = []
|
|
|
|
|
|
|
|
|
|
|
|
def process_line(line):
|
|
|
|
try:
|
|
|
|
res = json.loads(line)
|
|
|
|
except ValueError:
|
|
|
|
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))
|
2016-05-06 00:21:12 +03:00
|
|
|
|
|
|
|
|
2016-05-17 19:24:42 +03:00
|
|
|
def lint(files, **lintargs):
|
2016-05-06 00:21:12 +03:00
|
|
|
binary = os.environ.get('FLAKE8')
|
|
|
|
if not binary:
|
|
|
|
try:
|
|
|
|
binary = which.which('flake8')
|
|
|
|
except which.WhichError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if not binary:
|
|
|
|
print(FLAKE8_NOT_FOUND)
|
2016-06-02 16:05:01 +03:00
|
|
|
return 1
|
2016-05-06 00:21:12 +03:00
|
|
|
|
|
|
|
cmdargs = [
|
|
|
|
binary,
|
|
|
|
'--format', '{"path":"%(path)s","lineno":%(row)s,'
|
|
|
|
'"column":%(col)s,"rule":"%(code)s","message":"%(text)s"}',
|
|
|
|
]
|
|
|
|
|
|
|
|
exclude = lintargs.get('exclude')
|
|
|
|
if exclude:
|
|
|
|
cmdargs += ['--exclude', ','.join(lintargs['exclude'])]
|
|
|
|
|
|
|
|
cmdargs += files
|
|
|
|
|
2016-05-17 19:24:42 +03:00
|
|
|
# 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()
|
2016-05-06 00:21:12 +03:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
LINTER = {
|
|
|
|
'name': "flake8",
|
|
|
|
'description': "Python linter",
|
|
|
|
'include': [
|
|
|
|
'python/mozlint',
|
|
|
|
'tools/lint',
|
|
|
|
],
|
|
|
|
'exclude': [],
|
|
|
|
'type': 'external',
|
|
|
|
'payload': lint,
|
|
|
|
}
|