aboutsummaryrefslogtreecommitdiff
path: root/gitosis/app.py
blob: fa9772b14ba1adb3cb88667f683a14517ac2db03 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import sys
import logging
import optparse
import errno
import ConfigParser
 
log = logging.getLogger('gitosis.app')
 
class CannotReadConfigError(Exception):
    """Unable to read config file"""
 
    def __str__(self):
        return '%s%s' % (self.__doc__''.join(self.args))
 
class ConfigFileDoesNotExistError(CannotReadConfigError):
    """Configuration does not exist"""
 
class App(object):
    name = None
 
    def run(class_):
        app = class_()
        return app.main()
    run = classmethod(run)
 
    def main(self):
        self.setup_basic_logging()
        parser = self.create_parser()
        (optionsargs) = parser.parse_args()
        cfg = self.create_config(options)
        try:
            self.read_config(optionscfg)
        except CannotReadConfigErrore:
            log.error(str(e))
            sys.exit(1)
        self.setup_logging(cfg)
        self.handle_args(parsercfgoptionsargs)
 
    def setup_basic_logging(self):
        logging.basicConfig()
 
    def create_parser(self):
        parser = optparse.OptionParser()
        parser.set_defaults(
            config=os.path.expanduser('~/.gitosis.conf'),
            )
        parser.add_option('--config',
                          metavar='FILE',
                          help='read config from FILE',
                          )
 
        return parser
 
    def create_config(self, options):
        cfg = ConfigParser.RawConfigParser()
        return cfg
 
    def read_config(self, options, cfg):
        try:
            conffile = file(options.config)
        except (IOErrorOSError)e:
            if e.errno == errno.ENOENT:
                # special case this because gitosis-init wants to 
                # ignore this particular error case 
                raise ConfigFileDoesNotExistError(str(e))
            else:
                raise CannotReadConfigError(str(e))
        try:
            cfg.readfp(conffile)
        finally:
            conffile.close()
 
    def setup_logging(self, cfg):
        try:
            loglevel = cfg.get('gitosis''loglevel')
        except (ConfigParser.NoSectionError,
                ConfigParser.NoOptionError):
            pass
        else:
            try:
                symbolic = logging._levelNames[loglevel]
            except KeyError:
                log.warning(
                    'Ignored invalid loglevel configuration: %r',
                    loglevel,
                    )
            else:
                logging.root.setLevel(symbolic)
 
    def handle_args(self, parser, cfg, options, args):
        if args:
            parser.error('not expecting arguments')