Skip to content

Commit 2ec608c

Browse files
committed
ci: Safely fix python files
Uses ruff with --fix to fix python files in this folder Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
1 parent 15d43f2 commit 2ec608c

File tree

6 files changed

+33
-25
lines changed

6 files changed

+33
-25
lines changed

ci/compare_versions.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from packaging.version import parse, InvalidVersion
1615
import argparse
1716
import sys
1817

18+
from packaging.version import InvalidVersion, parse
19+
1920
try:
2021
from packaging.version import LegacyVersion
2122
except ImportError:
@@ -45,21 +46,21 @@
4546
try:
4647
versions.append(parse(version))
4748
except InvalidVersion:
48-
print("Invalid version parsed: {}".format(version))
49+
print(f"Invalid version parsed: {version}")
4950
sys.exit(1)
5051

5152
old, new = versions[0], versions[1]
5253
for version in [old, new]:
5354
if isinstance(version, LegacyVersion):
54-
print("Invalid version parsed: {}".format(version))
55+
print(f"Invalid version parsed: {version}")
5556
sys.exit(1)
5657

5758
if new == old:
5859
print("No version change")
5960
sys.exit(0)
6061
elif new > old:
61-
print("Upgrade detected ({} > {})".format(new, old))
62+
print(f"Upgrade detected ({new} > {old})")
6263
sys.exit(2)
6364

64-
print("Downgrade detected ({} < {})".format(new, old))
65+
print(f"Downgrade detected ({new} < {old})")
6566
sys.exit(3)

ci/fih_test_docker/damage_image.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616
import logging
1717
import struct
1818
import sys
19-
20-
from imgtool.image import (IMAGE_HEADER_SIZE, IMAGE_MAGIC,
21-
TLV_INFO_MAGIC, TLV_PROT_INFO_MAGIC, TLV_VALUES)
2219
from shutil import copyfile
2320

21+
from imgtool.image import (
22+
IMAGE_HEADER_SIZE,
23+
IMAGE_MAGIC,
24+
TLV_INFO_MAGIC,
25+
TLV_PROT_INFO_MAGIC,
26+
TLV_VALUES,
27+
)
28+
2429

2530
def get_tlv_type_string(tlv_type):
2631
tlvs = {v: f"IMAGE_TLV_{k}" for k, v in TLV_VALUES.items()}
27-
return tlvs.get(tlv_type, "UNKNOWN({:d})".format(tlv_type))
32+
return tlvs.get(tlv_type, f"UNKNOWN({tlv_type:d})")
2833

2934

3035
class ImageHeader:
@@ -53,7 +58,7 @@ def read_from_binary(in_file):
5358

5459
def __repr__(self):
5560
return "\n".join([
56-
" ih_magic = 0x{:X}".format(self.ih_magic),
61+
f" ih_magic = 0x{self.ih_magic:X}",
5762
" ih_load_addr = " + str(self.ih_load_addr),
5863
" ih_hdr_size = " + str(self.ih_hdr_size),
5964
" ih_protect_tlv_size = " + str(self.ih_protect_tlv_size),
@@ -82,7 +87,7 @@ def read_from_binary(in_file):
8287

8388
def __repr__(self):
8489
return "\n".join([
85-
" it_magic = 0x{:X}".format(self.it_magic),
90+
f" it_magic = 0x{self.it_magic:X}",
8691
" it_tlv_tot = " + str(self.it_tlv_tot)])
8792

8893
def __len__(self):
@@ -99,7 +104,7 @@ def __init__(self):
99104
def read_from_binary(in_file):
100105
tlv = ImageTLV()
101106
(tlv.it_type, _, tlv.it_len) = struct.unpack('<BBH', in_file.read(4))
102-
(tlv.it_value) = struct.unpack('<{:d}s'.format(tlv.it_len), in_file.read(tlv.it_len))
107+
(tlv.it_value) = struct.unpack(f'<{tlv.it_len:d}s', in_file.read(tlv.it_len))
103108
return tlv
104109

105110
def __len__(self):
@@ -126,7 +131,7 @@ def get_arguments():
126131

127132
def damage_tlv(image_offset, tlv_off, tlv, out_file_content):
128133
damage_offset = image_offset + tlv_off + 4
129-
logging.info(" Damaging TLV at offset 0x{:X}...".format(damage_offset))
134+
logging.info(f" Damaging TLV at offset 0x{damage_offset:X}...")
130135
value = bytearray(tlv.it_value[0])
131136
value[0] = (value[0] + 1) % 256
132137
out_file_content[damage_offset] = value[0]
@@ -142,17 +147,17 @@ def damage_image(args, in_file, out_file_content, image_offset):
142147
# Find the Image header
143148
image_header = ImageHeader.read_from_binary(in_file)
144149
if image_header.ih_magic != IMAGE_MAGIC:
145-
raise Exception("Invalid magic in image_header: 0x{:X} instead of 0x{:X}".format(image_header.ih_magic, IMAGE_MAGIC))
150+
raise Exception(f"Invalid magic in image_header: 0x{image_header.ih_magic:X} instead of 0x{IMAGE_MAGIC:X}")
146151

147152
# Find the TLV header
148153
tlv_info_offset = image_header.ih_hdr_size + image_header.ih_img_size
149154
in_file.seek(image_offset + tlv_info_offset, 0)
150155

151156
tlv_info = ImageTLVInfo.read_from_binary(in_file)
152157
if tlv_info.it_magic == TLV_PROT_INFO_MAGIC:
153-
logging.debug("Protected TLV found at offset 0x{:X}".format(tlv_info_offset))
158+
logging.debug(f"Protected TLV found at offset 0x{tlv_info_offset:X}")
154159
if image_header.ih_protect_tlv_size != tlv_info.it_tlv_tot:
155-
raise Exception("Invalid prot TLV len ({:d} vs. {:d})".format(image_header.ih_protect_tlv_size, tlv_info.it_tlv_tot))
160+
raise Exception(f"Invalid prot TLV len ({image_header.ih_protect_tlv_size:d} vs. {tlv_info.it_tlv_tot:d})")
156161

157162
# seek to unprotected TLV
158163
tlv_info_offset += tlv_info.it_tlv_tot
@@ -163,9 +168,9 @@ def damage_image(args, in_file, out_file_content, image_offset):
163168
if image_header.ih_protect_tlv_size != 0:
164169
raise Exception("No prot TLV was found.")
165170

166-
logging.debug("Unprotected TLV found at offset 0x{:X}".format(tlv_info_offset))
171+
logging.debug(f"Unprotected TLV found at offset 0x{tlv_info_offset:X}")
167172
if tlv_info.it_magic != TLV_INFO_MAGIC:
168-
raise Exception("Invalid magic in tlv info: 0x{:X} instead of 0x{:X}".format(tlv_info.it_magic, TLV_INFO_MAGIC))
173+
raise Exception(f"Invalid magic in tlv info: 0x{tlv_info.it_magic:X} instead of 0x{TLV_INFO_MAGIC:X}")
169174

170175
tlv_off = tlv_info_offset + len(ImageTLVInfo())
171176
tlv_end = tlv_info_offset + tlv_info.it_tlv_tot
@@ -175,11 +180,9 @@ def damage_image(args, in_file, out_file_content, image_offset):
175180
in_file.seek(image_offset + tlv_off, 0)
176181
tlv = ImageTLV.read_from_binary(in_file)
177182

178-
logging.debug(" tlv {:24s} len = {:4d}, len = {:4d}".format(get_tlv_type_string(tlv.it_type), tlv.it_len, len(tlv)))
183+
logging.debug(f" tlv {get_tlv_type_string(tlv.it_type):24s} len = {tlv.it_len:4d}, len = {len(tlv):4d}")
179184

180-
if is_valid_signature(tlv) and args.signature:
181-
damage_tlv(image_offset, tlv_off, tlv, out_file_content)
182-
elif tlv.it_type == TLV_VALUES['SHA256'] and args.image_hash:
185+
if is_valid_signature(tlv) and args.signature or tlv.it_type == TLV_VALUES['SHA256'] and args.image_hash:
183186
damage_tlv(image_offset, tlv_off, tlv, out_file_content)
184187

185188
tlv_off += len(tlv)

ci/fih_test_docker/generate_test_report.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import argparse
16+
1617
from utils import CATEGORIES, parse_yaml_file
1718

1819

@@ -27,11 +28,11 @@ def print_results(results):
2728
print(" {:s}: ({:d}):".format(CATEGORIES['SKIPPED'], test_with_skip))
2829
print(" {:s} ({:d}):".format(CATEGORIES['NO_BOOT'], test_with_skip - test_stats[CATEGORIES['BOOT']]))
2930
for last_line in failed_boot_last_lines.keys():
30-
print(" last line: {:s} ({:d})".format(last_line, failed_boot_last_lines[last_line]))
31+
print(f" last line: {last_line:s} ({failed_boot_last_lines[last_line]:d})")
3132
print(" {:s} ({:d})".format(CATEGORIES['BOOT'], test_stats[CATEGORIES['BOOT']]))
3233
print("{:s} ({:d}):".format(CATEGORIES['FAILED'], test_stats[CATEGORIES['TOTAL']] - test_stats[CATEGORIES['SUCCESS']]))
3334
for reason in exec_fail_reasons.keys():
34-
print(" {:s} ({:d})".format(reason, exec_fail_reasons[reason]))
35+
print(f" {reason:s} ({exec_fail_reasons[reason]:d})")
3536

3637

3738
def main():

ci/fih_test_docker/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import collections
16+
1617
import yaml
1718

1819
CATEGORIES = {

ci/fih_test_docker/validate_output.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import argparse
16+
1617
from utils import CATEGORIES, parse_yaml_file
1718

1819

ci/get_features.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import argparse
1818
import os.path
19+
1920
try:
2021
import tomllib
2122
except ModuleNotFoundError:
@@ -32,7 +33,7 @@
3233
try:
3334
cargo_toml = open(args.infile).read()
3435
except Exception:
35-
print("Error reading \"{}\"".format(args.infile))
36+
print(f"Error reading \"{args.infile}\"")
3637
exit(1)
3738

3839
config = tomllib.loads(cargo_toml)

0 commit comments

Comments
 (0)