Skip to content

Commit 9b4397a

Browse files
LukaszMrugalacarlescufi
authored andcommitted
scripts: Fix twisterlib for ruff - UP030, UP031, UP032
Fixes ruff linting errors UP030, UP031, UP032, which make you use format specifiers (fstrings) instead of printf formatting or str.format(). Signed-off-by: Lukasz Mrugala <[email protected]>
1 parent 7746a97 commit 9b4397a

18 files changed

+215
-226
lines changed

.ruff-excludes.toml

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -758,78 +758,54 @@
758758
]
759759
"./scripts/pylib/twister/twisterlib/cmakecache.py" = [
760760
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
761-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
762761
]
763762
"./scripts/pylib/twister/twisterlib/config_parser.py" = [
764763
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
765-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
766764
]
767765
"./scripts/pylib/twister/twisterlib/coverage.py" = [
768766
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
769-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
770-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
771767
]
772768
"./scripts/pylib/twister/twisterlib/environment.py" = [
773769
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
774-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
775770
]
776771
"./scripts/pylib/twister/twisterlib/handlers.py" = [
777772
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
778773
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
779-
"UP030", # https://docs.astral.sh/ruff/rules/format-literals
780-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
781-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
782774
]
783775
"./scripts/pylib/twister/twisterlib/hardwaremap.py" = [
784776
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
785-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
786777
]
787778
"./scripts/pylib/twister/twisterlib/harness.py" = [
788779
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
789-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
790-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
791780
]
792781
"./scripts/pylib/twister/twisterlib/platform.py" = [
793782
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
794-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
795783
]
796784
"./scripts/pylib/twister/twisterlib/quarantine.py" = [
797785
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
798-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
799786
]
800787
"./scripts/pylib/twister/twisterlib/reports.py" = [
801788
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
802-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
803-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
804789
]
805790
"./scripts/pylib/twister/twisterlib/runner.py" = [
806791
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
807-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
808-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
809792
]
810793
"./scripts/pylib/twister/twisterlib/size_calc.py" = [
811794
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
812-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
813795
]
814796
"./scripts/pylib/twister/twisterlib/testinstance.py" = [
815797
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
816-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
817798
]
818799
"./scripts/pylib/twister/twisterlib/testplan.py" = [
819800
"E402", # https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file
820801
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
821802
"F401", # https://docs.astral.sh/ruff/rules/unused-import
822-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
823-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
824803
]
825804
"./scripts/pylib/twister/twisterlib/testsuite.py" = [
826805
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
827-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
828-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
829806
]
830807
"./scripts/pylib/twister/twisterlib/twister_main.py" = [
831808
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
832-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
833809
]
834810
"./scripts/pylint/checkers/argparse-checker.py" = [
835811
"F821", # https://docs.astral.sh/ruff/rules/undefined-name

scripts/pylib/twister/twisterlib/cmakecache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _to_bool(cls, val):
5959
v = int(val)
6060
return v != 0
6161
except ValueError as exc:
62-
raise ValueError('invalid bool {}'.format(val)) from exc
62+
raise ValueError(f'invalid bool {val}') from exc
6363

6464
@classmethod
6565
def from_line(cls, line, line_no):
@@ -81,7 +81,7 @@ def from_line(cls, line, line_no):
8181
try:
8282
value = cls._to_bool(value)
8383
except ValueError as exc:
84-
args = exc.args + ('on line {}: {}'.format(line_no, line),)
84+
args = exc.args + (f'on line {line_no}: {line}',)
8585
raise ValueError(args) from exc
8686
# If the value is a CMake list (i.e. is a string which contains a ';'),
8787
# convert to a Python list.

scripts/pylib/twister/twisterlib/config_parser.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ def _cast_value(self, value, typestr):
164164
elif typestr.startswith("map"):
165165
return value
166166
else:
167-
raise ConfigurationError(
168-
self.filename, "unknown type '%s'" % value)
167+
raise ConfigurationError(self.filename, f"unknown type '{value}'")
169168

170169
def get_scenario(self, name):
171170
"""Get a dictionary representing the keys/values within a scenario
@@ -199,7 +198,7 @@ def get_scenario(self, name):
199198
)
200199
if k in d:
201200
if k == "filter":
202-
d[k] = "(%s) and (%s)" % (d[k], v)
201+
d[k] = f"({d[k]}) and ({v})"
203202
elif k not in ("extra_conf_files", "extra_overlay_confs",
204203
"extra_dtc_overlay_files"):
205204
if isinstance(d[k], str) and isinstance(v, list):
@@ -258,8 +257,8 @@ def get_scenario(self, name):
258257
if required:
259258
raise ConfigurationError(
260259
self.filename,
261-
"missing required value for '%s' in test '%s'" %
262-
(k, name))
260+
f"missing required value for '{k}' in test '{name}'"
261+
)
263262
else:
264263
if "default" in kinfo:
265264
default = kinfo["default"]
@@ -271,8 +270,8 @@ def get_scenario(self, name):
271270
d[k] = self._cast_value(d[k], kinfo["type"])
272271
except ValueError:
273272
raise ConfigurationError(
274-
self.filename, "bad %s value '%s' for key '%s' in name '%s'" %
275-
(kinfo["type"], d[k], k, name)
273+
self.filename,
274+
f"bad {kinfo['type']} value '{d[k]}' for key '{k}' in name '{name}'"
276275
) from None
277276

278277
return d

scripts/pylib/twister/twisterlib/coverage.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ def factory(tool, jobs=None):
3939
elif tool == 'gcovr':
4040
t = Gcovr()
4141
else:
42-
logger.error("Unsupported coverage tool specified: {}".format(tool))
42+
logger.error(f"Unsupported coverage tool specified: {tool}")
4343
return None
4444

4545
logger.debug(f"Select {tool} as the coverage tool...")
4646
return t
4747

4848
@staticmethod
4949
def retrieve_gcov_data(input_file):
50-
logger.debug("Working on %s" % input_file)
50+
logger.debug(f"Working on {input_file}")
5151
extracted_coverage_info = {}
5252
capture_data = False
5353
capture_complete = False
@@ -125,47 +125,61 @@ def create_gcda_files(self, extracted_coverage_info):
125125
with open(filename, 'wb') as fp:
126126
fp.write(hex_bytes)
127127
except ValueError:
128-
logger.exception("Unable to convert hex data for file: {}".format(filename))
128+
logger.exception(f"Unable to convert hex data for file: {filename}")
129129
gcda_created = False
130130
except FileNotFoundError:
131-
logger.exception("Unable to create gcda file: {}".format(filename))
131+
logger.exception(f"Unable to create gcda file: {filename}")
132132
gcda_created = False
133133
return gcda_created
134134

135135
def generate(self, outdir):
136136
coverage_completed = True
137-
for filename in glob.glob("%s/**/handler.log" % outdir, recursive=True):
137+
for filename in glob.glob(f"{outdir}/**/handler.log", recursive=True):
138138
gcov_data = self.__class__.retrieve_gcov_data(filename)
139139
capture_complete = gcov_data['complete']
140140
extracted_coverage_info = gcov_data['data']
141141
if capture_complete:
142142
gcda_created = self.create_gcda_files(extracted_coverage_info)
143143
if gcda_created:
144-
logger.debug("Gcov data captured: {}".format(filename))
144+
logger.debug(f"Gcov data captured: {filename}")
145145
else:
146-
logger.error("Gcov data invalid for: {}".format(filename))
146+
logger.error(f"Gcov data invalid for: {filename}")
147147
coverage_completed = False
148148
else:
149-
logger.error("Gcov data capture incomplete: {}".format(filename))
149+
logger.error(f"Gcov data capture incomplete: {filename}")
150150
coverage_completed = False
151151

152152
with open(os.path.join(outdir, "coverage.log"), "a") as coveragelog:
153153
ret = self._generate(outdir, coveragelog)
154154
if ret == 0:
155155
report_log = {
156-
"html": "HTML report generated: {}".format(os.path.join(outdir, "coverage", "index.html")),
157-
"lcov": "LCOV report generated: {}".format(os.path.join(outdir, "coverage.info")),
158-
"xml": "XML report generated: {}".format(os.path.join(outdir, "coverage", "coverage.xml")),
159-
"csv": "CSV report generated: {}".format(os.path.join(outdir, "coverage", "coverage.csv")),
160-
"txt": "TXT report generated: {}".format(os.path.join(outdir, "coverage", "coverage.txt")),
161-
"coveralls": "Coveralls report generated: {}".format(os.path.join(outdir, "coverage", "coverage.coveralls.json")),
162-
"sonarqube": "Sonarqube report generated: {}".format(os.path.join(outdir, "coverage", "coverage.sonarqube.xml"))
156+
"html": "HTML report generated: {}".format(
157+
os.path.join(outdir, "coverage", "index.html")
158+
),
159+
"lcov": "LCOV report generated: {}".format(
160+
os.path.join(outdir, "coverage.info")
161+
),
162+
"xml": "XML report generated: {}".format(
163+
os.path.join(outdir, "coverage", "coverage.xml")
164+
),
165+
"csv": "CSV report generated: {}".format(
166+
os.path.join(outdir, "coverage", "coverage.csv")
167+
),
168+
"txt": "TXT report generated: {}".format(
169+
os.path.join(outdir, "coverage", "coverage.txt")
170+
),
171+
"coveralls": "Coveralls report generated: {}".format(
172+
os.path.join(outdir, "coverage", "coverage.coveralls.json")
173+
),
174+
"sonarqube": "Sonarqube report generated: {}".format(
175+
os.path.join(outdir, "coverage", "coverage.sonarqube.xml")
176+
)
163177
}
164178
for r in self.output_formats.split(','):
165179
logger.info(report_log[r])
166180
else:
167181
coverage_completed = False
168-
logger.debug("All coverage data processed: {}".format(coverage_completed))
182+
logger.debug(f"All coverage data processed: {coverage_completed}")
169183
return coverage_completed
170184

171185

scripts/pylib/twister/twisterlib/environment.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,7 @@ def add_parse_arguments(parser = None) -> argparse.ArgumentParser:
286286

287287
# Start of individual args place them in alpha-beta order
288288

289-
board_root_list = ["%s/boards" % ZEPHYR_BASE,
290-
"%s/subsys/testsuite/boards" % ZEPHYR_BASE]
289+
board_root_list = [f"{ZEPHYR_BASE}/boards", f"{ZEPHYR_BASE}/subsys/testsuite/boards"]
291290

292291
modules = zephyr_module.parse_modules(ZEPHYR_BASE)
293292
for module in modules:
@@ -934,10 +933,10 @@ def parse_arguments(parser: argparse.ArgumentParser, args, options = None, on_in
934933
double_dash = len(options.extra_test_args)
935934
unrecognized = " ".join(options.extra_test_args[0:double_dash])
936935

937-
logger.error("Unrecognized arguments found: '%s'. Use -- to "
938-
"delineate extra arguments for test binary or pass "
939-
"-h for help.",
940-
unrecognized)
936+
logger.error(
937+
f"Unrecognized arguments found: '{unrecognized}'."
938+
" Use -- to delineate extra arguments for test binary or pass -h for help."
939+
)
941940

942941
sys.exit(1)
943942

@@ -1065,7 +1064,7 @@ def run_cmake_script(args=None):
10651064
args = []
10661065
script = os.fspath(args[0])
10671066

1068-
logger.debug("Running cmake script %s", script)
1067+
logger.debug(f"Running cmake script {script}")
10691068

10701069
cmake_args = ["-D{}".format(a.replace('"', '')) for a in args[1:]]
10711070
cmake_args.extend(['-P', script])
@@ -1093,12 +1092,12 @@ def run_cmake_script(args=None):
10931092
out = strip_ansi_sequences(out.decode())
10941093

10951094
if p.returncode == 0:
1096-
msg = "Finished running %s" % (args[0])
1095+
msg = f"Finished running {args[0]}"
10971096
logger.debug(msg)
10981097
results = {"returncode": p.returncode, "msg": msg, "stdout": out}
10991098

11001099
else:
1101-
logger.error("CMake script failure: %s" % (args[0]))
1100+
logger.error(f"CMake script failure: {args[0]}")
11021101
results = {"returncode": p.returncode, "returnmsg": out}
11031102

11041103
return results

0 commit comments

Comments
 (0)