Beispiel #1
0
    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)))
Beispiel #2
0
    def test_iterscan(self):
        path = os.path.join(self.dir, "iscan")
        os.mkdir(path)
        files = [
            os.path.normpath(os.path.join(path, x))
            for x in ["tmp", "blah", "dar"]
        ]
        # cheap version of a touch.
        map(lambda x: open(x, "w").close(), files)
        dirs = [
            os.path.normpath(os.path.join(path, x)) for x in ["a", "b", "c"]
        ]
        map(os.mkdir, dirs)
        dirs.append(path)
        for obj in livefs.iter_scan(path):
            self.assertInstance(obj, fs.fsBase)
            if fs.isreg(obj):
                self.assertTrue(obj.location in files)
            elif fs.isdir(obj):
                self.assertTrue(obj.location in dirs)
            else:
                raise Exception(
                    "unknown object popped up in testing dir, '%s'" % obj)
            self.check_attrs(obj, obj.location)
        # do offset verification now.
        offset = os.path.join(self.dir, "iscan")
        for obj in livefs.iter_scan(path, offset=offset):
            self.check_attrs(obj, obj.location, offset=offset)

        seen = []
        for obj in livefs.iter_scan(files[0]):
            self.check_attrs(obj, obj.location)
            seen.append(obj.location)
        self.assertEqual((files[0], ), tuple(sorted(seen)))
Beispiel #3
0
    def test_iterscan(self):
        path = os.path.join(self.dir, "iscan")
        os.mkdir(path)
        files = [os.path.normpath(os.path.join(path, x)) for x in [
                "tmp", "blah", "dar"]]
        # cheap version of a touch.
        map(lambda x:open(x, "w").close(), files)
        dirs = [os.path.normpath(os.path.join(path, x)) for x in [
                "a", "b", "c"]]
        map(os.mkdir, dirs)
        dirs.append(path)
        for obj in livefs.iter_scan(path):
            self.assertInstance(obj, fs.fsBase)
            if fs.isreg(obj):
                self.assertTrue(obj.location in files)
            elif fs.isdir(obj):
                self.assertTrue(obj.location in dirs)
            else:
                raise Exception(
                    "unknown object popped up in testing dir, '%s'" % obj)
            self.check_attrs(obj, obj.location)
        # do offset verification now.
        offset = os.path.join(self.dir, "iscan")
        for obj in livefs.iter_scan(path, offset=offset):
            self.check_attrs(obj, obj.location, offset=offset)

        seen = []
        for obj in livefs.iter_scan(files[0]):
            self.check_attrs(obj, obj.location)
            seen.append(obj.location)
        self.assertEqual((files[0],), tuple(sorted(seen)))
Beispiel #4
0
 def test_gen_obj_reg(self):
     path = os.path.join(self.dir, "reg_obj")
     open(path, "w").close()
     o = livefs.gen_obj(path)
     self.assertTrue(fs.isreg(o))
     self.check_attrs(o, path)
     o2 = livefs.gen_obj(path, inode=None)
     self.check_attrs(o, path)
     self.assertNotEqual(o.inode, o2.inode)
Beispiel #5
0
 def test_gen_obj_reg(self):
     path = os.path.join(self.dir, "reg_obj")
     open(path, "w").close()
     o = livefs.gen_obj(path)
     self.assertTrue(fs.isreg(o))
     self.check_attrs(o, path)
     o2 = livefs.gen_obj(path, inode=None)
     self.check_attrs(o, path)
     self.assertNotEqual(o.inode, o2.inode)
Beispiel #6
0
def default_copyfile(obj, mkdirs=False):
    """
    copy a :class:`pkgcore.fs.fs.fsBase` to its stated location.

    :param obj: :class:`pkgcore.fs.fs.fsBase` instance, exempting :class:`fsDir`
    :return: true if success, else an exception is thrown
    :raise EnvironmentError: permission errors

    """

    existent = False
    ensure_perms = get_plugin("fs_ops.ensure_perms")
    if not fs.isfs_obj(obj):
        raise TypeError(f'obj must be fsBase derivative: {obj!r}')
    elif fs.isdir(obj):
        raise TypeError(f'obj must not be a fsDir instance: {obj!r}')

    try:
        existing = gen_obj(obj.location)
        if fs.isdir(existing):
            raise CannotOverwrite(obj, existing)
        existent = True
    except OSError as oe:
        # verify the parent dir is there at least
        basefp = os.path.dirname(obj.location)
        if basefp.strip(os.path.sep) and not os.path.exists(basefp):
            if mkdirs:
                if not ensure_dirs(basefp, mode=0o750, minimal=True):
                    raise FailedCopy(obj, str(oe))
            else:
                raise
        existent = False

    if not existent:
        fp = obj.location
    else:
        fp = existent_fp = obj.location + "#new"

    if fs.isreg(obj):
        obj.data.transfer_to_path(fp)
    elif fs.issym(obj):
        os.symlink(obj.target, fp)
    elif fs.isfifo(obj):
        os.mkfifo(fp)
    elif fs.isdev(obj):
        dev = os.makedev(obj.major, obj.minor)
        os.mknod(fp, obj.mode, dev)
    else:
        ret = spawn([CP_BINARY, "-Rp", obj.location, fp])
        if ret != 0:
            raise FailedCopy(obj, f'got {ret} from {CP_BINARY} -Rp')

    ensure_perms(obj.change_attributes(location=fp))

    if existent:
        os.rename(existent_fp, obj.location)
    return True
Beispiel #7
0
def fix_fsobject(location):
    from pkgcore.fs import livefs, fs
    for obj in livefs.iter_scan(location):
        if not fs.isreg(obj) or not obj.basename.endswith(".la"):
            continue

        updated, content = rewrite_lafile(open(obj.location, 'r'))
        if updated:
            open(obj.location, 'w').write(content)
Beispiel #8
0
def fix_fsobject(location):
    from pkgcore.fs import livefs, fs
    for obj in livefs.iter_scan(location):
        if not fs.isreg(obj) or not obj.basename.endswith(".la"):
            continue

        with open(obj.location, 'r') as f:
            updated, content = rewrite_lafile(f, obj.basename)
        if updated:
            with open(obj.location, 'w') as f:
                f.write(content)
Beispiel #9
0
 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="path must be absolute, got %r" % path)
     self.assertEqual(obj.mode & 07777, st.st_mode & 07777)
     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)
Beispiel #10
0
 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="path must be absolute, got %r" % path)
     self.assertEqual(obj.mode & 07777, st.st_mode & 07777)
     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)
Beispiel #11
0
        # verify the parent dir is there at least
        basefp = os.path.dirname(obj.location)
        if basefp.strip(os.path.sep) and not os.path.exists(basefp):
            if mkdirs:
                if not ensure_dirs(basefp, mode=0750, minimal=True):
                    raise FailedCopy(obj, str(oe))
            else:
                raise
        existent = False

    if not existent:
        fp = obj.location
    else:
        fp = existent_fp = obj.location + "#new"

    if fs.isreg(obj):
        obj.data.transfer_to_path(fp)
    elif fs.issym(obj):
        os.symlink(obj.target, fp)
    elif fs.isfifo(obj):
        os.mkfifo(fp)
    elif fs.isdev(obj):
        dev = os.makedev(obj.major, obj.minor)
        os.mknod(fp, obj.mode, dev)
    else:
        ret = spawn([COPY_BINARY, "-Rp", obj.location, fp])
        if ret != 0:
            raise FailedCopy(obj, "got %i from %s -Rp" % ret)

    ensure_perms(obj.change_attributes(location=fp))
Beispiel #12
0
        # verify the parent dir is there at least
        basefp = os.path.dirname(obj.location)
        if basefp.strip(os.path.sep) and not os.path.exists(basefp):
            if mkdirs:
                if not ensure_dirs(basefp, mode=0750, minimal=True):
                    raise FailedCopy(obj, str(oe))
            else:
                raise
        existent = False

    if not existent:
        fp = obj.location
    else:
        fp = existent_fp = obj.location + "#new"

    if fs.isreg(obj):
        obj.data.transfer_to_path(fp)
    elif fs.issym(obj):
        os.symlink(obj.target, fp)
    elif fs.isfifo(obj):
        os.mkfifo(fp)
    elif fs.isdev(obj):
        dev = os.makedev(obj.major, obj.minor)
        os.mknod(fp, obj.mode, dev)
    else:
        ret = spawn([COPY_BINARY, "-Rp", obj.location, fp])
        if ret != 0:
            raise FailedCopy(obj, "got %i from %s -Rp" % ret)

    ensure_perms(obj.change_attributes(location=fp))