lib.Distro.paths(): Add method

After the introduction of path-based filters, accompany the existing
pkg_paths() method by a more fine-grained paths() method, taking a
PathFilter instead of a PackageFilter. It additionally takes a "pkg_names"
argument, which is then dispatched to the filter if it's a package filter.
The API isn't glorious, but works for now and is easily changeable in the
future: **kwargs comes to mind, or a PackageFilter which brings its own
package names.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-11-12 16:02:58 +01:00
commit dc9ef58c8b
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
2 changed files with 88 additions and 3 deletions

View file

@ -10,6 +10,8 @@ from typing import TYPE_CHECKING, Any
from .base import InputMode
from .log import ERR, INFO, WARNING, log
from .PathFilter import Verdict
from .pf.Pkg import PackageFilter
if TYPE_CHECKING:
from collections.abc import Collection
@ -20,7 +22,6 @@ if TYPE_CHECKING:
from .FlaggedFile import FlaggedFile
from .Package import Package
from .PathFilter import PathFilter
from .pf.Pkg import PackageFilter
class Distro(abc.ABC):
@ -519,10 +520,50 @@ class Distro(abc.ABC):
# compatibility with the single-package call
if isinstance(names, str):
names = [names]
if names is None and self.default_path_filter is not None:
elif names is None and self.default_path_filter is not None:
names = [p.name for p in await self.select()]
return await self._pkg_paths(names)
# -- paths
async def _paths(
self,
filter: PathFilter | None = None,
pkg_names: Collection[str] = [],
) -> Iterable[str]:
if filter and pkg_names and not isinstance(filter, PackageFilter):
raise Exception(
f'Tried to select files from {self} with non-package-filter {filter} '
f'and package list {", ".join(pkg_names)}'
)
if not filter:
filter = self.__default_path_filter
# -- pkg_names follows the select() convention: an empty
# list selects every package, which is listed in one
# whole-system query instead of by name
if isinstance(filter, PackageFilter):
pkg_names = [p.name for p in await self._select(pkg_names, filter)]
ret: list[str] = [
path for path in await self.pkg_paths(names = list(pkg_names))
]
elif pkg_names:
pkg_names = [p.name for p in await self._select_by_name(pkg_names)]
ret = [path for path in await self.pkg_paths(names = list(pkg_names))]
else:
# -- _pkg_paths() directly: pkg_paths() would re-resolve
# the default filter, which _paths() already did
ret = [path for path in await self._pkg_paths(None)]
if filter and not isinstance(filter, PackageFilter):
ret = [path for path in ret if await filter.run(path) == Verdict.ALLOW]
return ret
async def paths(
self,
filter: PathFilter | None = None,
pkg_names: Collection[str] = [],
) -> Iterable[str]:
return await self._paths(filter, pkg_names)
# -- verify
@abc.abstractmethod

View file

@ -1,6 +1,7 @@
from __future__ import annotations
import asyncio
import os
from typing import TYPE_CHECKING, Iterable, cast, override
@ -8,6 +9,7 @@ from jw.pkg.lib.Distro import Distro
from jw.pkg.lib.distros.suse.Distro import Distro as SuseDistro
from jw.pkg.lib.ec.Local import Local
from jw.pkg.lib.Package import Package
from jw.pkg.lib.PathFilter import Handle, PathFilter
from jw.pkg.lib.pf.Pkg import Pkg
from jw.pkg.lib.Result import Result
@ -252,7 +254,9 @@ class CannedRpmLocal(Local):
) -> Result:
self.cmds.append(cmd)
if '-qal' in cmd:
out = '/usr/bin/x\n/etc/a/a.conf.jw-tmpl\n'
out = ('/usr/bin/x\n'
'/etc/a/a.conf.jw-tmpl\n'
'/etc/b/b.conf.jw-tmpl\n')
elif '-ql' in cmd:
out = '/etc/a/a.conf.jw-tmpl\n/etc/b/b.conf.jw-tmpl\n'
else:
@ -264,6 +268,7 @@ sd = SuseDistro(cl, id = 'suse', os_release_str = OS_RELEASE_SUSE)
assert asyncio.run(sd.pkg_paths()) == [
'/usr/bin/x',
'/etc/a/a.conf.jw-tmpl',
'/etc/b/b.conf.jw-tmpl',
]
assert cl.cmds[-1] == ['/usr/bin/rpm', '-qal']
@ -286,6 +291,45 @@ sde = SuseDistro(
assert asyncio.run(sde.pkg_paths()) == []
assert '--queryformat' in cl.cmds[-1]
# -- paths() --
# -- No filter and a bare path filter take the one-shot
# whole-system query, the second then checking the files one by
# one; a package filter selects the backing packages and lists
# their files by name; a path filter with package names is
# refused
class _AllowList(PathFilter):
def __init__(self, allowed: set[str]) -> None:
super().__init__(spec = 'test:allow', handle = Handle(0))
self.allowed = allowed
@override
async def _access(self, path: str, mode: int = os.F_OK) -> bool:
return path in self.allowed
allow = _AllowList({'/etc/b/b.conf.jw-tmpl'})
assert sorted(asyncio.run(sd.paths())) == \
['/etc/a/a.conf.jw-tmpl', '/etc/b/b.conf.jw-tmpl', '/usr/bin/x']
assert cl.cmds[-1] == ['/usr/bin/rpm', '-qal']
assert sorted(
asyncio.run(sd.paths(filter = Pkg(spec = 'pkg:url=~jw.dev', file_context = cl)))
) == ['/etc/a/a.conf.jw-tmpl', '/etc/b/b.conf.jw-tmpl']
assert cl.cmds[-1] == ['/usr/bin/rpm', '-ql', 'a', 'b']
assert asyncio.run(sd.paths(filter = allow)) == ['/etc/b/b.conf.jw-tmpl']
assert cl.cmds[-1] == ['/usr/bin/rpm', '-qal']
try:
asyncio.run(sd.paths(filter = allow, pkg_names = ['a']))
assert False, 'Should have raised'
except AssertionError:
raise
except Exception:
pass
# -- _install_local_files --
# -- With dependency resolution, the files go to the package manager;