##
################################################################################
-def decode_string(str_to_decode):
+def decode_and_print(str_to_decode):
"""Decode a UTF-8 encoded bytes to string.
Args:
str_to_decode (byte stream): Byte stream to decode
Returns:
- String output. If there any encoding/decoding errors, it will replace it with
- UnicodeEncodeError.
+ String output. If there any encoding/decoding errors, it will not print anything
+ and return an empty string.
"""
+ output = ''
try:
output = str_to_decode.decode("utf-8", errors='replace')
- except UnicodeEncodeError:
- output = "UnicodeEncodeError"
- except UnicodeDecodeError:
- output = "UnicodeDecodeError"
- return output
+ print(output)
+ finally:
+ return output
def run_command(command_to_run, _cwd=None, _exit_on_fail=False, _output_file=None):
""" Runs the command.
if proc.poll() is not None:
break
if output:
- output_str = decode_string(output.strip())
- print(output_str)
+ output_str = decode_and_print(output.strip())
of.write(output_str + "\n")
else:
command_stdout, command_stderr = proc.communicate()
if len(command_stdout) > 0:
- print(decode_string(command_stdout))
+ decode_and_print(command_stdout)
if len(command_stderr) > 0:
- print(decode_string(command_stderr))
+ decode_and_print(command_stderr)
return_code = proc.returncode
if _exit_on_fail and return_code != 0: