From 8a0654abe2db919b04683d40100b469e8c3adc4b Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Mon, 3 Sep 2007 17:04:41 -0700 Subject: Refactor command line utilities to share setup. Hide internal gitosis-ssh and gitosis-gitweb, it's all in gitosis-run-hook. --- gitosis/run_hook.py | 90 +++++++++++++++++------------------------------------ 1 file changed, 29 insertions(+), 61 deletions(-) (limited to 'gitosis/run_hook.py') diff --git a/gitosis/run_hook.py b/gitosis/run_hook.py index 2bc4aaa..de5076e 100644 --- a/gitosis/run_hook.py +++ b/gitosis/run_hook.py @@ -4,36 +4,14 @@ Perform gitosis actions for a git hook. import errno import logging -import optparse import os import sys import shutil -from ConfigParser import RawConfigParser - from gitosis import repository from gitosis import ssh from gitosis import gitweb - -log = logging.getLogger('gitosis.run_hook') - -def die(msg): - log.error(msg) - sys.exit(1) - -def getParser(): - parser = optparse.OptionParser( - usage='%prog HOOK', - description='Perform gitosis actions for a git hook', - ) - parser.set_defaults( - config=os.path.expanduser('~/.gitosis.conf'), - ) - parser.add_option('--config', - metavar='FILE', - help='read config from FILE', - ) - return parser +from gitosis import app def post_update(cfg): try: @@ -51,43 +29,33 @@ def post_update(cfg): keydir='gitosis-export/keydir', ) -def main(): - logging.basicConfig(level=logging.INFO) - os.umask(0022) - - parser = getParser() - (options, args) = parser.parse_args() - try: - (hook,) = args - except ValueError: - parser.error('Missing argument HOOK.') +class Main(app.App): + def create_parser(self): + parser = super(Main, self).create_parser() + parser.set_usage('%prog [OPTS] HOOK') + parser.set_description( + 'Perform gitosis actions for a git hook') + return parser - cfg = RawConfigParser() - try: - conffile = file(options.config) - except (IOError, OSError), e: - if e.errno == errno.ENOENT: - # not existing is ok - pass - else: - # I trust the exception has the path. - die("Unable to read config file: %s." % e) - else: + def handle_args(self, parser, cfg, options, args): try: - cfg.readfp(conffile) - finally: - conffile.close() - - git_dir = os.environ.get('GIT_DIR') - if git_dir is None: - die('Must have GIT_DIR set in enviroment.') - os.chdir(git_dir) - os.environ['GIT_DIR'] = '.' - - if hook == 'post-update': - log.info('Running hook %s', hook) - post_update(cfg) - log.info('Done.') - else: - log.warning('Ignoring unknown hook: %r', hook) - return 0 + (hook,) = args + except ValueError: + parser.error('Missing argument HOOK.') + + log = logging.getLogger('gitosis.run_hook') + os.umask(0022) + + git_dir = os.environ.get('GIT_DIR') + if git_dir is None: + log.error('Must have GIT_DIR set in enviroment') + sys.exit(1) + os.chdir(git_dir) + os.environ['GIT_DIR'] = '.' + + if hook == 'post-update': + log.info('Running hook %s', hook) + post_update(cfg) + log.info('Done.') + else: + log.warning('Ignoring unknown hook: %r', hook) -- cgit v1.2.3