diff --git a/assemblers/io.weldr.qcow2 b/assemblers/io.weldr.qcow2 index 74f6d689..60db3dad 100755 --- a/assemblers/io.weldr.qcow2 +++ b/assemblers/io.weldr.qcow2 @@ -4,7 +4,6 @@ import contextlib import json import math import os -import shutil import subprocess import sys import tempfile @@ -32,11 +31,12 @@ def mount_api(dest): mount("/proc", f"{dest}/proc", "-o", "rbind"), \ mount("/sys", f"{dest}/sys", "-o", "rbind"), \ mount("none", f"{dest}/run", "-t", "tmpfs"): - yield + yield @contextlib.contextmanager def loop_device(image, size, offset=0): - r = subprocess.run(["losetup", "--partscan", "--show", "--find", "--sizelimit", str(size), "--offset", str(offset), image], stdout=subprocess.PIPE, encoding="utf-8", check=True) + r = subprocess.run(["losetup", "--partscan", "--show", "--find", "--sizelimit", str(size), "--offset", str(offset), + image], stdout=subprocess.PIPE, encoding="utf-8", check=True) loop = r.stdout.strip() try: yield loop @@ -45,7 +45,7 @@ def loop_device(image, size, offset=0): def main(tree, output_dir, options): filename = options["filename"] - partition_table_id = options["partition_table_id"] + # partition_table_id = options["partition_table_id"] root_fs_uuid = options["root_fs_uuid"] # Create a working directory on a tmpfs, maybe we should implicitly @@ -69,18 +69,20 @@ def main(tree, output_dir, options): # Populate the first partition of the image with an ext4 fs and fill it with the contents of the # tree we are operating on. - subprocess.run(["mkfs.ext4", "-d", tree, "-U", root_fs_uuid, "-E", f"offset={partition_offset}", image, f"{int(partition_size / 1024)}k"], input="y", encoding='utf-8', check=True) + subprocess.run(["mkfs.ext4", "-d", tree, "-U", root_fs_uuid, "-E", f"offset={partition_offset}", image, + f"{int(partition_size / 1024)}k"], input="y", encoding='utf-8', check=True) # Mount the created image as a loopback device with loop_device(image, size) as loop, \ - mount(f"{loop}p1", mountpoint): + mount(f"{loop}p1", mountpoint): # Install grub2 into the boot sector of the image, and copy the grub2 imagise into /boot/grub2 with mount_api(mountpoint): - subprocess.run(["chroot", mountpoint, "grub2-install", "--no-floppy", "--target", "i386-pc", loop], check=True) + subprocess.run(["chroot", mountpoint, "grub2-install", "--no-floppy", "--target", "i386-pc", loop], + check=True) - subprocess.run(["qemu-img", "convert", "-O" "qcow2", "-c", image, f"{output_dir}/{filename}"], check=True) + subprocess.run(["qemu-img", "convert", "-O", "qcow2", "-c", image, f"{output_dir}/{filename}"], check=True) if __name__ == '__main__': args = json.load(sys.stdin) - r = main(args["tree"], args["output_dir"], args["options"]) - sys.exit(r) + ret = main(args["tree"], args["output_dir"], args["options"]) + sys.exit(ret) diff --git a/osbuild.py b/osbuild.py index 3a2d3f94..4603d90f 100644 --- a/osbuild.py +++ b/osbuild.py @@ -30,6 +30,7 @@ if not os.path.exists(f"{libdir}/stages"): class StageFailed(Exception): def __init__(self, name, returncode, output): + super(StageFailed, self).__init__() self.name = name self.returncode = returncode self.output = output @@ -37,12 +38,13 @@ class StageFailed(Exception): class AssemblerFailed(Exception): def __init__(self, name, returncode, output): + super(AssemblerFailed, self).__init__() self.name = name self.returncode = returncode self.output = output -class tmpfs: +class TmpFs: def __init__(self, path="/run/osbuild"): self.root = tempfile.mkdtemp(prefix="osbuild-tmpfs-", dir=path) self.mounted = False @@ -96,7 +98,7 @@ class BuildRoot: os.rmdir(self.root) self.root = None - def run(self, argv, binds=[], readonly_binds=[], **kwargs): + def run(self, argv, binds=None, readonly_binds=None, **kwargs): """Runs a command in the buildroot. Its arguments mean the same as those for subprocess.run(). @@ -114,12 +116,12 @@ class BuildRoot: f"--machine={self.machine_name}", f"--directory={self.root}", f"--bind={libdir}/osbuild-run:/run/osbuild/osbuild-run", - *[f"--bind={b}" for b in binds], - *[f"--bind-ro={b}" for b in [argv[0] + ":" + command, *readonly_binds]], + *[f"--bind={b}" for b in (binds or [])], + *[f"--bind-ro={b}" for b in [argv[0] + ":" + command, + *(readonly_binds or [])]], "/run/osbuild/osbuild-run", command - ] + argv[1:], **kwargs - ) + ] + argv[1:], **kwargs) def __del__(self): self.unmount() @@ -239,13 +241,13 @@ class Pipeline: self.stages = [] self.assembler = None - def add_stage(self, name, options={}, resources=[]): + def add_stage(self, name, options=None, resources=None): base = self.stages[-1].id if self.stages else self.base - stage = Stage(name, base, options, resources) + stage = Stage(name, base, options or {}, resources or []) self.stages.append(stage) - def set_assembler(self, name, options={}, resources=[]): - self.assembler = Assembler(name, options, resources) + def set_assembler(self, name, options=None, resources=None): + self.assembler = Assembler(name, options or {}, resources or []) def run(self, output_dir, objects=None, interactive=False, check=True): os.makedirs("/run/osbuild", exist_ok=True) @@ -257,7 +259,7 @@ class Pipeline: results = { "stages": [] } - with tmpfs() as tree: + with TmpFs() as tree: if self.base: subprocess.run(["cp", "-a", f"{objects}/{self.base}/.", tree], check=True) diff --git a/stages/io.weldr.ansible b/stages/io.weldr.ansible index 3db4dea0..f64b9059 100755 --- a/stages/io.weldr.ansible +++ b/stages/io.weldr.ansible @@ -1,7 +1,6 @@ #!/usr/bin/python3 import json -import os import subprocess import sys @@ -10,7 +9,8 @@ def main(tree, options): playbook = options["playbook"] with open("/tmp/inventory", "w") as f: - f.write(f"osbuild-tree ansible_connection=chroot ansible_host={tree} ansible_python_interpreter=/usr/bin/python3") + f.write(f"osbuild-tree ansible_connection=chroot ansible_host={tree} " + f"ansible_python_interpreter=/usr/bin/python3") with open("/tmp/playbook.yml", "w") as f: if isinstance(playbook, str): @@ -29,5 +29,5 @@ def main(tree, options): if __name__ == '__main__': args = json.load(sys.stdin) - r = main(args["tree"], args["options"]) - sys.exit(r) + ret = main(args["tree"], args["options"]) + sys.exit(ret) diff --git a/stages/io.weldr.dnf b/stages/io.weldr.dnf index 9931fe78..da305934 100755 --- a/stages/io.weldr.dnf +++ b/stages/io.weldr.dnf @@ -1,7 +1,6 @@ #!/usr/bin/python3 import json -import os import subprocess import sys @@ -17,13 +16,13 @@ def main(tree, options): for repoid, repo in repos.items(): conf.write(f"[{repoid}]\n") for key, value in repo.items(): - if type(value) == str: + if isinstance(value, str): s = value - elif type(value) == list: + elif isinstance(value, list): s = " ".join(value) - elif type(value) == bool: + elif isinstance(value, bool): s = "1" if value else "0" - elif type(value) == int: + elif isinstance(value, int): s = str(value) else: print(f"unkown type for `{key}`: {value} ({type(value)})") diff --git a/stages/io.weldr.grub2 b/stages/io.weldr.grub2 index 9f0f2a6a..8fcfeb9c 100755 --- a/stages/io.weldr.grub2 +++ b/stages/io.weldr.grub2 @@ -3,7 +3,6 @@ import json import os import sys -import tempfile def main(tree, options): root_fs_uuid = options["root_fs_uuid"] diff --git a/stages/io.weldr.noop b/stages/io.weldr.noop index fa6579f2..7e183966 100755 --- a/stages/io.weldr.noop +++ b/stages/io.weldr.noop @@ -3,7 +3,7 @@ import json import sys -def main(tree, options): +def main(_tree, options): print("Not doing anything with these options:", json.dumps(options)) if __name__ == '__main__':