Skip to content

Commit

Permalink
core: add SimpleTreeFromPkgs for test usage
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Harring <[email protected]>
  • Loading branch information
ferringb committed Nov 24, 2023
1 parent 75a7751 commit e817c05
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/pkgcore/repository/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,67 @@
from ..ebuild.cpv import VersionedCPV


class SimpleTreeFromPkgs(prototype.tree):
"""Repository adapter that can work from a sequence of instantiated packages
This is useful for generating a repository on the fly (package.provided) or for
test usage.
"""

def __init__(
self,
packages: typing.Iterable,
livefs=False,
frozen=True,
repo_id: typing.Union[str, None] = None,
):
"""
Args:
packages: sequence of instantiated packages to use. cpv.VersionedCPV for example,
or FakePkg.
"""
super().__init__(frozen=frozen)
# just return the pkg we already have.
self.package_class = lambda x: x
self.repo_id = repo_id
self.livefs = livefs
self._packages = {} # type: dict[str, dict[str, list[typing.Any]]]
for pkg in packages:
self.notify_add_package(pkg)

def _get_categories(self, *arg: tuple[str, ...]):
if arg:
return ()
return tuple(self._packages)

def _get_packages(self, category: str) -> typing.Iterable[str]:
return tuple(self._packages[category])

def _get_versions(self, cp_key: tuple[str, str]) -> typing.Iterable[str]:
return tuple(pkg.fullver for pkg in self._packages[cp_key[0]][cp_key[1]])

def notify_add_package(self, pkg):
self._packages.setdefault(pkg.category, {}).setdefault(pkg.package, []).append(
pkg
)
super().notify_add_package(pkg)

def notify_remove_package(self, pkg):
try:
pkgs = [
p
for p in self._packages[pkg.category][pkg.package]
if p.fullver != pkg.fullver
]
if not pkgs:
del self._packages[pkg.category][pkg.package]
if not self._packages[pkg.category]:
del self._packages[pkg.category]
except KeyError:
pass
super().notify_remove_package(pkg)


class SimpleTree(prototype.tree):
"""in-memory repository used for testing or simple shims."""

Expand Down

0 comments on commit e817c05

Please sign in to comment.