|
25 | 25 | import logging |
26 | 26 | import paramiko |
27 | 27 | import os |
| 28 | +import itertools |
28 | 29 | from socket import gaierror as sock_gaierror, error as sock_error |
29 | 30 | from .exceptions import UnknownHostException, AuthenticationException, \ |
30 | 31 | ConnectionErrorException, SSHException |
@@ -296,21 +297,28 @@ def copy_file(self, local_file, remote_file): |
296 | 297 | :param remote_file: Remote filepath on remote host to copy file to |
297 | 298 | :type remote_file: str |
298 | 299 | """ |
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) |
314 | 316 | 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