root / kamaki / cli / one_command.py @ 8e3cbcfe
History | View | Annotate | Download (4.2 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 | 56d84a4e | Stavros Sachtouris | #parser.parser.print_help()
|
62 | 56d84a4e | Stavros Sachtouris | parser.print_help() |
63 | b6a99832 | Stavros Sachtouris | _groups_help(parser.arguments) |
64 | b6a99832 | Stavros Sachtouris | exit(0) |
65 | b6a99832 | Stavros Sachtouris | |
66 | b6a99832 | Stavros Sachtouris | nonargs = [term for term in parser.unparsed if not term.startswith('-')] |
67 | b6a99832 | Stavros Sachtouris | set_command_params(nonargs) |
68 | b6a99832 | Stavros Sachtouris | |
69 | b6a99832 | Stavros Sachtouris | global _best_match
|
70 | b6a99832 | Stavros Sachtouris | _best_match = [] |
71 | b6a99832 | Stavros Sachtouris | |
72 | db03f805 | Stavros Sachtouris | _cnf = parser.arguments['config']
|
73 | db03f805 | Stavros Sachtouris | group_spec = _cnf.get('global', '%s_cli' % group) |
74 | f724cd35 | Stavros Sachtouris | spec_module = _load_spec_module(group_spec, parser.arguments, '_commands')
|
75 | 334338ce | Stavros Sachtouris | if spec_module is None: |
76 | 334338ce | Stavros Sachtouris | raise CLIUnknownCommand(
|
77 | 334338ce | Stavros Sachtouris | 'Could not find specs for %s commands' % group,
|
78 | 334338ce | Stavros Sachtouris | details=[ |
79 | 334338ce | Stavros Sachtouris | 'Make sure %s is a valid command group' % group,
|
80 | 334338ce | Stavros Sachtouris | 'Refer to kamaki documentation for setting custom command',
|
81 | 334338ce | Stavros Sachtouris | 'groups or overide existing ones'])
|
82 | b6a99832 | Stavros Sachtouris | cmd_tree = _get_cmd_tree_from_spec(group, spec_module._commands) |
83 | b6a99832 | Stavros Sachtouris | |
84 | b6a99832 | Stavros Sachtouris | if _best_match:
|
85 | b6a99832 | Stavros Sachtouris | cmd = cmd_tree.get_command('_'.join(_best_match))
|
86 | b6a99832 | Stavros Sachtouris | else:
|
87 | b6a99832 | Stavros Sachtouris | cmd = _get_best_match_from_cmd_tree(cmd_tree, parser.unparsed) |
88 | b6a99832 | Stavros Sachtouris | _best_match = cmd.path.split('_')
|
89 | b6a99832 | Stavros Sachtouris | if cmd is None: |
90 | 334338ce | Stavros Sachtouris | kloger.info('Unexpected error: failed to load command (-d for more)')
|
91 | b6a99832 | Stavros Sachtouris | exit(1) |
92 | b6a99832 | Stavros Sachtouris | |
93 | b6a99832 | Stavros Sachtouris | update_parser_help(parser, cmd) |
94 | b6a99832 | Stavros Sachtouris | |
95 | b6a99832 | Stavros Sachtouris | if _help or not cmd.is_command: |
96 | 56d84a4e | Stavros Sachtouris | if cmd.cmd_class:
|
97 | 56d84a4e | Stavros Sachtouris | parser.required = getattr(cmd.cmd_class, 'required', None) |
98 | 56d84a4e | Stavros Sachtouris | parser.print_help() |
99 | 38db356b | Stavros Sachtouris | if getattr(cmd, 'long_help', False): |
100 | 38db356b | Stavros Sachtouris | print 'Details:\n', cmd.long_help |
101 | b6a99832 | Stavros Sachtouris | print_subcommands_help(cmd) |
102 | b6a99832 | Stavros Sachtouris | exit(0) |
103 | b6a99832 | Stavros Sachtouris | |
104 | eb46e9a1 | Stavros Sachtouris | cls = cmd.cmd_class |
105 | fa7d08b6 | Stavros Sachtouris | auth_base = init_cached_authenticator(_cnf, cloud, kloger) if (
|
106 | fa7d08b6 | Stavros Sachtouris | cloud) else None |
107 | 144b3551 | Stavros Sachtouris | executable = cls(parser.arguments, auth_base, cloud) |
108 | 56d84a4e | Stavros Sachtouris | parser.required = getattr(cls, 'required', None) |
109 | b6a99832 | Stavros Sachtouris | parser.update_arguments(executable.arguments) |
110 | b6a99832 | Stavros Sachtouris | for term in _best_match: |
111 | 264a13f7 | Stavros Sachtouris | parser.unparsed.remove(term) |
112 | 56d84a4e | Stavros Sachtouris | exec_cmd(executable, parser.unparsed, parser.print_help) |