From f4b42e677c64c443c14e8671db51fe58e6494588 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Guido=20G=C3=BCnther?= Date: Mon, 19 Mar 2012 14:01:43 +0100 Subject: [PATCH] gbp-create-remote-repo: make command and script generation testable --- gbp/scripts/create_remote_repo.py | 65 +++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/gbp/scripts/create_remote_repo.py b/gbp/scripts/create_remote_repo.py index 2a2e276..e483bf3 100644 --- a/gbp/scripts/create_remote_repo.py +++ b/gbp/scripts/create_remote_repo.py @@ -125,6 +125,47 @@ def parse_url(remote_url, name, pkg): return remote +def build_remote_script(remote): + """ + Create the script that will be run on the remote side + + >>> build_remote_script({'base': 'base', 'dir': 'dir', 'pkg': 'pkg'}) + '\\nset -e\\numask 002\\nif [ -d base"dir" ]; then\\n echo "Repository at "basedir" already exists - giving up."\\n exit 1\\nfi\\nmkdir -p base"dir"\\ncd base"dir"\\ngit init --bare --shared\\necho "pkg packaging" > description\\n' + """ + remote_script = """ +set -e +umask 002 +if [ -d %(base)s"%(dir)s" ]; then + echo "Repository at \"%(base)s%(dir)s\" already exists - giving up." + exit 1 +fi +mkdir -p %(base)s"%(dir)s" +cd %(base)s"%(dir)s" +git init --bare --shared +echo "%(pkg)s packaging" > description +""" % remote + return remote_script + + +def build_cmd(remote): + """ + Build the command we pass the script to + + >>> build_cmd({'scheme': ''}) + ['sh'] + >>> build_cmd({'scheme': 'ssh', 'host': 'host', 'port': 80}) + ['ssh', '-p', 80, 'host', 'sh'] + """ + cmd = [] + if remote["scheme"]: + cmd.append('ssh') + if remote["port"]: + cmd.extend(['-p', remote['port']]) + cmd.append(remote["host"]) + cmd.append('sh') + return cmd + + def read_yn(): fd = sys.stdin.fileno() try: @@ -216,29 +257,13 @@ def main(argv): if not read_yn(): raise GbpError, "Aborted." - # Create and run the remote script - remote_script = """ -set -e -umask 002 -if [ -d %(base)s"%(dir)s" ]; then - echo "Repository at \"%(base)s%(dir)s\" already exists - giving up." - exit 1 -fi -mkdir -p %(base)s"%(dir)s" -cd %(base)s"%(dir)s" -git init --bare --shared -echo "%(pkg)s packaging" > description -""" % remote - + remote_script = build_remote_script(remote) if options.verbose: print remote_script - if remote["scheme"]: - cmd = ["ssh"] - if remote["port"]: - cmd.extend(["-p", remote["port"]]) - cmd.append(remote["host"]) - cmd.append("sh") + cmd = build_cmd(remote) + if options.verbose: + print cmd proc = subprocess.Popen(cmd, stdin=subprocess.PIPE) proc.communicate(remote_script) -- 2.7.4