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,22 +289,23 @@ 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))
@ -326,22 +327,23 @@ if action == 'git-upload-pack' or action == 'git-receive-pack':
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. '
'Use `git push` to create new repositories.'.format(action))
create_pkgbase(cmdargv[1], user) create_pkgbase(cmdargv[1], user)
elif action == 'restore': elif action == 'restore':
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:
@ -358,7 +360,7 @@ elif action == 'restore':
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:
@ -366,7 +368,7 @@ elif action == 'adopt':
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:
@ -374,14 +376,14 @@ elif action == 'disown':
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.",
@ -395,5 +397,9 @@ elif action == 'help':
"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()