Make gitosis-init ignore error from non-existent config file.
Refactored config file reading and logging initialization to make things nicer.
This commit is contained in:
parent
96216c231e
commit
1dfe84754a
|
@ -2,8 +2,20 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
import optparse
|
import optparse
|
||||||
|
import errno
|
||||||
import ConfigParser
|
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):
|
class App(object):
|
||||||
name = None
|
name = None
|
||||||
|
|
||||||
|
@ -13,12 +25,21 @@ class App(object):
|
||||||
return app.main()
|
return app.main()
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
|
self.setup_basic_logging()
|
||||||
parser = self.create_parser()
|
parser = self.create_parser()
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
cfg = self.read_config(options)
|
cfg = self.create_config(options)
|
||||||
|
try:
|
||||||
|
self.read_config(options, cfg)
|
||||||
|
except CannotReadConfigError, e:
|
||||||
|
log.error(str(e))
|
||||||
|
sys.exit(1)
|
||||||
self.setup_logging(cfg)
|
self.setup_logging(cfg)
|
||||||
self.handle_args(parser, cfg, options, args)
|
self.handle_args(parser, cfg, options, args)
|
||||||
|
|
||||||
|
def setup_basic_logging(self):
|
||||||
|
logging.basicConfig()
|
||||||
|
|
||||||
def create_parser(self):
|
def create_parser(self):
|
||||||
parser = optparse.OptionParser()
|
parser = optparse.OptionParser()
|
||||||
parser.set_defaults(
|
parser.set_defaults(
|
||||||
|
@ -31,24 +52,26 @@ class App(object):
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def read_config(self, options):
|
def create_config(self, options):
|
||||||
cfg = ConfigParser.RawConfigParser()
|
cfg = ConfigParser.RawConfigParser()
|
||||||
|
return cfg
|
||||||
|
|
||||||
|
def read_config(self, options, cfg):
|
||||||
try:
|
try:
|
||||||
conffile = file(options.config)
|
conffile = file(options.config)
|
||||||
except (IOError, OSError), e:
|
except (IOError, OSError), e:
|
||||||
# I trust the exception has the path.
|
if e.errno == errno.ENOENT:
|
||||||
print >>sys.stderr, '%s: Unable to read config file: %s' \
|
# special case this because gitosis-init wants to
|
||||||
% (options.get_prog_name(), e)
|
# ignore this particular error case
|
||||||
sys.exit(1)
|
raise ConfigFileDoesNotExistError(str(e))
|
||||||
|
else:
|
||||||
|
raise CannotReadConfigError(str(e))
|
||||||
try:
|
try:
|
||||||
cfg.readfp(conffile)
|
cfg.readfp(conffile)
|
||||||
finally:
|
finally:
|
||||||
conffile.close()
|
conffile.close()
|
||||||
return cfg
|
|
||||||
|
|
||||||
def setup_logging(self, cfg):
|
def setup_logging(self, cfg):
|
||||||
logging.basicConfig()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
loglevel = cfg.get('gitosis', 'loglevel')
|
loglevel = cfg.get('gitosis', 'loglevel')
|
||||||
except (ConfigParser.NoSectionError,
|
except (ConfigParser.NoSectionError,
|
||||||
|
@ -58,9 +81,6 @@ class App(object):
|
||||||
try:
|
try:
|
||||||
symbolic = logging._levelNames[loglevel]
|
symbolic = logging._levelNames[loglevel]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# need to delay error reporting until we've called
|
|
||||||
# basicConfig
|
|
||||||
log = logging.getLogger('gitosis.app')
|
|
||||||
log.warning(
|
log.warning(
|
||||||
'Ignored invalid loglevel configuration: %r',
|
'Ignored invalid loglevel configuration: %r',
|
||||||
loglevel,
|
loglevel,
|
||||||
|
|
|
@ -123,6 +123,13 @@ class Main(app.App):
|
||||||
'Initialize a user account for use with gitosis')
|
'Initialize a user account for use with gitosis')
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
def read_config(self, *a, **kw):
|
||||||
|
# ignore errors that result from non-existent config file
|
||||||
|
try:
|
||||||
|
super(Main, self).read_config(*a, **kw)
|
||||||
|
except app.ConfigFileDoesNotExistError:
|
||||||
|
pass
|
||||||
|
|
||||||
def handle_args(self, parser, cfg, options, args):
|
def handle_args(self, parser, cfg, options, args):
|
||||||
super(Main, self).handle_args(parser, cfg, options, args)
|
super(Main, self).handle_args(parser, cfg, options, args)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue