Skip to content

Commit 497688b

Browse files
committed
scripts: Non-safely fix python files
Uses ruff with --unsafe-fixes to fix python files in this folder This is a separate commit so it can be reverted if it causes problems Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
1 parent 91212de commit 497688b

File tree

5 files changed

+23
-30
lines changed

5 files changed

+23
-30
lines changed

scripts/assemble.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@
3131

3232
def same_keys(a, b):
3333
"""Determine if the dicts a and b have the same keys in them"""
34-
for ak in a.keys():
34+
for ak in a:
3535
if ak not in b:
3636
return False
37-
for bk in b.keys():
38-
if bk not in a:
39-
return False
40-
return True
37+
return all(bk in a for bk in b)
4138

4239
offset_re = re.compile(r"^#define DT_FLASH_AREA_([0-9A-Z_]+)_OFFSET(_0)?\s+(0x[0-9a-fA-F]+|[0-9]+)$")
4340
size_re = re.compile(r"^#define DT_FLASH_AREA_([0-9A-Z_]+)_SIZE(_0)?\s+(0x[0-9a-fA-F]+|[0-9]+)$")
@@ -130,7 +127,7 @@ def main():
130127
sys.path.insert(0, os.path.join(zephyr_base, "scripts", "dts", "python-devicetree", "src"))
131128
import devicetree.edtlib
132129

133-
board = find_board_name(args.bootdir)
130+
find_board_name(args.bootdir)
134131

135132
edt_pickle = os.path.join(args.bootdir, "zephyr", "edt.pickle")
136133
with open(edt_pickle, 'rb') as f:

scripts/imgtool/dumpinfo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
262262
flag_string = hex(value)
263263
else:
264264
flag_string = ""
265-
for flag in image.IMAGE_F.keys():
265+
for flag in image.IMAGE_F:
266266
if value & image.IMAGE_F[flag]:
267267
if flag_string:
268268
flag_string += ("\n" + (" " * 20))

scripts/imgtool/image.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,10 @@ def create(self, key, public_key_format, enckey, dependencies=None,
598598
self.payload.extend(pad)
599599

600600
compression_flags = 0x0
601-
if compression_tlvs is not None:
602-
if compression_type in ["lzma2", "lzma2armthumb"]:
603-
compression_flags = IMAGE_F['COMPRESSED_LZMA2']
604-
if compression_type == "lzma2armthumb":
605-
compression_flags |= IMAGE_F['COMPRESSED_ARM_THUMB']
601+
if compression_tlvs is not None and compression_type in ["lzma2", "lzma2armthumb"]:
602+
compression_flags = IMAGE_F['COMPRESSED_LZMA2']
603+
if compression_type == "lzma2armthumb":
604+
compression_flags |= IMAGE_F['COMPRESSED_ARM_THUMB']
606605
# This adds the header to the payload as well
607606
if encrypt_keylen == 256:
608607
self.add_header(enckey, protected_tlv_size, compression_flags, 256)

scripts/imgtool/keys/general.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, file, *args, **kwargs):
1717
self.kwargs = kwargs
1818

1919
def __enter__(self):
20-
if isinstance(self.file_in, (str, bytes, os.PathLike)):
20+
if isinstance(self.file_in, str | bytes | os.PathLike):
2121
self.file = open(self.file_in, *self.args, **self.kwargs)
2222
else:
2323
self.file = self.file_in

scripts/imgtool/main.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343

4444
MIN_PYTHON_VERSION = (3, 6)
4545
if sys.version_info < MIN_PYTHON_VERSION:
46-
sys.exit("Python %s.%s or newer is required by imgtool."
47-
% MIN_PYTHON_VERSION)
46+
sys.exit("Python {}.{} or newer is required by imgtool.".format(*MIN_PYTHON_VERSION))
4847

4948
SlottedSemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision',
5049
'build', 'slot'])
@@ -358,9 +357,8 @@ def convert(self, value, param, ctx):
358357
try:
359358
return int(value, 0)
360359
except ValueError:
361-
self.fail('%s is not a valid integer. Please use code literals '
362-
'prefixed with 0b/0B, 0o/0O, or 0x/0X as necessary.'
363-
% value, param, ctx)
360+
self.fail(f'{value} is not a valid integer. Please use code literals '
361+
'prefixed with 0b/0B, 0o/0O, or 0x/0X as necessary.', param, ctx)
364362

365363

366364
@click.argument('outfile')
@@ -501,16 +499,15 @@ def sign(key, public_key_format, align, version, pad_sig, header_size,
501499
img.load(infile)
502500
key = load_key(key) if key else None
503501
enckey = load_key(encrypt) if encrypt else None
504-
if enckey and key:
505-
if ((isinstance(key, keys.ECDSA256P1) and
506-
not isinstance(enckey, keys.ECDSA256P1Public))
507-
or (isinstance(key, keys.ECDSA384P1) and
508-
not isinstance(enckey, keys.ECDSA384P1Public))
509-
or (isinstance(key, keys.RSA) and
510-
not isinstance(enckey, keys.RSAPublic))):
511-
# FIXME
512-
raise click.UsageError("Signing and encryption must use the same "
513-
"type of key")
502+
if enckey and key and ((isinstance(key, keys.ECDSA256P1) and
503+
not isinstance(enckey, keys.ECDSA256P1Public))
504+
or (isinstance(key, keys.ECDSA384P1) and
505+
not isinstance(enckey, keys.ECDSA384P1Public))
506+
or (isinstance(key, keys.RSA) and
507+
not isinstance(enckey, keys.RSAPublic))):
508+
# FIXME
509+
raise click.UsageError("Signing and encryption must use the same "
510+
"type of key")
514511

515512
if pad_sig and hasattr(key, 'pad_sig'):
516513
key.pad_sig = True
@@ -520,10 +517,10 @@ def sign(key, public_key_format, align, version, pad_sig, header_size,
520517
for tlv in custom_tlv:
521518
tag = int(tlv[0], 0)
522519
if tag in custom_tlvs:
523-
raise click.UsageError('Custom TLV %s already exists.' % hex(tag))
520+
raise click.UsageError(f'Custom TLV {hex(tag)} already exists.')
524521
if tag in image.TLV_VALUES.values():
525522
raise click.UsageError(
526-
'Custom TLV %s conflicts with predefined TLV.' % hex(tag))
523+
f'Custom TLV {hex(tag)} conflicts with predefined TLV.')
527524

528525
value = tlv[1]
529526
if value.startswith('0x'):

0 commit comments

Comments
 (0)