Skip to content

Commit 1be5c15

Browse files
pdgendtnashif
authored andcommitted
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>
1 parent 973eaff commit 1be5c15

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

.github/workflows/compliance.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
run: |
3939
pip3 install setuptools
4040
pip3 install wheel
41-
pip3 install python-magic lxml junitparser gitlint pylint pykwalify yamllint clang-format unidiff sphinx-lint
41+
pip3 install python-magic lxml junitparser gitlint pylint pykwalify yamllint clang-format unidiff sphinx-lint ruff
4242
pip3 install west
4343
4444
- name: west setup

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ MaintainersFormat.txt
102102
ModulesMaintainers.txt
103103
Nits.txt
104104
Pylint.txt
105+
Ruff.txt
105106
SphinxLint.txt
106107
TextEncoding.txt
107108
YAMLLint.txt

scripts/ci/check_compliance.py

+48
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,54 @@ def run(self):
16391639
self.check_file(file, fp)
16401640

16411641

1642+
class Ruff(ComplianceTest):
1643+
"""
1644+
Ruff
1645+
"""
1646+
name = "Ruff"
1647+
doc = "Check python files with ruff."
1648+
path_hint = "<git-top>"
1649+
1650+
def run(self):
1651+
for file in get_files(filter="d"):
1652+
if not file.endswith(".py"):
1653+
continue
1654+
1655+
try:
1656+
subprocess.run(
1657+
f"ruff check --force-exclude --output-format=json {file}",
1658+
check=True,
1659+
stdout=subprocess.PIPE,
1660+
stderr=subprocess.STDOUT,
1661+
shell=True,
1662+
cwd=GIT_TOP,
1663+
)
1664+
except subprocess.CalledProcessError as ex:
1665+
output = ex.output.decode("utf-8")
1666+
messages = json.loads(output)
1667+
for m in messages:
1668+
self.fmtd_failure(
1669+
"error",
1670+
f'Python lint error ({m.get("code")}) see {m.get("url")}',
1671+
file,
1672+
line=m.get("location", {}).get("row"),
1673+
col=m.get("location", {}).get("column"),
1674+
end_line=m.get("end_location", {}).get("row"),
1675+
end_col=m.get("end_location", {}).get("column"),
1676+
desc=m.get("message"),
1677+
)
1678+
try:
1679+
subprocess.run(
1680+
f"ruff format --force-exclude --diff {file}",
1681+
check=True,
1682+
shell=True,
1683+
cwd=GIT_TOP,
1684+
)
1685+
except subprocess.CalledProcessError:
1686+
desc = f"Run 'ruff format {file}'"
1687+
self.fmtd_failure("error", "Python format error", file, desc=desc)
1688+
1689+
16421690
class TextEncoding(ComplianceTest):
16431691
"""
16441692
Check that any text file is encoded in ascii or utf-8.

scripts/requirements-compliance.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pylint>=3
1010
unidiff
1111
yamllint
1212
sphinx-lint
13+
ruff

0 commit comments

Comments
 (0)