scripts: ci: guideline_check: fix `ruff` reports
Fix problems reported by `ruff` and remove exclusions from the config
file.
Signed-off-by: Jordan Yates <jordan@embeint.com>
diff --git a/.ruff-excludes.toml b/.ruff-excludes.toml
index 4ad8bde..7c095bf 100644
--- a/.ruff-excludes.toml
+++ b/.ruff-excludes.toml
@@ -290,11 +290,6 @@
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
]
-"./scripts/ci/guideline_check.py" = [
- "E501", # https://docs.astral.sh/ruff/rules/line-too-long
- "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
- "UP032", # https://docs.astral.sh/ruff/rules/f-string
-]
"./scripts/ci/stats/merged_prs.py" = [
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
@@ -1185,7 +1180,6 @@
"./scripts/ci/check_compliance.py",
"./scripts/ci/coverage/coverage_analysis.py",
"./scripts/ci/errno.py",
- "./scripts/ci/guideline_check.py",
"./scripts/ci/set_assignees.py",
"./scripts/ci/stats/merged_prs.py",
"./scripts/ci/test_plan.py",
diff --git a/scripts/ci/guideline_check.py b/scripts/ci/guideline_check.py
index 00e0b3c..4a106fb 100755
--- a/scripts/ci/guideline_check.py
+++ b/scripts/ci/guideline_check.py
@@ -2,10 +2,11 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2021 Intel Corporation
-import os
-import sh
import argparse
+import os
import re
+
+import sh
from unidiff import PatchSet
if "ZEPHYR_BASE" not in os.environ:
@@ -13,10 +14,11 @@
RESERVED_NAMES_SCRIPT = "/scripts/coccinelle/reserved_names.cocci"
-coccinelle_scripts = [RESERVED_NAMES_SCRIPT,
- "/scripts/coccinelle/same_identifier.cocci",
- #"/scripts/coccinelle/identifier_length.cocci",
- ]
+coccinelle_scripts = [
+ RESERVED_NAMES_SCRIPT,
+ "/scripts/coccinelle/same_identifier.cocci",
+ # "/scripts/coccinelle/identifier_length.cocci",
+]
coccinelle_reserved_names_exclude_regex = [
r"lib/libc/.*",
@@ -24,6 +26,7 @@
r"include/zephyr/posix/.*",
]
+
def parse_coccinelle(contents: str, violations: dict):
reg = re.compile("([a-zA-Z0-9_/]*\\.[ch]:[0-9]*)(:[0-9\\-]*: )(.*)")
for line in contents.split("\n"):
@@ -38,13 +41,11 @@
def parse_args():
parser = argparse.ArgumentParser(
- description="Check commits against Cocccinelle rules", allow_abbrev=False)
- parser.add_argument('-r', "--repository", required=False,
- help="Path to repository")
- parser.add_argument('-c', '--commits', default=None,
- help="Commit range in the form: a..b")
- parser.add_argument("-o", "--output", required=False,
- help="Print violation into a file")
+ description="Check commits against Cocccinelle rules", allow_abbrev=False
+ )
+ parser.add_argument('-r', "--repository", required=False, help="Path to repository")
+ parser.add_argument('-c', '--commits', default=None, help="Commit range in the form: a..b")
+ parser.add_argument("-o", "--output", required=False, help="Print violation into a file")
return parser.parse_args()
@@ -58,10 +59,7 @@
else:
repository_path = args.repository
- sh_special_args = {
- '_tty_out': False,
- '_cwd': repository_path
- }
+ sh_special_args = {'_tty_out': False, '_cwd': repository_path}
# pylint does not like the 'sh' library
# pylint: disable=too-many-function-args,unexpected-keyword-arg
@@ -72,11 +70,13 @@
numViolations = 0
for f in patch_set:
- if not f.path.endswith(".c") and not f.path.endswith(".h") or not os.path.exists(zephyr_base + "/" + f.path):
+ c_file = f.path.endswith(".c")
+ h_file = f.path.endswith(".h")
+ exists = os.path.exists(zephyr_base + "/" + f.path)
+ if not c_file and not h_file or not exists:
continue
for script in coccinelle_scripts:
-
skip_reserved_names = False
if script == RESERVED_NAMES_SCRIPT:
for path in coccinelle_reserved_names_exclude_regex:
@@ -87,16 +87,16 @@
if skip_reserved_names:
continue
- script_path =zephyr_base + "/" + script
+ script_path = zephyr_base + "/" + script
print(f"Running {script} on {f.path}")
try:
cocci = sh.coccicheck(
"--mode=report",
- "--cocci=" +
- script_path,
+ "--cocci=" + script_path,
f.path,
_timeout=10,
- **sh_special_args)
+ **sh_special_args,
+ )
parse_coccinelle(cocci, violations)
except sh.TimeoutException:
print("we timed out waiting, skipping...")
@@ -104,19 +104,16 @@
for hunk in f:
for line in hunk:
if line.is_added:
- violation = "{}:{}".format(f.path, line.target_line_no)
+ violation = f"{f.path}:{line.target_line_no}"
if violation in violations:
+ v_str = "\t\n".join(violations[violation])
+ out_str = f"{violation}:{v_str}"
numViolations += 1
if args.output:
with open(args.output, "a+") as fp:
- fp.write("{}:{}\n".format(
- violation, "\t\n".join(
- violations[violation])))
+ fp.write(f"{out_str}\n")
else:
- print(
- "{}:{}".format(
- violation, "\t\n".join(
- violations[violation])))
+ print(out_str)
return numViolations