From a982ed72c84d51923dcd903c8d399f8cbb1083ad Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 7 Dec 2019 15:03:00 +0200 Subject: Add: vmdb.NotString error class --- vmdb/__init__.py | 1 + vmdb/plugins/mkfs_plugin.py | 15 +++------------ vmdb/plugins/mkimg_plugin.py | 9 +++++++++ vmdb/step_list.py | 21 ++++++++++++++++----- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/vmdb/__init__.py b/vmdb/__init__.py index 91aa015..63cf1b7 100644 --- a/vmdb/__init__.py +++ b/vmdb/__init__.py @@ -22,6 +22,7 @@ from .step_list import ( StepRunnerList, StepRunnerInterface, NoMatchingRunner, + NotString, StepError, ) from .runcmd import ( diff --git a/vmdb/plugins/mkfs_plugin.py b/vmdb/plugins/mkfs_plugin.py index 500699c..406ce51 100644 --- a/vmdb/plugins/mkfs_plugin.py +++ b/vmdb/plugins/mkfs_plugin.py @@ -18,18 +18,10 @@ import cliapp -import logging import vmdb -class NotString(vmdb.StepError): - - def __init__(self, name, actual): - msg = '%s: value must be string, got %r' % (name, actual) - super().__init__(msg) - - class MkfsPlugin(cliapp.Plugin): def enable(self): @@ -46,13 +38,12 @@ class MkfsStepRunner(vmdb.StepRunnerInterface): tag = step['partition'] device = state.tags.get_dev(tag) - logging.debug('state: %r', state.as_dict()) if not isinstance(fstype, str): - raise NotString('mkfs', fstype) + raise vmdb.NotString('mkfs', fstype) if not isinstance(tag, str): - raise NotString('mkfs: tag', tag) + raise vmdb.NotString('mkfs: tag', tag) if not isinstance(device, str): - raise NotString('mkfs: device (for tag)', device) + raise vmdb.NotString('mkfs: device (for tag)', device) cmd = ['/sbin/mkfs', '-t', fstype] if 'label' in step: diff --git a/vmdb/plugins/mkimg_plugin.py b/vmdb/plugins/mkimg_plugin.py index 44fc57d..f68f7c3 100644 --- a/vmdb/plugins/mkimg_plugin.py +++ b/vmdb/plugins/mkimg_plugin.py @@ -40,4 +40,13 @@ class MkimgStepRunner(vmdb.StepRunnerInterface): def run(self, step, settings, state): filename = step['mkimg'] size = step['size'] + + if not isinstance(filename, str): + raise vmdb.NotString('mkimg', filename) + if not filename: + raise vmdb.IsEmptyString('mkimg', filename) + + if not isinstance(size, str): + raise vmdb.NotString('mkimg: size', size) + vmdb.runcmd(['qemu-img', 'create', '-f', 'raw', filename, size]) diff --git a/vmdb/step_list.py b/vmdb/step_list.py index f69b4bc..20e3ec6 100644 --- a/vmdb/step_list.py +++ b/vmdb/step_list.py @@ -16,6 +16,9 @@ # =*= License: GPL-3+ =*= +import logging + + import cliapp @@ -75,12 +78,20 @@ class StepRunnerList: class StepError(cliapp.AppException): - pass + def __init__(self, msg): + logging.error(msg) + super().__init__(msg) -class NoMatchingRunner(cliapp.AppException): +class NoMatchingRunner(StepError): def __init__(self, keys): - super(NoMatchingRunner, self).__init__( - 'No runner implements step with keys {}'.format( - ', '.join(keys))) + super().__init__( + 'No runner implements step with keys {}'.format(', '.join(keys))) + + +class NotString(StepError): # pragma: no cover + + def __init__(self, name, actual): + msg = '%s: value must be string, got %r' % (name, actual) + super().__init__(msg) -- cgit v1.2.1