未验证 提交 9925cc43 编写于 作者: A Amador Pahim

Merge branch 'clebergnu-fddrainer'

Signed-off-by: NAmador Pahim <apahim@redhat.com>
...@@ -30,10 +30,7 @@ import subprocess ...@@ -30,10 +30,7 @@ import subprocess
import threading import threading
import time import time
try: from io import BytesIO
from StringIO import StringIO
except ImportError:
from io import StringIO
from . import gdb from . import gdb
from . import runtime from . import runtime
...@@ -326,12 +323,11 @@ class FDDrainer(object): ...@@ -326,12 +323,11 @@ class FDDrainer(object):
""" """
self.fd = fd self.fd = fd
self.name = name self.name = name
self.data = StringIO() self.data = BytesIO()
# TODO: check if, when the process finishes, the FD doesn't # TODO: check if, when the process finishes, the FD doesn't
# automatically close. This may be used as the detection # automatically close. This may be used as the detection
# instead. # instead.
self._result = result self._result = result
self._lock = threading.Lock()
self._thread = None self._thread = None
self._logger = logger self._logger = logger
self._logger_prefix = logger_prefix self._logger_prefix = logger_prefix
...@@ -343,7 +339,7 @@ class FDDrainer(object): ...@@ -343,7 +339,7 @@ class FDDrainer(object):
""" """
Read from fd, storing and optionally logging the output Read from fd, storing and optionally logging the output
""" """
bfr = '' bfr = b''
while True: while True:
if self._ignore_bg_processes: if self._ignore_bg_processes:
has_io = select.select([self.fd], [], [], 1)[0] has_io = select.select([self.fd], [], [], 1)[0]
...@@ -354,19 +350,18 @@ class FDDrainer(object): ...@@ -354,19 +350,18 @@ class FDDrainer(object):
# Don't read unless there are new data available # Don't read unless there are new data available
continue continue
tmp = os.read(self.fd, 8192) tmp = os.read(self.fd, 8192)
if tmp == '': if not tmp:
break break
with self._lock:
self.data.write(tmp) self.data.write(tmp)
if self._verbose: if self._verbose:
bfr += tmp bfr += tmp
if tmp.endswith('\n'): if tmp.endswith(b'\n'):
for line in bfr.splitlines(): for line in bfr.splitlines():
if self._logger is not None: if self._logger is not None:
self._logger.debug(self._logger_prefix, line) self._logger.debug(self._logger_prefix, line)
if self._stream_logger is not None: if self._stream_logger is not None:
self._stream_logger.debug('%s\n', line) self._stream_logger.debug('%s\n', line)
bfr = '' bfr = b''
# Write the rest of the bfr unfinished by \n # Write the rest of the bfr unfinished by \n
if self._verbose and bfr: if self._verbose and bfr:
for line in bfr.splitlines(): for line in bfr.splitlines():
......
...@@ -238,13 +238,13 @@ class FDDrainerTests(unittest.TestCase): ...@@ -238,13 +238,13 @@ class FDDrainerTests(unittest.TestCase):
result = process.CmdResult() result = process.CmdResult()
fd_drainer = process.FDDrainer(read_fd, result, "test") fd_drainer = process.FDDrainer(read_fd, result, "test")
fd_drainer.start() fd_drainer.start()
for content in ("foo", "bar", "baz", "foo\nbar\nbaz\n\n"): for content in (b"foo", b"bar", b"baz", b"foo\nbar\nbaz\n\n"):
os.write(write_fd, content) os.write(write_fd, content)
os.write(write_fd, "finish") os.write(write_fd, b"finish")
os.close(write_fd) os.close(write_fd)
fd_drainer.flush() fd_drainer.flush()
self.assertEqual(fd_drainer.data.getvalue(), self.assertEqual(fd_drainer.data.getvalue(),
"foobarbazfoo\nbar\nbaz\n\nfinish") b"foobarbazfoo\nbar\nbaz\n\nfinish")
def test_log(self): def test_log(self):
class CatchHandler(logging.NullHandler): class CatchHandler(logging.NullHandler):
...@@ -268,11 +268,11 @@ class FDDrainerTests(unittest.TestCase): ...@@ -268,11 +268,11 @@ class FDDrainerTests(unittest.TestCase):
fd_drainer = process.FDDrainer(read_fd, result, "test", fd_drainer = process.FDDrainer(read_fd, result, "test",
logger=logger, verbose=True) logger=logger, verbose=True)
fd_drainer.start() fd_drainer.start()
os.write(write_fd, "should go to the log\n") os.write(write_fd, b"should go to the log\n")
os.close(write_fd) os.close(write_fd)
fd_drainer.flush() fd_drainer.flush()
self.assertEqual(fd_drainer.data.getvalue(), self.assertEqual(fd_drainer.data.getvalue(),
"should go to the log\n") b"should go to the log\n")
self.assertTrue(handler.caught_record) self.assertTrue(handler.caught_record)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册