Skip to content

Commit

Permalink
scripts: ci: check_compliance: Add python lint/format check
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
pdgendt committed Nov 7, 2024
1 parent 9104d26 commit 80df49f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/compliance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ MaintainersFormat.txt
ModulesMaintainers.txt
Nits.txt
Pylint.txt
Ruff.txt
SphinxLint.txt
TextEncoding.txt
YAMLLint.txt
28 changes: 28 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) 2024 Basalte bv
# SPDX-License-Identifier: Apache-2.0

line-length = 100
target-version = "py310"

[lint]
select = [
# zephyr-keep-sorted-start
"B", # flake8-bugbear
"E", # pycodestyle
"F", # pyflakes
"I", # isort
"SIM", # flake8-simplify
"UP", # pyupgrade
"W", # pycodestyle warnings
# zephyr-keep-sorted-stop
]

ignore = [
# zephyr-keep-sorted-start
"UP027", # deprecated pyupgrade rule
# zephyr-keep-sorted-stop
]

[format]
quote-style = "preserve"
line-ending = "lf"
48 changes: 48 additions & 0 deletions scripts/ci/check_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,54 @@ def run(self):
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 --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 --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.
Expand Down
1 change: 1 addition & 0 deletions scripts/requirements-compliance.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pylint>=3
unidiff
yamllint
sphinx-lint
ruff

0 comments on commit 80df49f

Please sign in to comment.