Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / one_command.py @ 761e0cbf

History | View | Annotate | Download (3.5 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 b6a99832 Stavros Sachtouris
from logging import getLogger
35 b6a99832 Stavros Sachtouris
36 b6a99832 Stavros Sachtouris
from kamaki.cli import get_command_group, set_command_params
37 b6a99832 Stavros Sachtouris
from kamaki.cli import print_subcommands_help, exec_cmd, update_parser_help
38 b6a99832 Stavros Sachtouris
from kamaki.cli import _groups_help, _load_spec_module
39 b6a99832 Stavros Sachtouris
40 b6a99832 Stavros Sachtouris
41 b6a99832 Stavros Sachtouris
kloger = getLogger('kamaki')
42 b6a99832 Stavros Sachtouris
43 b6a99832 Stavros Sachtouris
44 b6a99832 Stavros Sachtouris
def _get_cmd_tree_from_spec(spec, cmd_tree_list):
45 b6a99832 Stavros Sachtouris
    for tree in cmd_tree_list:
46 b6a99832 Stavros Sachtouris
        if tree.name == spec:
47 b6a99832 Stavros Sachtouris
            return tree
48 b6a99832 Stavros Sachtouris
    return None
49 b6a99832 Stavros Sachtouris
50 b6a99832 Stavros Sachtouris
51 b6a99832 Stavros Sachtouris
def _get_best_match_from_cmd_tree(cmd_tree, unparsed):
52 b6a99832 Stavros Sachtouris
    matched = [term for term in unparsed if not term.startswith('-')]
53 b6a99832 Stavros Sachtouris
    while matched:
54 b6a99832 Stavros Sachtouris
        try:
55 b6a99832 Stavros Sachtouris
            return cmd_tree.get_command('_'.join(matched))
56 b6a99832 Stavros Sachtouris
        except KeyError:
57 b6a99832 Stavros Sachtouris
            matched = matched[:-1]
58 b6a99832 Stavros Sachtouris
    return None
59 b6a99832 Stavros Sachtouris
60 b6a99832 Stavros Sachtouris
61 b6a99832 Stavros Sachtouris
def run(parser, _help):
62 b6a99832 Stavros Sachtouris
    group = get_command_group(list(parser.unparsed), parser.arguments)
63 b6a99832 Stavros Sachtouris
    if not group:
64 b6a99832 Stavros Sachtouris
        parser.parser.print_help()
65 b6a99832 Stavros Sachtouris
        _groups_help(parser.arguments)
66 b6a99832 Stavros Sachtouris
        exit(0)
67 b6a99832 Stavros Sachtouris
68 b6a99832 Stavros Sachtouris
    nonargs = [term for term in parser.unparsed if not term.startswith('-')]
69 b6a99832 Stavros Sachtouris
    set_command_params(nonargs)
70 b6a99832 Stavros Sachtouris
71 b6a99832 Stavros Sachtouris
    global _best_match
72 b6a99832 Stavros Sachtouris
    _best_match = []
73 b6a99832 Stavros Sachtouris
74 b6a99832 Stavros Sachtouris
    spec_module = _load_spec_module(group, parser.arguments, '_commands')
75 b6a99832 Stavros Sachtouris
76 b6a99832 Stavros Sachtouris
    cmd_tree = _get_cmd_tree_from_spec(group, spec_module._commands)
77 b6a99832 Stavros Sachtouris
78 b6a99832 Stavros Sachtouris
    if _best_match:
79 b6a99832 Stavros Sachtouris
        cmd = cmd_tree.get_command('_'.join(_best_match))
80 b6a99832 Stavros Sachtouris
    else:
81 b6a99832 Stavros Sachtouris
        cmd = _get_best_match_from_cmd_tree(cmd_tree, parser.unparsed)
82 b6a99832 Stavros Sachtouris
        _best_match = cmd.path.split('_')
83 b6a99832 Stavros Sachtouris
    if cmd is None:
84 b6a99832 Stavros Sachtouris
        kloger.info(
85 b6a99832 Stavros Sachtouris
            'Unexpected error: failed to load command (-d for details)')
86 b6a99832 Stavros Sachtouris
        exit(1)
87 b6a99832 Stavros Sachtouris
88 b6a99832 Stavros Sachtouris
    update_parser_help(parser, cmd)
89 b6a99832 Stavros Sachtouris
90 b6a99832 Stavros Sachtouris
    if _help or not cmd.is_command:
91 b6a99832 Stavros Sachtouris
        parser.parser.print_help()
92 b6a99832 Stavros Sachtouris
        print_subcommands_help(cmd)
93 b6a99832 Stavros Sachtouris
        exit(0)
94 b6a99832 Stavros Sachtouris
95 b6a99832 Stavros Sachtouris
    cls = cmd.get_class()
96 b6a99832 Stavros Sachtouris
    executable = cls(parser.arguments)
97 b6a99832 Stavros Sachtouris
    parser.update_arguments(executable.arguments)
98 b6a99832 Stavros Sachtouris
    #parsed, unparsed = parse_known_args(parser, executable.arguments)
99 b6a99832 Stavros Sachtouris
    for term in _best_match:
100 c8e17a67 Stavros Sachtouris
            parser.unparsed.remove(term)
101 b6a99832 Stavros Sachtouris
    exec_cmd(executable, parser.unparsed, parser.parser.print_help)