jw-pkg/src/python/jw/pkg/lib/pm/dpkg.py
Jan Lindemann 1e613a39c6 App, cmds, lib: Fix Any returns from typed functions
Add type annotations and casts to functions that were returning Any
where a specific type was declared, satisfying the new warn_return_any
mypy rule.

Fixes:
- log.py: get_caller_pos return type via cast
- AsyncRunner.py: cast T for fut.result()
- util.py: cast for getattr result, str() for args.username
- FileContext.py: verbose_default bool annotation
- SSHClient.py: cast SSHClient for dynamic import
- lib/App.py: cast ArgumentParser, add return types to inner funcs
- pm/rpm.py, dpkg.py: cast Iterable[Package]
- App.py: cast for self.args.func(), add return types to inner funcs
- BaseCmdPkgRelations.py: cast str for args.delimiter

Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL and pi.dev 0.81.1
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-09 12:07:37 +00:00

66 lines
2 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING, Iterable, cast
if TYPE_CHECKING:
from ..ExecContext import ExecContext
from ..base import InputMode
from ..Package import Package
from ..util import run_cmd, run_sudo
_meta_map: dict[str, str] | None = None
def meta_map() -> dict[str, str]:
global _meta_map
if _meta_map is None:
_meta_map = Package.order_tags(
{
'name': 'binary:Package',
'vendor': None, # deb doesn't have vendor field
'packager': None, # -- packager --
'url': 'Homepage',
'maintainer': 'Maintainer',
}
)
return _meta_map
async def _run(
cmd: list[str], sudo: bool = False, ec: ExecContext | None = None
) -> str:
return (
await run_sudo(cmd)
if sudo else await run_cmd(cmd, ec = ec, cmd_input = InputMode.NonInteractive)
).stdout_str
async def run_dpkg( # export
args: list[str],
sudo: bool = False,
ec: ExecContext | None = None
) -> str:
cmd = ['/usr/bin/dpkg']
cmd.extend(args)
return await _run(cmd, sudo, ec)
async def run_dpkg_query( # export
args: list[str],
sudo: bool = False,
ec: ExecContext | None = None
) -> str:
cmd = ['/usr/bin/dpkg-query']
cmd.extend(args)
return await _run(cmd, sudo, ec)
async def query_packages(names: Iterable[str] = [],
ec: ExecContext | None = None) -> Iterable[Package]:
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 = await run_dpkg_query(['-W', '-f=' + fmt_str, *names], sudo = False, ec = ec)
return cast('Iterable[Package]', Package.parse_specs_str(specs))
async def list_files(pkg: str, ec: ExecContext | None = None) -> list[str]:
file_list_str = await run_dpkg(['-L', pkg], sudo = False, ec = ec)
return file_list_str.splitlines()