Source code

Revision control

Other Tools

1
# This Source Code Form is subject to the terms of the Mozilla Public
2
# License, v. 2.0. If a copy of the MPL was not distributed with this
3
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5
from __future__ import absolute_import, print_function
6
7
import json
8
import os
9
import platform
10
import subprocess
11
import sys
12
13
import mozfile
14
15
from mozlint import result
16
from mozlint.pathutils import expand_exclusions
17
from mozlint.util import pip
18
19
here = os.path.abspath(os.path.dirname(__file__))
20
FLAKE8_REQUIREMENTS_PATH = os.path.join(here, 'flake8_requirements.txt')
21
22
FLAKE8_NOT_FOUND = """
23
Could not find flake8! Install flake8 and try again.
24
25
$ pip install -U --require-hashes -r {}
26
""".strip().format(FLAKE8_REQUIREMENTS_PATH)
27
28
29
FLAKE8_INSTALL_ERROR = """
30
Unable to install correct version of flake8
31
Try to install it manually with:
32
$ pip install -U --require-hashes -r {}
33
""".strip().format(FLAKE8_REQUIREMENTS_PATH)
34
35
LINE_OFFSETS = {
36
# continuation line under-indented for hanging indent
37
'E121': (-1, 2),
38
# continuation line missing indentation or outdented
39
'E122': (-1, 2),
40
# continuation line over-indented for hanging indent
41
'E126': (-1, 2),
42
# continuation line over-indented for visual indent
43
'E127': (-1, 2),
44
# continuation line under-indented for visual indent
45
'E128': (-1, 2),
46
# continuation line unaligned for hanging indend
47
'E131': (-1, 2),
48
# expected 1 blank line, found 0
49
'E301': (-1, 2),
50
# expected 2 blank lines, found 1
51
'E302': (-2, 3),
52
}
53
"""Maps a flake8 error to a lineoffset tuple.
54
55
The offset is of the form (lineno_offset, num_lines) and is passed
56
to the lineoffset property of an `Issue`.
57
"""
58
59
# We use sys.prefix to find executables as that gets modified with
60
# virtualenv's activate_this.py, whereas sys.executable doesn't.
61
if platform.system() == 'Windows':
62
bindir = os.path.join(sys.prefix, 'Scripts')
63
else:
64
bindir = os.path.join(sys.prefix, 'bin')
65
66
67
class NothingToLint(Exception):
68
"""Exception used to bail out of flake8's internals if all the specified
69
files were excluded.
70
"""
71
72
73
def setup(root, **lintargs):
74
if not pip.reinstall_program(FLAKE8_REQUIREMENTS_PATH):
75
print(FLAKE8_INSTALL_ERROR)
76
return 1
77
78
79
def lint(paths, config, **lintargs):
80
from flake8.main.application import Application
81
82
root = lintargs['root']
83
config_path = os.path.join(root, '.flake8')
84
85
if lintargs.get('fix'):
86
fix_cmd = [
87
os.path.join(bindir, 'autopep8'),
88
'--global-config', config_path,
89
'--in-place', '--recursive',
90
]
91
92
if config.get('exclude'):
93
fix_cmd.extend(['--exclude', ','.join(config['exclude'])])
94
95
subprocess.call(fix_cmd + paths)
96
97
# Run flake8.
98
app = Application()
99
100
output_file = mozfile.NamedTemporaryFile()
101
flake8_cmd = [
102
'--config', config_path,
103
'--output-file', output_file.name,
104
'--format', '{"path":"%(path)s","lineno":%(row)s,'
105
'"column":%(col)s,"rule":"%(code)s","message":"%(text)s"}',
106
'--filename', ','.join(['*.{}'.format(e) for e in config['extensions']]),
107
]
108
109
orig_make_file_checker_manager = app.make_file_checker_manager
110
111
def wrap_make_file_checker_manager(self):
112
"""Flake8 is very inefficient when it comes to applying exclusion
113
rules, using `expand_exclusions` to turn directories into a list of
114
relevant python files is an order of magnitude faster.
115
116
Hooking into flake8 here also gives us a convenient place to merge the
117
`exclude` rules specified in the root .flake8 with the ones added by
118
tools/lint/mach_commands.py.
119
"""
120
config.setdefault('exclude', []).extend(self.options.exclude)
121
self.options.exclude = None
122
self.args = self.args + list(expand_exclusions(paths, config, root))
123
124
if not self.args:
125
raise NothingToLint
126
return orig_make_file_checker_manager()
127
128
app.make_file_checker_manager = wrap_make_file_checker_manager.__get__(app, Application)
129
130
# Make sure to run from repository root so exclusions are joined to the
131
# repository root and not the current working directory.
132
oldcwd = os.getcwd()
133
os.chdir(root)
134
try:
135
app.run(flake8_cmd)
136
except NothingToLint:
137
pass
138
finally:
139
os.chdir(oldcwd)
140
141
results = []
142
143
def process_line(line):
144
# Escape slashes otherwise JSON conversion will not work
145
line = line.replace('\\', '\\\\')
146
try:
147
res = json.loads(line)
148
except ValueError:
149
print('Non JSON output from linter, will not be processed: {}'.format(line))
150
return
151
152
if res.get('code') in LINE_OFFSETS:
153
res['lineoffset'] = LINE_OFFSETS[res['code']]
154
155
results.append(result.from_config(config, **res))
156
157
map(process_line, output_file.readlines())
158
return results