|
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 |
@@ -274,21 +275,28 @@ def copy_file(self, local_file, remote_file): |
274 | 275 | :param remote_file: Remote filepath on remote host to copy file to |
275 | 276 | :type remote_file: str |
276 | 277 | """ |
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) |
292 | 294 | 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