bdt.py 1.95 KB
Newer Older
1
2
3
4
5
6
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Main entry point for bdt
"""

7
import os
8
9
10
11
12
import pkg_resources

import click
from click_plugins import with_plugins

13
from ..log import setup
14
from ..bootstrap import set_environment
15
logger = setup('bob')
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
43
44
45
46
47
48
49
50
51


class AliasedGroup(click.Group):
  ''' Class that handles prefix aliasing for commands '''
  def get_command(self, ctx, cmd_name):
    rv = click.Group.get_command(self, ctx, cmd_name)
    if rv is not None:
      return rv
    matches = [x for x in self.list_commands(ctx)
               if x.startswith(cmd_name)]
    if not matches:
      return None
    elif len(matches) == 1:
      return click.Group.get_command(self, ctx, matches[0])
    ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))


def raise_on_error(view_func):
    """Raise a click exception if returned value is not zero.

    Click exits successfully if anything is returned, in order to exit properly
    when something went wrong an exception must be raised.
    """

    from functools import wraps

    def _decorator(*args, **kwargs):
        value = view_func(*args, **kwargs)
        if value not in [None, 0]:
            exception = click.ClickException("Error occured")
            exception.exit_code = value
            raise exception
        return value
    return wraps(view_func)(_decorator)


52
53
54
55
56
# must set LANG and LC_ALL before using click
set_environment('LANG', 'en_US.UTF-8', os.environ, verbose=False)
set_environment('LC_ALL', os.environ['LANG'], os.environ, verbose=False)


57
58
59
60
61
@with_plugins(pkg_resources.iter_entry_points('bdt.cli'))
@click.group(cls=AliasedGroup,
             context_settings=dict(help_option_names=['-?', '-h', '--help']))
def main():
    """Bob Development Tools - see available commands below"""
62
63

    #sets up basic environment variables required everywhere
64
    from ..constants import CACERT
65
66

    set_environment('SSL_CERT_FILE', CACERT, os.environ)
67
    set_environment('REQUESTS_CA_BUNDLE', CACERT, os.environ)