2424import logging
2525import pathlib
2626import re
27+ from io import StringIO
2728from tempfile import TemporaryDirectory
2829
29- from pylint import epylint as linter
30-
3130import requests
3231
3332import sh
33+ from pylint import lint
34+ from pylint .reporters import JSONReporter
3435from sh .contrib import git
3536
3637from adabot import github_requests as github
4243from packaging .requirements import Requirement as pkg_Requirement
4344
4445
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+
4555# Define constants for error strings to make checking against them more robust:
4656ERROR_README_DOWNLOAD_FAILED = "Failed to download README"
4757ERROR_README_IMAGE_MISSING_ALT = "README image missing alt text"
166176# Cache the CircuitPython driver page so we can make sure every driver is linked to.
167177core_driver_page = None
168178
179+
169180class library_validator ():
170181 """ Class to hold instance variables needed to traverse the calling
171182 code, and the validator functions.
@@ -1084,27 +1095,26 @@ def validate_passes_linting(self, repo):
10841095 if file .name in ignored_py_files or str (file .parent ).endswith ("examples" ):
10851096 continue
10861097
1087- py_run_args = f" { file } --output-format=json"
1098+ pylint_args = [ str ( file )]
10881099 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 ( )
10921103
10931104 logging .debug ("Running pylint on %s" , file )
10941105
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 ()
10991109
1100- if pylint_stderr . getvalue () :
1110+ if pylint_stderr :
11011111 self .output_file_data .append (
1102- f"PyLint error ({ repo ['name' ]} ): '{ pylint_stderr . getvalue () } '"
1112+ f"PyLint error ({ repo ['name' ]} ): '{ pylint_stderr } '"
11031113 )
11041114 return [ERROR_OUTPUT_HANDLER ]
11051115
11061116 try :
1107- pylint_result = json .loads (pylint_stdout . getvalue () )
1117+ pylint_result = json .loads (pylint_stdout )
11081118 except json .JSONDecodeError as json_err :
11091119 self .output_file_data .append (
11101120 f"PyLint output JSONDecodeError: { json_err .msg } "
@@ -1116,6 +1126,6 @@ def validate_passes_linting(self, repo):
11161126
11171127 if self .keep_repos :
11181128 with open (repo_dir / '.pylint-ok' , 'w' ) as f :
1119- f .write (pylint_result )
1129+ f .write ('' . join ( pylint_result ) )
11201130
11211131 return []
0 commit comments