diff --git a/contrib/adc/perms b/contrib/adc/perms new file mode 100755 index 0000000..4e33dfe --- /dev/null +++ b/contrib/adc/perms @@ -0,0 +1,115 @@ +#!/usr/bin/env python +# +# Original author: Richard Bateman +# +# Any questions or concerns about how this works should be addressed to +# me, not to sitaram. Please note that neither I nor sitaram make any +# guarantees about the security or usefulness of this script. It may +# be used without warantee or any guarantee of any kind. +# +# That said, it works fine for me. +# +# This script is licensed under the New BSD license +# Copyright 2011 Richard Bateman +# + +import sys, os +from pygitolite import * + +def list(gl, user, repo, filter_var = ""): + perms = gl.get_perms(repo, user) + for var, ppl in perms.iteritems(): + if filter_var == "" or filter_var == var: + print "%s:" % var + for item in ppl: + print " %s" % item + +def clear(gl, user, repo, filter_var = ""): + try: + os.system(r"echo Are you sure? Type YES \(all caps\) to continue: ") + bval = raw_input() + if bval != "YES": + print "Canceling..." + + if filter_var == "": + gl.set_perms(repo, user, {}) + else: + perms = gl.get_perms(repo, user) + if filter_var in perms: + del perms[filter_var] + gl.set_perms(repo, user, perms) + print "Perms after clear:" + list(gl, user, repo) + except: + print "An error occured" + +def add(gl, user, repo, var, *users): + perms = gl.get_perms(repo, user) + if var not in perms: + perms[var] = [] + if len(users) == 0: + print "Usage: perms add %s %s " % (repo, var) + return + for cur in users: + if cur not in perms[var]: + perms[var].append(cur) + gl.set_perms(repo, user, perms) + list(gl, user, repo, var) + +def set(gl, user, repo, var, *users): + perms = gl.get_perms(repo, user) + perms[var] = [] + if len(users) == 0: + print "Usage: perms set %s %s " % (repo, var) + return + for cur in users: + if cur not in perms[var]: + perms[var].append(cur) + gl.set_perms(repo, user, perms) + list(gl, user, repo, var) + +def remove(gl, user, repo, var, *users): + perms = gl.get_perms(repo, user) + if var not in perms: + print "%s isn't a valid type" % var + return + if len(users) == 0: + print "No users specified to remove; perhaps you want clear?" + return + for cur in users: + if cur in perms[var]: + perms[var].remove(cur) + gl.set_perms(repo, user, perms) + list(gl, user, repo, var) + +commands = { + "list": list, + "clear": clear, + "add": add, + "set": set, + "remove": remove, + } + +if __name__ == "__main__": + if "GL_USER" not in os.environ: + raise "No user!" + user = os.environ["GL_USER"] + command = sys.argv[1] if len(sys.argv) > 2 else "" + if len(sys.argv) < 3 or command not in commands: + print "Usage: perms " + print " list [TYPE]" + print " clear " + print " add [user and group list]" + print " set [user and group list]" + print " remove [user and group list]" + sys.exit(1) + repo = sys.argv[2] + + gl = gitolite() + rights, owner = gl.get_rights_and_owner(repo, user) + + if owner != user: + print "Either %s does not exist or you are not the owner." % repo + sys.exit(1) + + commands[command](gl, user, repo, *sys.argv[3:]) diff --git a/contrib/adc/pygitolite.py b/contrib/adc/pygitolite.py new file mode 100644 index 0000000..aa250fb --- /dev/null +++ b/contrib/adc/pygitolite.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# +# Original author: Richard Bateman +# +# Any questions or concerns about how this works should be addressed to +# me, not to sitaram. Please note that neither I nor sitaram make any +# guarantees about the security or usefulness of this script. It may +# be used without warantee or any guarantee of any kind. +# +# This script is licensed under the New BSD license +# Copyright 2011 Richard Bateman +# + +import sys, os, subprocess + +class gitolite(object): + def __init__(self, **kvargs): + self.GL_BINDIR = kvargs["GL_BINDIR"] if "GL_BINDIR" in kvargs else os.environ["GL_BINDIR"] + self.user = kvargs["GL_USER"] if "GL_USER" in kvargs else os.environ["GL_USER"] + pass + + def gitolite_execute(self, command, std_inputdata = None): + cmd = "perl -I%s -Mgitolite -e '%s'" % (self.GL_BINDIR,command) + p = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE, stdin = subprocess.PIPE) + stdout, stderr = p.communicate(std_inputdata) + if p.returncode is not 0: + raise Exception(stderr) + return stdout.strip() + + def run_custom_command(self, repo, user, command, extra = None): + os.environ["SSH_ORIGINAL_COMMAND"] = "%s %s" % (command, repo) + return self.gitolite_execute('run_custom_command("%s")' % user, extra) + + def get_perms(self, repo, user): + full = self.run_custom_command(repo, user, "getperms") + plist = full.split("\n") + perms = {} + for line in plist: + if line == "": + continue + var, strlist = line.split(" ", 1) + perms[var] = strlist.split(" ") + + return perms + + def set_perms(self, repo, user, perms): + permstr = "" + for var, curlist in perms.iteritems(): + if len(curlist) == 0: + continue; + varstr = var + for cur in curlist: + varstr += " %s" % cur + permstr = permstr + "\n" + varstr + resp = self.run_custom_command(repo, user, "setperms", permstr.strip()) + + def valid_owned_repo(self, repo, user): + rights, user = self.get_rights_and_owner(repo, user) + return owner == user + + def get_rights_and_owner(self, repo, user): + if not repo.endswith(".git"): + repo = "%s.git" % repo + ans = self.gitolite_execute('cli_repo_rights("%s")' % repo) + perms, owner = ans.split(" ") + rights = {"Read": "R" in perms, "Write": "W" in perms, "Create": "C" in perms} + return rights, owner + +if __name__ == "__main__": + if "GL_USER" not in os.environ: + raise "No user!" + user = os.environ["GL_USER"] + repo = sys.argv[1] + + gl = gitolite() + print gl.get_rights_and_owner(repo, user) + print gl.get_perms(repo, user)