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("obj must be fsBase derivative: %r" % obj) elif fs.isdir(obj): raise TypeError("obj must not be a fsDir instance: %r" % obj) 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=0750, minimal=True): raise FailedCopy(obj, str(oe))
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
def add(self, obj): """ add a new fs obj to the set :param obj: must be a derivative of :obj:`pkgcore.fs.fs.fsBase` """ if not self.mutable: # weird, but keeping with set. raise AttributeError( f'{self.__class__} is frozen; no add functionality') if not fs.isfs_obj(obj): raise TypeError(f"'{obj}' is not a fs.fsBase class") self._dict[obj.location] = obj
def add(self, obj): """ add a new fs obj to the set :param obj: must be a derivative of :obj:`pkgcore.fs.fs.fsBase` """ if not self.mutable: # weird, but keeping with set. raise AttributeError( "%s is frozen; no add functionality" % self.__class__) if not fs.isfs_obj(obj): raise TypeError("'%s' is not a fs.fsBase class" % str(obj)) self._dict[obj.location] = obj
def __delitem__(self, obj): """ remove a fs obj to the set :type obj: a derivative of :obj:`pkgcore.fs.fs.fsBase` or a string location of an obj in the set. :raise KeyError: if the obj isn't found """ if not self.mutable: # weird, but keeping with set. raise AttributeError( f'{self.__class__} is frozen; no remove functionality') if fs.isfs_obj(obj): del self._dict[obj.location] else: del self._dict[normpath(obj)]
def __delitem__(self, obj): """ remove a fs obj to the set :type obj: a derivative of :obj:`pkgcore.fs.fs.fsBase` or a string location of an obj in the set. :raise KeyError: if the obj isn't found """ if not self.mutable: # weird, but keeping with set. raise AttributeError( "%s is frozen; no remove functionality" % self.__class__) if fs.isfs_obj(obj): del self._dict[obj.location] else: del self._dict[normpath(obj)]
def __contains__(self, key): if fs.isfs_obj(key): return key.location in self._dict return normpath(key) in self._dict
def __getitem__(self, obj): if fs.isfs_obj(obj): return self._dict[obj.location] return self._dict[normpath(obj)]
def discard(self, obj): if fs.isfs_obj(obj): self._dict.pop(obj.location, None) else: self._dict.pop(obj, None)