jw.pkg.cmds.distro.CmdSelect: Add command

Add the sub-command select to jw-pkg.py distro, along with an
Implementation for OpenSUSE.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-02-17 13:37:33 +01:00
commit 3ceb2b71b7
3 changed files with 74 additions and 0 deletions

View file

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from argparse import Namespace, ArgumentParser
import re
from ..CmdDistro import CmdDistro
from .lib.Package import Package
from .Cmd import Cmd
from .backend.BeSelect import BeSelect
class CmdSelect(Cmd): # export
def __init__(self, parent: CmdDistro) -> None:
super().__init__(parent, 'select', help="Select packages by filter")
def add_arguments(self, parser: ArgumentParser) -> None:
super().add_arguments(parser)
parser.add_argument("filter", help="Package filter string")
def filter_packages(self, filter: str, packages: list[Package]) -> list[Package]:
# -- Poor man's parsing for now
url_rx_str = re.sub(r'^\s*url\s*=~\s*', '', filter)
if url_rx_str == filter:
raise Exception(f'Unsupported filter string "{filter}"')
url_rx = re.compile(url_rx_str)
return [p for p in packages if re.search(url_rx, p.url) is not None]
async def _run(self, args: Namespace) -> None:
# TODO: Semantics probably change heavily in the future
for p in self.filter_packages(args.filter, await self._backend.all_installed_packages):
print(p.name)

View file

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import annotations
from typing import TYPE_CHECKING
import abc
from typing import Iterable
from .Backend import Backend as Base
if TYPE_CHECKING:
from ..CmdSelect import CmdSelect as Parent
from .Package import Package
class BeSelect(Base):
def __init__(self, parent: Parent):
super().__init__(parent)
@property
async def all_installed_packages(self) -> Iterable[Package]:
return await self._all_installed_packages()
@abc.abstractmethod
async def _all_installed_packages(self) -> Iterable[Package]:
pass

View file

@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
from typing import Iterable
from argparse import Namespace
from ...lib.Package import Package
from ...lib.rpm import all_installed_packages
from ...Cmd import Cmd
from ..BeSelect import BeSelect as Base
class Select(Base):
def __init__(self, parent: Cmd):
super().__init__(parent)
async def _all_installed_packages(self) -> Iterable[Package]:
return await all_installed_packages()