]> arthur.barton.de Git - bup.git/commitdiff
Adjust on-cmd on--server-cmd mux-cmd DemuxConn for python 3
authorRob Browning <rlb@defaultvalue.org>
Wed, 1 Jan 2020 20:44:25 +0000 (14:44 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sun, 2 Feb 2020 19:30:12 +0000 (13:30 -0600)
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
cmd/mux-cmd.py
cmd/on--server-cmd.py
cmd/on-cmd.py
lib/bup/helpers.py

index 1a500b658f6bc86c9dc052c4bf50688bcf4f6a97..f7be4c2f96da49f5b4accf636a84809cd94b67de 100755 (executable)
@@ -10,11 +10,11 @@ import os, sys, subprocess, struct
 
 from bup import options
 from bup.helpers import debug1, debug2, mux
-
+from bup.io import byte_stream
 
 # Give the subcommand exclusive access to stdin.
 orig_stdin = os.dup(0)
-devnull = os.open('/dev/null', os.O_RDONLY)
+devnull = os.open(os.devnull, os.O_RDONLY)
 os.dup2(devnull, 0)
 os.close(devnull)
 
@@ -41,9 +41,11 @@ p = subprocess.Popen(subcmd, stdin=orig_stdin, stdout=outw, stderr=errw,
                      close_fds=False, preexec_fn=close_fds)
 os.close(outw)
 os.close(errw)
-sys.stdout.write('BUPMUX')
 sys.stdout.flush()
-mux(p, sys.stdout.fileno(), outr, errr)
+out = byte_stream(sys.stdout)
+out.write(b'BUPMUX')
+out.flush()
+mux(p, out.fileno(), outr, errr)
 os.close(outr)
 os.close(errr)
 prv = p.wait()
index 64e79d294ad1343eaeb38891668d7081149da3c5..e5b7b19054880be08d59387c783f556a378fa541 100755 (executable)
@@ -9,7 +9,8 @@ from __future__ import absolute_import
 import sys, os, struct
 
 from bup import options, helpers, path
-
+from bup.compat import environ, py_maj
+from bup.io import byte_stream
 
 optspec = """
 bup on--server
@@ -25,15 +26,18 @@ if extra:
 # Normally we could just pass this on the command line, but since we'll often
 # be getting called on the other end of an ssh pipe, which tends to mangle
 # argv (by sending it via the shell), this way is much safer.
-buf = sys.stdin.read(4)
+
+stdin = byte_stream(sys.stdin)
+buf = stdin.read(4)
 sz = struct.unpack('!I', buf)[0]
 assert(sz > 0)
 assert(sz < 1000000)
-buf = sys.stdin.read(sz)
+buf = stdin.read(sz)
 assert(len(buf) == sz)
-argv = buf.split('\0')
+argv = buf.split(b'\0')
 argv[0] = path.exe()
-argv = [argv[0], 'mux', '--'] + argv
+argv = [argv[0], b'mux', b'--'] + argv
+
 
 # stdin/stdout are supposedly connected to 'bup server' that the caller
 # started for us (often on the other end of an ssh tunnel), so we don't want
@@ -52,10 +56,10 @@ argv = [argv[0], 'mux', '--'] + argv
 os.dup2(0, 3)
 os.dup2(1, 4)
 os.dup2(2, 1)
-fd = os.open('/dev/null', os.O_RDONLY)
+fd = os.open(os.devnull, os.O_RDONLY)
 os.dup2(fd, 0)
 os.close(fd)
 
-os.environ['BUP_SERVER_REVERSE'] = helpers.hostname()
+environ[b'BUP_SERVER_REVERSE'] = helpers.hostname()
 os.execvp(argv[0], argv)
 sys.exit(99)
index 0643ef8002e517e3808e436d32313911868f43d7..2c0e9fc808538ba4abb264be964bb3b36e0138c9 100755 (executable)
@@ -6,11 +6,13 @@ exec "$bup_python" "$0" ${1+"$@"}
 # end of bup preamble
 
 from __future__ import absolute_import
+from subprocess import PIPE
 import sys, os, struct, getopt, subprocess, signal
 
-from subprocess import PIPE
 from bup import options, ssh, path
+from bup.compat import argv_bytes
 from bup.helpers import DemuxConn, log
+from bup.io import byte_stream
 
 
 optspec = """
@@ -34,31 +36,34 @@ def handler(signum, frame):
 signal.signal(signal.SIGTERM, handler)
 signal.signal(signal.SIGINT, handler)
 
+sys.stdout.flush()
+out = byte_stream(sys.stdout)
+
 try:
     sp = None
     p = None
     ret = 99
 
-    hp = extra[0].split(':')
+    hp = argv_bytes(extra[0]).split(b':')
     if len(hp) == 1:
         (hostname, port) = (hp[0], None)
     else:
         (hostname, port) = hp
-    argv = extra[1:]
-    p = ssh.connect(hostname, port, 'on--server', stderr=PIPE)
+    argv = [argv_bytes(x) for x in extra[1:]]
+    p = ssh.connect(hostname, port, b'on--server', stderr=PIPE)
 
     try:
-        argvs = '\0'.join(['bup'] + argv)
+        argvs = b'\0'.join([b'bup'] + argv)
         p.stdin.write(struct.pack('!I', len(argvs)) + argvs)
         p.stdin.flush()
-        sp = subprocess.Popen([path.exe(), 'server'],
+        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, "w"))
-        for line in iter(dmc.readline, ""):
-            sys.stdout.write(line)
+        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
index a85c3784232ccae8625d0279751f41cc9f153cf3..38fbc244c1d79e3ee3ee0dee08d60a7200e3187f 100644 (file)
@@ -13,7 +13,7 @@ import hashlib, heapq, math, operator, time, grp, tempfile
 from bup import _helpers
 from bup import compat
 from bup.compat import argv_bytes, byte_int
-from bup.io import path_msg
+from bup.io import byte_stream, path_msg
 # This function should really be in helpers, not in bup.options.  But we
 # want options.py to be standalone so people can include it in other projects.
 from bup.options import _tty_width as tty_width
@@ -574,13 +574,13 @@ class DemuxConn(BaseConn):
         BaseConn.__init__(self, outp)
         # Anything that comes through before the sync string was not
         # multiplexed and can be assumed to be debug/log before mux init.
-        tail = ''
-        while tail != 'BUPMUX':
+        tail = b''
+        while tail != b'BUPMUX':
             b = os.read(infd, (len(tail) < 6) and (6-len(tail)) or 1)
             if not b:
                 raise IOError('demux: unexpected EOF during initialization')
             tail += b
-            sys.stderr.write(tail[:-6])  # pre-mux log messages
+            byte_stream(sys.stderr).write(tail[:-6])  # pre-mux log messages
             tail = tail[-6:]
         self.infd = infd
         self.reader = None
@@ -596,14 +596,14 @@ class DemuxConn(BaseConn):
         rl, wl, xl = select.select([self.infd], [], [], timeout)
         if not rl: return False
         assert(rl[0] == self.infd)
-        ns = ''.join(checked_reader(self.infd, 5))
+        ns = b''.join(checked_reader(self.infd, 5))
         n, fdw = struct.unpack('!IB', ns)
         assert(n <= MAX_PACKET)
         if fdw == 1:
             self.reader = checked_reader(self.infd, n)
         elif fdw == 2:
             for buf in checked_reader(self.infd, n):
-                sys.stderr.write(buf)
+                byte_stream(sys.stderr).write(buf)
         elif fdw == 3:
             self.closed = True
             debug2("DemuxConn: marked closed\n")
@@ -640,10 +640,10 @@ class DemuxConn(BaseConn):
     def _readline(self):
         def find_eol(buf):
             try:
-                return buf.index('\n')+1
+                return buf.index(b'\n')+1
             except ValueError:
                 return None
-        return ''.join(self._read_parts(find_eol))
+        return b''.join(self._read_parts(find_eol))
 
     def _read(self, size):
         csize = [size]
@@ -653,7 +653,7 @@ class DemuxConn(BaseConn):
                 return None
             else:
                 return csize[0]
-        return ''.join(self._read_parts(until_size))
+        return b''.join(self._read_parts(until_size))
 
     def has_input(self):
         return self._load_buf(0)