__init__.py 12.7 KB
Newer Older
1
2
3
4
5
6
7
8
#!/usr/bin/env python
# Copyright 2011-2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
9
10
#      copyright notice, this list of conditions and the following
#      disclaimer.
11
12
#
#   2. Redistributions in binary form must reproduce the above
13
14
15
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

from __future__ import print_function

import gevent.monkey
#Monkey-patch everything for gevent early on
gevent.monkey.patch_all()

import logging

43
from inspect import getargspec
44
from argparse import ArgumentParser, ArgumentError
45
from os.path import basename
46
from sys import exit, stdout, stderr, argv
47
48

try:
49
    from collections import OrderedDict
50
except ImportError:
51
    from ordereddict import OrderedDict
52

53
#from kamaki import clients
54
55
56
57
58
59
60
from kamaki.cli.errors import CLIError, CLISyntaxError,\
    CLICmdIncompleteError, CLICmdSpecError
from kamaki.cli.utils import bold, magenta, red, yellow,\
    print_list, print_dict, remove_colors
from kamaki.cli.command_tree import CommandTree
from kamaki.cli.argument import _arguments, parse_known_args
from kamaki.cli.history import History
61

62
cmd_spec_locations = [
63
64
65
66
67
68
69
70
71
72
73
    'kamaki.cli.commands',
    'kamaki.commands',
    'kamaki.cli',
    'kamaki',
    '']
_commands = CommandTree(name='kamaki',
    description='A command line tool for poking clouds')

# If empty, all commands are loaded, if not empty, only commands in this list
# e.g. [store, lele, list, lolo] is good to load store_list but not list_store
# First arg should always refer to a group
74
candidate_command_terms = []
75
76
77
78
allow_no_commands = False
allow_all_commands = False
allow_subclass_signatures = False

79

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
def _allow_class_in_cmd_tree(cls):
    global allow_all_commands
    if allow_all_commands:
        return True
    global allow_no_commands
    if allow_no_commands:
        return False

    term_list = cls.__name__.split('_')
    global candidate_command_terms
    index = 0
    for term in candidate_command_terms:
        try:
            index += 1 if term_list[index] == term else 0
        except IndexError:  # Whole term list matched!
            return True
    if allow_subclass_signatures:
        if index == len(candidate_command_terms) and len(term_list) > index:
            try:  # is subterm already in _commands?
                _commands.get_command('_'.join(term_list[:index + 1]))
            except KeyError:  # No, so it must be placed there
                return True
        return False

    return True if index == len(term_list) else False
105
106


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def command():
    """Class decorator that registers a class as a CLI command"""

    def decorator(cls):
        """Any class with name of the form cmd1_cmd2_cmd3_... is accepted"""

        if not _allow_class_in_cmd_tree(cls):
            return cls

        cls.description, sep, cls.long_description\
            = cls.__doc__.partition('\n')

        # Generate a syntax string based on main's arguments
        spec = getargspec(cls.main.im_func)
        args = spec.args[1:]
        n = len(args) - len(spec.defaults or ())
        required = ' '.join('<%s>' % x\
            .replace('____', '[:')\
            .replace('___', ':')\
            .replace('__', ']').\
            replace('_', ' ') for x in args[:n])
        optional = ' '.join('[%s]' % x\
            .replace('____', '[:')\
            .replace('___', ':')\
            .replace('__', ']').\
            replace('_', ' ') for x in args[n:])
        cls.syntax = ' '.join(x for x in [required, optional] if x)
        if spec.varargs:
            cls.syntax += ' <%s ...>' % spec.varargs

        # store each term, one by one, first
        _commands.add_command(cls.__name__, cls.description, cls)

        return cls
    return decorator
142

143

144
def _update_parser(parser, arguments):
145
146
147
148
149
150
    for name, argument in arguments.items():
        try:
            argument.update_parser(parser, name)
        except ArgumentError:
            pass

151

152
def _init_parser(exe):
153
154
155
156
157
    parser = ArgumentParser(add_help=False)
    parser.prog = '%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe
    _update_parser(parser, _arguments)
    return parser

158

159
def _print_error_message(cli_err):
160
161
162
163
164
165
166
167
168
169
170
171
172
    errmsg = '%s (%s)' % (cli_err, cli_err.status if cli_err.status else ' ')
    if cli_err.importance == 1:
        errmsg = magenta(errmsg)
    elif cli_err.importance == 2:
        errmsg = yellow(errmsg)
    elif cli_err.importance > 2:
        errmsg = red(errmsg)
    stdout.write(errmsg)
    if cli_err.details is not None and len(cli_err.details) > 0:
        print(': %s' % cli_err.details)
    else:
        print()

173

174
def get_command_group(unparsed):
175
176
177
178
179
180
181
    groups = _arguments['config'].get_groups()
    for grp_candidate in unparsed:
        if grp_candidate in groups:
            unparsed.remove(grp_candidate)
            return grp_candidate
    return None

182

183
def load_command(group, unparsed, reload_package=False):
184
185
186
187
188
189
190
191
192
193
194
195
196
    global candidate_command_terms
    candidate_command_terms = [group] + unparsed
    pkg = load_group_package(group, reload_package)

    #From all possible parsed commands, chose the first match in user string
    final_cmd = _commands.get_command(group)
    for term in unparsed:
        cmd = final_cmd.get_subcmd(term)
        if cmd is not None:
            final_cmd = cmd
            unparsed.remove(cmd.name)
    return final_cmd

197

198
def shallow_load():
199
200
201
202
203
204
205
    """Load only group names and descriptions"""
    global allow_no_commands
    allow_no_commands = True  # load only descriptions
    for grp in _arguments['config'].get_groups():
        load_group_package(grp)
    allow_no_commands = False

206

207
def load_group_package(group, reload_package=False):
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    spec_pkg = _arguments['config'].value.get(group, 'cli')
    if spec_pkg is None:
        return None
    for location in cmd_spec_locations:
        location += spec_pkg if location == '' else ('.' + spec_pkg)
        try:
            package = __import__(location, fromlist=['API_DESCRIPTION'])
        except ImportError:
            continue
        if reload_package:
            reload(package)
        for grp, descr in package.API_DESCRIPTION.items():
            _commands.add_command(grp, descr)
        return package
    raise CLICmdSpecError(details='Cmd Spec Package %s load failed' % spec_pkg)

224

225
def print_commands(prefix=None, full_depth=False):
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    cmd_list = _commands.get_groups() if prefix is None\
        else _commands.get_subcommands(prefix)
    cmds = {}
    for subcmd in cmd_list:
        if subcmd.sublen() > 0:
            sublen_str = '( %s more terms ... )' % subcmd.sublen()
            cmds[subcmd.name] = [subcmd.help, sublen_str]\
                if subcmd.has_description else subcmd_str
        else:
            cmds[subcmd.name] = subcmd.help
    if len(cmds) > 0:
        print('\nOptions:')
        print_dict(cmds, ident=12)
    if full_depth:
        _commands.pretty_print()

242

243
def setup_logging(silent=False, debug=False, verbose=False, include=False):
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    """handle logging for clients package"""

    def add_handler(name, level, prefix=''):
        h = logging.StreamHandler()
        fmt = logging.Formatter(prefix + '%(message)s')
        h.setFormatter(fmt)
        logger = logging.getLogger(name)
        logger.addHandler(h)
        logger.setLevel(level)

    if silent:
        add_handler('', logging.CRITICAL)
    elif debug:
        add_handler('requests', logging.INFO, prefix='* ')
        add_handler('clients.send', logging.DEBUG, prefix='> ')
        add_handler('clients.recv', logging.DEBUG, prefix='< ')
    elif verbose:
        add_handler('requests', logging.INFO, prefix='* ')
        add_handler('clients.send', logging.INFO, prefix='> ')
        add_handler('clients.recv', logging.INFO, prefix='< ')
    elif include:
        add_handler('clients.recv', logging.INFO)
    else:
        add_handler('', logging.WARNING)

269
270

def _exec_cmd(instance, cmd_args, help_method):
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    try:
        return instance.main(*cmd_args)
    except TypeError as err:
        if err.args and err.args[0].startswith('main()'):
            print(magenta('Syntax error'))
            if instance.get_argument('verbose'):
                print(unicode(err))
            help_method()
        else:
            raise
    except CLIError as err:
        if instance.get_argument('debug'):
            raise
        _print_error_message(err)
    return 1

287

288
def one_command():
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
    _debug = False
    _help = False
    _verbose = False
    try:
        exe = basename(argv[0])
        parser = _init_parser(exe)
        parsed, unparsed = parse_known_args(parser, _arguments)
        _colors = _arguments['config'].get('global', 'colors')
        if _colors != 'on':
            remove_colors()
        _history = History(_arguments['config'].get('history', 'file'))
        _history.add(' '.join([exe] + argv[1:]))
        _debug = _arguments['debug'].value
        _help = _arguments['help'].value
        _verbose = _arguments['verbose'].value
        if _arguments['version'].value:
            exit(0)

        group = get_command_group(unparsed)
        if group is None:
            parser.print_help()
            shallow_load()
            print_commands(full_depth=_debug)
            exit(0)

        cmd = load_command(group, unparsed)
        if _help or not cmd.is_command:
            if cmd.has_description:
                parser.description = cmd.help
            else:
                try:
                    parser.description\
                        = _commands.get_closest_ancestor_command(cmd.path).help
                except KeyError:
                    parser.description = ' '
            parser.prog = '%s %s ' % (exe, cmd.path.replace('_', ' '))
            if cmd.is_command:
                cli = cmd.get_class()
                parser.prog += cli.syntax
                _update_parser(parser, cli().arguments)
            else:
                parser.prog += '[...]'
            parser.print_help()

            # load one more level just to see what is missing
            global allow_subclass_signatures
            allow_subclass_signatures = True
            load_command(group, cmd.path.split('_')[1:], reload_package=True)

            print_commands(cmd.path, full_depth=_debug)
            exit(0)

        setup_logging(silent=_arguments['silent'].value,
            debug=_debug,
            verbose=_verbose,
            include=_arguments['include'].value)
        cli = cmd.get_class()
        executable = cli(_arguments)
        _update_parser(parser, executable.arguments)
        parser.prog = '%s %s %s'\
            % (exe, cmd.path.replace('_', ' '), cli.syntax)
        parsed, new_unparsed = parse_known_args(parser, _arguments)
        unparsed = [term for term in unparsed if term in new_unparsed]
        ret = _exec_cmd(executable, unparsed, parser.print_help)
        exit(ret)
    except CLIError as err:
        if _debug:
            raise
        _print_error_message(err)
        exit(1)

from command_shell import _fix_arguments, Shell

362

363
def _start_shell():
364
365
366
367
368
369
370
    shell = Shell()
    shell.set_prompt(basename(argv[0]))
    from kamaki import __version__ as version
    shell.greet(version)
    shell.do_EOF = shell.do_exit
    return shell

371

Stavros Sachtouris's avatar
Stavros Sachtouris committed
372
def run_shell():
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
    _fix_arguments()
    shell = _start_shell()
    _config = _arguments['config']
    _config.value = None
    for grp in _config.get_groups():
        global allow_all_commands
        allow_all_commands = True
        load_group_package(grp)
    setup_logging(silent=_arguments['silent'].value,
        debug=_arguments['debug'].value,
        verbose=_arguments['verbose'].value,
        include=_arguments['include'].value)
    shell.cmd_tree = _commands
    shell.run()

388
389

def main():
390

391
392
393
394
    if len(argv) <= 1:
        run_shell()
    else:
        one_command()