Ejemplo n.º 1
0
def gen_obj(path,
            stat=None,
            chksum_handlers=None,
            real_location=None,
            stat_func=os.lstat,
            **overrides):
    """
    given a fs path, and an optional stat, create an appropriate fs obj.

    :param stat: stat object to reuse if available
    :param real_location: real path to the object if path is the desired
        location, rather then existent location.
    :raise KeyError: if no obj type matches the stat checks
    :return: :obj:`pkgcore.fs.fs.fsBase` derivative
    """

    if real_location is None:
        real_location = path
    if stat is None:
        try:
            stat = stat_func(real_location)
        except EnvironmentError as e:
            if stat_func == os.lstat or e.errno != errno.ENOENT:
                raise
            stat = os.lstat(real_location)

    mode = stat.st_mode
    d = {
        "mtime": stat.st_mtime,
        "mode": S_IMODE(mode),
        "uid": stat.st_uid,
        "gid": stat.st_gid
    }
    if S_ISREG(mode):
        d["size"] = stat.st_size
        d["data"] = local_source(real_location)
        d["dev"] = stat.st_dev
        d["inode"] = stat.st_ino
        if chksum_handlers is not None:
            d["chf_types"] = chksum_handlers
        d.update(overrides)
        return fsFile(path, **d)

    d.update(overrides)
    if S_ISDIR(mode):
        return fsDir(path, **d)
    elif S_ISLNK(mode):
        d["target"] = os.readlink(real_location)
        return fsSymlink(path, **d)
    elif S_ISFIFO(mode):
        return fsFifo(path, **d)
    else:
        major, minor = get_major_minor(stat)
        d["minor"] = minor
        d["major"] = major
        d["mode"] = mode
        return fsDev(path, **d)
Ejemplo n.º 2
0
def gen_obj(path, stat=None, chksum_handlers=None, real_location=None,
            stat_func=os.lstat, **overrides):
    """
    given a fs path, and an optional stat, create an appropriate fs obj.

    :param stat: stat object to reuse if available
    :param real_location: real path to the object if path is the desired
        location, rather then existent location.
    :raise KeyError: if no obj type matches the stat checks
    :return: :obj:`pkgcore.fs.fs.fsBase` derivative
    """

    if real_location is None:
        real_location = path
    if stat is None:
        try:
            stat = stat_func(real_location)
        except EnvironmentError as e:
            if stat_func == os.lstat or e.errno != errno.ENOENT:
                raise
            stat = os.lstat(real_location)

    mode = stat.st_mode
    d = {"mtime":stat.st_mtime, "mode":S_IMODE(mode),
         "uid":stat.st_uid, "gid":stat.st_gid}
    if S_ISREG(mode):
        d["size"] = stat.st_size
        d["data"] = local_source(real_location)
        d["dev"] = stat.st_dev
        d["inode"] = stat.st_ino
        if chksum_handlers is not None:
            d["chf_types"] = chksum_handlers
        d.update(overrides)
        return fsFile(path, **d)

    d.update(overrides)
    if S_ISDIR(mode):
        return fsDir(path, **d)
    elif S_ISLNK(mode):
        d["target"] = os.readlink(real_location)
        return fsSymlink(path, **d)
    elif S_ISFIFO(mode):
        return fsFifo(path, **d)
    else:
        major, minor = get_major_minor(stat)
        d["minor"] = minor
        d["major"] = major
        d["mode"] = mode
        return fsDev(path, **d)
Ejemplo n.º 3
0
 def __init__(self, path, **kwds):
     if any(x not in kwds for x in ("major", "minor", "mode")):
         try:
             st = os.lstat(path)
         except FileNotFoundError:
             st = None
         if st is None or any(f(st.st_mode) for f in
             (stat.S_ISREG, stat.S_ISDIR, stat.S_ISFIFO)):
             kwds["strict"] = True
         else:
             major, minor = fs.get_major_minor(st)
             kwds["major"] = major
             kwds["minor"] = minor
             kwds["mode"] = st.st_mode
     super().__init__(path, **kwds)
Ejemplo n.º 4
0
 def __init__(self, path, **kwds):
     if any(x not in kwds for x in ("major", "minor", "mode")):
         try:
             st = os.lstat(path)
         except OSError, oe:
             if oe.errno != errno.ENOENT:
                 raise
             st = None
         if st is None or any(f(st.st_mode) for f in
             (stat.S_ISREG, stat.S_ISDIR, stat.S_ISFIFO)):
             kwds["strict"] = True
         else:
             major, minor = fs.get_major_minor(st)
             kwds["major"] = major
             kwds["minor"] = minor
             kwds["mode"] = st.st_mode
Ejemplo n.º 5
0
 def __init__(self, path, **kwds):
     if any(x not in kwds for x in ("major", "minor", "mode")):
         try:
             st = os.lstat(path)
         except OSError as oe:
             if oe.errno != errno.ENOENT:
                 raise
             st = None
         if st is None or any(f(st.st_mode) for f in
             (stat.S_ISREG, stat.S_ISDIR, stat.S_ISFIFO)):
             kwds["strict"] = True
         else:
             major, minor = fs.get_major_minor(st)
             kwds["major"] = major
             kwds["minor"] = minor
             kwds["mode"] = st.st_mode
     fs.fsDev.__init__(self, path, **kwds)
Ejemplo n.º 6
0
        d["inode"] = stat.st_ino
        if chksum_handlers is not None:
            d["chf_types"] = chksum_handlers
        d.update(overrides)
        return fsFile(path, **d)

    d.update(overrides)
    if S_ISDIR(mode):
        return fsDir(path, **d)
    elif S_ISLNK(mode):
        d["target"] = os.readlink(real_location)
        return fsSymlink(path, **d)
    elif S_ISFIFO(mode):
        return fsFifo(path, **d)
    else:
        major, minor = get_major_minor(stat)
        d["minor"] = minor
        d["major"] = major
        d["mode"] = mode
        return fsDev(path, **d)


# hmm. this code is roughly 25x slower then find.
# make it less slow somehow. the obj instantiation is a bit of a
# killer I'm afraid; without obj, looking at 2.3ms roughly best of 3
# 100 iterations, obj instantiation, 58ms.
# also, os.path.join is rather slow.
# in this case, we know it's always pegging one more dir on, so it's
# fine doing it this way (specially since we're relying on
# os.path.sep, not '/' :P)