Skip to content

Commit e4a04ca

Browse files
author
Kincaid Savoie
committed
copy_file now runs recursively when directory paths are supplied.
1 parent 9739900 commit e4a04ca

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

pssh/ssh_client.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import logging
2626
import paramiko
2727
import os
28+
import itertools
2829
from socket import gaierror as sock_gaierror, error as sock_error
2930
from .exceptions import UnknownHostException, AuthenticationException, \
3031
ConnectionErrorException, SSHException
@@ -266,20 +267,30 @@ def copy_file(self, local_file, remote_file):
266267
:param remote_file: Remote filepath on remote host to copy file to
267268
:type remote_file: str
268269
"""
269-
sftp = self._make_sftp()
270-
destination = remote_file.split(os.path.sep)
271-
remote_file = os.path.sep.join(destination)
272-
destination = destination[:-1]
273-
for directory in destination:
270+
if os.path.isfile(local_file):
271+
sftp = self._make_sftp()
272+
destination = remote_file.split(os.path.sep)
273+
remote_file = os.path.sep.join(destination)
274+
destination = destination[:-1]
275+
for directory in destination:
276+
try:
277+
sftp.stat(directory)
278+
except IOError:
279+
self.mkdir(sftp, directory)
274280
try:
275-
sftp.stat(directory)
276-
except IOError:
277-
self.mkdir(sftp, directory)
278-
try:
279-
sftp.put(local_file, remote_file)
280-
except Exception, error:
281-
logger.error("Error occured copying file to host %s - %s",
282-
self.host, error)
281+
sftp.put(local_file, remote_file)
282+
except Exception, error:
283+
logger.error("Error occured copying file to host %s - %s",
284+
self.host, error)
285+
else:
286+
logger.info("Copied local file %s to remote destination %s:%s",
287+
local_file, self.host, remote_file)
283288
else:
284-
logger.info("Copied local file %s to remote destination %s:%s",
285-
local_file, self.host, remote_file)
289+
file_list = os.listdir(local_file)
290+
local_files = []
291+
remote_files = []
292+
for file_name in file_list:
293+
local_files.append(os.path.join(local_file, file_name))
294+
remote_files.append(os.path.join(remote_file, file_name))
295+
for local_path, remote_path in itertools.izip(local_files, remote_files):
296+
self.copy_file(local_path, remote_path)

0 commit comments

Comments
 (0)