- try:
- c = (do_profile and [sys.executable, '-m', 'cProfile'] or []) + subcmd
- if not n and not outf and not errf:
- # shortcut when no bup-newliner stuff is needed
- os.execvp(c[0], c)
- else:
- p = subprocess.Popen(c, stdout=outf, stderr=errf,
- preexec_fn=force_tty)
- while 1:
- # if we get a signal while waiting, we have to keep waiting, just
- # in case our child doesn't die.
- ret = p.wait()
- forward_signals = False
- break
- except OSError as e:
- log('%s: %s\n' % (subcmd[0], e))
- ret = 98
- finally:
- if p and p.poll() == None:
- os.kill(p.pid, signal.SIGTERM)
- p.wait()
- if n:
- n.stdin.close()
+ fds = tuple([x for x in (src_out, src_err) if x is not None])
+ for fd in fds:
+ flags = fcntl.fcntl(fd, F_GETFL)
+ assert fcntl.fcntl(fd, F_SETFL, flags | os.O_NONBLOCK) == 0
+ while fds:
+ ready_fds, _, _ = select.select(fds, [], [])
+ width = tty_width()
+ for fd in ready_fds:
+ buf = os.read(fd, 4096)
+ dest = dest_out if fd == src_out else dest_err
+ if not buf:
+ fds = tuple([x for x in fds if x is not fd])
+ print_clean_line(dest, pending.pop(fd, []), width)
+ else:
+ split = sep_rx.split(buf)
+ if len(split) > 2:
+ while len(split) > 1:
+ content, sep = split[:2]
+ split = split[2:]
+ print_clean_line(dest,
+ pending.pop(fd, []) + [content],
+ width,
+ sep)
+ else:
+ assert(len(split) == 1)
+ pending.setdefault(fd, []).extend(split)
+ except BaseException as ex:
+ pending_ex = chain_ex(add_ex_tb(ex), pending_ex)
+ try:
+ # Try to finish each of the streams
+ for fd, pending_items in compat.items(pending):
+ dest = dest_out if fd == src_out else dest_err