lib.PackageFilter: Derive from PathFilter
This commit moves package-based filtering from the package level to the path level, using the new path filter framework. Derive PackageFilter from a new FileContextFilter base, which binds a PathFilter to the FileContext it guards, and implements _access(). The filter's space is the file list of the packages it selects, taken from the backing distro's pkg_files(). An address is allowed only if it names one of those files. The backing distro is taken from the file context, if the context carries one, and is otherwise autodetected from the host the context executes on, so the filter also works on a bare exec context. A context that is not an exec context and carries no distro rejects path queries with an error. The filter string now follows the loader syntax, pkg:url=~ regex, and a bare url=~regex like the old url=~jw.dev form is rejected; update JW_DEFAULT_PKG_FILTER values accordingly. PackageFilter's match() is renamed match_pkg(), to make clear that it matches packages, and Distro._select() calls the new name. ExecApp.path_filter builds the filter through PathFilter.create() and binds it to the exec context. CmdSelect passes its argument to the new constructor, CmdCopy hands the app's filter to the copy, and the App distro picks it up as the default package filter. Extend the unit test to cover the space membership and the distro lookup. Signed-off-by: Jan Lindemann <jan@janware.com> Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
This commit is contained in:
parent
d27bf01714
commit
f74dca5a45
3 changed files with 198 additions and 41 deletions
|
|
@ -271,7 +271,9 @@ sdf = SuseDistro(
|
|||
cl,
|
||||
id = 'suse',
|
||||
os_release_str = OS_RELEASE_SUSE,
|
||||
default_pkg_filter = PackageFilterString('url=~jw.dev')
|
||||
default_pkg_filter = PackageFilterString(
|
||||
spec = 'pkg:url=~jw.dev', file_context = cl
|
||||
)
|
||||
)
|
||||
assert asyncio.run(sdf.pkg_files()) == \
|
||||
['/etc/a/a.conf.jw-tmpl', '/etc/b/b.conf.jw-tmpl']
|
||||
|
|
@ -281,7 +283,9 @@ sde = SuseDistro(
|
|||
cl,
|
||||
id = 'suse',
|
||||
os_release_str = OS_RELEASE_SUSE,
|
||||
default_pkg_filter = PackageFilterString('url=~zzz-nomatch')
|
||||
default_pkg_filter = PackageFilterString(
|
||||
spec = 'pkg:url=~zzz-nomatch', file_context = cl
|
||||
)
|
||||
)
|
||||
assert asyncio.run(sde.pkg_files()) == []
|
||||
assert '--queryformat' in cl.cmds[-1]
|
||||
|
|
|
|||
|
|
@ -1,34 +1,134 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from typing import TYPE_CHECKING, Any, override
|
||||
|
||||
from jw.pkg.lib.FileContext import FileContext
|
||||
from jw.pkg.lib.Package import Package
|
||||
from jw.pkg.lib.PackageFilter import PackageFilterString
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from jw.pkg.lib.Result import Result
|
||||
|
||||
# -- A minimal file context; the match_pkg() role never touches its distro --
|
||||
|
||||
class _StubFC(FileContext):
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__(uri = 'file:///')
|
||||
|
||||
@override
|
||||
async def _get(
|
||||
self, path: str, wd: str | None, throw: bool, verbose: bool | None, title: str
|
||||
) -> Result:
|
||||
raise NotImplementedError()
|
||||
|
||||
fc = _StubFC()
|
||||
|
||||
pkg = Package(name = 'jw-core', url = 'https://example.com/jw-core')
|
||||
pkg_other = Package(name = 'jw-base', url = 'https://other.org/jw-base')
|
||||
pkg_nourl = Package(name = 'no-url')
|
||||
|
||||
# -- url=~ filters --
|
||||
|
||||
f = PackageFilterString('url=~example\\.com')
|
||||
assert f.match(pkg)
|
||||
assert not f.match(pkg_other)
|
||||
f = PackageFilterString(file_context = fc, spec = 'pkg:url=~example\\.com')
|
||||
assert f.match_pkg(pkg)
|
||||
assert not f.match_pkg(pkg_other)
|
||||
# A package without a url never matches
|
||||
assert not f.match(pkg_nourl)
|
||||
assert not f.match_pkg(pkg_nourl)
|
||||
|
||||
# Whitespace around the operator is tolerated
|
||||
f = PackageFilterString(' url =~ example')
|
||||
assert f.match(pkg)
|
||||
f = PackageFilterString(file_context = fc, spec = 'pkg: url =~ example')
|
||||
assert f.match_pkg(pkg)
|
||||
|
||||
# An unanchored regex matches anywhere in the url
|
||||
f = PackageFilterString('url=~jw-core$')
|
||||
assert f.match(pkg)
|
||||
assert not f.match(pkg_other)
|
||||
f = PackageFilterString(file_context = fc, spec = 'pkg:url=~jw-core$')
|
||||
assert f.match_pkg(pkg)
|
||||
assert not f.match_pkg(pkg_other)
|
||||
|
||||
# -- Unsupported definitions raise --
|
||||
|
||||
for definition in ('', 'url=example.com', 'name=jw-core', 'url ~ example'):
|
||||
for definition in ('',
|
||||
'url=example.com',
|
||||
'name=jw-core',
|
||||
'url ~ example',
|
||||
'pkg:url=example.com'):
|
||||
try:
|
||||
PackageFilterString(definition)
|
||||
PackageFilterString(file_context = fc, spec = definition)
|
||||
assert False, f'Should have raised for "{definition}"'
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# -- __repr__ --
|
||||
|
||||
f = PackageFilterString(file_context = fc, spec = 'pkg:url=~example\\.com')
|
||||
assert repr(f) == 'filter:pkg:url=~example\\.com'
|
||||
|
||||
class _FakeDistro:
|
||||
|
||||
def __init__(self, pkgs: list[tuple[str, str | None, list[str]]]) -> None:
|
||||
self.__pkgs = [
|
||||
(Package(name = name, url = url), files) for name, url, files in pkgs
|
||||
]
|
||||
|
||||
async def select(self,
|
||||
names: list[str] | None = None,
|
||||
filter: Any = None) -> list[Package]:
|
||||
return [p for p, _ in self.__pkgs if filter is None or filter.match_pkg(p)]
|
||||
|
||||
async def pkg_files(self, names: list[str] | None = None) -> list[str]:
|
||||
ret: list[str] = []
|
||||
for p, files in self.__pkgs:
|
||||
if names is None or p.name in names:
|
||||
ret.extend(files)
|
||||
return ret
|
||||
|
||||
fake = _FakeDistro(
|
||||
[
|
||||
(
|
||||
'fixture',
|
||||
'https://example.com/fixture.rpm',
|
||||
['/etc/foo.conf.jw-tmpl', '/etc/fixture.conf'],
|
||||
),
|
||||
('other', 'https://elsewhere.org/other.rpm', ['/etc/bar.conf']),
|
||||
]
|
||||
)
|
||||
|
||||
# -- _access(): the space is the selected packages' file list, and
|
||||
# the context's distro is probed on first use --
|
||||
async def __run_tests() -> None:
|
||||
from unittest import mock
|
||||
|
||||
from jw.pkg.lib.Distro import Distro
|
||||
from jw.pkg.lib.ec.Local import Local
|
||||
from jw.pkg.lib.PathFilter import Verdict
|
||||
|
||||
f2 = PackageFilterString(
|
||||
spec = 'pkg:url=~example\\.com', file_context = Local('local')
|
||||
)
|
||||
|
||||
async def __instantiate(*args: Any, **kwargs: Any) -> _FakeDistro:
|
||||
return fake
|
||||
|
||||
with mock.patch.object(Distro, 'instantiate', new = classmethod(__instantiate)):
|
||||
# The files of the selected package are in the space,
|
||||
# template and plain alike
|
||||
assert await f2.run('/etc/foo.conf.jw-tmpl') == Verdict.ALLOW
|
||||
assert await f2.run('/etc/fixture.conf') == Verdict.ALLOW
|
||||
# An address of a non-selected package is out of the space
|
||||
assert await f2.run('/etc/bar.conf') == Verdict.IGNORE
|
||||
# An address that is not a package file is out of the space
|
||||
assert await f2.run('/etc/foo.conf') == Verdict.IGNORE
|
||||
|
||||
# -- A context that cannot be probed rejects
|
||||
f4 = PackageFilterString(spec = 'pkg:url=~example\\.com', file_context = _StubFC())
|
||||
try:
|
||||
await f4.run('/etc/foo.conf')
|
||||
assert False, 'An unbacked context should reject path queries'
|
||||
except Exception as e:
|
||||
assert 'backed by distributions' in str(e)
|
||||
|
||||
asyncio.run(__run_tests())
|
||||
|
||||
print('All PackageFilter tests passed')
|
||||
|
|
|
|||
|
|
@ -349,14 +349,16 @@ async def run_tests() -> None:
|
|||
# -- The autodetected distro applies the app's documented
|
||||
# filter fallback: JW_DEFAULT_PKG_FILTER reaches
|
||||
# Distro.instantiate() as the default package filter
|
||||
os.environ['JW_DEFAULT_PKG_FILTER'] = 'url=~jw.dev'
|
||||
os.environ['JW_DEFAULT_PKG_FILTER'] = 'pkg:url=~jw.dev'
|
||||
try:
|
||||
astore = KvFile('kvfile://')
|
||||
assert set(await astore.readdir('')) == {'tmp'}
|
||||
flt = captured['kwargs'].get('default_pkg_filter')
|
||||
assert flt is not None
|
||||
assert flt.match(Package('fixture', url = 'https://jw.dev/fixture.rpm'))
|
||||
assert not flt.match(Package('other', url = 'https://elsewhere/other.rpm'))
|
||||
assert flt.match_pkg(Package('fixture', url = 'https://jw.dev/fixture.rpm'))
|
||||
assert not flt.match_pkg(
|
||||
Package('other', url = 'https://elsewhere/other.rpm')
|
||||
)
|
||||
finally:
|
||||
del os.environ['JW_DEFAULT_PKG_FILTER']
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue