|
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 |
@@ -266,20 +267,30 @@ def copy_file(self, local_file, remote_file): |
266 | 267 | :param remote_file: Remote filepath on remote host to copy file to |
267 | 268 | :type remote_file: str |
268 | 269 | """ |
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) |
274 | 280 | 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) |
283 | 288 | 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