Ejemplo n.º 1
0
    def isDir(self):
        """
        Attempts to determine whether the dir exists or not:
          Get the list of the parent dir.
          If the dirname is in parent dir and 'd' starts permissions.
        """
        try:
            ftp=FtpUtils.getFtpConnection(self)
            urlPath=self.split()['urlPath']
            # there has to be a '' path if we found the server.
            if urlPath=='':
                ftp.close()
                return True

            if urlPath.endswith('/'):
                urlPath.pop()

            parentDir=''
            dirName=urlPath
            if '/' in urlPath:
                (parentDir,dirName)=urlPath.rsplit('/',1)
            d=FtpUtils.getDirList(ftp,parentDir)
            ftp.quit()
            if dirName in d:
                if d[dirName]['permissions'].startswith('d'):
                    return True
            return False
        except  Exception,e:
            try:
                FtpUtils.handleException(e,self)
            except ufsi.PathNotFoundError,e:
                return False
Ejemplo n.º 2
0
    def isDir(self):
        """
        Attempts to determine whether the dir exists or not:
          Get the list of the parent dir.
          If the dirname is in parent dir and 'd' starts permissions.
        """
        try:
            ftp = FtpUtils.getFtpConnection(self)
            urlPath = self.split()['urlPath']
            # there has to be a '' path if we found the server.
            if urlPath == '':
                ftp.close()
                return True

            if urlPath.endswith('/'):
                urlPath.pop()

            parentDir = ''
            dirName = urlPath
            if '/' in urlPath:
                (parentDir, dirName) = urlPath.rsplit('/', 1)
            d = FtpUtils.getDirList(ftp, parentDir)
            ftp.quit()
            if dirName in d:
                if d[dirName]['permissions'].startswith('d'):
                    return True
            return False
        except Exception, e:
            try:
                FtpUtils.handleException(e, self)
            except ufsi.PathNotFoundError, e:
                return False
Ejemplo n.º 3
0
 def getDirList(self,filter=None,sort=None):
     """
     Returns a list of strings, the name of each dir or file in
     this directory.
     """
     try:
         ftp=FtpUtils.getFtpConnection(self.__path)
         d=FtpUtils.getDirList(ftp,self.__path.split()['urlPath'])
     except Exception,e:
         FtpUtils.handleException(e,self.__pathStr)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
 def isFile(self):
     """
     Attempts to determine whether the file exists or not:
       Get the list of the parent dir.
       If the filename is in the parent dir and '-' starts the
       permissions.
     
     This is simpler than isDir because we can list a file its
     self, and get the details from that.
     
     TODO: Exception: If filename is a dir and also has a filename
     file within it.
     
     """
     try:
         ftp = FtpUtils.getFtpConnection(self)
         d = FtpUtils.getDirList(ftp, self.split()['urlPath'])
         ftp.quit()
     except Exception, e:
         try:
             FtpUtils.handleException(e, self)
         except ufsi.PathNotFoundError, e:
             ftp.quit()
             return False
Ejemplo n.º 10
0
 def isFile(self):
     """
     Attempts to determine whether the file exists or not:
       Get the list of the parent dir.
       If the filename is in the parent dir and '-' starts the
       permissions.
     
     This is simpler than isDir because we can list a file its
     self, and get the details from that.
     
     TODO: Exception: If filename is a dir and also has a filename
     file within it.
     
     """
     try:
         ftp=FtpUtils.getFtpConnection(self)
         d=FtpUtils.getDirList(ftp,self.split()['urlPath'])
         ftp.quit()
     except Exception,e:
         try:
             FtpUtils.handleException(e,self)
         except ufsi.PathNotFoundError,e:
             ftp.quit()
             return False