How to use the pkgcore.fs.fs function in pkgcore

To help you get started, we’ve selected a few pkgcore examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pkgcore / pkgcore / tests / module / fs / test_fs.py View on Github external
def test_init(self):
        base.test_init(self)
        mkobj = self.make_obj
        self.assertRaises(TypeError, mkobj, major=-1, strict=True)
        self.assertRaises(TypeError, mkobj, minor=-1, strict=True)
        self.assertEqual(mkobj(major=1).major, 1)
        self.assertEqual(mkobj(minor=1).minor, 1)


class Test_fsFifo(TestCase, base):
    kls = fs.fsFifo


class Test_fsDir(TestCase, base):
    kls = fs.fsDir


class Test_Modules_Funcs(TestCase):

    def test_is_funcs(self):
        # verify it intercepts the missing attr
        self.assertFalse(fs.isdir(object()))
        self.assertFalse(fs.isreg(object()))
        self.assertFalse(fs.isfifo(object()))

        self.assertTrue(fs.isdir(fs.fsDir('/tmp', strict=False)))
        self.assertFalse(fs.isreg(fs.fsDir('/tmp', strict=False)))
        self.assertTrue(fs.isreg(fs.fsFile('/tmp', strict=False)))
github pkgcore / pkgcore / tests / module / fs / test_contents.py View on Github external
from snakeoil.currying import post_curry
from snakeoil.osutils import pjoin
from snakeoil.test import TestCase

from pkgcore.fs import fs, contents

mk_file = partial(fs.fsFile, strict=False)
mk_dir  = partial(fs.fsDir, strict=False)
mk_link = partial(fs.fsLink, strict=False)
mk_dev  = partial(fs.fsDev, strict=False)
mk_fifo = partial(fs.fsFifo, strict=False)

for x in ("File", "Dir", "Link", "Dev", "Fifo"):
    globals()["mk_" + x.lower()] = partial(
        getattr(fs, f"fs{x}"), strict=False)
del x


class TestContentsSet(TestCase):

    locals().update((x, globals()[x]) for x in
        ("mk_file", "mk_dir", "mk_link", "mk_dev", "mk_fifo"))

    def __init__(self, *a, **kw):
        TestCase.__init__(self, *a, **kw)
        self.files = list(map(self.mk_file, ["/etc/blah", "/etc/foo", "/etc/dar",
             "/tmp/dar",
             "/tmp/blah/foo/long/ass/file/name/but/not/that/bad/really"]))
        self.dirs = list(map(self.mk_dir, ["/tmp", "/blah", "/tmp/dar",
            "/usr/", "/usr/bin"]))
        self.links = [fs.fsLink(x, os.path.dirname(x), strict=False) for x in
github pkgcore / pkgcore / tests / module / fs / test_livefs.py View on Github external
def check_attrs(self, obj, path, offset=None):
        if offset is None:
            st = os.lstat(path)
        else:
            st = os.lstat(offset + '/' + path)
        if offset is not None:
            self.assertTrue(
                path.startswith("/"),
                msg=f"path must be absolute, got {path!r}")
        self.assertEqual(obj.mode & 0o7777, st.st_mode & 0o7777)
        self.assertEqual(obj.uid, st.st_uid)
        self.assertEqual(obj.gid, st.st_gid)
        if fs.isreg(obj):
            if offset is None:
                self.assertEqual(obj.data.path, path)
            else:
                self.assertEqual(obj.data.path,
                    offset + path)
github pkgcore / pkgcore / src / pkgcore / vdb / contents.py View on Github external
self.clear()
        for line in self._get_fd():
            if not line:
                continue
            s = line.split(" ")
            if s[0] in ("dir", "dev", "fif"):
                path = ' '.join(s[1:])
                if s[0] == 'dir':
                    obj = fs.fsDir(path, strict=False)
                elif s[0] == 'dev':
                    obj = LookupFsDev(path, strict=False)
                else:
                    obj = fs.fsFifo(path, strict=False)
            elif s[0] == "obj":
                path = ' '.join(s[1:-2])
                obj = fs.fsFile(
                    path, chksums={"md5":int(s[-2], 16)},
                        mtime=int(s[-1]), strict=False)
            elif s[0] == "sym":
                try:
                    p = s.index("->")
                    obj = fs.fsLink(' '.join(s[1:p]), ' '.join(s[p+1:-1]),
                        mtime=int(s[-1]), strict=False)

                except ValueError:
                    # XXX throw a corruption error
                    raise
            else:
                raise Exception(f"unknown entry type {line!r}")

            yield obj
github pkgcore / pkgcore / src / pkgcore / fs / ops.py View on Github external
if any(target._can_be_hardlinked(x) and do_link(target, x)
                            for target in candidates):
                        continue
                    candidates.append(x)

                copyfile(x, mkdirs=True)

            break
        except CannotOverwrite as cf:
            if not fs.issym(x):
                raise

            # by this time, all directories should've been merged.
            # thus we can check the target
            try:
                if not fs.isdir(gen_obj(pjoin(x.location, x.target))):
                    raise
            except OSError:
                raise cf
    return True
github pkgcore / pkgcore / pkgcore / fs / contents.py View on Github external
def iterdirs(self, invert=False):
        return (x for x in self if isinstance(x, fs.fsDir) is not invert)
github pkgcore / pkgcore / src / pkgcore / scripts / pquery.py View on Github external
for key, restricts in depset.find_cond_nodes(depset.restrictions, True):
                    if not restricts and key.intersects(revdep):
                        out.write(f' {name} on {revdep} through {key}')
                for key, restricts in depset.node_conds.items():
                    if key.intersects(revdep):
                        restricts = ' or '.join(map(str, restricts))
                        out.write(f' {name} on {revdep} through {key} if USE {restricts},')
        # If we printed anything at all print the newline now
        out.autoline = True
        if printed_something:
            out.write()

    if options.contents:
        color = {
            fs_module.fsDir: [out.bold, out.fg('blue')],
            fs_module.fsLink: [out.bold, out.fg('cyan')],
        }
        for obj in sorted(obj for obj in get_pkg_attr(pkg, 'contents', ())):
            if options.color:
                out.write(*(color.get(obj.__class__, []) + [obj] + [out.reset]))
            else:
                out.write(f'{obj!r}')

    if options.size:
        size = 0
        files = 0
        for location in (obj.location for obj in get_pkg_attr(pkg, 'contents', ())):
            files += 1
            size += os.lstat(location).st_size
        out.write(f'Total files: {files}')
        out.write(f'Total size: {sizeof_fmt(size)}')
github pkgcore / pkgcore / pkgcore / vdb / contents.py View on Github external
from snakeoil.fileutils import AtomicWriteFile

from pkgcore.fs import fs
from pkgcore.fs.contents import contentsSet

demandload(
    'errno',
    'os',
    'stat',
    'snakeoil.chksum:get_handler',
    'snakeoil.fileutils:readlines_utf8',
    'pkgcore:os_data',
)


class LookupFsDev(fs.fsDev):

    __slots__ = ()

    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)
github pkgcore / pkgcore / src / pkgcore / fs / ops.py View on Github external
:return: True on success, else an exception is thrown
    :raise EnvironmentError: if fs object attributes can't be enforced
    """

    m, o, g, t = d1.mode, d1.uid, d1.gid, d1.mtime
    if o is None:
        o = -1
    if g is None:
        g = -1
    if d2 is None:
        do_mode, do_chown, do_mtime = True, True, True
    else:

        do_mode = False
        try:
            if fs.isdir(d1) and fs.isdir(d2):
                # if it's preexisting, keep its perms.
                do_mode = False
            else:
                do_mode = (m is not None and m != d2.mode)
        except AttributeError:
            # yes.  this _is_ stupid.  vdb's don't always store all attributes
            do_mode = False

        do_chown = False
        try:
            do_chown = (o != d2.uid or g != d2.gid)
        except AttributeError:
            do_chown = True

        try:
            do_mtime = (t != d2.mtime)