Multi-User Private Git Server in Python

We are developing our second game and there is a new need. We have a Git server on our hosting, but Git doesn’t manage access control. There are already several solutions, but they aren’t appliable because of hosting’s requirements or they are “too big” with respect to our needs.

So, I was looking for a Python (yep, I’m a Python lover) script which could give me a solution. Surprisedly, I didn’t find it. Anyway, I found a very interesting ruby-based solution, and wrote something similar in Python. I have modified some aspects: I need a multi-repository solution and don’t need to distinguish between read and write access. Please, look at the original source for the theoretic aspects. Here is the code.

#!/usr/bin/env python
import sys, os
user = sys.argv[1]
command = os.environ['SSH_ORIGINAL_COMMAND']
userMap = {'username': ['reponame', 'reponame2'],
           'username2': ['reponame2', 'reponame3']}
if user and command:
  if command.split()[0] in \
        ['git-receive-pack', 'git-upload-pack']:
    if command.split()[1] in \
           for r in userMap[user]]:
      os.system('exec git-shell -c "'+command+'"')
    else: sys.stderr.write("You can't access this repository.")