git-serve: Move entry point to a main() method

Move the main program logic of git-serve to a main() method such that it
can be used as a module and easily be invoked by setuptools wrapper
scripts.

Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
This commit is contained in:
Lukas Fleischer 2016-09-20 08:53:41 +02:00
parent b8318d2587
commit 8468b6be4b

View file

@ -289,111 +289,117 @@ def usage(cmds):
exit(0) exit(0)
user = os.environ.get('AUR_USER') def main():
privileged = (os.environ.get('AUR_PRIVILEGED', '0') == '1') user = os.environ.get('AUR_USER')
ssh_cmd = os.environ.get('SSH_ORIGINAL_COMMAND') privileged = (os.environ.get('AUR_PRIVILEGED', '0') == '1')
ssh_client = os.environ.get('SSH_CLIENT') ssh_cmd = os.environ.get('SSH_ORIGINAL_COMMAND')
ssh_client = os.environ.get('SSH_CLIENT')
if not ssh_cmd: if not ssh_cmd:
die_with_help("Interactive shell is disabled.") die_with_help("Interactive shell is disabled.")
cmdargv = shlex.split(ssh_cmd) cmdargv = shlex.split(ssh_cmd)
action = cmdargv[0] action = cmdargv[0]
remote_addr = ssh_client.split(' ')[0] if ssh_client else None remote_addr = ssh_client.split(' ')[0] if ssh_client else None
if enable_maintenance: if enable_maintenance:
if remote_addr not in maintenance_exc: if remote_addr not in maintenance_exc:
die("The AUR is down due to maintenance. We will be back soon.") die("The AUR is down due to maintenance. We will be back soon.")
if action == 'git-upload-pack' or action == 'git-receive-pack': if action == 'git-upload-pack' or action == 'git-receive-pack':
if len(cmdargv) < 2: if len(cmdargv) < 2:
die_with_help("{:s}: missing path".format(action)) die_with_help("{:s}: missing path".format(action))
path = cmdargv[1].rstrip('/') path = cmdargv[1].rstrip('/')
if not path.startswith('/'): if not path.startswith('/'):
path = '/' + path path = '/' + path
if not path.endswith('.git'): if not path.endswith('.git'):
path = path + '.git' path = path + '.git'
pkgbase = path[1:-4] pkgbase = path[1:-4]
if not re.match(repo_regex, pkgbase): if not re.match(repo_regex, pkgbase):
die('{:s}: invalid repository name: {:s}'.format(action, pkgbase)) die('{:s}: invalid repository name: {:s}'.format(action, pkgbase))
if action == 'git-receive-pack' and pkgbase_exists(pkgbase): if action == 'git-receive-pack' and pkgbase_exists(pkgbase):
if not privileged and not pkgbase_has_write_access(pkgbase, user): if not privileged and not pkgbase_has_write_access(pkgbase, user):
die('{:s}: permission denied: {:s}'.format(action, user)) die('{:s}: permission denied: {:s}'.format(action, user))
os.environ["AUR_USER"] = user os.environ["AUR_USER"] = user
os.environ["AUR_PKGBASE"] = pkgbase os.environ["AUR_PKGBASE"] = pkgbase
os.environ["GIT_NAMESPACE"] = pkgbase os.environ["GIT_NAMESPACE"] = pkgbase
cmd = action + " '" + repo_path + "'" cmd = action + " '" + repo_path + "'"
os.execl(git_shell_cmd, git_shell_cmd, '-c', cmd) os.execl(git_shell_cmd, git_shell_cmd, '-c', cmd)
elif action == 'set-keywords': elif action == 'set-keywords':
if len(cmdargv) < 2: if len(cmdargv) < 2:
die_with_help("{:s}: missing repository name".format(action)) die_with_help("{:s}: missing repository name".format(action))
pkgbase_set_keywords(cmdargv[1], cmdargv[2:]) pkgbase_set_keywords(cmdargv[1], cmdargv[2:])
elif action == 'list-repos': elif action == 'list-repos':
if len(cmdargv) > 1: if len(cmdargv) > 1:
die_with_help("{:s}: too many arguments".format(action)) die_with_help("{:s}: too many arguments".format(action))
list_repos(user) list_repos(user)
elif action == 'setup-repo': elif action == 'setup-repo':
if len(cmdargv) < 2: if len(cmdargv) < 2:
die_with_help("{:s}: missing repository name".format(action)) die_with_help("{:s}: missing repository name".format(action))
if len(cmdargv) > 2: if len(cmdargv) > 2:
die_with_help("{:s}: too many arguments".format(action)) die_with_help("{:s}: too many arguments".format(action))
warn('{:s} is deprecated. Use `git push` to create new repositories.'.format(action)) warn('{:s} is deprecated. '
create_pkgbase(cmdargv[1], user) 'Use `git push` to create new repositories.'.format(action))
elif action == 'restore': create_pkgbase(cmdargv[1], user)
if len(cmdargv) < 2: elif action == 'restore':
die_with_help("{:s}: missing repository name".format(action)) if len(cmdargv) < 2:
if len(cmdargv) > 2: die_with_help("{:s}: missing repository name".format(action))
die_with_help("{:s}: too many arguments".format(action)) if len(cmdargv) > 2:
die_with_help("{:s}: too many arguments".format(action))
pkgbase = cmdargv[1] pkgbase = cmdargv[1]
if not re.match(repo_regex, pkgbase): if not re.match(repo_regex, pkgbase):
die('{:s}: invalid repository name: {:s}'.format(action, pkgbase)) die('{:s}: invalid repository name: {:s}'.format(action, pkgbase))
if pkgbase_exists(pkgbase): if pkgbase_exists(pkgbase):
die('{:s}: package base exists: {:s}'.format(action, pkgbase)) die('{:s}: package base exists: {:s}'.format(action, pkgbase))
create_pkgbase(pkgbase, user) create_pkgbase(pkgbase, user)
os.environ["AUR_USER"] = user os.environ["AUR_USER"] = user
os.environ["AUR_PKGBASE"] = pkgbase os.environ["AUR_PKGBASE"] = pkgbase
os.execl(git_update_cmd, git_update_cmd, 'restore') os.execl(git_update_cmd, git_update_cmd, 'restore')
elif action == 'adopt': elif action == 'adopt':
if len(cmdargv) < 2: if len(cmdargv) < 2:
die_with_help("{:s}: missing repository name".format(action)) die_with_help("{:s}: missing repository name".format(action))
if len(cmdargv) > 2: if len(cmdargv) > 2:
die_with_help("{:s}: too many arguments".format(action)) die_with_help("{:s}: too many arguments".format(action))
pkgbase = cmdargv[1] pkgbase = cmdargv[1]
pkgbase_adopt(pkgbase, user, privileged) pkgbase_adopt(pkgbase, user, privileged)
elif action == 'disown': elif action == 'disown':
if len(cmdargv) < 2: if len(cmdargv) < 2:
die_with_help("{:s}: missing repository name".format(action)) die_with_help("{:s}: missing repository name".format(action))
if len(cmdargv) > 2: if len(cmdargv) > 2:
die_with_help("{:s}: too many arguments".format(action)) die_with_help("{:s}: too many arguments".format(action))
pkgbase = cmdargv[1] pkgbase = cmdargv[1]
pkgbase_disown(pkgbase, user, privileged) pkgbase_disown(pkgbase, user, privileged)
elif action == 'set-comaintainers': elif action == 'set-comaintainers':
if len(cmdargv) < 2: if len(cmdargv) < 2:
die_with_help("{:s}: missing repository name".format(action)) die_with_help("{:s}: missing repository name".format(action))
pkgbase = cmdargv[1] pkgbase = cmdargv[1]
userlist = cmdargv[2:] userlist = cmdargv[2:]
pkgbase_set_comaintainers(pkgbase, userlist, user, privileged) pkgbase_set_comaintainers(pkgbase, userlist, user, privileged)
elif action == 'help': elif action == 'help':
cmds = { cmds = {
"adopt <name>": "Adopt a package base.", "adopt <name>": "Adopt a package base.",
"disown <name>": "Disown a package base.", "disown <name>": "Disown a package base.",
"help": "Show this help message and exit.", "help": "Show this help message and exit.",
"list-repos": "List all your repositories.", "list-repos": "List all your repositories.",
"restore <name>": "Restore a deleted package base.", "restore <name>": "Restore a deleted package base.",
"set-comaintainers <name> [...]": "Set package base co-maintainers.", "set-comaintainers <name> [...]": "Set package base co-maintainers.",
"set-keywords <name> [...]": "Change package base keywords.", "set-keywords <name> [...]": "Change package base keywords.",
"setup-repo <name>": "Create a repository (deprecated).", "setup-repo <name>": "Create a repository (deprecated).",
"git-receive-pack": "Internal command used with Git.", "git-receive-pack": "Internal command used with Git.",
"git-upload-pack": "Internal command used with Git.", "git-upload-pack": "Internal command used with Git.",
} }
usage(cmds) usage(cmds)
else: else:
die_with_help("invalid command: {:s}".format(action)) die_with_help("invalid command: {:s}".format(action))
if __name__ == '__main__':
main()