Source code for kiwi.cli

# Copyright (c) 2015 SUSE Linux GmbH.  All rights reserved.
# This file is part of kiwi.
# kiwi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# kiwi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with kiwi.  If not, see <>
usage: kiwi-ng -h | --help
       kiwi-ng [--profile=<name>...]
           image <command> [<args>...]
       kiwi-ng [--debug]
           result <command> [<args>...]
       kiwi-ng [--profile=<name>...]
           system <command> [<args>...]
       kiwi-ng compat <legacy_args>...
       kiwi-ng --compat <legacy_args>...
       kiwi-ng -v | --version
       kiwi-ng help

global options:
        use colors for warning and error messages
        print debug information
    -v --version
        show program version
        show manual page

global options for services: image, system
        create a log file containing all log information including
        debug information even if this is was not requested by the
        debug switch
        profile name, multiple profiles can be selected by passing
        this option multiple times
        specify an alternative shared cache directory. The directory
        is shared via bind mount between the build host and image
        root system and contains information about package repositories
        and their cache and meta data. [default: /var/cache/kiwi]
        image build type. If not set the default XML specified
        build type will be used
import sys
import os
import pkg_resources
from docopt import docopt

# project
from kiwi.exceptions import (
from kiwi.path import Path
from kiwi.version import __version__
from import Help

[docs]class Cli: """ **Implements the main command line interface** An instance of the Cli class builds the entry point for the application and implements methods to load further command plugins which itself provides their own command line interface """ def __init__(self): self.all_args = docopt( __doc__, version='KIWI (next generation) version ' + __version__, options_first=True ) self.command_args = self.all_args['<args>'] self.command_loaded = None
[docs] def show_and_exit_on_help_request(self): """ Execute man to show the selected manual page """ if self.all_args['help']: manual = Help()'kiwi') sys.exit(0)
[docs] def get_servicename(self): """ Extract service name from argument parse result :return: service name :rtype: str """ if self.all_args.get('image') is True: return 'image' elif self.all_args.get('system') is True: return 'system' elif self.all_args.get('result') is True: return 'result' elif self.all_args.get('--compat') is True: return 'compat' elif self.all_args.get('compat') is True: return 'compat' else: raise KiwiUnknownServiceName( 'Unknown/Invalid Servicename' )
[docs] def invoke_kiwicompat(self, compat_args): """ Execute kiwicompat with provided legacy KIWI command line arguments Example: .. code:: python invoke_kiwicompat( '--build', 'description', '--type', 'vmx', '-d', 'destination' ) :param list compat_args: legacy kiwi command arguments """ kiwicompat = Path.which('kiwicompat', access_mode=os.X_OK) try: os.execvp(kiwicompat, ['kiwicompat'] + compat_args) except Exception as e: raise KiwiCompatError( '%s: %s' % (type(e).__name__, format(e)) )
[docs] def get_command(self): """ Extract selected command name :return: command name :rtype: str """ return self.all_args['<command>']
[docs] def get_command_args(self): """ Extract argument dict for selected command :return: Contains dictionary of command arguments .. code:: python { '--command-option': 'value' } :rtype: dict """ return self._load_command_args()
[docs] def get_global_args(self): """ Extract argument dict for global arguments :return: Contains dictionary of global arguments .. code:: python { '--global-option': 'value' } :rtype: dict """ result = {} for arg, value in list(self.all_args.items()): if not arg == '<command>' and not arg == '<args>': result[arg] = value return result
[docs] def load_command(self): """ Loads task class plugin according to service and command name :return: loaded task module :rtype: object """ discovered_tasks = { entry_point.load() for entry_point in pkg_resources.iter_entry_points('kiwi.tasks') } service = self.get_servicename() command = self.get_command() if service == 'compat': compat_arguments = self.all_args['<legacy_args>'] if '--' in compat_arguments: compat_arguments.remove('--') return self.invoke_kiwicompat(compat_arguments) if not command: raise KiwiLoadCommandUndefined( 'No command specified for {0} service'.format(service) ) self.command_loaded = discovered_tasks.get( service + '_' + command ) if not self.command_loaded: prefix = 'usage:' discovered_tasks_for_service = '' for task in discovered_tasks: if task.startswith(service): discovered_tasks_for_service += '{0} kiwi-ng {1}\n'.format( prefix, task.replace('_', ' ') ) prefix = ' ' raise KiwiCommandNotLoaded( 'Command "{0}" not found\n\n{1}'.format( command, discovered_tasks_for_service ) ) return self.command_loaded
def _load_command_args(self): try: argv = [ self.get_servicename(), self.get_command() ] + self.command_args return docopt(self.command_loaded.__doc__, argv=argv) except Exception: raise KiwiCommandNotLoaded( '%s command not loaded' % self.get_command() )