Added script for creating new repositories
[invirt/packages/invirt-dev.git] / invirt-add-repo
diff --git a/invirt-add-repo b/invirt-add-repo
new file mode 100755 (executable)
index 0000000..bb48b49
--- /dev/null
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+import optparse
+import os
+import shutil
+import sys
+
+from invirt import builder, common
+from invirt.config import structs as config
+
+REPO_BASE = '/srv/git/invirt'
+HOOK_BASE = '/usr/share/invirt-dev/git-hooks'
+
+def main():
+    parser = optparse.OptionParser('%prog repo <category> <name>')
+    opts, args = parser.parse_args()
+    
+    if len(args) != 3:
+        parser.print_help()
+        return 1
+
+    category = args[1]
+    name = args[2]
+    principal = os.environ['REMOTE_USER']
+    repo_path = os.path.join(REPO_BASE, category, '%s.git' % name)
+
+    if os.path.exists(repo_path):
+        print >>sys.stderr, '%s already exists!' % repo_path
+        return 1
+
+    print 'Creating new repo at %s' % repo_path
+    os.makedirs(repo_path)
+    common.captureOutput(['git', 'init', '--bare'], cwd=repo_path)
+    print 'Replacing hooks directory with a symlink'
+    hooks_dir = os.path.join(repo_path, 'hooks')
+    shutil.rmtree(hooks_dir)
+    common.captureOutput(['chown', '-R', 'git:', repo_path])
+    if category == 'packages':
+        os.symlink(os.path.join(HOOK_BASE, 'sub'), hooks_dir)
+    else:
+        os.symlink(os.path.join(HOOK_BASE, 'other'), hooks_dir)
+    
+    builder.runHook('post-add-repo', [category, name, principal])
+
+if __name__ == '__main__':
+    sys.exit(main())