Esempio n. 1
0
    def commitTemp(self, fileentry):
        """
        This functions writes the file to the pool directory. If the file is not marked
        as tempfile, nothing is written.
        
        Files are processed in the following order:
        - a temp path is created
        - the file is written to this path
        - the original file is renamed to be deleted on success and stored as `file.deleteOnSuccess`
        - the tempfile is renamed to the original path
        - the original file can be removed by calling `Cleanup()`
        
        fileentry is the database entry the file is stored for.
        """
        if not self.isTempFile():
            # nothing to write -> return
            return True
        if not self.fileentry:
            self.fileentry = weakref.ref(fileentry)

        maxFileSize = fileentry.maxFileSize
        if self.size and self.size > maxFileSize:
            raise IOError, "File too big"

        # create temp path for current
        backupPath = None
        originalPath = DvPath(self._Path())

        newPath = DvPath(self._CreatePath(self.filekey, self.filename))
        tempPath = DvPath(str(newPath))
        tempPath.SetName(u"_temp_" + unicode(uuid.uuid4()))
        tempPath.SetExtension(newPath.GetExtension())

        if tempPath.Exists():
            tempPath.Delete()
        tempPath.CreateDirectories()
        size = 0
        try:
            out = open(tempPath.GetStr(), "wb")
            data = self.read(10000)
            while data:
                size += len(data)
                if maxFileSize and size > maxFileSize:
                    raise IOError, "File too big"
                out.write(data)
                data = self.read(10000)
            out.close()
            #file.close()
        except Exception, e:
            try:
                self.file.close()
            except:
                pass
            try:
                out.close()
            except:
                pass
            # reset old file
            tempPath.Delete()
            raise Exception, e
Esempio n. 2
0
 def test_Get(self):
     n = self.name
     p = DvPath(n)
     self.assert_(p.GetPath() == self.base)
     self.assert_(p.GetName() == "nofile")
     self.assert_(p.GetExtension() == "txt")
     self.assert_(p.GetNameExtension() == "nofile.txt")
     self.assert_(p.GetSize() == -1)
     self.assert_(p.GetLastDirectory() == "tmp_nivepathtest_000")
     p.IsFile()
     p.IsDirectory()
     p.Exists()
     p.IsValid()
Esempio n. 3
0
 def test_tempfilename(self):
     p = DvPath("file.txt")
     p.SetUniqueTempFileName()
     self.assert_(p.GetExtension() == "txt", p.GetExtension())