summaryrefslogtreecommitdiffstats
path: root/git_taskrepo/main.py
blob: b93797ddaac4285990c8326882e2f7453a42b6f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
52
53
54
55
56
57
58
59
60

# -*- coding: utf-8 -*-

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

import os
import sys
from optparse import Option, IndentedHelpFormatter, SUPPRESS_HELP
from command import CommandOptionParser, CommandContainer
import git.exc

__version__ = '0.1'

__all__ = (
    "main",
)


class BeakerOptionParser(CommandOptionParser):
    standard_option_list = [
    ]

# register default command plugins
import git_taskrepo.sub_commands
CommandContainer.register_module(git_taskrepo.sub_commands, prefix="cmd_")


def main():
    command_container = CommandContainer()
    formatter = IndentedHelpFormatter(max_help_position=60, width=120)
    parser = BeakerOptionParser(version=__version__,
            conflict_handler='resolve',
            command_container=command_container,
            default_command="help", formatter=formatter)

    # Need to deal with the possibility that requests is not importable...
    try:
        import requests
        maybe_http_error = (requests.HTTPError,)
    except ImportError:
        maybe_http_error = ()

    # This is parser.run(), but with more sensible error handling
    cmd, cmd_opts, cmd_args = parser.parse_args()
    try:
        return cmd.run(*cmd_args, **cmd_opts.__dict__)
    except git.exc.InvalidGitRepositoryError, e:
        sys.stderr.write('Not a valid git repo: %s\n' % e)
        return 1
    except git.exc, e:
        sys.stderr.write('GIT error: %s\n' % e)
        return 1
    except ValueError, e:
        sys.stderr.write('%s\n' % e)

if __name__ == '__main__':
    sys.exit(main())