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

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;