1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4 """Syncronized Standard IO Linebuffer implemented with cStringIO."""
11 class StdioBuffer(object):
12 def __init__(self
, shard
):
13 self
.queue
= Queue
.Queue()
17 def _pipe_handler(self
, system_pipe
, program_pipe
):
18 """Helper method for collecting stdio output. Output is collected until
19 a newline is seen, at which point an event is triggered and the line is
20 pushed to a buffer as a (stdio, line) tuple."""
21 buf
= cStringIO
.StringIO()
24 char
= program_pipe
.read(1)
25 if not char
and self
.shard
.poll() is not None:
28 if char
== '\n' or not pipe_running
:
31 self
.queue
.put((system_pipe
, line
))
33 self
.queue
.put((system_pipe
, None))
35 buf
= cStringIO
.StringIO()
37 def handle_pipe(self
, system_pipe
, program_pipe
):
38 t
= threading
.Thread(target
=self
._pipe
_handler
, args
=[system_pipe
,
44 """Emits a tuple of (sys.stderr, line), (sys.stdout, line), or (None, None)
45 if the process has finished. This is a blocking call."""
47 (pipe
, line
) = self
.queue
.get(True)
51 if self
.completed
>= 2: