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
|
# 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/.
from __future__ import unicode_literals
import re
import sys
from abc import ABCMeta, abstractmethod
from mozlog import get_default_logger, commandline, structuredlog
from mozlog.reader import LogHandler
from . import result
from .pathutils import filterpaths
class BaseType(object):
"""Abstract base class for all types of linters."""
__metaclass__ = ABCMeta
batch = False
def __call__(self, paths, linter, **lintargs):
"""Run `linter` against `paths` with `lintargs`.
:param paths: Paths to lint. Can be a file or directory.
:param linter: Linter definition 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.ResultContainer` objects.
"""
paths = filterpaths(paths, linter, **lintargs)
if not paths:
print("{}: no files to lint in specified paths".format(linter['name']))
return
if self.batch:
return self._lint(paths, linter, **lintargs)
errors = []
try:
for p in paths:
result = self._lint(p, linter, **lintargs)
if result:
errors.extend(result)
except KeyboardInterrupt:
pass
return errors
@abstractmethod
def _lint(self, path):
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):
pass
def _lint(self, path, linter, **lintargs):
payload = linter['payload']
with open(path, 'r') as fh:
lines = fh.readlines()
errors = []
for i, line in enumerate(lines):
if self.condition(payload, line):
errors.append(result.from_linter(linter, path=path, lineno=i+1))
return errors
class StringType(LineType):
"""Linter type that checks whether a substring is found."""
def condition(self, payload, line):
return payload in line
class RegexType(LineType):
"""Linter type that checks whether a regex match is found."""
def condition(self, payload, line):
return re.search(payload, line)
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.ResultContainer` objects.
"""
batch = True
def _lint(self, files, linter, **lintargs):
payload = linter['payload']
return payload(files, **lintargs)
class LintHandler(LogHandler):
def __init__(self, linter):
self.linter = linter
self.results = []
def lint(self, data):
self.results.append(result.from_linter(self.linter, **data))
class StructuredLogType(BaseType):
batch = True
def _lint(self, files, linter, **lintargs):
payload = linter["payload"]
handler = LintHandler(linter)
logger = linter.get("logger")
if logger is None:
logger = get_default_logger()
if logger is None:
logger = structuredlog.StructuredLogger(linter["name"])
commandline.setup_logging(logger, {}, {"mach": sys.stdout})
logger.add_handler(handler)
try:
payload(files, logger, **lintargs)
except KeyboardInterrupt:
pass
return handler.results
supported_types = {
'string': StringType(),
'regex': RegexType(),
'external': ExternalType(),
'structured_log': StructuredLogType()
}
"""Mapping of type string to an associated instance."""
|