diff --git a/lib/ansible/parsing/dataloader.py b/lib/ansible/parsing/dataloader.py index 0127f59cd3e..7c159b494c1 100644 --- a/lib/ansible/parsing/dataloader.py +++ b/lib/ansible/parsing/dataloader.py @@ -120,7 +120,7 @@ class DataLoader(): def path_exists(self, path): path = self.path_dwim(path) - return os.path.exists(to_bytes(path)) + return os.path.exists(to_bytes(path, errors='strict')) def is_file(self, path): path = self.path_dwim(path) diff --git a/lib/ansible/plugins/connection/chroot.py b/lib/ansible/plugins/connection/chroot.py index ba41ffb5d88..0778a5e22ca 100644 --- a/lib/ansible/plugins/connection/chroot.py +++ b/lib/ansible/plugins/connection/chroot.py @@ -127,7 +127,7 @@ class Connection(ConnectionBase): out_path = pipes.quote(self._prefix_login_path(out_path)) try: - with open(in_path, 'rb') as in_file: + with open(to_bytes(in_path, errors='strict'), 'rb') as in_file: try: p = self._buffered_exec_command('dd of=%s bs=%s' % (out_path, BUFSIZE), stdin=in_file) except OSError: @@ -153,7 +153,7 @@ class Connection(ConnectionBase): except OSError: raise AnsibleError("chroot connection requires dd command in the chroot") - with open(out_path, 'wb+') as out_file: + with open(to_bytes(out_path, errors='strict'), 'wb+') as out_file: try: chunk = p.stdout.read(BUFSIZE) while chunk: diff --git a/lib/ansible/plugins/connection/local.py b/lib/ansible/plugins/connection/local.py index 5004c3698db..79ba8ab8bc1 100644 --- a/lib/ansible/plugins/connection/local.py +++ b/lib/ansible/plugins/connection/local.py @@ -127,10 +127,10 @@ class Connection(ConnectionBase): super(Connection, self).put_file(in_path, out_path) display.vvv(u"{0} PUT {1} TO {2}".format(self._play_context.remote_addr, in_path, out_path)) - if not os.path.exists(in_path): + if not os.path.exists(to_bytes(in_path, errors='strict')): raise AnsibleFileNotFound("file or module does not exist: {0}".format(to_str(in_path))) try: - shutil.copyfile(in_path, out_path) + shutil.copyfile(to_bytes(in_path, errors='strict'), to_bytes(out_path, errors='strict')) except shutil.Error: raise AnsibleError("failed to copy: {0} and {1} are the same".format(to_str(in_path), to_str(out_path))) except IOError as e: diff --git a/lib/ansible/plugins/connection/paramiko_ssh.py b/lib/ansible/plugins/connection/paramiko_ssh.py index 52bbdf05d8b..aceeabd2b85 100644 --- a/lib/ansible/plugins/connection/paramiko_ssh.py +++ b/lib/ansible/plugins/connection/paramiko_ssh.py @@ -42,6 +42,7 @@ from ansible import constants as C from ansible.errors import AnsibleError, AnsibleConnectionFailure, AnsibleFileNotFound from ansible.plugins.connection import ConnectionBase from ansible.utils.path import makedirs_safe +from ansible.utils.unicode import to_bytes try: from __main__ import display @@ -270,7 +271,7 @@ class Connection(ConnectionBase): display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) - if not os.path.exists(in_path): + if not os.path.exists(to_bytes(in_path, errors='strict')): raise AnsibleFileNotFound("file or module does not exist: %s" % in_path) try: @@ -279,7 +280,7 @@ class Connection(ConnectionBase): raise AnsibleError("failed to open a SFTP connection (%s)" % e) try: - self.sftp.put(in_path, out_path) + self.sftp.put(to_bytes(in_path, errors='strict'), to_bytes(out_path, errors='strict')) except IOError: raise AnsibleError("failed to transfer file to %s" % out_path) @@ -305,7 +306,7 @@ class Connection(ConnectionBase): raise AnsibleError("failed to open a SFTP connection (%s)", e) try: - self.sftp.get(in_path, out_path) + self.sftp.get(to_bytes(in_path, errors='strict'), to_bytes(out_path, errors='strict')) except IOError: raise AnsibleError("failed to transfer file from %s" % in_path) diff --git a/lib/ansible/plugins/connection/ssh.py b/lib/ansible/plugins/connection/ssh.py index 0a0b2bb04bc..b4e6729ee4a 100644 --- a/lib/ansible/plugins/connection/ssh.py +++ b/lib/ansible/plugins/connection/ssh.py @@ -631,7 +631,7 @@ class Connection(ConnectionBase): super(Connection, self).put_file(in_path, out_path) display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self.host) - if not os.path.exists(in_path): + if not os.path.exists(to_bytes(in_path, errors='strict')): raise AnsibleFileNotFound("file or module does not exist: {0}".format(to_str(in_path))) # scp and sftp require square brackets for IPv6 addresses, but diff --git a/lib/ansible/utils/hashing.py b/lib/ansible/utils/hashing.py index 5e378db79f4..76c34eefbf4 100644 --- a/lib/ansible/utils/hashing.py +++ b/lib/ansible/utils/hashing.py @@ -21,6 +21,7 @@ __metaclass__ = type import os from ansible.errors import AnsibleError +from ansible.utils.unicode import to_bytes # Note, sha1 is the only hash algorithm compatible with python2.4 and with # FIPS-140 mode (as of 11-2014) @@ -54,12 +55,12 @@ def secure_hash_s(data, hash_func=sha1): def secure_hash(filename, hash_func=sha1): ''' Return a secure hash hex digest of local file, None if file is not present or a directory. ''' - if not os.path.exists(filename) or os.path.isdir(filename): + if not os.path.exists(to_bytes(filename, errors='strict')) or os.path.isdir(to_bytes(filename, errors='strict')): return None digest = hash_func() blocksize = 64 * 1024 try: - infile = open(filename, 'rb') + infile = open(to_bytes(filename, errors='strict'), 'rb') block = infile.read(blocksize) while block: digest.update(block)