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:
parent
973eaff5a2
commit
1be5c157d9
4 changed files with 51 additions and 1 deletions
2
.github/workflows/compliance.yml
vendored
2
.github/workflows/compliance.yml
vendored
|
@ -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
1
.gitignore
vendored
|
@ -102,6 +102,7 @@ MaintainersFormat.txt
|
|||
ModulesMaintainers.txt
|
||||
Nits.txt
|
||||
Pylint.txt
|
||||
Ruff.txt
|
||||
SphinxLint.txt
|
||||
TextEncoding.txt
|
||||
YAMLLint.txt
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -10,3 +10,4 @@ pylint>=3
|
|||
unidiff
|
||||
yamllint
|
||||
sphinx-lint
|
||||
ruff
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue