|
3 | 3 | import collections
|
4 | 4 | import logging
|
5 | 5 | import sys
|
| 6 | +import re |
| 7 | +from subprocess import Popen, PIPE |
6 | 8 |
|
7 | 9 | from pylint.epylint import py_run
|
8 | 10 | from pyls import hookimpl, lsp
|
@@ -154,12 +156,149 @@ def _build_pylint_flags(settings):
|
154 | 156 | def pyls_settings():
|
155 | 157 | # Default pylint to disabled because it requires a config
|
156 | 158 | # file to be useful.
|
157 |
| - return {'plugins': {'pylint': {'enabled': False, 'args': []}}} |
| 159 | + return {'plugins': {'pylint': { |
| 160 | + 'enabled': False, |
| 161 | + 'args': [], |
| 162 | + # disabled by default as it can slow down the workflow |
| 163 | + 'executable': None, |
| 164 | + }}} |
158 | 165 |
|
159 | 166 |
|
160 | 167 | @hookimpl
|
161 | 168 | def pyls_lint(config, document, is_saved):
|
| 169 | + """Run pylint linter.""" |
162 | 170 | settings = config.plugin_settings('pylint')
|
163 | 171 | log.debug("Got pylint settings: %s", settings)
|
| 172 | + # pylint >= 2.5.0 is required for working through stdin and only |
| 173 | + # available with python3 |
| 174 | + if settings.get('executable') and sys.version_info[0] >= 3: |
| 175 | + flags = build_args_stdio(settings) |
| 176 | + pylint_executable = settings.get('executable', 'pylint') |
| 177 | + return pylint_lint_stdin(pylint_executable, document, flags) |
164 | 178 | flags = _build_pylint_flags(settings)
|
165 | 179 | return PylintLinter.lint(document, is_saved, flags=flags)
|
| 180 | + |
| 181 | + |
| 182 | +def build_args_stdio(settings): |
| 183 | + """Build arguments for calling pylint. |
| 184 | +
|
| 185 | + :param settings: client settings |
| 186 | + :type settings: dict |
| 187 | +
|
| 188 | + :return: arguments to path to pylint |
| 189 | + :rtype: list |
| 190 | + """ |
| 191 | + pylint_args = settings.get('args') |
| 192 | + if pylint_args is None: |
| 193 | + return [] |
| 194 | + return pylint_args |
| 195 | + |
| 196 | + |
| 197 | +def pylint_lint_stdin(pylint_executable, document, flags): |
| 198 | + """Run pylint linter from stdin. |
| 199 | +
|
| 200 | + This runs pylint in a subprocess with popen. |
| 201 | + This allows passing the file from stdin and as a result |
| 202 | + run pylint on unsaved files. Can slowdown the workflow. |
| 203 | +
|
| 204 | + :param pylint_executable: path to pylint executable |
| 205 | + :type pylint_executable: string |
| 206 | + :param document: document to run pylint on |
| 207 | + :type document: pyls.workspace.Document |
| 208 | + :param flags: arguments to path to pylint |
| 209 | + :type flags: list |
| 210 | +
|
| 211 | + :return: linting diagnostics |
| 212 | + :rtype: list |
| 213 | + """ |
| 214 | + pylint_result = _run_pylint_stdio(pylint_executable, document, flags) |
| 215 | + return _parse_pylint_stdio_result(document, pylint_result) |
| 216 | + |
| 217 | + |
| 218 | +def _run_pylint_stdio(pylint_executable, document, flags): |
| 219 | + """Run pylint in popen. |
| 220 | +
|
| 221 | + :param pylint_executable: path to pylint executable |
| 222 | + :type pylint_executable: string |
| 223 | + :param document: document to run pylint on |
| 224 | + :type document: pyls.workspace.Document |
| 225 | + :param flags: arguments to path to pylint |
| 226 | + :type flags: list |
| 227 | +
|
| 228 | + :return: result of calling pylint |
| 229 | + :rtype: string |
| 230 | + """ |
| 231 | + log.debug("Calling %s with args: '%s'", pylint_executable, flags) |
| 232 | + try: |
| 233 | + cmd = [pylint_executable] |
| 234 | + cmd.extend(flags) |
| 235 | + cmd.extend(['--from-stdin', document.path]) |
| 236 | + p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) |
| 237 | + except IOError: |
| 238 | + log.debug("Can't execute %s. Trying with 'python -m pylint'", pylint_executable) |
| 239 | + cmd = ['python', '-m', 'pylint'] |
| 240 | + cmd.extend(flags) |
| 241 | + cmd.extend(['--from-stdin', document.path]) |
| 242 | + p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) |
| 243 | + (stdout, stderr) = p.communicate(document.source.encode()) |
| 244 | + if stderr: |
| 245 | + log.error("Error while running pylint '%s'", stderr.decode()) |
| 246 | + return stdout.decode() |
| 247 | + |
| 248 | + |
| 249 | +def _parse_pylint_stdio_result(document, stdout): |
| 250 | + """Parse pylint results. |
| 251 | +
|
| 252 | + :param document: document to run pylint on |
| 253 | + :type document: pyls.workspace.Document |
| 254 | + :param stdout: pylint results to parse |
| 255 | + :type stdout: string |
| 256 | +
|
| 257 | + :return: linting diagnostics |
| 258 | + :rtype: list |
| 259 | + """ |
| 260 | + diagnostics = [] |
| 261 | + lines = stdout.splitlines() |
| 262 | + for raw_line in lines: |
| 263 | + parsed_line = re.match(r'(.*):(\d*):(\d*): (\w*): (.*)', raw_line) |
| 264 | + if not parsed_line: |
| 265 | + log.debug("Pylint output parser can't parse line '%s'", raw_line) |
| 266 | + continue |
| 267 | + |
| 268 | + parsed_line = parsed_line.groups() |
| 269 | + if len(parsed_line) != 5: |
| 270 | + log.debug("Pylint output parser can't parse line '%s'", raw_line) |
| 271 | + continue |
| 272 | + |
| 273 | + _, line, character, code, msg = parsed_line |
| 274 | + line = int(line) - 1 |
| 275 | + character = int(character) |
| 276 | + severity_map = { |
| 277 | + 'C': lsp.DiagnosticSeverity.Information, |
| 278 | + 'E': lsp.DiagnosticSeverity.Error, |
| 279 | + 'F': lsp.DiagnosticSeverity.Error, |
| 280 | + 'R': lsp.DiagnosticSeverity.Hint, |
| 281 | + 'W': lsp.DiagnosticSeverity.Warning, |
| 282 | + } |
| 283 | + severity = severity_map[code[0]] |
| 284 | + diagnostics.append( |
| 285 | + { |
| 286 | + 'source': 'pylint', |
| 287 | + 'code': code, |
| 288 | + 'range': { |
| 289 | + 'start': { |
| 290 | + 'line': line, |
| 291 | + 'character': character |
| 292 | + }, |
| 293 | + 'end': { |
| 294 | + 'line': line, |
| 295 | + # no way to determine the column |
| 296 | + 'character': len(document.lines[line]) - 1 |
| 297 | + } |
| 298 | + }, |
| 299 | + 'message': msg, |
| 300 | + 'severity': severity, |
| 301 | + } |
| 302 | + ) |
| 303 | + |
| 304 | + return diagnostics |
0 commit comments