|
1
|
|
|
from subprocess import Popen, PIPE |
|
2
|
|
|
import sys |
|
3
|
|
|
|
|
4
|
|
|
from coalib.bears.LocalBear import LocalBear |
|
5
|
|
|
from coalib.results.Result import Result, RESULT_SEVERITY |
|
6
|
|
|
from coalib.settings.Setting import typed_list |
|
7
|
|
|
from coalib.misc.Shell import escape_path_argument |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
# We omit this case in our tests for technical reasons |
|
11
|
|
|
if sys.version_info < (3, 3): # pragma: no cover |
|
12
|
|
|
raise ImportError("PyLint does not support python3 < 3.3") |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class PyLintBear(LocalBear): |
|
16
|
|
|
def parse_result(self, file, pylint_line): |
|
17
|
|
|
parts = pylint_line.split("|", maxsplit=2) |
|
18
|
|
|
|
|
19
|
|
|
line_nr = int(parts[0]) |
|
20
|
|
|
severity = parts[1] |
|
21
|
|
|
if severity == "warning": |
|
22
|
|
|
severity = RESULT_SEVERITY.NORMAL |
|
23
|
|
|
elif severity in ["error", "fatal"]: |
|
24
|
|
|
severity = RESULT_SEVERITY.MAJOR |
|
25
|
|
|
else: # convention and refactor |
|
26
|
|
|
severity = RESULT_SEVERITY.INFO |
|
27
|
|
|
|
|
28
|
|
|
message = parts[2] |
|
29
|
|
|
|
|
30
|
|
|
return Result.from_values(self, |
|
31
|
|
|
message, |
|
32
|
|
|
file=file, |
|
33
|
|
|
line=line_nr, |
|
34
|
|
|
severity=severity) |
|
35
|
|
|
|
|
36
|
|
|
def run(self, |
|
37
|
|
|
filename, |
|
38
|
|
|
file, |
|
39
|
|
|
pylint_disable: typed_list(str)=("fixme"), |
|
40
|
|
|
pylint_enable: typed_list(str)=None, |
|
41
|
|
|
pylint_cli_options: str=""): |
|
42
|
|
|
''' |
|
43
|
|
|
Checks the code with pylint. This will run pylint over each file |
|
44
|
|
|
separately. |
|
45
|
|
|
|
|
46
|
|
|
:param pylint_disable: Disable the message, report, category or |
|
47
|
|
|
checker with the given id(s). |
|
48
|
|
|
:param pylint_enable: Enable the message, report, category or |
|
49
|
|
|
checker with the given id(s). |
|
50
|
|
|
:param pylint_cli_options: Any command line options you wish to be |
|
51
|
|
|
passed to pylint. |
|
52
|
|
|
''' |
|
53
|
|
|
command = ('pylint -r n --msg-template="{line}|{category}|' |
|
54
|
|
|
'{msg}. ({msg_id}, {symbol}, {obj})" ' |
|
55
|
|
|
+ escape_path_argument(filename)) |
|
56
|
|
|
if pylint_disable: |
|
57
|
|
|
command += " --disable=" + ",".join(pylint_disable) |
|
58
|
|
|
if pylint_enable: |
|
59
|
|
|
command += " --enable=" + ",".join(pylint_enable) |
|
60
|
|
|
if pylint_cli_options: |
|
61
|
|
|
command += " " + pylint_cli_options |
|
62
|
|
|
|
|
63
|
|
|
process = Popen(command, |
|
64
|
|
|
shell=True, |
|
65
|
|
|
stdout=PIPE, |
|
66
|
|
|
stderr=PIPE, |
|
67
|
|
|
universal_newlines=True) |
|
68
|
|
|
process.wait() |
|
69
|
|
|
current_lines = "" |
|
70
|
|
|
for line in process.stdout.readlines(): |
|
71
|
|
|
if line.startswith("***"): |
|
72
|
|
|
continue |
|
73
|
|
|
|
|
74
|
|
|
if current_lines != "" and line.split("|", 1)[0].isdigit(): |
|
75
|
|
|
yield self.parse_result(filename, current_lines) |
|
76
|
|
|
current_lines = "" |
|
77
|
|
|
|
|
78
|
|
|
current_lines += line |
|
79
|
|
|
|
|
80
|
|
|
if current_lines != "": |
|
81
|
|
|
yield self.parse_result(filename, current_lines) |
|
82
|
|
|
|
|
83
|
|
|
process.stdout.close() |
|
84
|
|
|
process.stderr.close() |
|
85
|
|
|
|