From 354ae9b576798ae0cbbf6898269a132140b5d3c5 Mon Sep 17 00:00:00 2001 From: Till Maas Date: Jun 05 2019 06:16:47 +0000 Subject: [PATCH 1/2] qcow2: Simplify tempfile cleanup Use the name attribute of the named temporary file to access it. It already contains the full path. Joining the user-specified path before it fails if it is None (the default) or would check the wrong path, it is specified. --- diff --git a/inventory/standard-inventory-qcow2 b/inventory/standard-inventory-qcow2 index 966c6dc..8b433da 100755 --- a/inventory/standard-inventory-qcow2 +++ b/inventory/standard-inventory-qcow2 @@ -107,7 +107,7 @@ class AdditionalDrives(object): path = str(path) if path is not None else None drive_file = tempfile.NamedTemporaryFile(dir=path) drive_file.truncate(size) - cls._tempfiles.append({'file': drive_file, 'path': path}) + cls._tempfiles.append(drive_file) logger.info("Created temporary sparse file '%s'." % drive_file.name) # translate data into qemu command options result += ["-drive", "file=%s,media=disk,if=virtio" % drive_file.name] @@ -118,11 +118,11 @@ class AdditionalDrives(object): def cleanup(cls): """Close all temporary files created by this class """ - for tempfile in cls._tempfiles: - fullname = os.path.join(tempfile['path'], tempfile['file'].name) + for drive_file in cls._tempfiles: + fullname = drive_file.name logger.info("Closing and removing temporary sparse file '%s'" % fullname) if os.path.isfile(fullname): - tempfile['file'].close() + drive_file.close() def print_bad_inventory(): From 2acb3cce637a4f87b602fb6de33f70739b5c3b61 Mon Sep 17 00:00:00 2001 From: Till Maas Date: Jun 05 2019 06:17:26 +0000 Subject: [PATCH 2/2] qcow2: Do not use percent templating for logging The logging methods already do percent templating if the message this logged. This avoids unnecessary templating in case the loglevel would suppress the message. --- diff --git a/inventory/standard-inventory-qcow2 b/inventory/standard-inventory-qcow2 index 8b433da..f2b1a1d 100755 --- a/inventory/standard-inventory-qcow2 +++ b/inventory/standard-inventory-qcow2 @@ -108,7 +108,7 @@ class AdditionalDrives(object): drive_file = tempfile.NamedTemporaryFile(dir=path) drive_file.truncate(size) cls._tempfiles.append(drive_file) - logger.info("Created temporary sparse file '%s'." % drive_file.name) + logger.info("Created temporary sparse file '%s'.", drive_file.name) # translate data into qemu command options result += ["-drive", "file=%s,media=disk,if=virtio" % drive_file.name] atexit.register(cls.cleanup) @@ -120,7 +120,7 @@ class AdditionalDrives(object): """ for drive_file in cls._tempfiles: fullname = drive_file.name - logger.info("Closing and removing temporary sparse file '%s'" % fullname) + logger.info("Closing and removing temporary sparse file '%s'", fullname) if os.path.isfile(fullname): drive_file.close()