]> arthur.barton.de Git - bup.git/blob - lib/cmd/mux-cmd.py
Split src tree python use to config/bin/python and dev/bup-python
[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")/../../config/bin/python" || exit $?
13 exec "$bup_python" "$0"
14 """
15 # end of bup preamble
16
17 from __future__ import absolute_import
18 import os, struct, subprocess, sys
19
20 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/..']
21
22 from bup import compat, options
23 from bup.helpers import debug1, debug2, mux
24 from bup.io import byte_stream
25
26 # Give the subcommand exclusive access to stdin.
27 orig_stdin = os.dup(0)
28 devnull = os.open(os.devnull, os.O_RDONLY)
29 os.dup2(devnull, 0)
30 os.close(devnull)
31
32 optspec = """
33 bup mux command [arguments...]
34 --
35 """
36 o = options.Options(optspec)
37 opt, flags, extra = o.parse(compat.argv[1:])
38 if len(extra) < 1:
39     o.fatal('command is required')
40
41 subcmd = extra
42
43 debug2('bup mux: starting %r\n' % (extra,))
44
45 outr, outw = os.pipe()
46 errr, errw = os.pipe()
47 def close_fds():
48     os.close(outr)
49     os.close(errr)
50
51 p = subprocess.Popen(subcmd, stdin=orig_stdin, stdout=outw, stderr=errw,
52                      close_fds=False, preexec_fn=close_fds)
53 os.close(outw)
54 os.close(errw)
55 sys.stdout.flush()
56 out = byte_stream(sys.stdout)
57 out.write(b'BUPMUX')
58 out.flush()
59 mux(p, out.fileno(), outr, errr)
60 os.close(outr)
61 os.close(errr)
62 prv = p.wait()
63
64 if prv:
65     debug1('%s exited with code %d\n' % (extra[0], prv))
66
67 debug1('bup mux: done\n')
68
69 sys.exit(prv)