root / kamaki / cli / one_command.py @ 01413001
History | View | Annotate | Download (4 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 kamaki.cli import get_command_group, set_command_params |
35 | b6a99832 | Stavros Sachtouris | from kamaki.cli import print_subcommands_help, exec_cmd, update_parser_help |
36 | b6a99832 | Stavros Sachtouris | from kamaki.cli import _groups_help, _load_spec_module |
37 | 334338ce | Stavros Sachtouris | from kamaki.cli import 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 | 144b3551 | Stavros Sachtouris | def run(auth_base, 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 | 362adf50 | Stavros Sachtouris | group_spec = parser.arguments['config'].get('global', '%s_cli' % group) |
72 | f724cd35 | Stavros Sachtouris | spec_module = _load_spec_module(group_spec, parser.arguments, '_commands')
|
73 | 334338ce | Stavros Sachtouris | if spec_module is None: |
74 | 334338ce | Stavros Sachtouris | raise CLIUnknownCommand(
|
75 | 334338ce | Stavros Sachtouris | 'Could not find specs for %s commands' % group,
|
76 | 334338ce | Stavros Sachtouris | details=[ |
77 | 334338ce | Stavros Sachtouris | 'Make sure %s is a valid command group' % group,
|
78 | 334338ce | Stavros Sachtouris | 'Refer to kamaki documentation for setting custom command',
|
79 | 334338ce | Stavros Sachtouris | 'groups or overide existing ones'])
|
80 | b6a99832 | Stavros Sachtouris | cmd_tree = _get_cmd_tree_from_spec(group, spec_module._commands) |
81 | b6a99832 | Stavros Sachtouris | |
82 | b6a99832 | Stavros Sachtouris | if _best_match:
|
83 | b6a99832 | Stavros Sachtouris | cmd = cmd_tree.get_command('_'.join(_best_match))
|
84 | b6a99832 | Stavros Sachtouris | else:
|
85 | b6a99832 | Stavros Sachtouris | cmd = _get_best_match_from_cmd_tree(cmd_tree, parser.unparsed) |
86 | b6a99832 | Stavros Sachtouris | _best_match = cmd.path.split('_')
|
87 | b6a99832 | Stavros Sachtouris | if cmd is None: |
88 | 334338ce | Stavros Sachtouris | kloger.info('Unexpected error: failed to load command (-d for more)')
|
89 | b6a99832 | Stavros Sachtouris | exit(1) |
90 | b6a99832 | Stavros Sachtouris | |
91 | b6a99832 | Stavros Sachtouris | update_parser_help(parser, cmd) |
92 | b6a99832 | Stavros Sachtouris | |
93 | b6a99832 | Stavros Sachtouris | if _help or not cmd.is_command: |
94 | b6a99832 | Stavros Sachtouris | parser.parser.print_help() |
95 | b6a99832 | Stavros Sachtouris | print_subcommands_help(cmd) |
96 | b6a99832 | Stavros Sachtouris | exit(0) |
97 | b6a99832 | Stavros Sachtouris | |
98 | eb46e9a1 | Stavros Sachtouris | cls = cmd.cmd_class |
99 | 144b3551 | Stavros Sachtouris | executable = cls(parser.arguments, auth_base, cloud) |
100 | b6a99832 | Stavros Sachtouris | parser.update_arguments(executable.arguments) |
101 | b6a99832 | Stavros Sachtouris | #parsed, unparsed = parse_known_args(parser, executable.arguments)
|
102 | b6a99832 | Stavros Sachtouris | for term in _best_match: |
103 | c8e17a67 | Stavros Sachtouris | parser.unparsed.remove(term) |
104 | b6a99832 | Stavros Sachtouris | exec_cmd(executable, parser.unparsed, parser.parser.print_help) |