Encode string/unicode data before writing with
@@ -9,6 +9,7 @@ from subprocess import PIPE, Popen
import click
import gevent
+import six
from pyinfra import logger
from pyinfra.api.util import get_file_io, make_command, read_buffer
@@ -105,11 +106,16 @@ def run_shell_command(
def put_file(
- state, host, file_io, remote_file,
+ state, host, filename_or_io, remote_file,
sudo=False, sudo_user=None, su_user=None, print_output=False,
):
- with open(remote_file, 'wb') as remote_f, get_file_io(file_io) as local_f:
- remote_f.write(local_f.read())
+ with open(remote_file, 'wb') as remote_f, get_file_io(filename_or_io) as local_f:
+ data = local_f.read()
+
+ if isinstance(data, six.text_type):
+ data = data.encode()
+
+ remote_f.write(data)
if print_output:
print('{0}file copied: {1}'.format(host.print_prefix, remote_file))
