aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTommi Virtanen <tv@eagain.net>2007-09-03 20:54:14 -0700
committerTommi Virtanen <tv@eagain.net>2007-09-03 20:54:14 -0700
commit1dfe84754a1dce077b6228aab551275c34275ea0 (patch)
tree559ca2d323f6d462815fa3bd5da9e8866008c2cf /gitosis
parentCreated user get home from somewhere, let adduser create it. (diff)
downloadgitosis-gentoo-1dfe84754a1dce077b6228aab551275c34275ea0.tar.gz
gitosis-gentoo-1dfe84754a1dce077b6228aab551275c34275ea0.tar.bz2
gitosis-gentoo-1dfe84754a1dce077b6228aab551275c34275ea0.zip
Make gitosis-init ignore error from non-existent config file.
Refactored config file reading and logging initialization to make things nicer.
Diffstat (limited to 'gitosis')
-rw-r--r--gitosis/app.py44
-rw-r--r--gitosis/init.py7
2 files changed, 39 insertions, 12 deletions
diff --git a/gitosis/app.py b/gitosis/app.py
index f827ac2..99830b0 100644
--- a/gitosis/app.py
+++ b/gitosis/app.py
@@ -2,8 +2,20 @@ 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
@@ -13,12 +25,21 @@ class App(object):
return app.main()
def main(self):
+ self.setup_basic_logging()
parser = self.create_parser()
(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.handle_args(parser, cfg, options, args)
+ def setup_basic_logging(self):
+ logging.basicConfig()
+
def create_parser(self):
parser = optparse.OptionParser()
parser.set_defaults(
@@ -31,24 +52,26 @@ class App(object):
return parser
- def read_config(self, options):
+ def create_config(self, options):
cfg = ConfigParser.RawConfigParser()
+ return cfg
+
+ def read_config(self, options, cfg):
try:
conffile = file(options.config)
except (IOError, OSError), e:
- # I trust the exception has the path.
- print >>sys.stderr, '%s: Unable to read config file: %s' \
- % (options.get_prog_name(), e)
- sys.exit(1)
+ 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()
- return cfg
def setup_logging(self, cfg):
- logging.basicConfig()
-
try:
loglevel = cfg.get('gitosis', 'loglevel')
except (ConfigParser.NoSectionError,
@@ -58,9 +81,6 @@ class App(object):
try:
symbolic = logging._levelNames[loglevel]
except KeyError:
- # need to delay error reporting until we've called
- # basicConfig
- log = logging.getLogger('gitosis.app')
log.warning(
'Ignored invalid loglevel configuration: %r',
loglevel,
diff --git a/gitosis/init.py b/gitosis/init.py
index ba2e4c6..0da6ed3 100644
--- a/gitosis/init.py
+++ b/gitosis/init.py
@@ -123,6 +123,13 @@ class Main(app.App):
'Initialize a user account for use with gitosis')
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):
super(Main, self).handle_args(parser, cfg, options, args)