- p = subprocess.Popen([subpath(subcmd)] + argv[2:],
- stdout=outf, stderr=errf, preexec_fn=force_tty)
- ret = p.wait()
- except OSError, e:
- log('%s: %s\n' % (subpath(subcmd), e))
- ret = 98
- except SigException, e:
- log('\nbup: %s\n' % e)
- killsig = e.signum
- ret = 94
-finally:
- if p and p.poll() == None:
- os.kill(p.pid, killsig)
- p.wait()
- if n:
- n.stdin.close()
+ fds = tuple([x for x in (src_out, src_err) if x is not None])
+ 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)
+ while len(split) > 1:
+ content, sep = split[:2]
+ split = split[2:]
+ print_clean_line(dest,
+ pending.pop(fd, []) + [content],
+ width,
+ sep)
+ assert(len(split) == 1)
+ if split[0]:
+ pending.setdefault(fd, []).extend(split)
+ except BaseException as ex:
+ pending_ex = add_ex_ctx(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
+ try:
+ print_clean_line(dest, pending_items, width)
+ except (EnvironmentError, EOFError) as ex:
+ pending_ex = add_ex_ctx(add_ex_tb(ex), pending_ex)
+ except BaseException as ex:
+ pending_ex = add_ex_ctx(add_ex_tb(ex), pending_ex)
+ if pending_ex:
+ raise pending_ex
+
+def run_subcmd(subcmd):
+
+ c = (do_profile and [sys.executable, '-m', 'cProfile'] or []) + subcmd
+ if not (fix_stdout or fix_stderr):
+ os.execvp(c[0], c)
+
+ p = None
+ try:
+ p = subprocess.Popen(c,
+ stdout=PIPE if fix_stdout else sys.stdout,
+ stderr=PIPE if fix_stderr else sys.stderr,
+ preexec_fn=force_tty,
+ bufsize=4096,
+ close_fds=True)
+ # Assume p will receive these signals and quit, which will
+ # then cause us to quit.
+ for sig in (signal.SIGINT, signal.SIGTERM, signal.SIGQUIT):
+ signal.signal(sig, signal.SIG_IGN)
+
+ filter_output(fix_stdout and p.stdout.fileno() or None,
+ fix_stderr and p.stderr.fileno() or None,
+ fix_stdout and sys.stdout.fileno() or None,
+ fix_stderr and sys.stderr.fileno() or None)
+ return p.wait()
+ except BaseException as ex:
+ add_ex_tb(ex)