Skip to content

Commit 5ea50a4

Browse files
authored
Merge pull request #90 from nathanchance/ruff
Add ruff.toml and clean up warnings
2 parents fec03ef + 66ba09e commit 5ea50a4

4 files changed

Lines changed: 79 additions & 42 deletions

File tree

boot-qemu.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# pylint: disable=invalid-name
33

44
import argparse
5+
import contextlib
56
import os
67
from pathlib import Path
78
import platform
@@ -14,9 +15,23 @@
1415

1516
base_folder = Path(__file__).resolve().parent
1617
supported_architectures = [
17-
"arm", "arm32_v5", "arm32_v6", "arm32_v7", "arm64", "arm64be", "m68k",
18-
"mips", "mipsel", "ppc32", "ppc32_mac", "ppc64", "ppc64le", "riscv",
19-
"s390", "x86", "x86_64"
18+
"arm",
19+
"arm32_v5",
20+
"arm32_v6",
21+
"arm32_v7",
22+
"arm64",
23+
"arm64be",
24+
"m68k",
25+
"mips",
26+
"mipsel",
27+
"ppc32",
28+
"ppc32_mac",
29+
"ppc64",
30+
"ppc64le",
31+
"riscv",
32+
"s390",
33+
"x86",
34+
"x86_64",
2035
]
2136

2237

@@ -59,29 +74,29 @@ def parse_arguments():
5974
"--interactive",
6075
"--shell",
6176
action="store_true",
62-
help= # noqa: E251
63-
"Instead of immediately shutting down the machine upon successful boot, pass 'rdinit=/bin/sh' on the kernel command line to allow interacting with the machine via a shell."
77+
help=
78+
"Instead of immediately shutting down the machine upon successful boot, pass 'rdinit=/bin/sh' on the kernel command line to allow interacting with the machine via a shell.",
6479
)
6580
parser.add_argument(
6681
"-k",
6782
"--kernel-location",
6883
required=True,
6984
type=str,
70-
help= # noqa: E251
71-
"Path to kernel image or kernel build folder to search for image in. Can be an absolute or relative path."
85+
help=
86+
"Path to kernel image or kernel build folder to search for image in. Can be an absolute or relative path.",
7287
)
7388
parser.add_argument(
7489
"--no-kvm",
7590
action="store_true",
76-
help= # noqa: E251
77-
"Do not use KVM for acceleration even when supported (only recommended for debugging)."
91+
help=
92+
"Do not use KVM for acceleration even when supported (only recommended for debugging).",
7893
)
7994
parser.add_argument(
8095
"-s",
8196
"--smp",
8297
type=int,
83-
help= # noqa: E251
84-
"Number of processors for virtual machine. By default, only machines spawned with KVM will use multiple vCPUS."
98+
help=
99+
"Number of processors for virtual machine. By default, only machines spawned with KVM will use multiple vCPUS.",
85100
)
86101
parser.add_argument(
87102
"-t",
@@ -194,7 +209,7 @@ def get_smp_value(args):
194209
# CONFIG_NR_CPUS then get the actual value if possible.
195210
config_nr_cpus = 8
196211
if config_file:
197-
with open(config_file, encoding='utf-8') as file:
212+
with config_file.open(encoding='utf-8') as file:
198213
for line in file:
199214
if "CONFIG_NR_CPUS=" in line:
200215
config_nr_cpus = int(line.split("=", 1)[1])
@@ -380,19 +395,19 @@ def get_efi_args(guest_arch):
380395
Path("edk2/aarch64/QEMU_EFI.fd"), # Arch Linux (current)
381396
Path("edk2-armvirt/aarch64/QEMU_EFI.fd"), # Arch Linux (old)
382397
Path("qemu-efi-aarch64/QEMU_EFI.fd"), # Debian and Ubuntu
383-
None # Terminator
398+
None, # Terminator
384399
],
385400
"x86_64": [
386401
Path("edk2/x64/OVMF_CODE.fd"), # Arch Linux (current), Fedora
387402
Path("edk2-ovmf/x64/OVMF_CODE.fd"), # Arch Linux (old)
388403
Path("OVMF/OVMF_CODE.fd"), # Debian and Ubuntu
389-
None # Terminator
390-
]
404+
None, # Terminator
405+
],
391406
} # yapf: disable
392407

393408
if guest_arch not in efi_img_locations:
394409
utils.yellow(
395-
f"EFI boot requested for unsupported architecture ('{guest_arch}'), ignoring..."
410+
f"EFI boot requested for unsupported architecture ('{guest_arch}'), ignoring...",
396411
)
397412
return []
398413

@@ -427,7 +442,7 @@ def get_efi_args(guest_arch):
427442
efi_vars_locations = [
428443
Path("edk2/x64/OVMF_VARS.fd"), # Arch Linux and Fedora
429444
Path("OVMF/OVMF_VARS.fd"), # Debian and Ubuntu
430-
None # Terminator
445+
None, # Terminator
431446
]
432447
for efi_vars_location in efi_vars_locations:
433448
if efi_vars_location is None:
@@ -446,7 +461,7 @@ def get_efi_args(guest_arch):
446461
"-drive", f"if=pflash,format=raw,file={efi_img_qemu},readonly=on",
447462
"-drive", f"if=pflash,format=raw,file={efi_vars_qemu}",
448463
"-object", "rng-random,filename=/dev/urandom,id=rng0",
449-
"-device", "virtio-rng-pci"
464+
"-device", "virtio-rng-pci",
450465
] # yapf: disable
451466

452467

@@ -619,10 +634,7 @@ def get_qemu_args(cfg):
619634
elif arch == "x86_64":
620635
qemu_args += ["-cpu", "Nehalem"]
621636

622-
if arch == "x86":
623-
qemu = "qemu-system-i386"
624-
else:
625-
qemu = "qemu-system-x86_64"
637+
qemu = "qemu-system-i386" if arch == "x86" else "qemu-system-x86_64"
626638

627639
# Make sure QEMU is available in PATH, otherwise there is little point to
628640
# continuing.
@@ -636,18 +648,15 @@ def get_qemu_args(cfg):
636648

637649
# '-dtb'
638650
if dtb:
639-
# If we are in a boot folder, look for them in the dts folder in it
640-
if "boot" in str(kernel):
641-
dtb_dir = "dts"
651+
# If we are in a boot folder, look for them in the dts folder in it.
642652
# Otherwise, assume there is a dtbs folder in the same folder as the
643653
# kernel image (tuxmake)
644-
else:
645-
dtb_dir = "dtbs"
654+
dtb_dir = "dts" if "boot" in str(kernel) else "dtbs"
646655

647656
dtb = kernel.parent.joinpath(dtb_dir, dtb)
648657
if not dtb.exists():
649658
utils.die(
650-
f"'{dtb.stem}' is required for booting but it could not be found at '{dtb}'"
659+
f"'{dtb.stem}' is required for booting but it could not be found at '{dtb}'",
651660
)
652661

653662
qemu_args += ["-dtb", dtb]
@@ -686,7 +695,7 @@ def get_qemu_args(cfg):
686695
# with subprocess.Popen()
687696
qemu = shutil.which(qemu)
688697

689-
cfg["qemu_cmd"] = [qemu] + qemu_args
698+
cfg["qemu_cmd"] = [qemu, *qemu_args]
690699

691700
return cfg
692701

@@ -777,11 +786,9 @@ def launch_qemu(cfg):
777786
gdb_cmd += [kernel_location.joinpath("vmlinux")]
778787
gdb_cmd += ["-ex", "target remote :1234"]
779788

780-
with subprocess.Popen(gdb_cmd) as gdb_process:
781-
try:
782-
gdb_process.wait()
783-
except KeyboardInterrupt:
784-
pass
789+
with subprocess.Popen(gdb_cmd) as gdb_process, \
790+
contextlib.suppress(KeyboardInterrupt):
791+
gdb_process.wait()
785792

786793
utils.red("Killing QEMU...")
787794
qemu_process.kill()

boot-uml.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ def parse_arguments():
2323
"-i",
2424
"--interactive",
2525
action="store_true",
26-
help= # noqa: E251
27-
"Instead of immediately shutting down upon successful boot, pass 'init=/bin/sh' to the UML executable to allow interacting with UML via a shell."
26+
help=
27+
"Instead of immediately shutting down upon successful boot, pass 'init=/bin/sh' to the UML executable to allow interacting with UML via a shell.",
2828
)
2929
parser.add_argument(
3030
"-k",
3131
"--kernel-location",
3232
required=True,
3333
type=str,
34-
help= # noqa: E251
35-
"Path to UML executable ('linux') or kernel build folder to search for executable in. Can be an absolute or relative path."
34+
help=
35+
"Path to UML executable ('linux') or kernel build folder to search for executable in. Can be an absolute or relative path.",
3636
)
3737

3838
return parser.parse_args()

ruff.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
select = [
2+
'A', # flake8-builtins
3+
'ARG', # flake8-unused-arguments
4+
'B', # flake8-bugbear
5+
'C4', # flake8-comprehensions
6+
'COM', # flake8-commas
7+
'E', # pycodestyle
8+
'F', # pyflakes
9+
'PIE', # flake8-pie
10+
'PL', # pylint
11+
'PTH', # flake8-use-pathlib
12+
'RET', # flake8-return
13+
'RUF', # ruff
14+
'S', # flake8-bandit
15+
'SIM', # flake8-simplify
16+
'SLF', # flake8-self
17+
'UP', # pyupgrade
18+
'W', # pycodestyle
19+
]
20+
ignore = [
21+
'E501', # line-too-long
22+
'PLR0911', # too-many-return-statments
23+
'PLR0912', # too-many-branches
24+
'PLR0913', # too-many-arguments
25+
'PLR0915', # too-many-statements
26+
'PLR2004', # magic-value-comparison
27+
]
28+
target-version = 'py38'

utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ def check_cmd(cmd):
1414
cmd (str): External command name or path.
1515
"""
1616
if not shutil.which(cmd):
17-
die(f"The external command '{cmd}' is needed but it could not be found in PATH, please install it!"
18-
)
17+
die(
18+
f"The external command '{cmd}' is needed but it could not be found in PATH, please install it!",
19+
)
1920

2021

2122
def die(string):
@@ -57,8 +58,9 @@ def get_full_kernel_path(kernel_location, image, arch=None):
5758
# Otherwise, it is in the architecture's boot directory
5859
else:
5960
if not arch:
60-
die(f"Kernel image ('{image}') is in the arch/ directory but 'arch' was not provided!"
61-
)
61+
die(
62+
f"Kernel image ('{image}') is in the arch/ directory but 'arch' was not provided!",
63+
)
6264
kernel = kernel_location.joinpath("arch", arch, "boot", image)
6365

6466
if not kernel.exists():

0 commit comments

Comments
 (0)