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)
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 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 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)
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.assertEqual(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.assertEqual(repr(child), pathRepr)
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 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))
class ZipFilePathTestCase(AbstractFilePathTestCase): """ Test various L{ZipPath} path manipulations as well as reprs for L{ZipPath} and L{ZipArchive}. """ 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] 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 test_zipArchiveRepr(self): """ Make sure that invoking ZipArchive's repr prints the correct class name and an absolute path to the zip file. """ pathRepr = 'ZipArchive(%r)' % (os.path.abspath(self.cmn + '.zip'),) # Check for an absolute path self.assertEquals(repr(self.path), 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) # Check using a path without the cwd prepended self.assertEquals(repr(relpath), pathRepr)
class ZipFilePathTestCase(AbstractFilePathTestCase): """ Test various L{ZipPath} path manipulations as well as reprs for L{ZipPath} and L{ZipArchive}. """ 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] 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 test_zipArchiveRepr(self): """ Make sure that invoking ZipArchive's repr prints the correct class name and an absolute path to the zip file. """ pathRepr = 'ZipArchive(%r)' % (os.path.abspath(self.cmn + '.zip'), ) # Check for an absolute path self.assertEquals(repr(self.path), 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) # Check using a path without the cwd prepended self.assertEquals(repr(relpath), 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 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]
class ZipFilePathTestCase(AbstractFilePathTestCase): """ Test various L{ZipPath} path manipulations as well as reprs for L{ZipPath} and L{ZipArchive}. """ 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] 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 test_zipPathReprParentDirSegment(self): """ The repr of a ZipPath with C{".."} in the internal part of its path includes the C{".."} rather than applying the usual parent directory meaning. """ child = self.path.child("foo").child("..").child("bar") pathRepr = "ZipPath(%r)" % ( self.cmn + ".zip" + os.sep.join(["", "foo", "..", "bar"])) self.assertEquals(repr(child), pathRepr) def test_zipPathReprEscaping(self): """ Bytes in the ZipPath path which have special meaning in Python string literals are escaped in the ZipPath repr. """ child = self.path.child("'") path = self.cmn + ".zip" + os.sep.join(["", "'"]) pathRepr = "ZipPath('%s')" % (path.encode('string-escape'),) self.assertEquals(repr(child), pathRepr) def test_zipArchiveRepr(self): """ Make sure that invoking ZipArchive's repr prints the correct class name and an absolute path to the zip file. """ pathRepr = 'ZipArchive(%r)' % (os.path.abspath(self.cmn + '.zip'),) # Check for an absolute path self.assertEquals(repr(self.path), 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) # Check using a path without the cwd prepended self.assertEquals(repr(relpath), pathRepr)
def readFromArchive(_): filePath = ZipArchive(archivePath.path) for segment in segments: filePath = filePath.child(segment) return filePath.getContent()
class ZipFilePathTests(AbstractFilePathTests): """ Test various L{ZipPath} path manipulations as well as reprs for L{ZipPath} and L{ZipArchive}. """ 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 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.nativecmn + ".zip" + os.sep + 'foo'),) # Check for an absolute path self.assertEqual(repr(child), 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) child = relpath.child("foo") # Check using a path without the cwd prepended self.assertEqual(repr(child), pathRepr) def test_zipPathReprParentDirSegment(self): """ The repr of a ZipPath with C{".."} in the internal part of its path includes the C{".."} rather than applying the usual parent directory meaning. """ child = self.path.child("foo").child("..").child("bar") pathRepr = "ZipPath(%r)" % ( self.nativecmn + ".zip" + os.sep.join(["", "foo", "..", "bar"])) self.assertEqual(repr(child), pathRepr) 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)' % (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)
class ZipFilePathTestCase(AbstractFilePathTestCase): """ Test various L{ZipPath} path manipulations as well as reprs for L{ZipPath} and L{ZipArchive}. """ 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] 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 test_zipPathReprParentDirSegment(self): """ The repr of a ZipPath with C{".."} in the internal part of its path includes the C{".."} rather than applying the usual parent directory meaning. """ child = self.path.child("foo").child("..").child("bar") pathRepr = "ZipPath(%r)" % (self.cmn + ".zip" + os.sep.join(["", "foo", "..", "bar"])) self.assertEquals(repr(child), pathRepr) def test_zipPathReprEscaping(self): """ Bytes in the ZipPath path which have special meaning in Python string literals are escaped in the ZipPath repr. """ child = self.path.child("'") path = self.cmn + ".zip" + os.sep.join(["", "'"]) pathRepr = "ZipPath('%s')" % (path.encode('string-escape'), ) self.assertEquals(repr(child), pathRepr) def test_zipArchiveRepr(self): """ Make sure that invoking ZipArchive's repr prints the correct class name and an absolute path to the zip file. """ pathRepr = 'ZipArchive(%r)' % (os.path.abspath(self.cmn + '.zip'), ) # Check for an absolute path self.assertEquals(repr(self.path), 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) # Check using a path without the cwd prepended self.assertEquals(repr(relpath), pathRepr)
class ZipFilePathTests(AbstractFilePathTests): """ Test various L{ZipPath} path manipulations as well as reprs for L{ZipPath} and L{ZipArchive}. """ 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 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})".format( os.path.abspath(self.nativecmn + ".zip" + os.sep + "foo"), ) # Check for an absolute path self.assertEqual(repr(child), 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) child = relpath.child("foo") # Check using a path without the cwd prepended self.assertEqual(repr(child), pathRepr) def test_zipPathReprParentDirSegment(self): """ The repr of a ZipPath with C{".."} in the internal part of its path includes the C{".."} rather than applying the usual parent directory meaning. """ child = self.path.child("foo").child("..").child("bar") pathRepr = "ZipPath(%r)" % ( self.nativecmn + ".zip" + os.sep.join(["", "foo", "..", "bar"]) ) self.assertEqual(repr(child), pathRepr) 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)