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