@@ -249,11 +249,19 @@ def mkdir(self, sftp, directory):
249249
250250 Catches and logs at error level remote IOErrors on creating directory.
251251 """
252+ sub_dirs = [_dir for _dir in directory .split (os .path .sep ) if _dir ][:- 1 ]
253+ sub_dirs = os .path .sep + os .path .sep .join (sub_dirs ) if directory .startswith (os .path .sep ) \
254+ else os .path .sep .join (sub_dirs )
255+ if sub_dirs :
256+ try :
257+ sftp .stat (sub_dirs )
258+ except IOError :
259+ return self .mkdir (sftp , sub_dirs )
252260 try :
253261 sftp .mkdir (directory )
254262 except IOError , error :
255- logger .error ("Error occured creating directory on %s - %s" ,
256- self .host , error )
263+ logger .error ("Error occured creating directory %s on %s - %s" ,
264+ directory , self .host , error )
257265
258266 def copy_file (self , local_file , remote_file ):
259267 """Copy local file to host via SFTP/SCP
@@ -267,19 +275,20 @@ def copy_file(self, local_file, remote_file):
267275 :type remote_file: str
268276 """
269277 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 :
274- try :
275- sftp .stat (directory )
276- except IOError :
277- self .mkdir (sftp , directory )
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 )
278287 try :
279288 sftp .put (local_file , remote_file )
280289 except Exception , error :
281- logger .error ("Error occured copying file to host %s - %s" ,
282- self .host , error )
290+ logger .error ("Error occured copying file %s to remote destination %s: %s - %s" ,
291+ local_file , self .host , remote_file , error )
283292 else :
284293 logger .info ("Copied local file %s to remote destination %s:%s" ,
285294 local_file , self .host , remote_file )
0 commit comments