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