Use shared git repositories
[invirt/packages/invirt-dev.git] / invirt-add-repo
1 #!/usr/bin/env python
2
3 import optparse
4 import os
5 import shutil
6 import sys
7
8 from invirt import builder, common
9 from invirt.config import structs as config
10
11 REPO_BASE = '/srv/git/invirt'
12 HOOK_BASE = '/usr/share/invirt-dev/git-hooks'
13
14 def main():
15     parser = optparse.OptionParser('%prog repo <category> <name>')
16     opts, args = parser.parse_args()
17     
18     if len(args) != 3:
19         parser.print_help()
20         return 1
21
22     category = args[1]
23     name = args[2]
24     principal = os.environ['REMOTE_USER']
25     repo_path = os.path.join(REPO_BASE, category, '%s.git' % name)
26
27     if os.path.exists(repo_path):
28         print >>sys.stderr, '%s already exists!' % repo_path
29         return 1
30
31     print 'Creating new repo at %s' % repo_path
32     os.makedirs(repo_path)
33     common.captureOutput(['git', 'init', '--bare', '--shared=group'], cwd=repo_path)
34     common.captureOutput(['git', 'config', 'receive.denyNonFastForwards', 'false'], cwd=repo_path)
35     common.captureOutput(['chgrp', 'repo', '-R', '.'], cwd=repo_path)
36     print 'Replacing hooks directory with a symlink'
37     hooks_dir = os.path.join(repo_path, 'hooks')
38     shutil.rmtree(hooks_dir)
39     common.captureOutput(['chown', '-R', 'git:', repo_path])
40     if category == 'packages':
41         os.symlink(os.path.join(HOOK_BASE, 'sub'), hooks_dir)
42     else:
43         os.symlink(os.path.join(HOOK_BASE, 'other'), hooks_dir)
44     
45     builder.runHook('post-add-repo', [category, name, principal])
46
47 if __name__ == '__main__':
48     sys.exit(main())