Scott Baker | 92fa104 | 2014-09-26 12:23:27 -0700 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | import base64 |
| 4 | import os |
| 5 | import sys |
| 6 | import subprocess |
| 7 | import StringIO |
| 8 | |
| 9 | if len(sys.argv)<3: |
| 10 | print >> sys.stderr, "syntax: diff-opencloud <localfn> <remotehost:remotefn>" |
| 11 | sys.exit(-1) |
| 12 | |
| 13 | srcfn = sys.argv[1] |
| 14 | dest = sys.argv[2] |
| 15 | |
| 16 | if not ":" in dest: |
| 17 | print >> sys.stderr, "malformed desthost:destfn" |
| 18 | sys.exit(-1) |
| 19 | |
| 20 | (hostname,destfn) = dest.split(":",1) |
| 21 | |
| 22 | if destfn.endswith("/"): |
| 23 | destfn = destfn + os.path.basename(srcfn) |
| 24 | |
| 25 | script = 'echo START; base64 %s' % destfn |
| 26 | |
| 27 | file("/tmp/script","w").write(script) |
| 28 | |
Scott Baker | f8fe45b | 2014-09-26 12:36:39 -0700 | [diff] [blame] | 29 | p = subprocess.Popen(["ssh", "-A", hostname], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 30 | (enctext,stderr) = p.communicate(input=script) |
| 31 | |
| 32 | if stderr: |
| 33 | print >> sys.stderr, stderr |
| 34 | |
| 35 | if "No such file" in stderr: |
| 36 | sys.exit(-1) |
Scott Baker | 92fa104 | 2014-09-26 12:23:27 -0700 | [diff] [blame] | 37 | |
| 38 | enctext = enctext.split("START")[1] |
| 39 | |
| 40 | text = base64.b64decode(enctext) |
| 41 | |
| 42 | file("/tmp/diff-src","w").write(text) |
| 43 | os.system("diff /tmp/diff-src %s" % srcfn) |
| 44 | |
| 45 | """ |
| 46 | SRCPATHNAME=$1 |
| 47 | DESTHOSTNAME=$2 |
| 48 | DESTPATHNAME=$3 |
| 49 | echo "base64 -d -i > $DESTPATHNAME <<EOF" > /tmp/ssh-up |
| 50 | base64 $SRCPATHNAME >> /tmp/ssh-up |
| 51 | echo "EOF" >> /tmp/ssh-up |
| 52 | ssh -A $DESTHOSTNAME < /tmp/ssh-up |
| 53 | """ |