Skip to content

Commit 4af7aa0

Browse files
author
Kincaid Savoie
committed
Broke directory copy logic into copy_dir; added recurse flag.
1 parent 6e09d52 commit 4af7aa0

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pssh/ssh_client.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,16 @@ def mkdir(self, sftp, directory):
285285
return self.mkdir(sftp, sub_dirs)
286286
return True
287287

288-
def copy_file(self, local_file, remote_file):
288+
def _copy_dir(self, local_dir, remote_dir):
289+
"""Calls copy_file on every file in the specified directory, copying
290+
them to the specified remote directory."""
291+
file_list = os.listdir(local_dir)
292+
for file_name in file_list:
293+
local_path = os.path.join(local_dir, file_name)
294+
remote_path = os.path.join(remote_dir, file_name)
295+
self.copy_file(local_path, remote_path)
296+
297+
def copy_file(self, local_file, remote_file, recurse=False):
289298
"""Copy local file to host via SFTP/SCP
290299
291300
Copy is done natively using SFTP/SCP version 2 protocol, no scp command \
@@ -296,14 +305,14 @@ def copy_file(self, local_file, remote_file):
296305
:param remote_file: Remote filepath on remote host to copy file to
297306
:type remote_file: str
298307
"""
299-
if os.path.isfile(local_file):
308+
if os.path.isfile(local_file) or not recurse:
300309
sftp = self._make_sftp()
301310
destination = [_dir for _dir in remote_file.split(os.path.sep)
302311
if _dir][:-1][0]
303312
if remote_file.startswith(os.path.sep):
304313
destination = os.path.sep + destination
305314
try:
306-
sftp.stat(destination)
315+
sftp.stat(destination[0])
307316
except IOError:
308317
self.mkdir(sftp, destination)
309318
sftp.chdir()
@@ -313,8 +322,4 @@ def copy_file(self, local_file, remote_file):
313322
logger.error("Error occured copying file %s to remote destination %s:%s - %s",
314323
local_file, self.host, remote_file, error)
315324
else:
316-
file_list = os.listdir(local_file)
317-
for file_name in file_list:
318-
local_path = os.path.join(local_file, file_name)
319-
remote_path = os.path.join(remote_file, file_name)
320-
self.copy_file(local_path, remote_path)
325+
self._copy_dir(local_file, remote_file)

0 commit comments

Comments
 (0)