@@ -239,7 +239,7 @@ def _make_sftp(self):
239239 transport .open_session ()
240240 return paramiko .SFTPClient .from_transport (transport )
241241
242- def mkdir (self , sftp , directory ):
242+ def _mkdir (self , sftp , directory ):
243243 """Make directory via SFTP channel
244244
245245 :param sftp: SFTP client object
@@ -249,19 +249,41 @@ 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 )
260252 try :
261253 sftp .mkdir (directory )
262254 except IOError , error :
263255 logger .error ("Error occured creating directory %s on %s - %s" ,
264256 directory , self .host , error )
257+ logger .debug ("Creating remote directory %s" , directory )
258+ return True
259+
260+ def mkdir (self , sftp , directory ):
261+ """Make directory via SFTP channel.
262+
263+ Parent paths in the directory are created if they do not exist.
264+
265+ :param sftp: SFTP client object
266+ :type sftp: :mod:`paramiko.SFTPClient`
267+ :param directory: Remote directory to create
268+ :type directory: str
269+
270+ Catches and logs at error level remote IOErrors on creating directory.
271+ """
272+ try :
273+ parent_path , sub_dirs = directory .split (os .path .sep , 1 )
274+ except ValueError :
275+ parent_path = directory .split (os .path .sep , 1 )[0 ]
276+ sub_dirs = None
277+ if not parent_path and directory .startswith (os .path .sep ):
278+ parent_path , sub_dirs = sub_dirs .split (os .path .sep , 1 )
279+ try :
280+ sftp .stat (parent_path )
281+ except IOError :
282+ self ._mkdir (sftp , parent_path )
283+ sftp .chdir (parent_path )
284+ if sub_dirs :
285+ return self .mkdir (sftp , sub_dirs )
286+ return True
265287
266288 def copy_file (self , local_file , remote_file ):
267289 """Copy local file to host via SFTP/SCP
@@ -276,14 +298,14 @@ def copy_file(self, local_file, remote_file):
276298 """
277299 sftp = self ._make_sftp ()
278300 destination = [_dir for _dir in remote_file .split (os .path .sep )
279- if _dir ][:- 1 ]
301+ if _dir ][:- 1 ][ 0 ]
280302 if remote_file .startswith (os .path .sep ):
281- destination [0 ] = os .path .sep + destination [0 ]
282- # import ipdb; ipdb.set_trace()
303+ destination = os .path .sep + destination
283304 try :
284305 sftp .stat (destination )
285306 except IOError :
286307 self .mkdir (sftp , destination )
308+ sftp .chdir ()
287309 try :
288310 sftp .put (local_file , remote_file )
289311 except Exception , error :
0 commit comments