+
+sep_rx = re.compile(r'([\r\n])')
+
+def print_clean_line(dest, content, width, sep=None):
+ """Write some or all of content, followed by sep, to the dest fd after
+ padding the content with enough spaces to fill the current
+ terminal width or truncating it to the terminal width if sep is a
+ carriage return."""
+ global sep_rx
+ assert sep in ('\r', '\n', None)
+ if not content:
+ if sep:
+ os.write(dest, sep)
+ return
+ for x in content:
+ assert not sep_rx.match(x)
+ content = ''.join(content)
+ if sep == '\r' and len(content) > width:
+ content = content[width:]
+ os.write(dest, content)
+ if len(content) < width:
+ os.write(dest, ' ' * (width - len(content)))
+ os.write(dest, sep)
+
+def filter_output(src_out, src_err, dest_out, dest_err):
+ """Transfer data from src_out to dest_out and src_err to dest_err via
+ print_clean_line until src_out and src_err close."""
+ global sep_rx
+ assert not isinstance(src_out, bool)
+ assert not isinstance(src_err, bool)
+ assert not isinstance(dest_out, bool)
+ assert not isinstance(dest_err, bool)
+ assert src_out is not None or src_err is not None
+ assert (src_out is None) == (dest_out is None)
+ assert (src_err is None) == (dest_err is None)
+ pending = {}
+ pending_ex = None
+ try:
+ 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)