Skip to content

Commit

Permalink
Use yapf for formatting python scripts (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuta authored Dec 15, 2021
1 parent 3fb1b3a commit 0b09fcb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 32 deletions.
33 changes: 22 additions & 11 deletions tools/docker2initramfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,42 @@
def main():
parser = argparse.ArgumentParser(
description="Converts a Docker image into cpio (initramfs format).")
parser.add_argument("outfile",
help="The output path.")
parser.add_argument("outfile", help="The output path.")
parser.add_argument("image",
help="The docker image name (e.g. python:slim).")
args = parser.parse_args()

container_id = f"docker-initramfs-tmp"
try:
subprocess.run(["docker", "rm", container_id],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=False)
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False)
subprocess.run(
["docker", "create", "--name", container_id, "-t", args.image],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True)
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True)
with tempfile.NamedTemporaryFile() as temp_file:
temp_file = Path(temp_file.name)
subprocess.run(["docker", "export", f"--output={temp_file}", container_id],
stderr=subprocess.STDOUT, check=True)
subprocess.run(
["docker", "export", f"--output={temp_file}", container_id],
stderr=subprocess.STDOUT,
check=True)
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir = Path(temp_dir)
subprocess.run(["tar", "xf", temp_file],
cwd=temp_dir, check=True)
cwd=temp_dir,
check=True)

# XXX: This is a hack to get around the fact that the Docker overrides
# the /etc/resolv.conf file.
(temp_dir / "etc" / "resolv.conf").write_text("nameserver 1.1.1.1")
(temp_dir / "etc" /
"resolv.conf").write_text("nameserver 1.1.1.1")

filelist = list(
map(lambda p: "./" + str(p.relative_to(temp_dir)), temp_dir.glob("**/*")))
map(lambda p: "./" + str(p.relative_to(temp_dir)),
temp_dir.glob("**/*")))
subprocess.run(
["cpio", "--create", "--format=newc"],
input="\n".join(filelist).encode("ascii"),
Expand All @@ -46,10 +54,13 @@ def main():
)
except subprocess.CalledProcessError as e:
sys.exit(
f"{e.stdout.decode('utf-8', 'backslashreplace')}\n\nError: failed to export {args.image}")
f"{e.stdout.decode('utf-8', 'backslashreplace')}\n\nError: failed to export {args.image}"
)
finally:
subprocess.run(["docker", "rm", container_id],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=False)
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False)


if __name__ == "__main__":
Expand Down
8 changes: 5 additions & 3 deletions tools/embed-symbol-table.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ def main():
sys.exit("embed-symbol-table.py: failed to locate the symbol table")
if image.find(START_MAKER, offset + 1) >= 0:
print(hex(offset), hex(image.find(START_MAKER, offset + 1)))
sys.exit("embed-symbol-table.py: found multiple empty symbol tables (perhaps because " +
"START_MAKER is not sufficiently long to be unique?)")
sys.exit(
"embed-symbol-table.py: found multiple empty symbol tables (perhaps because "
+ "START_MAKER is not sufficiently long to be unique?)")

# Parse the nm output and extract symbol names and theier addresses.
symbols = {}
Expand Down Expand Up @@ -55,7 +56,8 @@ def main():
max_size = offset_end - offset
if len(symbol_table) > max_size:
sys.exit(
f"embed-symbol-table.py: Too many symbols; please expand the symbol table area (max_size={max_size / 1024}KiB, created={len(symbol_table) / 1024}KiB)")
f"embed-symbol-table.py: Too many symbols; please expand the symbol table area (max_size={max_size / 1024}KiB, created={len(symbol_table) / 1024}KiB)"
)

# Embed the symbol table.
image = image[:offset] + symbol_table + image[offset + len(symbol_table):]
Expand Down
13 changes: 9 additions & 4 deletions tools/inspect-init-in-docker-image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@

def main():
parser = argparse.ArgumentParser(
description="Determines the init argv specified in a Docker image (i.e. CMD).")
parser.add_argument("image",
help="The docker image name (e.g. python:slim or docker.io/library/python:alpine).")
description=
"Determines the init argv specified in a Docker image (i.e. CMD).")
parser.add_argument(
"image",
help=
"The docker image name (e.g. python:slim or docker.io/library/python:alpine)."
)
args = parser.parse_args()

try:
stdout = subprocess.check_output(
["docker", "image", "inspect", args.image])
except subprocess.CalledProcessError as e:
sys.exit(
f"{e.stdout.decode('utf-8', 'backslashreplace')}\n\nError: failed to inspect {args.image}")
f"{e.stdout.decode('utf-8', 'backslashreplace')}\n\nError: failed to inspect {args.image}"
)

data = json.loads(stdout)[0]["Config"]
print(shlex.join(data["Cmd"]))
Expand Down
38 changes: 24 additions & 14 deletions tools/run-qemu.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@
import sys

COMMON_ARGS = [
"-serial", "mon:stdio", "-no-reboot",
"-serial",
"mon:stdio",
"-no-reboot",
]

ARCHS = {
"x64": {
"bin": "qemu-system-x86_64",
"args": COMMON_ARGS + [
"-m", "512",
"-cpu", "Icelake-Server",

"-device", "virtio-net,netdev=net0,disable-legacy=on,disable-modern=off",
"-netdev", "user,id=net0,hostfwd=tcp:127.0.0.1:20022-:22,hostfwd=tcp:127.0.0.1:20080-:80",
"-object", "filter-dump,id=fiter0,netdev=net0,file=virtio-net.pcap",

"-device", "isa-debug-exit,iobase=0x501,iosize=2",
"-d", "guest_errors,unimp",
"x64": {
"bin":
"qemu-system-x86_64",
"args":
COMMON_ARGS + [
"-m",
"1024",
"-cpu",
"Icelake-Server",
"-device",
"virtio-net,netdev=net0,disable-legacy=on,disable-modern=off",
"-netdev",
"user,id=net0,hostfwd=tcp:127.0.0.1:20022-:22,hostfwd=tcp:127.0.0.1:20080-:80",
"-object",
"filter-dump,id=fiter0,netdev=net0,file=virtio-net.pcap",
"-device",
"isa-debug-exit,iobase=0x501,iosize=2",
"-d",
"guest_errors,unimp",
]
}
}
Expand Down Expand Up @@ -85,7 +94,8 @@ def main():
p = subprocess.run(argv, preexec_fn=os.setsid)
if p.returncode != 33:
sys.exit(
f"\nrun-qemu.py: qemu exited with failure status (status={p.returncode})")
f"\nrun-qemu.py: qemu exited with failure status (status={p.returncode})"
)


if __name__ == "__main__":
Expand Down

0 comments on commit 0b09fcb

Please sign in to comment.