]> arthur.barton.de Git - bup.git/commitdiff
on: convert to internal command
authorRob Browning <rlb@defaultvalue.org>
Fri, 12 Feb 2021 20:40:46 +0000 (14:40 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sat, 6 Mar 2021 18:29:39 +0000 (12:29 -0600)
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/cmd/on.py
lib/cmd/bup

index 960d06949c17ef31f71160f84541d2d4c46e2c16..bb5e3f3117a41eded776545426b66672bc63dd42 100755 (executable)
@@ -1,29 +1,8 @@
-#!/bin/sh
-"""": # -*-python-*-
-# https://sourceware.org/bugzilla/show_bug.cgi?id=26034
-export "BUP_ARGV_0"="$0"
-arg_i=1
-for arg in "$@"; do
-    export "BUP_ARGV_${arg_i}"="$arg"
-    shift
-    arg_i=$((arg_i + 1))
-done
-# Here to end of preamble replaced during install
-bup_python="$(dirname "$0")/../../../config/bin/python" || exit $?
-exec "$bup_python" "$0"
-"""
-# end of bup preamble
-
 from __future__ import absolute_import
-
-# Intentionally replace the dirname "$0" that python prepends
-import os, sys
-sys.path[0] = os.path.dirname(os.path.realpath(__file__)) + '/../..'
-
 from subprocess import PIPE
-import getopt, signal, struct, subprocess
+import getopt, os, signal, struct, subprocess, sys
 
-from bup import compat, options, ssh, path
+from bup import options, ssh, path
 from bup.compat import argv_bytes
 from bup.helpers import DemuxConn, log
 from bup.io import byte_stream
@@ -35,65 +14,67 @@ bup on <hostname> save ...
 bup on <hostname> split ...
 bup on <hostname> get ...
 """
-o = options.Options(optspec, optfunc=getopt.getopt)
-opt, flags, extra = o.parse(compat.argv[1:])
-if len(extra) < 2:
-    o.fatal('arguments expected')
 
-class SigException(Exception):
-    def __init__(self, signum):
-        self.signum = signum
-        Exception.__init__(self, 'signal %d received' % signum)
-def handler(signum, frame):
-    raise SigException(signum)
+def main(argv):
+    o = options.Options(optspec, optfunc=getopt.getopt)
+    opt, flags, extra = o.parse_bytes(argv[1:])
+    if len(extra) < 2:
+        o.fatal('arguments expected')
 
-signal.signal(signal.SIGTERM, handler)
-signal.signal(signal.SIGINT, handler)
+    class SigException(Exception):
+        def __init__(self, signum):
+            self.signum = signum
+            Exception.__init__(self, 'signal %d received' % signum)
+    def handler(signum, frame):
+        raise SigException(signum)
 
-sys.stdout.flush()
-out = byte_stream(sys.stdout)
+    signal.signal(signal.SIGTERM, handler)
+    signal.signal(signal.SIGINT, handler)
 
-try:
-    sp = None
-    p = None
-    ret = 99
-
-    hp = argv_bytes(extra[0]).split(b':')
-    if len(hp) == 1:
-        (hostname, port) = (hp[0], None)
-    else:
-        (hostname, port) = hp
-    argv = [argv_bytes(x) for x in extra[1:]]
-    p = ssh.connect(hostname, port, b'on--server', stderr=PIPE)
+    sys.stdout.flush()
+    out = byte_stream(sys.stdout)
 
     try:
-        argvs = b'\0'.join([b'bup'] + argv)
-        p.stdin.write(struct.pack('!I', len(argvs)) + argvs)
-        p.stdin.flush()
-        sp = subprocess.Popen([path.exe(), b'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, "wb"))
-        for line in iter(dmc.readline, b''):
-            out.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:
+        sp = None
+        p = None
         ret = 99
-    log('\nbup on: %s\n' % e)
 
-sys.exit(ret)
+        hp = argv_bytes(extra[0]).split(b':')
+        if len(hp) == 1:
+            (hostname, port) = (hp[0], None)
+        else:
+            (hostname, port) = hp
+        argv = [argv_bytes(x) for x in extra[1:]]
+        p = ssh.connect(hostname, port, b'on--server', stderr=PIPE)
+
+        try:
+            argvs = b'\0'.join([b'bup'] + argv)
+            p.stdin.write(struct.pack('!I', len(argvs)) + argvs)
+            p.stdin.flush()
+            sp = subprocess.Popen([path.exe(), b'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, "wb"))
+            for line in iter(dmc.readline, b''):
+                out.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)
index 0fe3e19cf312ef4972be9017af60b5620d8de157..ea021e0a16ecde940e611786dc5911863baac5b9 100755 (executable)
@@ -205,6 +205,7 @@ try:
                            b'meta',
                            b'midx',
                            b'mux',
+                           b'on',
                            b'prune-older',
                            b'random',
                            b'rm',