Skip to content

Commit f5ea647

Browse files
author
Dan
committed
Pass-through sftp object when recursing directories on copy_file
1 parent 953d89e commit f5ea647

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

pssh/ssh_client.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,39 +327,41 @@ def mkdir(self, sftp, directory):
327327
return self.mkdir(sftp, sub_dirs)
328328
return True
329329

330-
def _copy_dir(self, local_dir, remote_dir):
330+
def _copy_dir(self, local_dir, remote_dir, sftp):
331331
"""Call copy_file on every file in the specified directory, copying
332332
them to the specified remote directory."""
333333
file_list = os.listdir(local_dir)
334334
for file_name in file_list:
335335
local_path = os.path.join(local_dir, file_name)
336336
remote_path = os.path.join(remote_dir, file_name)
337-
self.copy_file(local_path, remote_path, recurse=True)
337+
self.copy_file(local_path, remote_path, recurse=True,
338+
sftp=sftp)
338339

339-
def copy_file(self, local_file, remote_file, recurse=False):
340+
def copy_file(self, local_file, remote_file, recurse=False,
341+
sftp=None):
340342
"""Copy local file to host via SFTP/SCP
341-
343+
342344
Copy is done natively using SFTP/SCP version 2 protocol, no scp command \
343345
is used or required.
344-
346+
345347
:param local_file: Local filepath to copy to remote host
346348
:type local_file: str
347349
:param remote_file: Remote filepath on remote host to copy file to
348350
:type remote_file: str
349351
:param recurse: Whether or not to descend into directories recursively.
350352
:type recurse: bool
351-
353+
352354
:raises: :mod:`ValueError` when a directory is supplied to ``local_file`` \
353355
and ``recurse`` is not set
354356
:raises: :mod:`IOError` on I/O errors writing files
355357
:raises: :mod:`OSError` on OS errors like permission denied
356358
"""
357359
if os.path.isdir(local_file) and recurse:
358-
return self._copy_dir(local_file, remote_file)
360+
return self._copy_dir(local_file, remote_file, sftp)
359361
elif os.path.isdir(local_file) and not recurse:
360362
raise ValueError("Recurse must be true if local_file is a "
361363
"directory.")
362-
sftp = self._make_sftp()
364+
sftp = self._make_sftp() if not sftp else sftp
363365
destination = self._parent_paths_split(remote_file)
364366
try:
365367
sftp.stat(destination)

0 commit comments

Comments
 (0)