Skip to content

Commit ca12de5

Browse files
author
Kincaid Savoie
committed
Broke directory copy logic into copy_dir; added recurse flag.
1 parent 426f289 commit ca12de5

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

pssh/ssh_client.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,16 @@ def mkdir(self, sftp, directory):
263263
logger.error("Error occured creating directory %s on %s - %s",
264264
directory, self.host, error)
265265

266-
def copy_file(self, local_file, remote_file):
266+
def _copy_dir(self, local_dir, remote_dir):
267+
"""Calls copy_file on every file in the specified directory, copying
268+
them to the specified remote directory."""
269+
file_list = os.listdir(local_dir)
270+
for file_name in file_list:
271+
local_path = os.path.join(local_dir, file_name)
272+
remote_path = os.path.join(remote_dir, file_name)
273+
self.copy_file(local_path, remote_path)
274+
275+
def copy_file(self, local_file, remote_file, recurse=False):
267276
"""Copy local file to host via SFTP/SCP
268277
269278
Copy is done natively using SFTP/SCP version 2 protocol, no scp command \
@@ -274,25 +283,21 @@ def copy_file(self, local_file, remote_file):
274283
:param remote_file: Remote filepath on remote host to copy file to
275284
:type remote_file: str
276285
"""
277-
if os.path.isfile(local_file):
286+
if os.path.isfile(local_file) or not recurse:
278287
sftp = self._make_sftp()
279288
destination = [_dir for _dir in remote_file.split(os.path.sep)
280289
if _dir][:-1]
281290
if remote_file.startswith(os.path.sep):
282291
destination[0] = os.path.sep + destination[0]
283292
# import ipdb; ipdb.set_trace()
284293
try:
285-
sftp.stat(destination)
294+
sftp.stat(destination[0])
286295
except IOError:
287-
self.mkdir(sftp, destination)
296+
self.mkdir(sftp, destination[0])
288297
try:
289298
sftp.put(local_file, remote_file)
290299
except Exception, error:
291300
logger.error("Error occured copying file %s to remote destination %s:%s - %s",
292301
local_file, self.host, remote_file, error)
293302
else:
294-
file_list = os.listdir(local_file)
295-
for file_name in file_list:
296-
local_path = os.path.join(local_file, file_name)
297-
remote_path = os.path.join(remote_file, file_name)
298-
self.copy_file(local_path, remote_path)
303+
self._copy_dir(local_file, remote_file)

0 commit comments

Comments
 (0)