def setUp(self): AbstractFilePathTests.setUp(self) zipit(self.cmn, self.cmn + b".zip") self.nativecmn = _coerceToFilesystemEncoding("", self.cmn) self.path = ZipArchive(self.cmn + b".zip") self.root = self.path self.all = [x.replace(self.cmn, self.cmn + b".zip") for x in self.all]
def zippedResource(self, request, archiveName, name, *names): archivePath = self._elementsRoot.child("{0}.zip".format(archiveName)) try: filePath = ZipArchive(archivePath.path) except IOError: self.log.error("Zip archive not found: {archive.path}", archive=archivePath) return self.notFoundResource(request) except BadZipfile: self.log.error("Bad zip archive: {archive.path}", archive=archivePath) return self.notFoundResource(request) filePath = filePath.child(name) for name in names: filePath = filePath.child(name) try: return filePath.getContent() except KeyError: self.log.error( "File not found in ZIP archive: {filePath.path}", filePath=filePath, archive=archivePath, ) return self.notFoundResource(request)
def test_zipArchiveRepr(self): """ Make sure that invoking ZipArchive's repr prints the correct class name and an absolute path to the zip file. """ path = ZipArchive(self.nativecmn + ".zip") pathRepr = "ZipArchive({!r})".format(os.path.abspath(self.nativecmn + ".zip")) # Check for an absolute path self.assertEqual(repr(path), pathRepr) # Create a path to the file rooted in the current working directory relativeCommon = self.nativecmn.replace(os.getcwd() + os.sep, "", 1) + ".zip" relpath = ZipArchive(relativeCommon) # Check using a path without the cwd prepended self.assertEqual(repr(relpath), pathRepr)
async def cachedZippedResource( self, request: IRequest, url: URL, archiveName: str, name: str, *names: Any, ) -> KleinRenderable: """ Retrieve a cached resource from a zip file. """ archivePath = await self.cacheFromURL(url, f"{archiveName}.zip") try: filePath = ZipArchive(str(archivePath)) except BadZipfile as e: self._log.error( "Corrupt zip archive {path}: {error}", path=archivePath, error=e, ) try: archivePath.unlink() except (OSError, IOError) as e: self._log.critical( "Failed to remove corrupt zip archive {path}: {error}", path=archivePath, error=e, ) return internalErrorResponse(request) except (OSError, IOError) as e: self._log.critical( "Unable to open zip archive {path}: {error}", path=archivePath, error=e, ) return notFoundResponse(request) filePath = filePath.child(name) for name in names: filePath = filePath.child(name) try: return filePath.getContent() except KeyError: self._log.error( "File not found in ZIP archive: {filePath.path}", filePath=filePath, archive=archivePath, ) return notFoundResponse(request)
def test_zipPathRepr(self): """ Make sure that invoking ZipPath's repr prints the correct class name and an absolute path to the zip file. """ child = self.path.child("foo") pathRepr = "ZipPath(%r)" % (os.path.abspath(self.cmn + ".zip" + os.sep + 'foo'), ) # Check for an absolute path self.assertEquals(repr(child), pathRepr) # Create a path to the file rooted in the current working directory relativeCommon = self.cmn.replace(os.getcwd() + os.sep, "", 1) + ".zip" relpath = ZipArchive(relativeCommon) child = relpath.child("foo") # Check using a path without the cwd prepended self.assertEquals(repr(child), pathRepr)
def mapPath(self, fsPathString): """ Map the given FS path to a ZipPath, by looking at the ZipImporter's "archive" attribute and using it as our ZipArchive root, then walking down into the archive from there. @return: a L{zippath.ZipPath} or L{zippath.ZipArchive} instance. """ za = ZipArchive(self.importer.archive) myPath = FilePath(self.importer.archive) itsPath = FilePath(fsPathString) if myPath == itsPath: return za # This is NOT a general-purpose rule for sys.path or __file__: # zipimport specifically uses regular OS path syntax in its pathnames. segs = itsPath.segmentsFrom(myPath) zp = za for seg in segs: zp = zp.child(seg) return zp
def cachedZippedResource(self, request, url, archiveName, name, *names): archivePath = yield self.cacheFromURL(url, "{0}.zip".format(archiveName)) try: filePath = ZipArchive(archivePath.path) except BadZipfile as e: self.log.error( "Corrupt zip archive {archive.path}: {error}", archive=archivePath, error=e, ) try: archivePath.remove() except (OSError, IOError): pass returnValue(self.notFoundResource(request)) except (OSError, IOError) as e: self.log.error( "Unable to open zip archive {archive.path}: {error}", archive=archivePath, error=e, ) returnValue(self.notFoundResource(request)) filePath = filePath.child(name) for name in names: filePath = filePath.child(name) try: returnValue(filePath.getContent()) except KeyError: self.log.error( "File not found in ZIP archive: {filePath.path}", filePath=filePath, archive=archivePath, ) returnValue(self.notFoundResource(request))
def setUp(self): AbstractFilePathTestCase.setUp(self) zipit(self.cmn, self.cmn + '.zip') self.path = ZipArchive(self.cmn + '.zip') self.root = self.path self.all = [x.replace(self.cmn, self.cmn + '.zip') for x in self.all]