def mkdirs(SFTP, remotePath): currentPath = remotePath Skipped = [] while True: err = forgive(lambda: SFTP.mkdir(currentPath, 511)) # #Note: Existense isn't checked for to reduce the number of remote calls. if err: if err.errno is None: # The dir exists. return elif not isinstance(err, IOError): # #Pending: Check: Permission errors could result in infinite loops. raise err else: # Try to create the parent path. currentPath, skipped = pathSplit(currentPath) Skipped.append(skipped) if not currentPath or currentPath == '/': raise Exception('Failed to create the dir: %s' % remotePath) else: if not Skipped: break else: currentPath += '/%s' % Skipped.pop(0)
def interactive(func, message=None, raiseError=False): r"""Helps with re-running tasks till there were no errors. """ while True: e = forgive(func) if not e: return print e if raw_input((message or 'Fix and continue...') + ' (Y/n):').lower() == 'n': if raiseError: raise e return e
def isLocked(filePath, tempPath=None): # #Pending: Check, whether there is a proper and robust way to check the lock status, instead of renaming the path. r"""Checks whether the given path is locked. """ if not exists(filePath): return if not tempPath: while True: tempPath = '%s.%s' % (filePath, getRandomString()) if not exists(tempPath): break if forgive(lambda: rename(filePath, tempPath)): return True rename(tempPath, filePath) return False
def _upload(SFTP, localPath, remotePath): debug('uploading %s to %s' % (localPath, remotePath)) pathType = getPathType(localPath) if pathType == 1: # file retry(lambda: SFTP.put(localPath, remotePath) or 1) # #Note: 1 is returned to notify retry of a success. elif pathType > 1: # dir / junction err = forgive(lambda: mkdirs(SFTP, remotePath)) if err and not isinstance(err, IOError): raise err for item in listdir(localPath): # #Note: With dir uploads, the uploads are merged (added / overwritten) with existing paths. retry(lambda: _upload(SFTP, *pair(localPath, remotePath, item))) #pylint: disable=cell-var-from-loop else: # Path doesn't exist. raise Exception('Invalid source path: %s' % localPath) return remotePath
def test_requireAncestor(self): assert not requireAncestor(baseDir, tempDir) assert forgive(lambda: requireAncestor(tempDir, baseDir))
def exists(self, path): err = forgive(lambda: self._SFTP.stat(path)) return True if not err else (isinstance(err, IOError) and err.errno == errno.ENOENT)