]> arthur.barton.de Git - bup.git/blob - lib/cmd/mux-cmd.py
Bypass Python 3 glibc argv problems by routing args through env
[bup.git] / lib / cmd / mux-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 # https://sourceware.org/bugzilla/show_bug.cgi?id=26034
4 export "BUP_ARGV_0"="$0"
5 arg_i=1
6 for arg in "$@"; do
7     export "BUP_ARGV_${arg_i}"="$arg"
8     shift
9     arg_i=$((arg_i + 1))
10 done
11 # Here to end of preamble replaced during install
12 bup_python="$(dirname "$0")/bup-python" || exit $?
13 exec "$bup_python" "$0"
14 """
15 # end of bup preamble
16
17 from __future__ import absolute_import
18 import os, sys, subprocess, struct
19
20 from bup import compat, options
21 from bup.helpers import debug1, debug2, mux
22 from bup.io import byte_stream
23
24 # Give the subcommand exclusive access to stdin.
25 orig_stdin = os.dup(0)
26 devnull = os.open(os.devnull, os.O_RDONLY)
27 os.dup2(devnull, 0)
28 os.close(devnull)
29
30 optspec = """
31 bup mux command [arguments...]
32 --
33 """
34 o = options.Options(optspec)
35 opt, flags, extra = o.parse(compat.argv[1:])
36 if len(extra) < 1:
37     o.fatal('command is required')
38
39 subcmd = extra
40
41 debug2('bup mux: starting %r\n' % (extra,))
42
43 outr, outw = os.pipe()
44 errr, errw = os.pipe()
45 def close_fds():
46     os.close(outr)
47     os.close(errr)
48
49 p = subprocess.Popen(subcmd, stdin=orig_stdin, stdout=outw, stderr=errw,
50                      close_fds=False, preexec_fn=close_fds)
51 os.close(outw)
52 os.close(errw)
53 sys.stdout.flush()
54 out = byte_stream(sys.stdout)
55 out.write(b'BUPMUX')
56 out.flush()
57 mux(p, out.fileno(), outr, errr)
58 os.close(outr)
59 os.close(errr)
60 prv = p.wait()
61
62 if prv:
63     debug1('%s exited with code %d\n' % (extra[0], prv))
64
65 debug1('bup mux: done\n')
66
67 sys.exit(prv)