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'], cwd=repo_path) |
---|
34 | print 'Replacing hooks directory with a symlink' |
---|
35 | hooks_dir = os.path.join(repo_path, 'hooks') |
---|
36 | shutil.rmtree(hooks_dir) |
---|
37 | common.captureOutput(['chown', '-R', 'git:', repo_path]) |
---|
38 | if category == 'packages': |
---|
39 | os.symlink(os.path.join(HOOK_BASE, 'sub'), hooks_dir) |
---|
40 | else: |
---|
41 | os.symlink(os.path.join(HOOK_BASE, 'other'), hooks_dir) |
---|
42 | |
---|
43 | builder.runHook('post-add-repo', [category, name, principal]) |
---|
44 | |
---|
45 | if __name__ == '__main__': |
---|
46 | sys.exit(main()) |
---|