1#!/usr/bin/env python
2#
3# Original author: Richard Bateman <taxilian@gmail.com>
4#
5# Any questions or concerns about how this works should be addressed to
6# me, not to sitaram. Please note that neither I nor sitaram make any
7# guarantees about the security or usefulness of this script. It may
8# be used without warantee or any guarantee of any kind.
9#
10# That said, it works fine for me.
11#
12# This script is licensed under the New BSD license
13# Copyright 2011 Richard Bateman
14#
15
16import sys, os
17from pygitolite import *
18
19def list(gl, user, repo, filter_var = ""):
20    perms = gl.get_perms(repo, user)
21    for var, ppl in perms.iteritems():
22        if filter_var == "" or filter_var == var:
23            print "%s:" % var
24            for item in ppl:
25                print "    %s" % item
26
27def clear(gl, user, repo, filter_var = ""):
28    try:
29        os.system(r"echo Are you sure? Type YES \(all caps\) to continue: ")
30        bval = raw_input()
31        if bval != "YES":
32            print "Canceling..."
33
34        if filter_var == "":
35            gl.set_perms(repo, user, {})
36        else:
37            perms = gl.get_perms(repo, user)
38            if filter_var in perms:
39                del perms[filter_var]
40                gl.set_perms(repo, user, perms)
41        print "Perms after clear:"
42        list(gl, user, repo)
43    except:
44        print "An error occured"
45
46def add(gl, user, repo, var, *users):
47    perms = gl.get_perms(repo, user)
48    if var not in perms:
49        perms[var] = []
50    if len(users) == 0:
51        print "Usage: perms add %s %s <username>" % (repo, var)
52        return
53    for cur in users:
54        if cur not in perms[var]:
55            perms[var].append(cur)
56    gl.set_perms(repo, user, perms)
57    list(gl, user, repo, var)
58
59def set(gl, user, repo, var, *users):
60    perms = gl.get_perms(repo, user)
61    perms[var] = []
62    if len(users) == 0:
63        print "Usage: perms set %s %s <username>" % (repo, var)
64        return
65    for cur in users:
66        if cur not in perms[var]:
67            perms[var].append(cur)
68    gl.set_perms(repo, user, perms)
69    list(gl, user, repo, var)
70
71def remove(gl, user, repo, var, *users):
72    perms = gl.get_perms(repo, user)
73    if var not in perms:
74        print "%s isn't a valid type" % var
75        return
76    if len(users) == 0:
77        print "No users specified to remove; perhaps you want clear?"
78        return
79    for cur in users:
80        if cur in perms[var]:
81            perms[var].remove(cur)
82    gl.set_perms(repo, user, perms)
83    list(gl, user, repo, var)
84
85commands = {
86        "list": list,
87        "clear": clear,
88        "add": add,
89        "set": set,
90        "remove": remove,
91        }
92
93if __name__ == "__main__":
94    if "GL_USER" not in os.environ:
95        raise "No user!"
96    user = os.environ["GL_USER"]
97    command = sys.argv[1] if len(sys.argv) > 2 else ""
98    if len(sys.argv) < 3 or command not in commands:
99        print "Usage: perms <command> <repository> <args>"
100        print "             list <repository> [TYPE]"
101        print "             clear <repository>"
102        print "             add <repository> <TYPE> [user and group list]"
103        print "             set <repository> <TYPE> [user and group list]"
104        print "             remove <repository> <TYPE> [user and group list]"
105        sys.exit(1)
106    repo = sys.argv[2]
107
108    gl = gitolite()
109    rights, owner = gl.get_rights_and_owner(repo, user)
110
111    if owner != user:
112        print "Either %s does not exist or you are not the owner." % repo
113        sys.exit(1)
114
115    commands[command](gl, user, repo, *sys.argv[3:])
116