From a9d8e51ec2c82cf126f7c67083ef0f51c77e879d Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Tue, 2 May 2017 11:52:21 +1000 Subject: [PATCH] Move dib-block-device implementation into cmd.py Move the command-line driver components into cmd.py. No functional change. Change-Id: I1919f6e865acae14ee95cd025c9c7b75ca266a9c --- diskimage_builder/block_device/cmd.py | 70 +++++++++++++++++++ .../tests/functional/test_blockdevice.py | 4 +- setup.cfg | 2 +- 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 diskimage_builder/block_device/cmd.py diff --git a/diskimage_builder/block_device/cmd.py b/diskimage_builder/block_device/cmd.py new file mode 100644 index 00000000..0d5bb352 --- /dev/null +++ b/diskimage_builder/block_device/cmd.py @@ -0,0 +1,70 @@ +# Copyright 2016-2017 Andreas Florath (andreas@florath.net) +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import argparse +import logging + +from diskimage_builder.block_device.blockdevice import BlockDevice +from diskimage_builder import logging_config + + +def generate_phase_doc(): + phase_doc = "" + bdattrs = dir(BlockDevice) + for attr in bdattrs: + if attr.startswith("cmd_"): + phase_doc += " '" + attr[4:] + "'\n" + method = getattr(BlockDevice, attr, None) + # The first line is the line that is used + phase_doc += " " + method.__doc__.split("\n")[0] + "\n" + return phase_doc + + +def main(): + logging_config.setup() + logger = logging.getLogger(__name__) + phase_doc = generate_phase_doc() + + parser = argparse.ArgumentParser( + formatter_class=argparse.RawDescriptionHelpFormatter, + description="Create block device layer", + epilog="Available phases:\n" + phase_doc) + parser.add_argument('--phase', required=True, + help="phase to execute") + parser.add_argument('--params', required=True, + help="parameters for block device handling") + parser.add_argument('--symbol', required=False, + help="symbol to query for getval") + args = parser.parse_args() + + logger.info("phase [%s]" % args.phase) + logger.info("params [%s]" % args.params) + if args.symbol: + logger.info("symbol [%s]" % args.symbol) + + bd = BlockDevice(args) + + # Check if the method is available + method = getattr(bd, "cmd_" + args.phase, None) + if callable(method): + # If so: call it. + return method() + else: + logger.error("phase [%s] does not exists" % args.phase) + return 1 + + return 0 + +if __name__ == "__main__": + main() diff --git a/diskimage_builder/tests/functional/test_blockdevice.py b/diskimage_builder/tests/functional/test_blockdevice.py index ed89f126..519a22bc 100644 --- a/diskimage_builder/tests/functional/test_blockdevice.py +++ b/diskimage_builder/tests/functional/test_blockdevice.py @@ -15,7 +15,7 @@ import logging import subprocess import testtools -from diskimage_builder import block_device +from diskimage_builder.block_device.blockdevice import BlockDevice from diskimage_builder.block_device.level0 import localloop from diskimage_builder.logging_config import setup @@ -24,7 +24,7 @@ from diskimage_builder.logging_config import setup setup() -class StateSavingBlockDevice(block_device.BlockDevice): +class StateSavingBlockDevice(BlockDevice): def cmd_create(self): logging.info("StateSavingBlockDevice cmd_create()") super(StateSavingBlockDevice, self).cmd_create() diff --git a/setup.cfg b/setup.cfg index 2878a1f9..f9daac21 100644 --- a/setup.cfg +++ b/setup.cfg @@ -52,7 +52,7 @@ universal = 1 [entry_points] console_scripts = element-info = diskimage_builder.element_dependencies:main - dib-block-device = diskimage_builder.block_device:main + dib-block-device = diskimage_builder.block_device.cmd:main disk-image-create = diskimage_builder.disk_image_create:main ramdisk-image-create = diskimage_builder.disk_image_create:main