def getTarInfo(tarFilePath,tarPath): tf=tarFilePath.getFile() tf.open('r') tar=tarfile.TarFile('','r',tf) try: ti=tar.getmember(tarPath) except KeyError,e: raise ufsi.PathNotFoundError('"%s" in "%s" not found' %(tarFilePath,tarPath))
def getDirList(self, pattern=None): """ TODO: a dir must end in a '/' char """ ms = TarUtils.getMembers(self.__path.getTarFilePath()) ms = map(lambda ti: ti.name, ms) ms = filter(lambda s: s.startswith(self.__tarPathStr), ms) if not ms: raise ufsi.PathNotFoundError('Path "%s" not found' % self.__path) ms = map(lambda s: s[len(self.__tarPathStr):], ms) ms = filter(lambda s: '/' not in s, ms) ms = filter(str, ms) return ms
def open(self,mode='r'): """ """ if mode in ['r','rb']: tarFile=self.__path.getTarFilePath() tf=tarFile.getFile() tf.open('r') self.__tarFileObject=tar=tarfile.TarFile('','r',tf) try: self.__fileHandle=tar.extractfile(self.__tarPathStr) except KeyError,e: raise ufsi.PathNotFoundError('Path "%s" not found' %self.__tarPathStr,e)
def getStat(self): """ Returns a dict of information about this file. Expects the path to NOT end in a '/'. """ try: s = self.__path.split() ftp = FtpUtils.getFtpConnection(self.__path) dl = FtpUtils.getDirList(ftp, s['urlPath']) ftp.quit() if not dl: raise ufsi.PathNotFoundError( 'Path "%s" not found.' % self.__path, e) return dl[s['fileName']] except Exception, e: FtpUtils.handleException(e, self.__pathStr)
def open(self, mode=None): """ Opens a file for reading or writing. Mode must be one of r, rb, w or wb. If the file was previously open it is first closed. Raises: * PathNotFoundError if the file couldn't be found. * InvalidArgumentError if the mode isn't one of the accepted values. """ # close any previously open file self.close() # do some validation on the mode if mode not in ('r', 'rb', 'w', 'wb'): raise ufsi.InvalidArgumentError('Invalid mode: "%s"' % mode) self.__openMode = mode urlPath = self.__path.split()['urlPath'] if not urlPath: raise ufsi.PathNotFoundError( 'FTP path "%s" contains no file path.' % self.__path) try: ftp = self.__ftpObject = FtpUtils.getFtpConnection(self.__path) # execute the appropriate ftp command(s) if 'b' in mode: ftp.voidcmd('TYPE I') else: ftp.voidcmd('TYPE A') if 'r' in mode: ftpCmd = 'RETR ' + urlPath else: ftpCmd = 'STOR ' + urlPath self.__ftpDataSocket = ftp.transfercmd(ftpCmd) self.__ftpDataFile = self.__ftpDataSocket.makefile(mode) except Exception, e: FtpUtils.handleException(e, self.__pathStr)
def handleException(e,path): """ Handles any exception that may be generated by this class. This class doesn't recover from an exception, it just raises an ufsi compliant exception. Postconditions: * An exception has been raised. TODO: move to HttpUtils.py """ if isinstance(e,urllib2.HTTPError): if e.code==404: raise ufsi.PathNotFoundError( 'HTTP server returned 404 error whilst ' 'trying to access url: "%s"'%path,e) else: # we don't know what it is so re-raise it raise e
def getStat(self): """ Returns a dict of information about this directory. Common values are: * size - size of the file system (as a string - may contain commas). TODO: remove commas from size. * permissions - a unix permissions string like: '-rwxrwxrwx' * owner - the owner of the file * group - the group of the file """ try: # We must find the dir name in it's parent directory s=self.__path.split() urlPath=s['urlPath'] if urlPath.endswith('/'): urlPath.pop() # Currently can't get any details from the root dir if urlPath=='': return {} # get parent dir (even if it's empty) parentDir='' dirName=urlPath if '/' in urlPath: (parentDir,dirName)=urlPath.rsplit('/',1) # cater for a urlPath of /dir parentDir+='/' ftp=FtpUtils.getFtpConnection(self.__path) dirList=FtpUtils.getDirList(ftp,parentDir) if dirName not in dirList: raise ufsi.PathNotFoundError('Path "%s" not found.' %self.__path) return dirList[dirName] except Exception,e: FtpUtils.handleException(e,self.__pathStr)
def _raisePathNotFoundError(e, path): raise ufsi.PathNotFoundError('Path "%s" not found.' % path, e)