Completed
Pull Request — develop (#449)
by
unknown
41s
created

_custom_excepthook()   B

Complexity

Conditions 5

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
c 0
b 0
f 0
dl 0
loc 11
rs 8.5454

1 Method

Rating   Name   Duplication   Size   Complexity  
A excepthook() 0 8 4
1
'''
2
Anaconda Cloud command line manager
3
'''
4
from __future__ import print_function, unicode_literals
5
6
import logging
7
import sys
8
9
from argparse import ArgumentParser, RawDescriptionHelpFormatter
10
from os import makedirs
11
from os.path import join, exists, isfile
12
from logging.handlers import RotatingFileHandler
13
14
from binstar_client import __version__ as version
15
from binstar_client import commands as command_module
16
from binstar_client.commands.login import interactive_login
17
from binstar_client import errors
18
from binstar_client.utils import USER_LOGDIR
19
20
from clyent import add_subparser_modules
21
22
23
logger = logging.getLogger('binstar')
24
25
26
def file_or_token(value):
27
    """
28
    If value is a file path and the file exists its contents are stripped and returned,
29
    otherwise value is returned.
30
    """
31
    if isfile(value):
32
        with open(value) as fd:
33
            return fd.read().strip()
34
35
    if any(char in value for char in '/\\.'):
36
        # This chars will never be in a token value, but may be in a path
37
        # The error message will be handled by the parser
38
        raise ValueError()
39
40
    return value
41
42
43
def _custom_excepthook(logger, show_traceback=False):
44
    def excepthook(exc_type, exc_value, exc_traceback):
45
        if issubclass(exc_type, KeyboardInterrupt) or issubclass(exc_type, errors.BinstarError):
46
            return
47
48
        if show_traceback:
49
            logger.error('', exc_info=(exc_type, exc_value, exc_traceback))
50
        else:
51
            logger.error('Error: %s', exc_value)
52
53
    return excepthook
54
55
56
def _setup_logging(logger, log_level=logging.INFO, show_traceback=False):
57
    logger.setLevel(logging.DEBUG)
58
59
    if not exists(USER_LOGDIR):
60
        makedirs(USER_LOGDIR)
61
62
    log_file = join(USER_LOGDIR, 'cli.log')
63
64
    file_handler = RotatingFileHandler(log_file, maxBytes=10 * (1024 ** 2), backupCount=5)
65
    file_handler.setLevel(logging.DEBUG)
66
67
    console_handler = logging.StreamHandler()
68
    console_handler.setLevel(log_level)
69
70
    console_handler.setFormatter(logging.Formatter('%(message)s'))
71
    file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s %(name)-15s %(message)s'))
72
73
    logger.addHandler(console_handler)
74
    logger.addHandler(file_handler)
75
76
    sys.excepthook = _custom_excepthook(logger, show_traceback=show_traceback)
77
78
79
def add_default_arguments(parser, version=None):
80
    output_group = parser.add_argument_group('output')
81
    output_group.add_argument('--show-traceback', action='store_true',
82
                              help='Show the full traceback for chalmers user errors (default: %(default)s)')
83
    output_group.add_argument('-v', '--verbose',
84
                              action='store_const', help='print debug information ot the console',
85
                              dest='log_level',
86
                              default=logging.INFO, const=logging.DEBUG)
87
    output_group.add_argument('-q', '--quiet',
88
                              action='store_const', help='Only show warnings or errors the console',
89
                              dest='log_level', const=logging.WARNING)
90
91
    if version:
92
        parser.add_argument('-V', '--version', action='version',
93
                            version="%%(prog)s Command line client (version %s)" % (version,))
94
95
96
def binstar_main(sub_command_module, args=None, exit=True, description=None, version=None, epilog=None):
97
    parser = ArgumentParser(description=description, epilog=epilog,
98
                            formatter_class=RawDescriptionHelpFormatter)
99
100
    add_default_arguments(parser, version)
101
    bgroup = parser.add_argument_group('anaconda-client options')
102
    bgroup.add_argument('-t', '--token', type=file_or_token,
103
                        help="Authentication token to use. "
104
                             "May be a token or a path to a file containing a token")
105
    bgroup.add_argument('-s', '--site',
106
                        help='select the anaconda-client site to use', default=None)
107
108
    add_subparser_modules(parser, sub_command_module, 'conda_server.subcommand')
109
110
    args = parser.parse_args(args)
111
112
    _setup_logging(logger, log_level=args.log_level, show_traceback=args.show_traceback)
113
114
    try:
115
        try:
116
            if not hasattr(args, 'main'):
117
                parser.error("A sub command must be given. "
118
                             "To show all available sub commands, run:\n\n\t anaconda -h\n")
119
            return args.main(args)
120
        except errors.Unauthorized:
121
            if not sys.stdin.isatty() or args.token:
122
                # Don't try the interactive login
123
                # Just exit
124
                raise
125
126
            logger.info('The action you are performing requires authentication, '
127
                        'please sign in:')
128
            interactive_login(args)
129
            return args.main(args)
130
    except errors.ShowHelp:
131
        args.sub_parser.print_help()
132
        if exit:
133
            raise SystemExit(1)
134
        else:
135
            return 1
136
137
138
def main(args=None, exit=True):
139
    binstar_main(command_module, args, exit,
140
                 description=__doc__, version=version)
141
142
143
if __name__ == '__main__':
144
    main()
145