]> arthur.barton.de Git - bup.git/blob - cmd/mux-cmd.py
7a8a9eac1a9ea950abf350e87b32b0de86363346
[bup.git] / cmd / mux-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6 # end of bup preamble
7 import os, sys, subprocess, struct
8 from bup import options
9 from bup.helpers import *
10
11 # Give the subcommand exclusive access to stdin.
12 orig_stdin = os.dup(0)
13 devnull = os.open('/dev/null', os.O_RDONLY)
14 os.dup2(devnull, 0)
15 os.close(devnull)
16
17 optspec = """
18 bup mux command [arguments...]
19 --
20 """
21 o = options.Options(optspec)
22 (opt, flags, extra) = o.parse(sys.argv[1:])
23 if len(extra) < 1:
24     o.fatal('command is required')
25
26 subcmd = extra
27
28 debug2('bup mux: starting %r\n' % (extra,))
29
30 outr, outw = os.pipe()
31 errr, errw = os.pipe()
32 def close_fds():
33     os.close(outr)
34     os.close(errr)
35
36 p = subprocess.Popen(subcmd, stdin=orig_stdin, stdout=outw, stderr=errw,
37                      preexec_fn=close_fds)
38 os.close(outw)
39 os.close(errw)
40 sys.stdout.write('BUPMUX')
41 sys.stdout.flush()
42 mux(p, sys.stdout.fileno(), outr, errr)
43 os.close(outr)
44 os.close(errr)
45 prv = p.wait()
46
47 if prv:
48     debug1('%s exited with code %d\n' % (extra[0], prv))
49
50 debug1('bup mux: done\n')
51
52 sys.exit(prv)