scripts: ci: check_compliance: Add python lint/format check

Add a compliance test using ruff, for both linting and formatting of
newly added python files.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
This commit is contained in:
Pieter De Gendt 2024-11-06 13:54:35 +01:00 committed by Anas Nashif
commit 1be5c157d9
4 changed files with 51 additions and 1 deletions

View file

@ -38,7 +38,7 @@ jobs:
run: |
pip3 install setuptools
pip3 install wheel
pip3 install python-magic lxml junitparser gitlint pylint pykwalify yamllint clang-format unidiff sphinx-lint
pip3 install python-magic lxml junitparser gitlint pylint pykwalify yamllint clang-format unidiff sphinx-lint ruff
pip3 install west
- name: west setup

1
.gitignore vendored
View file

@ -102,6 +102,7 @@ MaintainersFormat.txt
ModulesMaintainers.txt
Nits.txt
Pylint.txt
Ruff.txt
SphinxLint.txt
TextEncoding.txt
YAMLLint.txt

View file

@ -1639,6 +1639,54 @@ class KeepSorted(ComplianceTest):
self.check_file(file, fp)
class Ruff(ComplianceTest):
"""
Ruff
"""
name = "Ruff"
doc = "Check python files with ruff."
path_hint = "<git-top>"
def run(self):
for file in get_files(filter="d"):
if not file.endswith(".py"):
continue
try:
subprocess.run(
f"ruff check --force-exclude --output-format=json {file}",
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
cwd=GIT_TOP,
)
except subprocess.CalledProcessError as ex:
output = ex.output.decode("utf-8")
messages = json.loads(output)
for m in messages:
self.fmtd_failure(
"error",
f'Python lint error ({m.get("code")}) see {m.get("url")}',
file,
line=m.get("location", {}).get("row"),
col=m.get("location", {}).get("column"),
end_line=m.get("end_location", {}).get("row"),
end_col=m.get("end_location", {}).get("column"),
desc=m.get("message"),
)
try:
subprocess.run(
f"ruff format --force-exclude --diff {file}",
check=True,
shell=True,
cwd=GIT_TOP,
)
except subprocess.CalledProcessError:
desc = f"Run 'ruff format {file}'"
self.fmtd_failure("error", "Python format error", file, desc=desc)
class TextEncoding(ComplianceTest):
"""
Check that any text file is encoded in ascii or utf-8.

View file

@ -10,3 +10,4 @@ pylint>=3
unidiff
yamllint
sphinx-lint
ruff