def __init__(self, archivePathname): """ Create a ZipArchive, treating the archive at archivePathname as a zip file. @param archivePathname: a L{bytes} or L{unicode}, naming a path in the filesystem. """ self.path = archivePathname self.zipfile = ZipFile(_coerceToFilesystemEncoding('', archivePathname)) self.pathInArchive = _coerceToFilesystemEncoding(archivePathname, '') # zipfile is already wasting O(N) memory on cached ZipInfo instances, # so there's no sense in trying to do this lazily or intelligently self.childmap = {} # map parent: list of children for name in self.zipfile.namelist(): name = _coerceToFilesystemEncoding(self.path, name).split(self.sep) for x in range(len(name)): child = name[-x] parent = self.sep.join(name[:-x]) if parent not in self.childmap: self.childmap[parent] = {} self.childmap[parent][child] = 1 parent = _coerceToFilesystemEncoding(archivePathname, '')
def zipit(dirname, zfname): """ Create a zipfile on zfname, containing the contents of dirname' """ dirname = _coerceToFilesystemEncoding('', dirname) zfname = _coerceToFilesystemEncoding('', zfname) with zipfile.ZipFile(zfname, "w") as zf: for root, ignored, files, in os.walk(dirname): for fname in files: fspath = os.path.join(root, fname) arcpath = os.path.join(root, fname)[len(dirname)+1:] zf.write(fspath, arcpath)
def child(self, path): """ Return a new ZipPath representing a path in C{self.archive} which is a child of this path. @note: Requesting the C{".."} (or other special name) child will not cause L{InsecurePath} to be raised since these names do not have any special meaning inside a zip archive. Be particularly careful with the C{path} attribute (if you absolutely must use it) as this means it may include special names with special meaning outside of the context of a zip archive. """ joiner = _coerceToFilesystemEncoding(path, ZIP_PATH_SEP) pathInArchive = _coerceToFilesystemEncoding(path, self.pathInArchive) return ZipPath(self.archive, joiner.join([pathInArchive, path]))
def __repr__(self): factoryName = reflect.qual(self.factory.__class__) if hasattr(self, 'socket'): return '<%s on %r>' % ( factoryName, _coerceToFilesystemEncoding('', self.port)) else: return '<%s (not listening)>' % (factoryName,)
def render(self, request): """ Render me to a web client. Load my file, execute it in a special namespace (with 'request' and '__file__' global vars) and finish the request. Output to the web-page will NOT be handled with print - standard output goes to the log - but with request.write. """ request.setHeader(b"x-powered-by", networkString("Twisted/%s" % copyright.version)) namespace = {'request': request, '__file__': _coerceToFilesystemEncoding("", self.filename), 'registry': self.registry} try: execfile(self.filename, namespace, namespace) except IOError as e: if e.errno == 2: #file not found request.setResponseCode(http.NOT_FOUND) request.write(resource.NoResource("File not found.").render(request)) except: io = NativeStringIO() traceback.print_exc(file=io) output = util._PRE(io.getvalue()) if _PY3: output = output.encode("utf8") request.write(output) request.finish() return server.NOT_DONE_YET
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 getsize(self): """ Retrieve this file's size. @return: file size, in bytes """ pathInArchive = _coerceToFilesystemEncoding("", self.pathInArchive) return self.archive.zipfile.NameToInfo[pathInArchive].file_size
def sep(self): """ Return a zip directory separator. @return: The zip directory separator. @returntype: The same type as C{self.path}. """ return _coerceToFilesystemEncoding(self.path, ZIP_PATH_SEP)
def __init__(self, archive, pathInArchive): """ Don't construct me directly. Use C{ZipArchive.child()}. @param archive: a L{ZipArchive} instance. @param pathInArchive: a ZIP_PATH_SEP-separated string. """ self.archive = archive self.pathInArchive = pathInArchive # self.path pretends to be os-specific because that's the way the # 'zipimport' module does it. sep = _coerceToFilesystemEncoding(pathInArchive, ZIP_PATH_SEP) archiveFilename = _coerceToFilesystemEncoding( pathInArchive, archive.zipfile.filename) self.path = os.path.join(archiveFilename, *(self.pathInArchive.split(sep)))
def getTypeAndEncoding(filename, types, encodings, defaultType): p, ext = filepath.FilePath(filename).splitext() ext = filepath._coerceToFilesystemEncoding('', ext.lower()) if ext in encodings: enc = encodings[ext] ext = os.path.splitext(p)[1].lower() else: enc = None type = types.get(ext, defaultType) return type, enc
def getModificationTime(self): """ Retrieve this file's last modification time. This is the time of modification recorded in the zipfile. @return: a number of seconds since the epoch. """ pathInArchive = _coerceToFilesystemEncoding("", self.pathInArchive) return time.mktime( self.archive.zipfile.NameToInfo[pathInArchive].date_time + (0, 0, 0))
def startListening(self): """ Create and bind my socket, and begin listening on it. This is called on unserialization, and must be called after creating a server to begin listening on the specified port. """ tcp._reservedFD.reserve() log.msg( "%s starting on %r" % ( self._getLogPrefix(self.factory), _coerceToFilesystemEncoding("", self.port), ) ) if self.wantPID: self.lockFile = lockfile.FilesystemLock(self.port + b".lock") if not self.lockFile.lock(): raise error.CannotListenError(None, self.port, "Cannot acquire lock") else: if not self.lockFile.clean: try: # This is a best-attempt at cleaning up # left-over unix sockets on the filesystem. # If it fails, there's not much else we can # do. The bind() below will fail with an # exception that actually propagates. if stat.S_ISSOCK(os.stat(self.port).st_mode): os.remove(self.port) except BaseException: pass self.factory.doStart() try: if self._preexistingSocket is not None: skt = self._preexistingSocket self._preexistingSocket = None else: skt = self.createInternetSocket() skt.bind(self.port) except OSError as le: raise error.CannotListenError(None, self.port, le) else: if _inFilesystemNamespace(self.port): # Make the socket readable and writable to the world. os.chmod(self.port, self.mode) skt.listen(self.backlog) self.connected = True self.socket = skt self.fileno = self.socket.fileno self.numberAccepts = 100 self.startReading()
def ResourceTemplate(path, registry): from quixote import ptl_compile glob = {'__file__': _coerceToFilesystemEncoding("", path), 'resource': resource.ErrorPage(500, "Whoops! Internal Error", rpyNoResource), 'registry': registry} e = ptl_compile.compile_template(open(path), path) code = compile(e, "<source>", "exec") eval(code, glob, glob) return glob['resource']
def _logConnectionLostMsg(self): """ Log message for closing socket """ log.msg( "(UNIX Port %s Closed)" % ( _coerceToFilesystemEncoding( "", self.port, ) ) )
def startListening(self): """ Create and bind my socket, and begin listening on it. This is called on unserialization, and must be called after creating a server to begin listening on the specified port. """ log.msg("%s starting on %r" % ( self._getLogPrefix(self.factory), _coerceToFilesystemEncoding('', self.port))) if self.wantPID: self.lockFile = lockfile.FilesystemLock(self.port + b".lock") if not self.lockFile.lock(): raise error.CannotListenError(None, self.port, "Cannot acquire lock") else: if not self.lockFile.clean: try: # This is a best-attempt at cleaning up # left-over unix sockets on the filesystem. # If it fails, there's not much else we can # do. The bind() below will fail with an # exception that actually propagates. if stat.S_ISSOCK(os.stat(self.port).st_mode): os.remove(self.port) except: pass self.factory.doStart() try: if self._preexistingSocket is not None: skt = self._preexistingSocket self._preexistingSocket = None else: skt = self.createInternetSocket() skt.bind(self.port) except socket.error as le: raise error.CannotListenError(None, self.port, le) else: if _inFilesystemNamespace(self.port): # Make the socket readable and writable to the world. os.chmod(self.port, self.mode) skt.listen(self.backlog) self.connected = True self.socket = skt self.fileno = self.socket.fileno self.numberAccepts = 100 self.startReading()
def __init__(self, archivePathname): """ Create a ZipArchive, treating the archive at archivePathname as a zip file. @param archivePathname: a L{bytes} or L{unicode}, naming a path in the filesystem. """ self.path = archivePathname self.zipfile = ZipFile(_coerceToFilesystemEncoding("", archivePathname)) self.pathInArchive = _coerceToFilesystemEncoding(archivePathname, "") # zipfile is already wasting O(N) memory on cached ZipInfo instances, # so there's no sense in trying to do this lazily or intelligently self.childmap: Dict[str, Dict[str, int]] = {} for name in self.zipfile.namelist(): name = _coerceToFilesystemEncoding(self.path, name).split(self.sep) for x in range(len(name)): child = name[-x] parent = self.sep.join(name[:-x]) if parent not in self.childmap: self.childmap[parent] = {} self.childmap[parent][child] = 1 parent = _coerceToFilesystemEncoding(archivePathname, "")
def ResourceTemplate(path, registry): from quixote import ptl_compile # type: ignore[import] glob = { "__file__": _coerceToFilesystemEncoding("", path), "resource": resource.ErrorPage(500, "Whoops! Internal Error", rpyNoResource), "registry": registry, } with open(path) as f: # Not closed by quixote as of 2.9.1 e = ptl_compile.compile_template(f, path) code = compile(e, "<source>", "exec") eval(code, glob, glob) return glob["resource"]
def connected(protocols): client, server, port = protocols try: portPath = _coerceToFilesystemEncoding('', port.getHost().name) self.assertEqual( "<AccumulatingProtocol #%s on %s>" % (server.transport.sessionno, portPath), str(server.transport)) self.assertEqual( "AccumulatingProtocol,%s,%s" % (server.transport.sessionno, portPath), server.transport.logstr) peerAddress = server.factory.peerAddresses[0] self.assertIsInstance(peerAddress, UNIXAddress) finally: # Be certain to drop the connection so the test completes. server.transport.loseConnection()
def ResourceScript(path, registry): """ I am a normal py file which must define a 'resource' global, which should be an instance of (a subclass of) web.resource.Resource; it will be renderred. """ cs = CacheScanner(path, registry) glob = {'__file__': _coerceToFilesystemEncoding("", path), 'resource': noRsrc, 'registry': registry, 'cache': cs.cache, 'recache': cs.recache} try: execfile(path, glob, glob) except AlreadyCached as ac: return ac.args[0] rsrc = glob['resource'] if cs.doCache and rsrc is not noRsrc: registry.cachePath(path, rsrc) return rsrc
def __repr__(self): parts = [_coerceToFilesystemEncoding( self.sep, os.path.abspath(self.archive.path))] parts.extend(self.pathInArchive.split(self.sep)) ossep = _coerceToFilesystemEncoding(self.sep, os.sep) return "ZipPath(%r)" % (ossep.join(parts),)
def open(self, mode="r"): pathInArchive = _coerceToFilesystemEncoding('', self.pathInArchive) return self.archive.zipfile.open(pathInArchive, mode=mode)
def _logConnectionLostMsg(self): """ Log message for closing socket """ log.msg('(UNIX Port %s Closed)' % ( _coerceToFilesystemEncoding('', self.port,)))
def __repr__(self): name = self.name if name: name = _coerceToFilesystemEncoding('', self.name) return 'UNIXAddress(%r)' % (name,)
def __repr__(self) -> str: name = self.name if name: name = _coerceToFilesystemEncoding("", self.name) return "UNIXAddress(%r)" % (name, )
def __repr__(self) -> str: name = self.name if name: name = _coerceToFilesystemEncoding("", self.name) return f"UNIXAddress({name!r})"