fix warnings generated by pylint

This commit is contained in:
Martin Sehnoutka 2019-07-17 12:57:35 +02:00 committed by Lars Karlitski
parent 082bb267df
commit f04cb3836f
6 changed files with 34 additions and 32 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)})")

View file

@ -3,7 +3,6 @@
import json
import os
import sys
import tempfile
def main(tree, options):
root_fs_uuid = options["root_fs_uuid"]

View file

@ -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__':