summaryrefslogtreecommitdiffstats
path: root/python/mozlint/mozlint/types.py
blob: 1a9a0bd473a3f34002651f192aaf0bf94853cf1b (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# 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 os
import re
import sys
from abc import ABCMeta, abstractmethod

from mozlog import commandline, get_default_logger, structuredlog
from mozlog.reader import LogHandler
from mozpack.files import FileFinder

from . import result
from .pathutils import expand_exclusions, filterpaths, findobject


class BaseType(object):
    """Abstract base class for all types of linters."""

    __metaclass__ = ABCMeta
    batch = False

    def __call__(self, paths, config, **lintargs):
        """Run linter defined by `config` against `paths` with `lintargs`.

        :param paths: Paths to lint. Can be a file or directory.
        :param config: Linter config the paths are being linted against.
        :param lintargs: External arguments to the linter not defined in
                         the definition, but passed in by a consumer.
        :returns: A list of :class:`~result.Issue` objects.
        """
        log = lintargs["log"]

        if lintargs.get("use_filters", True):
            paths, exclude = filterpaths(
                lintargs["root"],
                paths,
                config["include"],
                config.get("exclude", []),
                config.get("extensions", []),
            )
            config["exclude"] = exclude
        elif config.get("exclude"):
            del config["exclude"]

        if not paths:
            return {"results": [], "fixed": 0}

        log.debug(
            "Passing the following paths:\n{paths}".format(
                paths="  \n".join(paths),
            )
        )

        if self.batch:
            return self._lint(paths, config, **lintargs)

        errors = []

        try:
            for p in paths:
                result = self._lint(p, config, **lintargs)
                if result:
                    errors.extend(result)
        except KeyboardInterrupt:
            pass
        return errors

    def _lint_dir(self, path, config, **lintargs):
        if not config.get("extensions"):
            patterns = ["**"]
        else:
            patterns = ["**/*.{}".format(e) for e in config["extensions"]]

        exclude = [os.path.relpath(e, path) for e in config.get("exclude", [])]
        finder = FileFinder(path, ignore=exclude)

        errors = []
        for pattern in patterns:
            for p, f in finder.find(pattern):
                errors.extend(self._lint(os.path.join(path, p), config, **lintargs))
        return errors

    @abstractmethod
    def _lint(self, path, config, **lintargs):
        pass


class LineType(BaseType):
    """Abstract base class for linter types that check each line individually.

    Subclasses of this linter type will read each file and check the provided
    payload against each line one by one.
    """

    __metaclass__ = ABCMeta

    @abstractmethod
    def condition(payload, line, config):
        pass

    def _lint(self, path, config, **lintargs):
        if os.path.isdir(path):
            return self._lint_dir(path, config, **lintargs)

        payload = config["payload"]
        with open(path, "r", errors="replace") as fh:
            lines = fh.readlines()

        errors = []
        for i, line in enumerate(lines):
            if self.condition(payload, line, config):
                errors.append(result.from_config(config, path=path, lineno=i + 1))

        return errors


class StringType(LineType):
    """Linter type that checks whether a substring is found."""

    def condition(self, payload, line, config):
        return payload in line


class RegexType(LineType):
    """Linter type that checks whether a regex match is found."""

    def condition(self, payload, line, config):
        flags = 0
        if config.get("ignore-case"):
            flags |= re.IGNORECASE

        return re.search(payload, line, flags)


class ExternalType(BaseType):
    """Linter type that runs an external function.

    The function is responsible for properly formatting the results
    into a list of :class:`~result.Issue` objects.
    """

    batch = True

    def _lint(self, files, config, **lintargs):
        func = findobject(config["payload"])
        return func(files, config, **lintargs)


class ExternalFileType(ExternalType):
    batch = False


class GlobalType(ExternalType):
    """Linter type that runs an external global linting function just once.

    The function is responsible for properly formatting the results
    into a list of :class:`~result.Issue` objects.
    """

    batch = True

    def _lint(self, files, config, **lintargs):
        # Global lints are expensive to invoke.  Try to avoid running
        # them based on extensions and exclusions.
        try:
            next(expand_exclusions(files, config, lintargs["root"]))
        except StopIteration:
            return []

        func = findobject(config["payload"])
        return func(config, **lintargs)


class LintHandler(LogHandler):
    def __init__(self, config):
        self.config = config
        self.results = []

    def lint(self, data):
        self.results.append(result.from_config(self.config, **data))


class StructuredLogType(BaseType):
    batch = True

    def _lint(self, files, config, **lintargs):
        handler = LintHandler(config)
        logger = config.get("logger")
        if logger is None:
            logger = get_default_logger()
        if logger is None:
            logger = structuredlog.StructuredLogger(config["name"])
            commandline.setup_logging(logger, {}, {"mach": sys.stdout})
        logger.add_handler(handler)

        func = findobject(config["payload"])
        try:
            func(files, config, logger, **lintargs)
        except KeyboardInterrupt:
            pass
        return handler.results


supported_types = {
    "string": StringType(),
    "regex": RegexType(),
    "external": ExternalType(),
    "external-file": ExternalFileType(),
    "global": GlobalType(),
    "structured_log": StructuredLogType(),
}
"""Mapping of type string to an associated instance."""