Skip to content

Commit b3b2e9f

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

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
@@ -274,21 +275,28 @@ def copy_file(self, local_file, remote_file):
274275
:param remote_file: Remote filepath on remote host to copy file to
275276
:type remote_file: str
276277
"""
277-
sftp = self._make_sftp()
278-
destination = [_dir for _dir in remote_file.split(os.path.sep)
279-
if _dir][:-1]
280-
if remote_file.startswith(os.path.sep):
281-
destination[0] = os.path.sep + destination[0]
282-
# import ipdb; ipdb.set_trace()
283-
try:
284-
sftp.stat(destination)
285-
except IOError:
286-
self.mkdir(sftp, destination)
287-
try:
288-
sftp.put(local_file, remote_file)
289-
except Exception, error:
290-
logger.error("Error occured copying file %s to remote destination %s:%s - %s",
291-
local_file, self.host, remote_file, error)
278+
if os.path.isfile(local_file):
279+
sftp = self._make_sftp()
280+
destination = [_dir for _dir in remote_file.split(os.path.sep)
281+
if _dir][:-1]
282+
if remote_file.startswith(os.path.sep):
283+
destination[0] = os.path.sep + destination[0]
284+
# import ipdb; ipdb.set_trace()
285+
try:
286+
sftp.stat(destination)
287+
except IOError:
288+
self.mkdir(sftp, destination)
289+
try:
290+
sftp.put(local_file, remote_file)
291+
except Exception, error:
292+
logger.error("Error occured copying file %s to remote destination %s:%s - %s",
293+
local_file, self.host, remote_file, error)
292294
else:
293-
logger.info("Copied local file %s to remote destination %s:%s",
294-
local_file, self.host, remote_file)
295+
file_list = os.listdir(local_file)
296+
local_files = []
297+
remote_files = []
298+
for file_name in file_list:
299+
local_files.append(os.path.join(local_file, file_name))
300+
remote_files.append(os.path.join(remote_file, file_name))
301+
for local_path, remote_path in itertools.izip(local_files, remote_files):
302+
self.copy_file(local_path, remote_path)

0 commit comments

Comments
 (0)