jw-pkg/src/python/jw/pkg/lib/pm/rpm.py

45 lines
1.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from __future__ import annotations
from typing import Iterable, TYPE_CHECKING
if TYPE_CHECKING:
from ..ExecContext import ExecContext
from ..util import run_cmd, run_sudo
from ..Package import Package, meta_tags
_meta_map: dict[str, str]|None = None
def meta_map():
global _meta_map
if _meta_map is None:
_meta_map = Package.order_tags({
'name': 'Name',
'vendor': 'Vendor',
'packager': 'Packager',
'url': 'URL',
'maintainer': None, # RPM doesn't have a maintainer field
})
return _meta_map
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, ec=ec, **kwargs)
return await run_cmd(cmd, ec=ec, **kwargs)
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, ec=ec)
return Package.parse_specs_str(specs)
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()