Skip to content

Commit 3edfe3a

Browse files
authored
Merge pull request #80 from nathanchance/linting-fixes
boot-utils: Fixes for flake8 and pylint
2 parents 9ef7866 + 3da9dd2 commit 3edfe3a

4 files changed

Lines changed: 54 additions & 45 deletions

File tree

.github/workflows/main.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
# Run shellcheck and shfmt on all shell files and yapf on all Python files in this repository
1+
# Run shellcheck and shfmt on all shell files and several Python linters on all Python files in this repository
22
name: Lint checks
33
on: [push, pull_request]
44
jobs:
5+
python:
6+
strategy:
7+
fail-fast: false
8+
matrix:
9+
version: ['3.11', '3.10', '3.9', '3.8']
10+
uses: ClangBuiltLinux/actions-workflows/.github/workflows/python_lint.yml@main
11+
with:
12+
python_version: ${{ matrix.version }}
513
shellcheck:
614
uses: ClangBuiltLinux/actions-workflows/.github/workflows/shellcheck.yml@main
715
shfmt:
816
uses: ClangBuiltLinux/actions-workflows/.github/workflows/shfmt.yml@main
9-
yapf:
10-
uses: ClangBuiltLinux/actions-workflows/.github/workflows/yapf.yml@main

boot-qemu.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
# pylint: disable=invalid-name
23

34
import argparse
45
import os
@@ -7,6 +8,7 @@
78
import re
89
import shutil
910
import subprocess
11+
import sys
1012

1113
import utils
1214

@@ -54,28 +56,28 @@ def parse_arguments():
5456
"--interactive",
5557
"--shell",
5658
action="store_true",
57-
help=
59+
help= # noqa: E251
5860
"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."
5961
)
6062
parser.add_argument(
6163
"-k",
6264
"--kernel-location",
6365
required=True,
6466
type=str,
65-
help=
67+
help= # noqa: E251
6668
"Path to kernel image or kernel build folder to search for image in. Can be an absolute or relative path."
6769
)
6870
parser.add_argument(
6971
"--no-kvm",
7072
action="store_true",
71-
help=
73+
help= # noqa: E251
7274
"Do not use KVM for acceleration even when supported (only recommended for debugging)."
7375
)
7476
parser.add_argument(
7577
"-s",
7678
"--smp",
7779
type=int,
78-
help=
80+
help= # noqa: E251
7981
"Number of processors for virtual machine. By default, only machines spawned with KVM will use multiple vCPUS."
8082
)
8183
parser.add_argument(
@@ -112,24 +114,25 @@ def can_use_kvm(can_test_for_kvm, guest_arch):
112114
if can_test_for_kvm:
113115
# /dev/kvm must exist to use KVM with QEMU
114116
if Path("/dev/kvm").exists():
115-
guest_arch = args.architecture
116117
host_arch = platform.machine()
117118

118119
if host_arch == "aarch64":
119120
# If /dev/kvm exists on aarch64, KVM is supported for aarch64 guests
120121
if "arm64" in guest_arch:
121122
return True
122123
# 32-bit EL1 is not always supported, test for it first
123-
if guest_arch == "arm" or guest_arch == "arm32_v7":
124-
check_32_bit_el1_exec = base_folder.joinpath(
124+
if guest_arch in ("arm", "arm32_v7"):
125+
check_32_bit_el1 = base_folder.joinpath(
125126
"utils", "aarch64_32_bit_el1_supported")
126-
check_32_bit_el1 = subprocess.run([check_32_bit_el1_exec])
127-
return check_32_bit_el1.returncode == 0
127+
try:
128+
subprocess.run([check_32_bit_el1], check=True)
129+
except subprocess.CalledSubprocessError:
130+
return False
131+
return True
128132

129133
if host_arch == "x86_64" and "x86" in guest_arch:
130134
# Check /proc/cpuinfo for whether or not the machine supports hardware virtualization
131-
with open("/proc/cpuinfo") as f:
132-
cpuinfo = f.read()
135+
cpuinfo = Path("/proc/cpuinfo").read_text(encoding='utf-8')
133136
# SVM is AMD, VMX is Intel
134137
return cpuinfo.count("svm") > 0 or cpuinfo.count("vmx") > 0
135138

@@ -186,8 +189,8 @@ def get_smp_value(args):
186189
# CONFIG_NR_CPUS then get the actual value if possible.
187190
config_nr_cpus = 8
188191
if config_file:
189-
with open(config_file) as f:
190-
for line in f:
192+
with open(config_file, encoding='utf-8') as file:
193+
for line in file:
191194
if "CONFIG_NR_CPUS=" in line:
192195
config_nr_cpus = int(line.split("=", 1)[1])
193196
break
@@ -400,7 +403,7 @@ def get_qemu_args(cfg):
400403
qemu = "qemu-system-arm"
401404
qemu_args += ["-machine", "romulus-bmc"]
402405

403-
elif arch == "arm" or arch == "arm32_v7":
406+
elif arch in ("arm", "arm32_v7"):
404407
append += " console=ttyAMA0 earlycon"
405408
kernel_arch = "arm"
406409
qemu_args += ["-machine", "virt"]
@@ -410,7 +413,7 @@ def get_qemu_args(cfg):
410413
else:
411414
qemu = "qemu-system-arm"
412415

413-
elif arch == "arm64" or arch == "arm64be":
416+
elif arch in ("arm64", "arm64be"):
414417
append += " console=ttyAMA0 earlycon"
415418
kernel_arch = "arm64"
416419
kernel_image = "Image.gz"
@@ -452,7 +455,7 @@ def get_qemu_args(cfg):
452455
qemu_args += ["-cpu", "m68040"]
453456
qemu_args += ["-M", "q800"]
454457

455-
elif arch == "mips" or arch == "mipsel":
458+
elif arch in ("mips", "mipsel"):
456459
kernel_arch = "mips"
457460
kernel_image = "vmlinux"
458461
qemu = f"qemu-system-{arch}"
@@ -658,23 +661,22 @@ def launch_qemu(cfg):
658661
utils.check_cmd("lsof")
659662
lsof = subprocess.run(["lsof", "-i:1234"],
660663
stdout=subprocess.DEVNULL,
661-
stderr=subprocess.DEVNULL)
664+
stderr=subprocess.DEVNULL,
665+
check=False)
662666
if lsof.returncode == 0:
663667
utils.die("Port 1234 is already in use, is QEMU running?")
664668

665669
utils.green("Starting QEMU with GDB connection on port 1234...")
666-
qemu_process = subprocess.Popen(qemu_cmd + ["-s", "-S"])
667-
668-
utils.green("Starting GDB...")
669-
utils.check_cmd(gdb_bin)
670-
gdb_cmd = [gdb_bin]
671-
gdb_cmd += [kernel_location.joinpath("vmlinux")]
672-
gdb_cmd += ["-ex", "target remote :1234"]
673-
subprocess.run(gdb_cmd)
670+
with subprocess.Popen(qemu_cmd + ["-s", "-S"]) as qemu_process:
671+
utils.green("Starting GDB...")
672+
utils.check_cmd(gdb_bin)
673+
gdb_cmd = [gdb_bin]
674+
gdb_cmd += [kernel_location.joinpath("vmlinux")]
675+
gdb_cmd += ["-ex", "target remote :1234"]
676+
subprocess.run(gdb_cmd, check=False)
674677

675-
utils.red("Killing QEMU...")
676-
qemu_process.kill()
677-
qemu_process.wait()
678+
utils.red("Killing QEMU...")
679+
qemu_process.kill()
678680

679681
answer = input("Re-run QEMU + gdb? [y/n] ")
680682
if answer.lower() == "n":
@@ -695,14 +697,14 @@ def launch_qemu(cfg):
695697
utils.red("ERROR: QEMU timed out!")
696698
else:
697699
utils.red("ERROR: QEMU did not exit cleanly!")
698-
exit(ex.returncode)
700+
sys.exit(ex.returncode)
699701

700702

701703
if __name__ == '__main__':
702-
args = parse_arguments()
704+
arguments = parse_arguments()
703705

704706
# Build configuration from arguments and QEMU flags
705-
cfg = setup_cfg(args)
706-
cfg = get_qemu_args(cfg)
707+
config = setup_cfg(arguments)
708+
config = get_qemu_args(config)
707709

708-
launch_qemu(cfg)
710+
launch_qemu(config)

boot-uml.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
# pylint: disable=invalid-name
23

34
import argparse
45
from pathlib import Path
@@ -22,15 +23,15 @@ def parse_arguments():
2223
"-i",
2324
"--interactive",
2425
action="store_true",
25-
help=
26+
help= # noqa: E251
2627
"Instead of immediately shutting down upon successful boot, pass 'init=/bin/sh' to the UML executable to allow interacting with UML via a shell."
2728
)
2829
parser.add_argument(
2930
"-k",
3031
"--kernel-location",
3132
required=True,
3233
type=str,
33-
help=
34+
help= # noqa: E251
3435
"Path to UML executable ('linux') or kernel build folder to search for executable in. Can be an absolute or relative path."
3536
)
3637

@@ -57,17 +58,17 @@ def decomp_rootfs():
5758
return rootfs
5859

5960

60-
def run_kernel(kernel, rootfs, interactive):
61+
def run_kernel(kernel_image, rootfs, interactive):
6162
"""
6263
Run UML command with path to rootfs and additional arguments based on user
6364
input.
6465
6566
Parameters:
66-
* kernel (Path): kernel Path object containing full path to kernel.
67+
* kernel_image (Path): kernel Path object containing full path to kernel.
6768
* rootfs (Path): rootfs Path object containing full path to rootfs.
6869
* interactive (bool): Whether or not to run UML interactively.
6970
"""
70-
uml_cmd = [kernel, f"ubd0={rootfs}"]
71+
uml_cmd = [kernel_image, f"ubd0={rootfs}"]
7172
if interactive:
7273
uml_cmd += ["init=/bin/sh"]
7374
print(f"$ {' '.join([str(element) for element in uml_cmd])}")
@@ -77,6 +78,5 @@ def run_kernel(kernel, rootfs, interactive):
7778
if __name__ == '__main__':
7879
args = parse_arguments()
7980
kernel = utils.get_full_kernel_path(args.kernel_location, "linux")
80-
rootfs = decomp_rootfs()
8181

82-
run_kernel(kernel, rootfs, args.interactive)
82+
run_kernel(kernel, decomp_rootfs(), args.interactive)

utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pathlib import Path
44
import shutil
5+
import sys
56

67

78
def check_cmd(cmd):
@@ -26,7 +27,7 @@ def die(string):
2627
automatically.
2728
"""
2829
red(f"ERROR: {string}")
29-
exit(1)
30+
sys.exit(1)
3031

3132

3233
def get_full_kernel_path(kernel_location, image, arch=None):
@@ -51,7 +52,7 @@ def get_full_kernel_path(kernel_location, image, arch=None):
5152
else:
5253
# If the image is an uncompressed vmlinux or a UML image, it is in the
5354
# root of the build folder
54-
if image == "vmlinux" or image == "linux":
55+
if image in ("vmlinux", "linux"):
5556
kernel = kernel_location.joinpath(image)
5657
# Otherwise, it is in the architecture's boot directory
5758
else:

0 commit comments

Comments
 (0)