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
def _Convert(self, profile): source = self.files.get(profile.source) if not source or not source.tempfile: # convert only if tempfile return False, () if not source: return False, [_(u"Image not found: ") + profile.source] p = DvPath() p.SetUniqueTempFileName() p.SetExtension(profile.extension) destPath = str(p) try: try: source.file.seek(0) except: pass try: iObj = Image.open(source) except IOError: # no file to be converted return False, () iObj = iObj.convert("RGB") # resize size = [profile.width, profile.height] if size[0] != 0 or size[1] != 0: if size[0] == 0: size[0] = size[1] elif size[1] == 0: size[1] = size[0] x, y = iObj.size if x > size[0]: y = y * size[0] / x; x = size[0] if y > size[1]: x = x * size[1] / y; y = size[1] size = x, y iObj = iObj.resize(size, Image.ANTIALIAS) iObj.save(destPath, profile.format) try: source.file.seek(0) except: pass # file meta data imgFile = open(destPath) filename = DvPath(profile.dest+"_"+source.filename) filename.SetExtension(profile.extension) file = File(filekey=profile.dest, filename=str(filename), file=imgFile, size=p.GetSize(), path=destPath, extension=profile.extension, tempfile=True) self.files.set(profile.dest, file) finally: # clean temp file p.Delete() return True, []
def test_fncs(self): temp = tempfile.gettempdir() p = DvPath(temp) p.AppendDirectory("tmp_nivepathtest_000") p.Delete(deleteSubdirs=True) p.CreateDirectories() self.assert_(p.IsDirectory()) p.CreateDirectoriesExcp() p.Rename("tmp_nivepathtest_111") p.AppendDirectory("tmp_nivepathtest_000") p.AppendDirectory("tmp_nivepathtest_000") p.CreateDirectories() self.assert_(p.IsDirectory()) p = DvPath(temp) p.AppendDirectory("tmp_nivepathtest_000") p.Delete(deleteSubdirs=False) self.assert_(p.IsDirectory() == True) p.Delete(deleteSubdirs=True) self.assert_(p.IsDirectory() == False)
def Cleanup(self, files): """ Cleanup tempfiles after succesful writes """ if not isinstance(files, dict): files = {"":files} for key, file in files.items(): if not hasattr(file, "deleteOnSuccess"): continue path = DvPath(file.deleteOnSuccess) try: path.Delete() except: pass
def tearDown(self): self.db.Close() p = DvPath(dbpath) p.Delete() pass
def setUp(self): p = DvPath(dbpath) p.Delete() self.db = Sqlite3Manager() self.db.Connect(dbpath) pass