Example #1
0
def openfile(*paths, **kwargs):
    '''
    Returns a read-only file-like object even if the path terminates inside a container file.

    If the path is a regular os accessible path mode may be passed through as a keyword argument.
    If the path terminates in a container file, mode is ignored.

    If req=True (Default) NoSuchPath will also be raised if the path exists, but is a directory

    Example:
        fd = openfile('/foo/bar/baz.egg/path/inside/zip/to/file')
        if fd == None:
            return
        fbuf = fd.read()
        fd.close()
    '''
    reqd = kwargs.get('req', True)
    mode = kwargs.get('mode', 'r')
    fpath = parsePath(*paths)
    paths = [p for p in paths if p]

    if not fpath:
        if reqd:
            raise s_exc.NoSuchPath(path='/'.join(paths))
        return None
    if not fpath.isfile():
        if reqd:
            fpath.close()
            raise s_exc.NoSuchPath(path=fpath.path())
        return None

    return FpOpener(fpath)
Example #2
0
def openfiles(*paths, **kwargs):
    '''
    Yields a read-only file-like object for each path even if the path terminates inside a container file.
    Paths may use python's fnmatch glob matching

    If the path is a regular os accessible path mode may be passed through as a keyword argument.
    If the path terminates in a container file, mode is ignored.

    If req=True (Default) NoSuchPath will also be raised if ANY matching path exists, but is a directory

    Example:
        for fd in openfiles('/foo/bar/*.egg/dir0/zz*/nest.zip'):
            fbuf = fd.read()
    '''
    reqd = kwargs.get('req', False)
    mode = kwargs.get('mode', 'r')

    nopaths = True
    for fpath in parsePaths(*paths):
        nopaths = False
        if not fpath.isfile():
            if not reqd:
                continue
            fpath.close()
            raise s_exc.NoSuchPath(path=fpath.path())
        yield FpOpener(fpath)
    if nopaths and reqd:
        raise s_exc.NoSuchPath(path='/'.join(paths))
Example #3
0
    def next(self):
        '''
        This is the workhorse method for path specific processing of container children.
        The object should consume as much as possible of the path before instantiating a new
        object

        At a minimum, each container should override:
        innrOpen(path)
        innrEnum(path)
        '''

        # get longest consistent path
        partlen = len(self.pparts)

        # the end of the line
        if self.idx == partlen - 1:
            return

        # since it's a container we only care about the "inner path"
        maxpath = None
        checkpath = None
        cidx = self.idx + 1

        while cidx < partlen:
            checkpath = normpath(*self.pparts[self.idx + 1:cidx + 1])
            if not self.innrExists(checkpath):
                break
            maxpath = checkpath
            cidx += 1

        if maxpath is None:
            self.close()
            raise s_exc.NoSuchPath(path=os.path.join(*self.pparts))
        self.maxidx = cidx

        # if the max path is a dir, we're finished
        if self.innrIsdir(maxpath):
            self._isfile = False

            if self.maxidx != partlen:
                self.close()
                raise s_exc.NoSuchPath(path=os.path.join(*self.pparts))

        # if end of the path we're finished
        if self.maxidx == partlen:
            return

        # supported file
        tempfd = self.innrTmpExtract(maxpath)

        cls = _fdClass(tempfd)
        if not cls:
            return
        tempfd.seek(0)

        return cls(self.pparts, self.maxidx - 1, parent=self, fd=tempfd)
Example #4
0
    async def getCellApi(self, link, user, path):

        if not path:
            return await CryoApi.anit(self, link, user)

        if len(path) == 1:
            tank = await self.init(path[0])
            return await TankApi.anit(tank, link, user)

        raise s_exc.NoSuchPath(path=path)
Example #5
0
def _pathClass(*paths):
    '''
    Returns the class to handle the type of item located at path.  This function
    only operates on regular os.accessible paths
    '''
    path = s_common.genpath(*paths)
    if not os.path.exists(path):
        raise s_exc.NoSuchPath(path=path)

    if os.path.isdir(path):
        return path_ctors.get('fs.reg.file')
    with open(path, 'rb') as fd:
        mime = _mimeFile(fd)
    return path_ctors.get(mime)
Example #6
0
    def next(self):
        '''
        This is the workhorse method that can contain path specific processing of children.
        The object should consume as much as possible of the path before creating the
        child class

        NOTE: Override for container formats
        '''

        # get longest consistent path
        partlen = len(self.pparts)
        # the end of the line
        if self.idx == partlen - 1:
            if os.path.isdir(s_common.genpath(*self.pparts[:self.idx + 1])):
                self._isfile = False
            return

        maxpath = None
        checkpath = s_common.genpath(*self.pparts[:self.idx + 1])
        cidx = self.idx + 1

        while cidx < partlen:
            checkpath = s_common.genpath(*self.pparts[:cidx + 1])
            if not os.path.exists(checkpath):
                break
            maxpath = checkpath
            cidx += 1

        self.maxidx = cidx

        # if the max path is a dir, we're finished
        if os.path.isdir(maxpath):
            self._isfile = False

            if self.maxidx != partlen:
                self.close()
                raise s_exc.NoSuchPath(path=os.path.join(*self.pparts))

        # if end of the path we're finished
        if self.maxidx == partlen:
            return

        # supported container file
        cls = _pathClass(*self.pparts[:self.maxidx])
        return cls(self.pparts, self.maxidx - 1, parent=self)
Example #7
0
    async def setPathLink(self, srcpath, dstpath):
        '''
        Add a link from the given srcpath to the dstpath.
        NOTE: This causes the item at dstpath to be incref'd
        '''
        srcpkey = self._pathToPkey(srcpath)
        dstpkey = self._pathToPkey(dstpath)

        buid = self.slab.get(dstpkey, db=self.pathdb)
        if buid is None:
            raise s_exc.NoSuchPath(path=dstpath)

        oldb = self.slab.pop(srcpkey, db=self.pathdb)
        if oldb is not None:
            self._incRefObj(oldb, valu=-1)

        self._incRefObj(buid, valu=1)
        self.slab.put(srcpkey, buid, db=self.pathdb)