Reimplemented ACL generation to avoid quadratic complexity.
[gitosis/httpauth.git] / gitosis / run_hook.py
blobe3d4ea17e42dcb9be820ea4f89b3db303e651cba
1 """
2 Perform gitosis actions for a git hook.
3 """
5 import errno
6 import logging
7 import os
8 import sys
9 import shutil
11 from ConfigParser import NoSectionError, NoOptionError
13 from gitosis import repository
14 from gitosis import ssh
15 from gitosis import gitweb
16 from gitosis import gitdaemon
17 from gitosis import htaccess
18 from gitosis import app
19 from gitosis import util
20 from gitosis import group
21 from gitosis import serve
23 def autoinit_repos(config):
24 try:
25 do_init = config.getboolean('gitosis', 'init-on-config')
26 except (NoSectionError, NoOptionError):
27 do_init = False
29 if not do_init:
30 return
32 for (section, name, topdir, subpath) in gitweb.enum_cfg_repos(config):
33 if os.path.exists(os.path.join(topdir,subpath)):
34 continue
36 try:
37 serve.auto_init_repo(config,topdir,subpath)
38 except GitInitError, e:
39 log.warning('Auto-init failed: %r' % e)
40 except GitError, e:
41 log.warning('Git error in init: %r' % e)
44 def post_update(cfg, git_dir):
45 export = os.path.join(git_dir, 'gitosis-export')
46 try:
47 shutil.rmtree(export)
48 except OSError, e:
49 if e.errno == errno.ENOENT:
50 pass
51 else:
52 raise
53 repository.export(git_dir=git_dir, path=export)
54 os.rename(
55 os.path.join(export, 'gitosis.conf'),
56 os.path.join(export, '..', 'gitosis.conf'),
58 # re-read config to get up-to-date settings
59 cfg.read(os.path.join(export, '..', 'gitosis.conf'))
60 autoinit_repos(config=cfg)
61 gitweb.set_descriptions(
62 config=cfg,
64 generated = util.getGeneratedFilesDir(config=cfg)
65 gitweb.generate_project_list(
66 config=cfg,
67 path=os.path.join(generated, 'projects.list'),
69 gitdaemon.set_export_ok(
70 config=cfg,
72 if htaccess.gen_htaccess_if_enabled(config=cfg):
73 group.generate_group_list(
74 config=cfg,
75 path=os.path.join(generated, 'groups'),
77 authorized_keys = util.getSSHAuthorizedKeysPath(config=cfg)
78 ssh.writeAuthorizedKeys(
79 path=authorized_keys,
80 keydir=os.path.join(export, 'keydir'),
83 class Main(app.App):
84 def create_parser(self):
85 parser = super(Main, self).create_parser()
86 parser.set_usage('%prog [OPTS] HOOK')
87 parser.set_description(
88 'Perform gitosis actions for a git hook')
89 return parser
91 def handle_args(self, parser, cfg, options, args):
92 try:
93 (hook,) = args
94 except ValueError:
95 parser.error('Missing argument HOOK.')
97 log = logging.getLogger('gitosis.run_hook')
98 os.umask(0022)
100 git_dir = os.environ.get('GIT_DIR')
101 if git_dir is None:
102 log.error('Must have GIT_DIR set in enviroment')
103 sys.exit(1)
105 if hook == 'post-update':
106 log.info('Running hook %s', hook)
107 post_update(cfg, git_dir)
108 log.info('Done.')
109 else:
110 log.warning('Ignoring unknown hook: %r', hook)