From 1be5c157d9bf409ce43405d6c66cd41a60d9282f Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Wed, 6 Nov 2024 13:54:35 +0100 Subject: [PATCH] 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 --- .github/workflows/compliance.yml | 2 +- .gitignore | 1 + scripts/ci/check_compliance.py | 48 +++++++++++++++++++++++++++++ scripts/requirements-compliance.txt | 1 + 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/.github/workflows/compliance.yml b/.github/workflows/compliance.yml index 48dfa237aca..a91a8e534cc 100644 --- a/.github/workflows/compliance.yml +++ b/.github/workflows/compliance.yml @@ -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 diff --git a/.gitignore b/.gitignore index c33e2d93747..b545443db31 100644 --- a/.gitignore +++ b/.gitignore @@ -102,6 +102,7 @@ MaintainersFormat.txt ModulesMaintainers.txt Nits.txt Pylint.txt +Ruff.txt SphinxLint.txt TextEncoding.txt YAMLLint.txt diff --git a/scripts/ci/check_compliance.py b/scripts/ci/check_compliance.py index 47edd292e41..40b90e2aa39 100755 --- a/scripts/ci/check_compliance.py +++ b/scripts/ci/check_compliance.py @@ -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 = "" + + 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. diff --git a/scripts/requirements-compliance.txt b/scripts/requirements-compliance.txt index a4396986934..273b4f37dc5 100644 --- a/scripts/requirements-compliance.txt +++ b/scripts/requirements-compliance.txt @@ -10,3 +10,4 @@ pylint>=3 unidiff yamllint sphinx-lint +ruff