From 525fa34387aafe876a952a8d6923b59d5e1927a5 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Fri, 6 Mar 2026 17:25:19 +0100 Subject: [PATCH] lib.pm: Add parameter ec: ExecContext to functions Functions in lib.pm (i.e. run_dpkg(), run_rpm() and friends) also get an ExecContext-type parameter. Use them in lib/distros/*/Distro. Signed-off-by: Jan Lindemann --- src/python/jw/pkg/lib/distros/arch/Distro.py | 6 ++--- .../jw/pkg/lib/distros/debian/Distro.py | 6 ++--- src/python/jw/pkg/lib/distros/suse/Distro.py | 6 ++--- src/python/jw/pkg/lib/pm/dpkg.py | 25 +++++++++++-------- src/python/jw/pkg/lib/pm/rpm.py | 21 ++++++++++------ 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/python/jw/pkg/lib/distros/arch/Distro.py b/src/python/jw/pkg/lib/distros/arch/Distro.py index bd5df671..c9a72e9d 100644 --- a/src/python/jw/pkg/lib/distros/arch/Distro.py +++ b/src/python/jw/pkg/lib/distros/arch/Distro.py @@ -29,20 +29,20 @@ class Distro(Base): args = ['-Su'] if args.download_only: args.append('-w') - return await self.util.pacman(args) + return await self.pacman(args) async def _reboot_required(self, verbose: bool) -> bool: raise NotImplementedError('distro reboot-required is not yet implemented for Arch-like distributions') async def _select(self, names: Iterable[str]) -> Iterable[Package]: - return await query_packages(names) + raise NotImplementedError('distro select is not yet implemented for Arch-like distributions') async def _install(self, names: Iterable[str], only_update: bool) -> None: if only_update: raise NotImplementedError('--only-update is not yet implemented for pacman') args = ['-S', '--needed'] args.extend(args.packages) - await self.util.pacman(args) + await self.pacman(args) async def _delete(self, names: Iterable[str]) -> None: raise NotImplementedError('distro delete not yet implemented for Arch-like distributions') diff --git a/src/python/jw/pkg/lib/distros/debian/Distro.py b/src/python/jw/pkg/lib/distros/debian/Distro.py index 2cbbac1c..b865e1f1 100644 --- a/src/python/jw/pkg/lib/distros/debian/Distro.py +++ b/src/python/jw/pkg/lib/distros/debian/Distro.py @@ -26,7 +26,7 @@ class Distro(Base): return await self.sudo(cmd, mod_env=mod_env) async def dpkg(self, *args, **kwargs): - return await run_dpkg(*args, **kwargs) + return await run_dpkg(*args, ec=self.ctx, **kwargs) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -58,7 +58,7 @@ class Distro(Base): return False async def _select(self, names: Iterable[str]) -> Iterable[Package]: - return await query_packages(names) + return await query_packages(names, ec=self.ctx) async def _install(self, names: Iterable[str], only_update: bool) -> None: args = ['install'] @@ -72,4 +72,4 @@ class Distro(Base): return await self.dpkg(['-P', *names], sudo=True) async def _pkg_files(self, name: str) -> Iterable[str]: - return await list_files(name) + return await list_files(name, ec=self.ctx) diff --git a/src/python/jw/pkg/lib/distros/suse/Distro.py b/src/python/jw/pkg/lib/distros/suse/Distro.py index 51cc2e82..7b35273a 100644 --- a/src/python/jw/pkg/lib/distros/suse/Distro.py +++ b/src/python/jw/pkg/lib/distros/suse/Distro.py @@ -24,7 +24,7 @@ class Distro(Base): return await self.run(cmd, verbose=verbose) async def rpm(self, *args, **kwargs) -> Result: - return await run_rpm(*args, **kwargs) + return await run_rpm(*args, ec=self.ctx, **kwargs) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -50,7 +50,7 @@ class Distro(Base): return False async def _select(self, names: Iterable[str]) -> Iterable[Package]: - return await query_packages(names) + return await query_packages(names, ec=self.ctx) async def _install(self, names: Iterable[str], only_update: bool) -> None: cmd = 'update' if only_update else 'install' @@ -60,4 +60,4 @@ class Distro(Base): return await self.rpm(['-e', *names], sudo=True) async def _pkg_files(self, name: str) -> Iterable[str]: - return await list_files(name) + return await list_files(name, ec=self.ctx) diff --git a/src/python/jw/pkg/lib/pm/dpkg.py b/src/python/jw/pkg/lib/pm/dpkg.py index 213b5789..71dceee7 100644 --- a/src/python/jw/pkg/lib/pm/dpkg.py +++ b/src/python/jw/pkg/lib/pm/dpkg.py @@ -1,6 +1,11 @@ # -*- coding: utf-8 -*- -from typing import Iterable +from __future__ import annotations + +from typing import Iterable, TYPE_CHECKING + +if TYPE_CHECKING: + from ..ExecContext import ExecContext from ..util import run_cmd, run_sudo @@ -20,26 +25,26 @@ def meta_map(): }) return _meta_map -async def run_dpkg(args: list[str], sudo: bool=False): # export +async def run_dpkg(args: list[str], sudo: bool=False, ec: ExecContext=None): # export cmd = ['/usr/bin/dpkg'] cmd.extend(args) if sudo: - return await run_sudo(cmd) - return await run_cmd(cmd) + return await run_sudo(cmd, ec=ec) + return await run_cmd(cmd, ec=ec) -async def run_dpkg_query(args: list[str], sudo: bool=False): # export +async def run_dpkg_query(args: list[str], sudo: bool=False, ec: ExecContext=None): # export cmd = ['/usr/bin/dpkg-query'] cmd.extend(args) if sudo: return await run_sudo(cmd) - return await run_cmd(cmd) + return await run_cmd(cmd, ec=ec) -async def query_packages(names: Iterable[str] = []) -> Iterable[Package]: # export +async def query_packages(names: Iterable[str] = [], ec: ExecContext=None) -> Iterable[Package]: # export fmt_str = '|'.join([(f'${{{tag}}}' if tag else '') for tag in meta_map().values()]) + r'\n' # dpkg-query -W -f='${binary:Package}|${Maintainer}| ... \n' - specs, stderr, status = await run_dpkg_query(['-W', '-f=' + fmt_str, *names], sudo=False) + specs, stderr, status = await run_dpkg_query(['-W', '-f=' + fmt_str, *names], sudo=False, ec=ec) return Package.parse_specs_str(specs) -async def list_files(pkg: str) -> list[str]: # export - file_list_str, stderr, status = await run_dpkg(['-L', pkg], sudo=False) +async def list_files(pkg: str, ec: ExecContext=None) -> list[str]: # export + file_list_str, stderr, status = await run_dpkg(['-L', pkg], sudo=False, ec=ec) return file_list_str.splitlines() diff --git a/src/python/jw/pkg/lib/pm/rpm.py b/src/python/jw/pkg/lib/pm/rpm.py index cd9ac70f..37be5611 100644 --- a/src/python/jw/pkg/lib/pm/rpm.py +++ b/src/python/jw/pkg/lib/pm/rpm.py @@ -1,6 +1,11 @@ # -*- coding: utf-8 -*- -from typing import Iterable +from __future__ import annotations + +from typing import Iterable, TYPE_CHECKING + +if TYPE_CHECKING: + from ..ExecContext import ExecContext from ..util import run_cmd, run_sudo @@ -20,21 +25,21 @@ def meta_map(): }) return _meta_map -async def run_rpm(args: list[str], sudo: bool=False, **kwargs): # export +async def run_rpm(args: list[str], sudo: bool=False, ec: ExecContext=None, **kwargs): # export cmd = ['/usr/bin/rpm'] cmd.extend(args) if sudo: - return await run_sudo(cmd, **kwargs) - return await run_cmd(cmd, **kwargs) + return await run_sudo(cmd, ec=ec, **kwargs) + return await run_cmd(cmd, ec=ec, **kwargs) -async def query_packages(names: Iterable[str] = []) -> Iterable[Package]: # export +async def query_packages(names: Iterable[str] = [], ec: ExecContext=None) -> Iterable[Package]: # export fmt_str = '|'.join([(f'%{{{tag}}}' if tag else '') for tag in meta_map().values()]) + r'\n' opts = ['-q', '--queryformat', fmt_str] if not names: opts.append('-a') - specs, stderr, status = await run_rpm([*opts, *names], throw=True, sudo=False) + specs, stderr, status = await run_rpm([*opts, *names], throw=True, sudo=False, ec=ec) return Package.parse_specs_str(specs) -async def list_files(pkg: str) -> list[str]: # export - file_list_str, stderr, status = await run_rpm(['-ql', pkg], throw=True, sudo=False) +async def list_files(pkg: str, ec: ExecContext=None) -> list[str]: # export + file_list_str, stderr, status = await run_rpm(['-ql', pkg], throw=True, sudo=False, ec=ec) return file_list_str.splitlines()