Skip to content

Commit

Permalink
Fix output stream failures (Windows) (#4375)
Browse files Browse the repository at this point in the history
* POC of removing tqdm

* iteration over IOError for stream output

* added comments and test

* commenting out test

* trying a different import
  • Loading branch information
memsharded authored Jan 25, 2019
1 parent 256c147 commit 0b7b3ca
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
28 changes: 16 additions & 12 deletions conans/client/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,22 @@ def write(self, data, front=None, back=None, newline=False):
data = decode_text(data) # Keep python 2 compatibility

if self._color and (front or back):
color = "%s%s" % (front or '', back or '')
end = (Style.RESET_ALL + "\n") if newline else Style.RESET_ALL # @UndefinedVariable
data = "%s%s%s" % (color, data, end)
else:
if newline:
data = "%s\n" % data

try:
self._stream.write(data)
except UnicodeError:
data = data.encode("utf8").decode("ascii", "ignore")
self._stream.write(data)
data = "%s%s%s%s" % (front or '', back or '', data, Style.RESET_ALL)
if newline:
data = "%s\n" % data

# https://github.com/conan-io/conan/issues/4277
# Windows output locks produce IOErrors
for _ in range(3):
try:
self._stream.write(data)
break
except IOError:
import time
time.sleep(0.02)
except UnicodeError:
data = data.encode("utf8").decode("ascii", "ignore")

self._stream.flush()

def info(self, data):
Expand Down
30 changes: 30 additions & 0 deletions conans/test/unittests/client/conan_output_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# coding=utf-8

import unittest
from types import MethodType

from six import StringIO

from conans.client.output import ConanOutput
from mock import mock


class ConanOutputTest(unittest.TestCase):

def test_blocked_output(self):
# https://github.com/conan-io/conan/issues/4277
stream = StringIO()

def write_raise(self, data):
write_raise.counter = getattr(write_raise, "counter", 0) + 1
if write_raise.counter < 2:
raise IOError("Stdout locked")
self.super_write(data)
stream.super_write = stream.write
stream.write = MethodType(write_raise, stream)
out = ConanOutput(stream)

with mock.patch("time.sleep") as sleep:
out.write("Hello world")
sleep.assert_any_call(0.02)
self.assertEqual("Hello world", stream.getvalue())
13 changes: 3 additions & 10 deletions conans/util/progress_bar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import os
from contextlib import contextmanager

Expand All @@ -18,17 +17,15 @@ class _FileReaderWithProgressBar(object):

def __init__(self, fileobj, output, desc=None):
pb_kwargs = self.tqdm_defaults.copy()
self._ori_output = output

# If there is no terminal, just print a beat every TIMEOUT_BEAT seconds.
if not output.is_terminal:
output = _NoTerminalOutput(output)
pb_kwargs['mininterval'] = TIMEOUT_BEAT_SECONDS

self._output = output
self._fileobj = fileobj
self.seek(0, os.SEEK_END)
self._pb = tqdm(total=self.tell(), desc=desc, file=output, **pb_kwargs)
self._tqdm_bar = tqdm(total=self.tell(), desc=desc, file=output, **pb_kwargs)
self.seek(0)

def seekable(self):
Expand All @@ -43,15 +40,11 @@ def tell(self):
def read(self, size):
prev = self.tell()
ret = self._fileobj.read(size)
self._pb.update(self.tell() - prev)
self._tqdm_bar.update(self.tell() - prev)
return ret

def pb_close(self):
self._pb.close()

def pb_write(self, message):
""" Allow to write messages to output without interfering with the progress bar """
tqdm.write(message, file=self._ori_output)
self._tqdm_bar.close()


class _NoTerminalOutput(object):
Expand Down

0 comments on commit 0b7b3ca

Please sign in to comment.