jw-pkg/src/python/jw/pkg/cmds/distro/lib/rpm.py
Jan Lindemann 565946643b jw.pkg.*.run_xxx(): Return exit status
Most run_xxx() return stdout and stderr. There's no way, really, for
the caller to get hold of the exit code of the spawned executable. It
can pass throw=true, catch, and assume a non-zero exit status. But
that's not semantically clean, since the spawned function can well be
a test function which is expected to return a non-zero status code,
and the caller might be interested in what code that was, exactly.

The clearest way to solve this is to return the exit code as well.
This commit does that.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-03 11:23:30 +01:00

36 lines
1.2 KiB
Python

# -*- coding: utf-8 -*-
from typing import Iterable
from ....lib.util import run_cmd, run_sudo
from .Package import Package
async def run_rpm(args: list[str], sudo: bool=False): # export
cmd = ['/usr/bin/rpm']
cmd.extend(args)
if sudo:
return await run_sudo(cmd)
return await run_cmd(cmd)
async def all_installed_packages() -> Iterable[Package]: # export
ret: list[Package] = []
opts: list[str] = []
query_tags: list[str] = []
query_tags.append('Name')
query_tags.append('Vendor')
query_tags.append('Packager')
query_tags.append('URL')
opts.append('--queryformat')
tag_str = '|'.join([f'%{{{tag}}}' for tag in query_tags]) + r'\n'
opts.append(tag_str)
package_list_str, stderr, status = await run_rpm(['-qa', *opts], sudo=False)
for package_str in package_list_str.splitlines():
tags = package_str.split('|')
ret.append(Package(name=tags[0], vendor=tags[1], packager=tags[2], url=tags[3]))
return ret
async def list_files(pkg: str) -> list[str]: # export
opts: list[str] = []
file_list_str, stderr, status = await run_rpm(['-ql', pkg, *opts], sudo=False)
return file_list_str.splitlines()