24
24
import logging
25
25
import pathlib
26
26
import re
27
+ from io import StringIO
27
28
from tempfile import TemporaryDirectory
28
29
29
- from pylint import epylint as linter
30
-
31
30
import requests
32
31
33
32
import sh
33
+ from pylint import lint
34
+ from pylint .reporters import JSONReporter
34
35
from sh .contrib import git
35
36
36
37
from adabot import github_requests as github
42
43
from packaging .requirements import Requirement as pkg_Requirement
43
44
44
45
46
+ class CapturedJsonReporter (JSONReporter ):
47
+
48
+ def __init__ (self ):
49
+ self ._stringio = StringIO ()
50
+ super ().__init__ (self ._stringio )
51
+
52
+ def get_result (self ):
53
+ return self ._stringio .getvalue ()
54
+
45
55
# Define constants for error strings to make checking against them more robust:
46
56
ERROR_README_DOWNLOAD_FAILED = "Failed to download README"
47
57
ERROR_README_IMAGE_MISSING_ALT = "README image missing alt text"
166
176
# Cache the CircuitPython driver page so we can make sure every driver is linked to.
167
177
core_driver_page = None
168
178
179
+
169
180
class library_validator ():
170
181
""" Class to hold instance variables needed to traverse the calling
171
182
code, and the validator functions.
@@ -1084,27 +1095,26 @@ def validate_passes_linting(self, repo):
1084
1095
if file .name in ignored_py_files or str (file .parent ).endswith ("examples" ):
1085
1096
continue
1086
1097
1087
- py_run_args = f" { file } --output-format=json"
1098
+ pylint_args = [ str ( file )]
1088
1099
if (repo_dir / '.pylintrc' ).exists ():
1089
- py_run_args += (
1090
- f" --rcfile= { str ( repo_dir / '.pylintrc' ) } "
1091
- )
1100
+ pylint_args += [ f"--rcfile= { str ( repo_dir / '.pylintrc' ) } " ]
1101
+
1102
+ reporter = CapturedJsonReporter ( )
1092
1103
1093
1104
logging .debug ("Running pylint on %s" , file )
1094
1105
1095
- pylint_stdout , pylint_stderr = linter .py_run (
1096
- py_run_args ,
1097
- return_std = True
1098
- )
1106
+ linted = lint .Run (pylint_args , reporter = reporter , exit = False )
1107
+ pylint_stderr = ''
1108
+ pylint_stdout = reporter .get_result ()
1099
1109
1100
- if pylint_stderr . getvalue () :
1110
+ if pylint_stderr :
1101
1111
self .output_file_data .append (
1102
- f"PyLint error ({ repo ['name' ]} ): '{ pylint_stderr . getvalue () } '"
1112
+ f"PyLint error ({ repo ['name' ]} ): '{ pylint_stderr } '"
1103
1113
)
1104
1114
return [ERROR_OUTPUT_HANDLER ]
1105
1115
1106
1116
try :
1107
- pylint_result = json .loads (pylint_stdout . getvalue () )
1117
+ pylint_result = json .loads (pylint_stdout )
1108
1118
except json .JSONDecodeError as json_err :
1109
1119
self .output_file_data .append (
1110
1120
f"PyLint output JSONDecodeError: { json_err .msg } "
@@ -1116,6 +1126,6 @@ def validate_passes_linting(self, repo):
1116
1126
1117
1127
if self .keep_repos :
1118
1128
with open (repo_dir / '.pylint-ok' , 'w' ) as f :
1119
- f .write (pylint_result )
1129
+ f .write ('' . join ( pylint_result ) )
1120
1130
1121
1131
return []
0 commit comments