1
1
import json
2
2
import logging
3
- import shlex
4
3
import subprocess
5
4
from pathlib import Path
6
- from subprocess import DEVNULL , PIPE , STDOUT
5
+ from subprocess import PIPE , STDOUT
6
+ from typing import List , Tuple
7
7
8
8
from docker .types import Mount
9
9
@@ -35,27 +35,28 @@ def run_eslint(file_path):
35
35
36
36
def run_shellcheck (file_path ):
37
37
shellcheck_process = subprocess .run (
38
- 'shellcheck --format=json {}' . format ( file_path ) ,
38
+ f 'shellcheck --format=json { file_path } ' ,
39
39
shell = True ,
40
40
stdout = PIPE ,
41
41
stderr = STDOUT ,
42
+ check = False ,
42
43
universal_newlines = True ,
43
44
)
44
45
45
46
if shellcheck_process .returncode == 2 :
46
- logging .debug ('Failed to execute shellcheck:\n {}' . format ( shellcheck_process .stdout ) )
47
- return list ()
47
+ logging .debug (f 'Failed to execute shellcheck:\n { shellcheck_process .stdout } ' )
48
+ return []
48
49
49
50
try :
50
51
shellcheck_json = json .loads (shellcheck_process .stdout )
51
52
except json .JSONDecodeError :
52
- return list ()
53
+ return []
53
54
54
- return _shellcheck_extract_relevant_warnings (shellcheck_json )
55
+ return _extract_shellcheck_warnings (shellcheck_json )
55
56
56
57
57
- def _shellcheck_extract_relevant_warnings (shellcheck_json ):
58
- issues = list ()
58
+ def _extract_shellcheck_warnings (shellcheck_json ):
59
+ issues = []
59
60
for issue in shellcheck_json :
60
61
if issue ['level' ] in ['warning' , 'error' ]:
61
62
issues .append ({
@@ -72,10 +73,11 @@ def run_luacheck(file_path):
72
73
luacheckrc_path = Path (Path (__file__ ).parent , 'config' , 'luacheckrc' )
73
74
74
75
luacheck_process = subprocess .run (
75
- 'luacheck -q --ranges --config {} {}' . format ( luacheckrc_path , file_path ) ,
76
+ f 'luacheck -q --ranges --config { luacheckrc_path } { file_path } ' ,
76
77
shell = True ,
77
78
stdout = PIPE ,
78
79
stderr = STDOUT ,
80
+ check = False ,
79
81
universal_newlines = True ,
80
82
)
81
83
return _luacheck_parse_linter_output (luacheck_process .stdout )
@@ -86,11 +88,11 @@ def _luacheck_parse_linter_output(output):
86
88
https://luacheck.readthedocs.io/en/stable/warnings.html
87
89
ignore_cases = ['(W611)', '(W612)', '(W613)', '(W614)', '(W621)', '(W631)']
88
90
'''
89
- issues = list ()
91
+ issues = []
90
92
for line in output .splitlines ():
91
93
try :
92
94
line_number , columns , code_and_message = _luacheck_split_issue_line (line )
93
- code , message = _luacheck_separate_message_and_code (code_and_message )
95
+ code , message = _separate_message_and_code (code_and_message )
94
96
if not code .startswith ('(W6' ):
95
97
issues .append ({
96
98
'line' : int (line_number ),
@@ -100,8 +102,8 @@ def _luacheck_parse_linter_output(output):
100
102
})
101
103
else :
102
104
pass
103
- except (IndexError , ValueError ) as e :
104
- logging .warning ('Lualinter failed to parse line: {}\n {}' . format ( line , e ) )
105
+ except (IndexError , ValueError ) as error :
106
+ logging .warning (f 'Lualinter failed to parse line: { line } \n { error } ' )
105
107
106
108
return issues
107
109
@@ -111,7 +113,7 @@ def _luacheck_split_issue_line(line):
111
113
return split_by_colon [1 ], split_by_colon [2 ], ':' .join (split_by_colon [3 :]).strip ()
112
114
113
115
114
- def _luacheck_separate_message_and_code (message_string ) :
116
+ def _separate_message_and_code (message_string : str ) -> Tuple [ str , str ] :
115
117
return message_string [1 :5 ], message_string [6 :].strip ()
116
118
117
119
@@ -120,19 +122,26 @@ def _luacheck_get_first_column(columns):
120
122
121
123
122
124
def run_pylint (file_path ):
123
- pylint_process = subprocess .run ('pylint --output-format=json {}' .format (file_path ), shell = True , stdout = PIPE , stderr = STDOUT , universal_newlines = True )
125
+ pylint_process = subprocess .run (
126
+ f'pylint --output-format=json { file_path } ' ,
127
+ shell = True ,
128
+ stdout = PIPE ,
129
+ stderr = STDOUT ,
130
+ check = False ,
131
+ universal_newlines = True
132
+ )
124
133
125
134
try :
126
135
pylint_json = json .loads (pylint_process .stdout )
127
136
except json .JSONDecodeError :
128
- logging .warning ('Failed to execute pylint:\n {}' . format ( pylint_process .stdout ) )
129
- return list ()
137
+ logging .warning (f 'Failed to execute pylint:\n { pylint_process .stdout } ' )
138
+ return []
130
139
131
140
return _pylint_extract_relevant_warnings (pylint_json )
132
141
133
142
134
143
def _pylint_extract_relevant_warnings (pylint_json ):
135
- issues = list ()
144
+ issues = []
136
145
for issue in pylint_json :
137
146
if issue ['type' ] in ['error' , 'warning' ]:
138
147
for unnecessary_information in ['module' , 'obj' , 'path' , 'message-id' ]:
@@ -141,27 +150,32 @@ def _pylint_extract_relevant_warnings(pylint_json):
141
150
return issues
142
151
143
152
144
- def run_rubocop (file_path ):
145
- rubocop_p = subprocess .run (
146
- shlex .split (f'rubocop --format json { file_path } ' ),
147
- stdout = PIPE ,
148
- stderr = DEVNULL ,
149
- check = False ,
153
+ def run_rubocop (file_path : str ) -> List [dict ]:
154
+ container_path = '/input'
155
+ process = run_docker_container (
156
+ 'pipelinecomponents/rubocop:latest' ,
157
+ combine_stderr_stdout = False ,
158
+ mounts = [
159
+ Mount (container_path , file_path , type = 'bind' , read_only = True ),
160
+ ],
161
+ command = f'rubocop --format json -- { container_path } ' ,
150
162
)
151
- linter_output = json .loads (rubocop_p .stdout )
152
-
153
- issues = []
154
- for offense in linter_output ['files' ][0 ]['offenses' ]:
155
- issues .append (
156
- {
157
- 'symbol' : offense ['cop_name' ],
158
- 'line' : offense ['location' ]['start_line' ],
159
- 'column' : offense ['location' ]['column' ],
160
- 'message' : offense ['message' ],
161
- }
162
- )
163
163
164
- return issues
164
+ try :
165
+ linter_output = json .loads (process .stdout )
166
+ except json .JSONDecodeError :
167
+ logging .warning (f'Failed to execute rubocop linter:\n { process .stderr } ' )
168
+ return []
169
+
170
+ return [
171
+ {
172
+ 'symbol' : offense ['cop_name' ],
173
+ 'line' : offense ['location' ]['start_line' ],
174
+ 'column' : offense ['location' ]['column' ],
175
+ 'message' : offense ['message' ],
176
+ }
177
+ for offense in linter_output ['files' ][0 ]['offenses' ]
178
+ ]
165
179
166
180
167
181
def run_phpstan (file_path ):
0 commit comments