summaryrefslogtreecommitdiffstats
path: root/tools/lint/python/flake8.py
blob: 49ab04b019be53bb351f4efafd1930e9e3eb65b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# 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 platform
import subprocess
import sys

import mozfile
import mozpack.path as mozpath

from mozlint import result
from mozlint.pathutils import expand_exclusions

here = os.path.abspath(os.path.dirname(__file__))
FLAKE8_REQUIREMENTS_PATH = os.path.join(here, "flake8_requirements.txt")

FLAKE8_NOT_FOUND = """
Could not find flake8! Install flake8 and try again.

    $ pip install -U --require-hashes -r {}
""".strip().format(
    FLAKE8_REQUIREMENTS_PATH
)


FLAKE8_INSTALL_ERROR = """
Unable to install correct version of flake8
Try to install it manually with:
    $ pip install -U --require-hashes -r {}
""".strip().format(
    FLAKE8_REQUIREMENTS_PATH
)

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 an `Issue`.
"""


def default_bindir():
    # We use sys.prefix to find executables as that gets modified with
    # virtualenv's activate_this.py, whereas sys.executable doesn't.
    if platform.system() == "Windows":
        return os.path.join(sys.prefix, "Scripts")
    else:
        return os.path.join(sys.prefix, "bin")


class NothingToLint(Exception):
    """Exception used to bail out of flake8's internals if all the specified
    files were excluded.
    """


def setup(root, **lintargs):
    virtualenv_manager = lintargs["virtualenv_manager"]
    try:
        virtualenv_manager.install_pip_requirements(
            FLAKE8_REQUIREMENTS_PATH, quiet=True
        )
    except subprocess.CalledProcessError:
        print(FLAKE8_INSTALL_ERROR)
        return 1


def lint(paths, config, **lintargs):
    from flake8.main.application import Application

    log = lintargs["log"]
    root = lintargs["root"]
    virtualenv_bin_path = lintargs.get("virtualenv_bin_path")
    config_path = os.path.join(root, ".flake8")

    if lintargs.get("fix"):
        fix_cmd = [
            os.path.join(virtualenv_bin_path or default_bindir(), "autopep8"),
            "--global-config",
            config_path,
            "--in-place",
            "--recursive",
        ]

        if config.get("exclude"):
            fix_cmd.extend(["--exclude", ",".join(config["exclude"])])

        subprocess.call(fix_cmd + paths)

    # Run flake8.
    app = Application()
    log.debug("flake8 version={}".format(app.version))

    output_file = mozfile.NamedTemporaryFile(mode="r")
    flake8_cmd = [
        "--config",
        config_path,
        "--output-file",
        output_file.name,
        "--format",
        '{"path":"%(path)s","lineno":%(row)s,'
        '"column":%(col)s,"rule":"%(code)s","message":"%(text)s"}',
        "--filename",
        ",".join(["*.{}".format(e) for e in config["extensions"]]),
    ]
    log.debug("Command: {}".format(" ".join(flake8_cmd)))

    orig_make_file_checker_manager = app.make_file_checker_manager

    def wrap_make_file_checker_manager(self):
        """Flake8 is very inefficient when it comes to applying exclusion
        rules, using `expand_exclusions` to turn directories into a list of
        relevant python files is an order of magnitude faster.

        Hooking into flake8 here also gives us a convenient place to merge the
        `exclude` rules specified in the root .flake8 with the ones added by
        tools/lint/mach_commands.py.
        """
        # Ignore exclude rules if `--no-filter` was passed in.
        config.setdefault("exclude", [])
        if lintargs.get("use_filters", True):
            config["exclude"].extend(map(mozpath.normpath, self.options.exclude))

        # Since we use the root .flake8 file to store exclusions, we haven't
        # properly filtered the paths through mozlint's `filterpaths` function
        # yet. This mimics that though there could be other edge cases that are
        # different. Maybe we should call `filterpaths` directly, though for
        # now that doesn't appear to be necessary.
        filtered = [
            p for p in paths if not any(p.startswith(e) for e in config["exclude"])
        ]

        self.args = self.args + list(expand_exclusions(filtered, config, root))

        if not self.args:
            raise NothingToLint
        return orig_make_file_checker_manager()

    app.make_file_checker_manager = wrap_make_file_checker_manager.__get__(
        app, Application
    )

    # Make sure to run from repository root so exclusions are joined to the
    # repository root and not the current working directory.
    oldcwd = os.getcwd()
    os.chdir(root)
    try:
        app.run(flake8_cmd)
    except NothingToLint:
        pass
    finally:
        os.chdir(oldcwd)

    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 res.get("code") in LINE_OFFSETS:
            res["lineoffset"] = LINE_OFFSETS[res["code"]]

        results.append(result.from_config(config, **res))

    list(map(process_line, output_file.readlines()))
    return results