Skip to content

Commit 2379b7a

Browse files
author
Kincaid Savoie
committed
copy_file now runs recursively when directory paths are supplied.
1 parent 6fc8f60 commit 2379b7a

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

pssh/ssh_client.py

Lines changed: 25 additions & 17 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
@@ -296,21 +297,28 @@ def copy_file(self, local_file, remote_file):
296297
:param remote_file: Remote filepath on remote host to copy file to
297298
:type remote_file: str
298299
"""
299-
sftp = self._make_sftp()
300-
destination = [_dir for _dir in remote_file.split(os.path.sep)
301-
if _dir][:-1][0]
302-
if remote_file.startswith(os.path.sep):
303-
destination = os.path.sep + destination
304-
try:
305-
sftp.stat(destination)
306-
except IOError:
307-
self.mkdir(sftp, destination)
308-
sftp.chdir()
309-
try:
310-
sftp.put(local_file, remote_file)
311-
except Exception, error:
312-
logger.error("Error occured copying file %s to remote destination %s:%s - %s",
313-
local_file, self.host, remote_file, error)
300+
if os.path.isfile(local_file):
301+
sftp = self._make_sftp()
302+
destination = [_dir for _dir in remote_file.split(os.path.sep)
303+
if _dir][:-1][0]
304+
if remote_file.startswith(os.path.sep):
305+
destination = os.path.sep + destination
306+
try:
307+
sftp.stat(destination)
308+
except IOError:
309+
self.mkdir(sftp, destination)
310+
sftp.chdir()
311+
try:
312+
sftp.put(local_file, remote_file)
313+
except Exception, error:
314+
logger.error("Error occured copying file %s to remote destination %s:%s - %s",
315+
local_file, self.host, remote_file, error)
314316
else:
315-
logger.info("Copied local file %s to remote destination %s:%s",
316-
local_file, self.host, remote_file)
317+
file_list = os.listdir(local_file)
318+
local_files = []
319+
remote_files = []
320+
for file_name in file_list:
321+
local_files.append(os.path.join(local_file, file_name))
322+
remote_files.append(os.path.join(remote_file, file_name))
323+
for local_path, remote_path in itertools.izip(local_files, remote_files):
324+
self.copy_file(local_path, remote_path)

0 commit comments

Comments
 (0)