X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=cmd%2Fon-cmd.py;h=0643ef8002e517e3808e436d32313911868f43d7;hb=c38e02f0f5daa4ddf4d2c0a553d717d1f3ad02e5;hp=cebff1aab52ee13aab85cf86e03b7486fa66ef04;hpb=9bb94e0f684e311f7a8c71a912b6d0e812a04e5f;p=bup.git diff --git a/cmd/on-cmd.py b/cmd/on-cmd.py index cebff1a..0643ef8 100755 --- a/cmd/on-cmd.py +++ b/cmd/on-cmd.py @@ -1,12 +1,23 @@ -#!/usr/bin/env python +#!/bin/sh +"""": # -*-python-*- +bup_python="$(dirname "$0")/bup-python" || exit $? +exec "$bup_python" "$0" ${1+"$@"} +""" +# end of bup preamble + +from __future__ import absolute_import import sys, os, struct, getopt, subprocess, signal + +from subprocess import PIPE from bup import options, ssh, path -from bup.helpers import * +from bup.helpers import DemuxConn, log + optspec = """ bup on index ... bup on save ... bup on split ... +bup on get ... """ o = options.Options(optspec, optfunc=getopt.getopt) (opt, flags, extra) = o.parse(sys.argv[1:]) @@ -23,39 +34,47 @@ def handler(signum, frame): signal.signal(signal.SIGTERM, handler) signal.signal(signal.SIGINT, handler) -sp = None -p = None -ret = 99 - try: + sp = None + p = None + ret = 99 + hp = extra[0].split(':') if len(hp) == 1: (hostname, port) = (hp[0], None) else: (hostname, port) = hp - argv = extra[1:] - p = ssh.connect(hostname, port, 'on--server') - - argvs = '\0'.join(['bup'] + argv) - p.stdin.write(struct.pack('!I', len(argvs)) + argvs) - p.stdin.flush() - - sp = subprocess.Popen([path.exe(), 'server'], - stdin=p.stdout, stdout=p.stdin) - p.stdin.close() - p.stdout.close() - -finally: - while 1: - # if we get a signal while waiting, we have to keep waiting, just - # in case our child doesn't die. - try: - ret = p.wait() - sp.wait() - break - except SigException, e: - log('\nbup on: %s\n' % e) - os.kill(p.pid, e.signum) - ret = 84 + p = ssh.connect(hostname, port, 'on--server', stderr=PIPE) + + try: + argvs = '\0'.join(['bup'] + argv) + p.stdin.write(struct.pack('!I', len(argvs)) + argvs) + p.stdin.flush() + sp = subprocess.Popen([path.exe(), 'server'], + stdin=p.stdout, stdout=p.stdin) + p.stdin.close() + p.stdout.close() + # Demultiplex remote client's stderr (back to stdout/stderr). + dmc = DemuxConn(p.stderr.fileno(), open(os.devnull, "w")) + for line in iter(dmc.readline, ""): + sys.stdout.write(line) + finally: + while 1: + # if we get a signal while waiting, we have to keep waiting, just + # in case our child doesn't die. + try: + ret = p.wait() + if sp: + sp.wait() + break + except SigException as e: + log('\nbup on: %s\n' % e) + os.kill(p.pid, e.signum) + ret = 84 +except SigException as e: + if ret == 0: + ret = 99 + log('\nbup on: %s\n' % e) + sys.exit(ret)