From f5ea647b491d0cfc30ebf9abd9f4cfe6c62d2fe6 Mon Sep 17 00:00:00 2001 From: Dan <22e889d8@opayq.com> Date: Wed, 4 Jan 2017 18:04:04 +0000 Subject: [PATCH] Pass-through sftp object when recursing directories on copy_file --- pssh/ssh_client.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pssh/ssh_client.py b/pssh/ssh_client.py index be957ae9..fa653638 100644 --- a/pssh/ssh_client.py +++ b/pssh/ssh_client.py @@ -327,39 +327,41 @@ def mkdir(self, sftp, directory): return self.mkdir(sftp, sub_dirs) return True - def _copy_dir(self, local_dir, remote_dir): + def _copy_dir(self, local_dir, remote_dir, sftp): """Call copy_file on every file in the specified directory, copying them to the specified remote directory.""" file_list = os.listdir(local_dir) for file_name in file_list: local_path = os.path.join(local_dir, file_name) remote_path = os.path.join(remote_dir, file_name) - self.copy_file(local_path, remote_path, recurse=True) + self.copy_file(local_path, remote_path, recurse=True, + sftp=sftp) - def copy_file(self, local_file, remote_file, recurse=False): + def copy_file(self, local_file, remote_file, recurse=False, + sftp=None): """Copy local file to host via SFTP/SCP - + Copy is done natively using SFTP/SCP version 2 protocol, no scp command \ is used or required. - + :param local_file: Local filepath to copy to remote host :type local_file: str :param remote_file: Remote filepath on remote host to copy file to :type remote_file: str :param recurse: Whether or not to descend into directories recursively. :type recurse: bool - + :raises: :mod:`ValueError` when a directory is supplied to ``local_file`` \ and ``recurse`` is not set :raises: :mod:`IOError` on I/O errors writing files :raises: :mod:`OSError` on OS errors like permission denied """ if os.path.isdir(local_file) and recurse: - return self._copy_dir(local_file, remote_file) + return self._copy_dir(local_file, remote_file, sftp) elif os.path.isdir(local_file) and not recurse: raise ValueError("Recurse must be true if local_file is a " "directory.") - sftp = self._make_sftp() + sftp = self._make_sftp() if not sftp else sftp destination = self._parent_paths_split(remote_file) try: sftp.stat(destination)