wrapper function for writing to stdout

This commit is contained in:
Tomas Kopecek 2019-02-19 13:55:06 +01:00 committed by Mike McLean
parent 8e41dd6d09
commit a4d41bd078
3 changed files with 19 additions and 10 deletions

View file

@ -343,6 +343,21 @@ Running Tasks:
return rv
def write_to_stdout(contents):
"""Helper function to write str/bytes to stdout
https://docs.python.org/3/library/sys.html#sys.displayhook
"""
try:
sys.stdout.write(contents)
except UnicodeEncodeError:
bytes = contents.encode(sys.stdout.encoding, 'backslashreplace')
if hasattr(sys.stdout, 'buffer'):
sys.stdout.buffer.write(bytes)
else:
contents = bytes.decode(sys.stdout.encoding, 'strict')
sys.stdout.write(contents)
def watch_logs(session, tasklist, opts, poll_interval):
print("Watching logs (this may be safely interrupted)...")
@ -391,10 +406,7 @@ def watch_logs(session, tasklist, opts, poll_interval):
sys.stdout.write("\n")
sys.stdout.write("==> %s <==\n" % currlog)
lastlog = currlog
if six.PY3:
sys.stdout.buffer.write(contents)
else:
sys.stdout.write(contents)
write_to_stdout(contents)
if opts.follow:

View file

@ -479,7 +479,7 @@ class BaseTaskHandler(object):
fsrc = six.moves.urllib.request.urlopen(url)
if not os.path.exists(os.path.dirname(fn)):
os.makedirs(os.path.dirname(fn))
with open(fn, 'w') as fdst:
with open(fn, 'wb') as fdst:
shutil.copyfileobj(fsrc, fdst)
fsrc.close()
else:

View file

@ -5,7 +5,7 @@ import time
import koji
from koji.plugin import export_cli
from koji_cli.lib import _, activate_session, OptionParser, watch_tasks, \
list_task_output_all_volumes
list_task_output_all_volumes, write_to_stdout
import six
@ -98,10 +98,7 @@ def handle_runroot(options, session, args):
log = session.downloadTaskOutput(task_id, 'runroot.log', volume=volume)
# runroot output, while normally text, can be *anything*, so
# treat it as binary
if six.PY3:
sys.stdout.buffer.write(log)
else:
sys.stdout.write(log)
write_to_stdout(log)
info = session.getTaskInfo(task_id)
if info is None:
sys.exit(1)