@@ -360,13 +360,7 @@ def copy_file(self, local_file, remote_file, recurse=False):
360360 raise ValueError ("Recurse must be true if local_file is a "
361361 "directory." )
362362 sftp = self ._make_sftp ()
363- try :
364- destination = os .path .sep .join ([_dir for _dir in remote_file .split (os .path .sep )
365- if _dir ][:- 1 ])
366- except IndexError :
367- destination = ''
368- if remote_file .startswith (os .path .sep ) or not destination :
369- destination = os .path .sep + destination
363+ destination = self ._parent_path_split (remote_file )
370364 if os .path .sep in remote_file :
371365 self .mkdir (sftp , destination )
372366 sftp .chdir ()
@@ -415,15 +409,13 @@ def copy_file_to_local(self, remote_file, local_file, recurse=False):
415409 elif remote_dir_exists and not recurse :
416410 raise ValueError ("Recurse must be true if local_file is a "
417411 "directory." )
418- destination = [_dir for _dir in local_file .split (os .path .sep )
419- if _dir ][:- 1 ][0 ]
420- if local_file .startswith (os .path .sep ):
421- destination = os .path .sep + destination
412+ destination = self ._parent_path_split (local_file )
422413 if not os .path .exists (destination ):
423414 try :
424415 os .makedirs (destination )
425- except OSError :
416+ except OSError , exception :
426417 logger .error ("Unable to create local directory structure." )
418+ raise exception
427419 try :
428420 sftp .get (remote_file , local_file )
429421 except Exception , error :
@@ -432,3 +424,11 @@ def copy_file_to_local(self, remote_file, local_file, recurse=False):
432424 else :
433425 logger .info ("Copied local file %s from remote destination %s:%s" ,
434426 local_file , self .host , remote_file )
427+
428+ @staticmethod
429+ def _parent_path_split (file_path ):
430+ destination = [_dir for _dir in file_path .split (os .path .sep )
431+ if _dir ][:- 1 ][0 ]
432+ if file_path .startswith (os .path .sep ):
433+ destination = os .path .sep + destination
434+ return destination
0 commit comments