Skip to content

Commit

Permalink
scripts: compliance: handle multi-line output from pylint
Browse files Browse the repository at this point in the history
Switch from plain text to JSON output in the pylint compliance check in
order to handle multi-line messages, which were so far being dropped
by the regex.

Fixes zephyrproject-rtos#68037.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
  • Loading branch information
carlescufi committed May 10, 2024
1 parent f4c067c commit 54300e6
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions scripts/ci/check_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import argparse
import collections
from email.utils import parseaddr
import json
import logging
import os
from pathlib import Path
Expand Down Expand Up @@ -178,7 +179,6 @@ def fmtd_failure(self, severity, title, file, line=None, col=None, desc=""):
self._result(fail, fail.text)
self.fmtd_failures.append(fail)


class EndTest(Exception):
"""
Raised by ComplianceTest.error()/skip() to end the test.
Expand Down Expand Up @@ -1169,7 +1169,7 @@ def run(self):
else:
python_environment["PYTHONPATH"] = check_script_dir

pylintcmd = ["pylint", "--rcfile=" + pylintrc,
pylintcmd = ["pylint", "--output-format=json2", "--rcfile=" + pylintrc,
"--load-plugins=argparse-checker"] + py_files
logger.info(cmd2str(pylintcmd))
try:
Expand All @@ -1181,21 +1181,19 @@ def run(self):
env=python_environment)
except subprocess.CalledProcessError as ex:
output = ex.output.decode("utf-8")
regex = r'^\s*(\S+):(\d+):(\d+):\s*([A-Z]\d{4}):\s*(.*)$'

matches = re.findall(regex, output, re.MULTILINE)
for m in matches:
# https://pylint.pycqa.org/en/latest/user_guide/messages/messages_overview.html#
messages = json.loads(output)['messages']
for m in messages:
severity = 'unknown'
if m[3][0] in ('F', 'E'):
if m['messageId'][0] in ('F', 'E'):
severity = 'error'
elif m[3][0] in ('W','C', 'R', 'I'):
elif m['messageId'][0] in ('W','C', 'R', 'I'):
severity = 'warning'
self.fmtd_failure(severity, m[3], m[0], m[1], col=m[2],
desc=m[4])
self.fmtd_failure(severity, m['messageId'], m['path'],
m['line'], col=str(m['column']), desc=m['message']
+ f" ({m['symbol']})")

# If the regex has not matched add the whole output as a failure
if len(matches) == 0:
if len(messages) == 0:
# If there are no specific messages add the whole output as a failure
self.failure(output)


Expand Down

0 comments on commit 54300e6

Please sign in to comment.