Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / one_command.py @ 58602137

History | View | Annotate | Download (4.1 kB)

1 b6a99832 Stavros Sachtouris
# Copyright 2012-2013 GRNET S.A. All rights reserved.
2 b6a99832 Stavros Sachtouris
#
3 b6a99832 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 b6a99832 Stavros Sachtouris
# without modification, are permitted provided that the following
5 b6a99832 Stavros Sachtouris
# conditions are met:
6 b6a99832 Stavros Sachtouris
#
7 b6a99832 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 b6a99832 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 b6a99832 Stavros Sachtouris
#      disclaimer.
10 b6a99832 Stavros Sachtouris
#
11 b6a99832 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 b6a99832 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 b6a99832 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 b6a99832 Stavros Sachtouris
#      provided with the distribution.
15 b6a99832 Stavros Sachtouris
#
16 b6a99832 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 b6a99832 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 b6a99832 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 b6a99832 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 b6a99832 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 b6a99832 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 b6a99832 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 b6a99832 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 b6a99832 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 b6a99832 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 b6a99832 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 b6a99832 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 b6a99832 Stavros Sachtouris
#
29 b6a99832 Stavros Sachtouris
# The views and conclusions contained in the software and
30 b6a99832 Stavros Sachtouris
# documentation are those of the authors and should not be
31 b6a99832 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 b6a99832 Stavros Sachtouris
# or implied, of GRNET S.A.command
33 b6a99832 Stavros Sachtouris
34 db03f805 Stavros Sachtouris
from kamaki.cli import (
35 db03f805 Stavros Sachtouris
    get_command_group, set_command_params, print_subcommands_help, exec_cmd,
36 db03f805 Stavros Sachtouris
    update_parser_help, _groups_help, _load_spec_module,
37 db03f805 Stavros Sachtouris
    init_cached_authenticator, kloger)
38 334338ce Stavros Sachtouris
from kamaki.cli.errors import CLIUnknownCommand
39 b6a99832 Stavros Sachtouris
40 b6a99832 Stavros Sachtouris
41 b6a99832 Stavros Sachtouris
def _get_cmd_tree_from_spec(spec, cmd_tree_list):
42 b6a99832 Stavros Sachtouris
    for tree in cmd_tree_list:
43 b6a99832 Stavros Sachtouris
        if tree.name == spec:
44 b6a99832 Stavros Sachtouris
            return tree
45 334338ce Stavros Sachtouris
    raise CLIUnknownCommand('Unknown command: %s' % spec)
46 b6a99832 Stavros Sachtouris
47 b6a99832 Stavros Sachtouris
48 b6a99832 Stavros Sachtouris
def _get_best_match_from_cmd_tree(cmd_tree, unparsed):
49 b6a99832 Stavros Sachtouris
    matched = [term for term in unparsed if not term.startswith('-')]
50 b6a99832 Stavros Sachtouris
    while matched:
51 b6a99832 Stavros Sachtouris
        try:
52 b6a99832 Stavros Sachtouris
            return cmd_tree.get_command('_'.join(matched))
53 b6a99832 Stavros Sachtouris
        except KeyError:
54 b6a99832 Stavros Sachtouris
            matched = matched[:-1]
55 b6a99832 Stavros Sachtouris
    return None
56 b6a99832 Stavros Sachtouris
57 b6a99832 Stavros Sachtouris
58 db03f805 Stavros Sachtouris
def run(cloud, parser, _help):
59 b6a99832 Stavros Sachtouris
    group = get_command_group(list(parser.unparsed), parser.arguments)
60 b6a99832 Stavros Sachtouris
    if not group:
61 b6a99832 Stavros Sachtouris
        parser.parser.print_help()
62 b6a99832 Stavros Sachtouris
        _groups_help(parser.arguments)
63 b6a99832 Stavros Sachtouris
        exit(0)
64 b6a99832 Stavros Sachtouris
65 b6a99832 Stavros Sachtouris
    nonargs = [term for term in parser.unparsed if not term.startswith('-')]
66 b6a99832 Stavros Sachtouris
    set_command_params(nonargs)
67 b6a99832 Stavros Sachtouris
68 b6a99832 Stavros Sachtouris
    global _best_match
69 b6a99832 Stavros Sachtouris
    _best_match = []
70 b6a99832 Stavros Sachtouris
71 db03f805 Stavros Sachtouris
    _cnf = parser.arguments['config']
72 db03f805 Stavros Sachtouris
    group_spec = _cnf.get('global', '%s_cli' % group)
73 f724cd35 Stavros Sachtouris
    spec_module = _load_spec_module(group_spec, parser.arguments, '_commands')
74 334338ce Stavros Sachtouris
    if spec_module is None:
75 334338ce Stavros Sachtouris
        raise CLIUnknownCommand(
76 334338ce Stavros Sachtouris
            'Could not find specs for %s commands' % group,
77 334338ce Stavros Sachtouris
            details=[
78 334338ce Stavros Sachtouris
                'Make sure %s is a valid command group' % group,
79 334338ce Stavros Sachtouris
                'Refer to kamaki documentation for setting custom command',
80 334338ce Stavros Sachtouris
                'groups or overide existing ones'])
81 b6a99832 Stavros Sachtouris
    cmd_tree = _get_cmd_tree_from_spec(group, spec_module._commands)
82 b6a99832 Stavros Sachtouris
83 b6a99832 Stavros Sachtouris
    if _best_match:
84 b6a99832 Stavros Sachtouris
        cmd = cmd_tree.get_command('_'.join(_best_match))
85 b6a99832 Stavros Sachtouris
    else:
86 b6a99832 Stavros Sachtouris
        cmd = _get_best_match_from_cmd_tree(cmd_tree, parser.unparsed)
87 b6a99832 Stavros Sachtouris
        _best_match = cmd.path.split('_')
88 b6a99832 Stavros Sachtouris
    if cmd is None:
89 334338ce Stavros Sachtouris
        kloger.info('Unexpected error: failed to load command (-d for more)')
90 b6a99832 Stavros Sachtouris
        exit(1)
91 b6a99832 Stavros Sachtouris
92 b6a99832 Stavros Sachtouris
    update_parser_help(parser, cmd)
93 b6a99832 Stavros Sachtouris
94 b6a99832 Stavros Sachtouris
    if _help or not cmd.is_command:
95 b6a99832 Stavros Sachtouris
        parser.parser.print_help()
96 38db356b Stavros Sachtouris
        if getattr(cmd, 'long_help', False):
97 38db356b Stavros Sachtouris
            print 'Details:\n', cmd.long_help
98 b6a99832 Stavros Sachtouris
        print_subcommands_help(cmd)
99 b6a99832 Stavros Sachtouris
        exit(0)
100 b6a99832 Stavros Sachtouris
101 eb46e9a1 Stavros Sachtouris
    cls = cmd.cmd_class
102 fa7d08b6 Stavros Sachtouris
    auth_base = init_cached_authenticator(_cnf, cloud, kloger) if (
103 fa7d08b6 Stavros Sachtouris
        cloud) else None
104 144b3551 Stavros Sachtouris
    executable = cls(parser.arguments, auth_base, cloud)
105 b6a99832 Stavros Sachtouris
    parser.update_arguments(executable.arguments)
106 b6a99832 Stavros Sachtouris
    for term in _best_match:
107 c8e17a67 Stavros Sachtouris
            parser.unparsed.remove(term)
108 b6a99832 Stavros Sachtouris
    exec_cmd(executable, parser.unparsed, parser.parser.print_help)