]> arthur.barton.de Git - bup.git/blob - lib/bup/cmd/mux.py
Remove Client __del__ in favor of context management
[bup.git] / lib / bup / cmd / mux.py
1
2 from __future__ import absolute_import
3 import os, subprocess, sys
4
5 from bup import options
6 from bup.helpers import debug1, debug2, mux
7 from bup.io import byte_stream
8
9
10 optspec = """
11 bup mux command [arguments...]
12 --
13 """
14
15 def main(argv):
16     # Give the subcommand exclusive access to stdin.
17     orig_stdin = os.dup(0)
18     devnull = os.open(os.devnull, os.O_RDONLY)
19     os.dup2(devnull, 0)
20     os.close(devnull)
21
22     o = options.Options(optspec)
23     opt, flags, extra = o.parse_bytes(argv[1:])
24     if len(extra) < 1:
25         o.fatal('command is required')
26
27     subcmd = extra
28
29     debug2('bup mux: starting %r\n' % (extra,))
30
31     outr, outw = os.pipe()
32     errr, errw = os.pipe()
33     def close_fds():
34         os.close(outr)
35         os.close(errr)
36
37     p = subprocess.Popen(subcmd, stdin=orig_stdin, stdout=outw, stderr=errw,
38                          close_fds=False, preexec_fn=close_fds)
39     os.close(outw)
40     os.close(errw)
41     sys.stdout.flush()
42     out = byte_stream(sys.stdout)
43     out.write(b'BUPMUX')
44     out.flush()
45     mux(p, out.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)