Skip to content

Commit c735776

Browse files
committed
scripts: build: ruff compliance
Update scripts in the build folder to pass all enabled `ruff` checks. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent 5ac87d2 commit c735776

27 files changed

+400
-623
lines changed

.ruff-excludes.toml

Lines changed: 0 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -119,159 +119,8 @@
119119
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
120120
"SIM112", # https://docs.astral.sh/ruff/rules/uncapitalized-environment-variables
121121
]
122-
"./scripts/build/check_init_priorities.py" = [
123-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
124-
"F401", # https://docs.astral.sh/ruff/rules/unused-import
125-
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
126-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
127-
"UP039", # https://docs.astral.sh/ruff/rules/unnecessary-class-parentheses
128-
]
129-
"./scripts/build/check_init_priorities_test.py" = [
130-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
131-
]
132-
"./scripts/build/elf_parser.py" = [
133-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
134-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
135-
"SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler
136-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
137-
]
138-
"./scripts/build/file2hex.py" = [
139-
"B023", # https://docs.astral.sh/ruff/rules/function-uses-loop-variable
140-
"B905", # https://docs.astral.sh/ruff/rules/zip-without-explicit-strict
141-
]
142122
"./scripts/build/gen_app_partitions.py" = [
143123
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
144-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
145-
"UP030", # https://docs.astral.sh/ruff/rules/format-literals
146-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
147-
]
148-
"./scripts/build/gen_cfb_font_header.py" = [
149-
"E101", # https://docs.astral.sh/ruff/rules/mixed-spaces-and-tabs
150-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
151-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
152-
]
153-
"./scripts/build/gen_device_deps.py" = [
154-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
155-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
156-
]
157-
"./scripts/build/gen_image_info.py" = [
158-
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
159-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
160-
"SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler
161-
]
162-
"./scripts/build/gen_isr_tables.py" = [
163-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
164-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
165-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
166-
]
167-
"./scripts/build/gen_isr_tables_parser_carrays.py" = [
168-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
169-
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
170-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
171-
"UP030", # https://docs.astral.sh/ruff/rules/format-literals
172-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
173-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
174-
]
175-
"./scripts/build/gen_isr_tables_parser_local.py" = [
176-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
177-
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
178-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
179-
"UP030", # https://docs.astral.sh/ruff/rules/format-literals
180-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
181-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
182-
]
183-
"./scripts/build/gen_kobject_list.py" = [
184-
"E101", # https://docs.astral.sh/ruff/rules/mixed-spaces-and-tabs
185-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
186-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
187-
"SIM102", # https://docs.astral.sh/ruff/rules/collapsible-if
188-
"SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler
189-
"SIM401", # https://docs.astral.sh/ruff/rules/if-else-block-instead-of-dict-get
190-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
191-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
192-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
193-
"W191", # https://docs.astral.sh/ruff/rules/tab-indentation
194-
]
195-
"./scripts/build/gen_kobject_placeholders.py" = [
196-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
197-
"SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler
198-
]
199-
"./scripts/build/gen_offset_header.py" = [
200-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
201-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
202-
]
203-
"./scripts/build/gen_relocate_app.py" = [
204-
"E101", # https://docs.astral.sh/ruff/rules/mixed-spaces-and-tabs
205-
]
206-
"./scripts/build/gen_strerror_table.py" = [
207-
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
208-
"SIM105", # https://docs.astral.sh/ruff/rules/suppressible-exception
209-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
210-
]
211-
"./scripts/build/gen_strsignal_table.py" = [
212-
"SIM105", # https://docs.astral.sh/ruff/rules/suppressible-exception
213-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
214-
]
215-
"./scripts/build/gen_symtab.py" = [
216-
"B007", # https://docs.astral.sh/ruff/rules/unused-loop-control-variable
217-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
218-
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
219-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
220-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
221-
]
222-
"./scripts/build/gen_syscalls.py" = [
223-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
224-
"E713", # https://docs.astral.sh/ruff/rules/not-in-test
225-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
226-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
227-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
228-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
229-
]
230-
"./scripts/build/llext_inject_slids.py" = [
231-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
232-
"SIM113", # https://docs.astral.sh/ruff/rules/enumerate-for-loop
233-
"SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler
234-
"UP039", # https://docs.astral.sh/ruff/rules/unnecessary-class-parentheses
235-
]
236-
"./scripts/build/llext_prepare_exptab.py" = [
237-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
238-
"F541", # https://docs.astral.sh/ruff/rules/f-string-missing-placeholders
239-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
240-
"SIM113", # https://docs.astral.sh/ruff/rules/enumerate-for-loop
241-
"SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler
242-
"UP039", # https://docs.astral.sh/ruff/rules/unnecessary-class-parentheses
243-
]
244-
"./scripts/build/mergehex.py" = [
245-
"B904", # https://docs.astral.sh/ruff/rules/raise-without-from-inside-except
246-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
247-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
248-
]
249-
"./scripts/build/parse_syscalls.py" = [
250-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
251-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
252-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
253-
]
254-
"./scripts/build/process_gperf.py" = [
255-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
256-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
257-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
258-
]
259-
"./scripts/build/subfolder_list.py" = [
260-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
261-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
262-
]
263-
"./scripts/build/uf2conv.py" = [
264-
"B011", # https://docs.astral.sh/ruff/rules/assert-false
265-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
266-
"E711", # https://docs.astral.sh/ruff/rules/none-comparison
267-
"E722", # https://docs.astral.sh/ruff/rules/bare-except
268-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
269-
"SIM102", # https://docs.astral.sh/ruff/rules/collapsible-if
270-
"SIM103", # https://docs.astral.sh/ruff/rules/needless-bool
271-
"SIM118", # https://docs.astral.sh/ruff/rules/in-dict-keys
272-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
273-
"UP031", # https://docs.astral.sh/ruff/rules/printf-string-formatting
274-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
275124
]
276125
"./scripts/ci/check_compliance.py" = [
277126
"B904", # https://docs.astral.sh/ruff/rules/raise-without-from-inside-except

scripts/build/check_init_priorities.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
# This is needed to load edt.pickle files.
3232
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "dts", "python-devicetree", "src"))
33-
from devicetree import edtlib # pylint: disable=unused-import
3433

3534
# Prefix used for "struct device" reference initialized based on devicetree
3635
# entries with a known ordinal.
@@ -62,7 +61,7 @@ class Priority:
6261
name: the section name
6362
"""
6463

65-
def __init__(self, level, priority):
64+
def __init__(self, level: str, priority: int):
6665
for idx, level_name in enumerate(_DEVICE_INIT_LEVELS):
6766
if level_name == level:
6867
self._level = idx
@@ -71,17 +70,15 @@ def __init__(self, level, priority):
7170
self._level_priority = (self._level, self._priority)
7271
return
7372

74-
raise ValueError("Unknown level in %s" % level)
73+
raise ValueError(f"Unknown level in {level}")
7574

7675
def __repr__(self):
77-
return "<%s %s %d>" % (
78-
self.__class__.__name__,
79-
_DEVICE_INIT_LEVELS[self._level],
80-
self._priority,
81-
)
76+
level = _DEVICE_INIT_LEVELS[self._level]
77+
return f"<{self.__class__.__name__} {level} {self._priority}>"
8278

8379
def __str__(self):
84-
return "%s+%d" % (_DEVICE_INIT_LEVELS[self._level], self._priority)
80+
level = _DEVICE_INIT_LEVELS[self._level]
81+
return f"{level}+{self._priority}"
8582

8683
def __lt__(self, other):
8784
return self._level_priority < other._level_priority
@@ -156,7 +153,7 @@ def _load_level_addr(self):
156153
raise ValueError(f"Missing init symbols, found: {self._init_level_addr}")
157154

158155
if not self._init_level_end:
159-
raise ValueError(f"Missing init section end symbol")
156+
raise ValueError("Missing init section end symbol")
160157

161158
def _device_ord_from_name(self, sym_name):
162159
"""Find a device ordinal from a symbol name."""

scripts/build/check_init_priorities_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
Tests for check_init_priorities
77
"""
88

9-
from unittest import mock
109
import pathlib
1110
import unittest
11+
from unittest import mock
1212

13+
import check_init_priorities
1314
from elftools.elf.relocation import Section
1415
from elftools.elf.sections import SymbolTableSection
1516

16-
import check_init_priorities
17-
1817

1918
class TestPriority(unittest.TestCase):
2019
"""Tests for the Priority class."""

scripts/build/elf_parser.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
import struct
88
import sys
9-
from packaging import version
109

1110
import elftools
1211
from elftools.elf.elffile import ELFFile
1312
from elftools.elf.sections import SymbolTableSection
13+
from packaging import version
1414

1515
if version.parse(elftools.__version__) < version.parse('0.24'):
1616
sys.exit("pyelftools is out of date, need version 0.24 or later")
@@ -62,7 +62,7 @@ class DeviceOrdinals(_Symbol):
6262
def __init__(self, elf, sym):
6363
super().__init__(elf, sym)
6464
format = "<" if self.elf.little_endian else ">"
65-
format += "{:d}h".format(len(self.data) // 2)
65+
format += f"{len(self.data) // 2:d}h"
6666
self._ordinals = struct.unpack(format, self.data)
6767
self._ordinals_split = []
6868

@@ -126,7 +126,7 @@ class ZephyrElf:
126126
"""
127127

128128
def __init__(self, kernel, edt, device_start_symbol):
129-
self.elf = ELFFile(open(kernel, "rb"))
129+
self.elf = ELFFile(open(kernel, "rb")) # noqa : SIM115
130130
self.relocatable = self.elf['e_type'] == 'ET_REL'
131131
self.edt = edt
132132
self.devices = []
@@ -287,11 +287,11 @@ def device_dependency_graph(self, title, comment):
287287
# Split iteration so nodes and edges are grouped in source
288288
for dev in self.devices:
289289
if dev.ordinal == DeviceOrdinals.DEVICE_HANDLE_NULL:
290-
text = '{:s}\\nHandle: {:d}'.format(dev.sym.name, dev.handle)
290+
text = f'{dev.sym.name:s}\\nHandle: {dev.handle:d}'
291291
else:
292292
n = self.edt.dep_ord2node[dev.ordinal]
293-
text = '{:s}\\nOrdinal: {:d} | Handle: {:d}\\n{:s}'.format(
294-
n.name, dev.ordinal, dev.handle, n.path
293+
text = (
294+
f'{n.name:s}\\nOrdinal: {dev.ordinal:d} | Handle: {dev.handle:d}\\n{n.path:s}'
295295
)
296296
dot.node(str(dev.ordinal), text)
297297
for dev in self.devices:

scripts/build/file2hex.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ def get_nice_string(list_or_iterator):
7474

7575
def make_hex(chunk):
7676
hexdata = codecs.encode(chunk, 'hex').decode("utf-8")
77-
hexlist = map(''.join, zip(*[iter(hexdata)] * 2))
77+
hexlist = map(''.join, zip(*[iter(hexdata)] * 2, strict=False))
7878
print(get_nice_string(hexlist) + ',')
7979

8080

8181
def make_string_literal(chunk):
8282
hexdata = codecs.encode(chunk, 'hex').decode("utf-8")
83-
hexlist = map(''.join, zip(*[iter(hexdata)] * 2))
83+
hexlist = map(''.join, zip(*[iter(hexdata)] * 2, strict=False))
8484
print(''.join("\\x" + str(x) for x in hexlist), end='')
8585

8686

@@ -118,7 +118,7 @@ def main():
118118
else:
119119
print('"', end='')
120120
remainder = args.length
121-
for chunk in iter(lambda: fp.read(min(1024, remainder)), b''):
121+
for chunk in iter(lambda rem=remainder: fp.read(min(1024, rem)), b''):
122122
make_string_literal(chunk)
123123
remainder = remainder - len(chunk)
124124
print('"', end='')
@@ -129,7 +129,7 @@ def main():
129129
make_hex(chunk)
130130
else:
131131
remainder = args.length
132-
for chunk in iter(lambda: fp.read(min(1024, remainder)), b''):
132+
for chunk in iter(lambda rem=remainder: fp.read(min(1024, rem)), b''):
133133
make_hex(chunk)
134134
remainder = remainder - len(chunk)
135135

scripts/build/gen_app_partitions.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,16 @@
168168
# "(@z_data_smem_z_libc_partition_bss_end@ - @z_data_smem_z_libc_partition_bss_start@)"
169169
# )
170170

171-
import sys
172171
import argparse
173172
import json
174173
import os
175174
import re
175+
import sys
176176
from collections import OrderedDict
177+
178+
import elftools.common.exceptions
177179
from elftools.elf.elffile import ELFFile
178180
from elftools.elf.sections import SymbolTableSection
179-
import elftools.common.exceptions
180181

181182
SZ = 'size'
182183
SRC = 'sources'
@@ -518,7 +519,7 @@ def parse_args():
518519
action="append",
519520
default=[],
520521
metavar=("LIBRARY", "PARTITION"),
521-
help="Include globals for a particular library or object filename into a designated partition",
522+
help="Include globals for a library or object filename into a designated partition",
522523
)
523524
parser.add_argument("--pinoutput", required=False, help="Output ld file for pinned sections")
524525
parser.add_argument(
@@ -580,7 +581,7 @@ def main():
580581
if args.verbose:
581582
print("Partitions retrieved:")
582583
for key in partsorted:
583-
print(" {0}: size {1}: {2}".format(key, partsorted[key][SZ], partsorted[key][SRC]))
584+
print(f" {key}: size {partsorted[key][SZ]}: {partsorted[key][SRC]}")
584585

585586
if args.pinoutput:
586587
decreasing_tuples = sorted(
@@ -593,9 +594,7 @@ def main():
593594
if args.verbose:
594595
print("Pinned partitions retrieved:")
595596
for key in partsorted:
596-
print(
597-
" {0}: size {1}: {2}".format(key, partsorted[key][SZ], partsorted[key][SRC])
598-
)
597+
print(f" {key}: size {partsorted[key][SZ]}: {partsorted[key][SRC]}")
599598

600599

601600
if __name__ == '__main__':

0 commit comments

Comments
 (0)