cmds.distro.lib.dpkg: Add module

As with cmds.distro.lib.rpm, the dpkg module is a place for Python
abstractions of the Debian package manager tools dpkg and friends.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-03-04 09:53:14 +01:00
commit 7f72d17f7a

View file

@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
from typing import Iterable
from ....lib.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': 'binary:Package',
'vendor': None, # deb doesn't have vendor field
'packager': None, # -- packager --
'url': 'Homepage',
'maintainer': 'Maintainer',
})
return _meta_map
async def run_dpkg(args: list[str], sudo: bool=False): # export
cmd = ['/usr/bin/dpkg']
cmd.extend(args)
if sudo:
return await run_sudo(cmd)
return await run_cmd(cmd)
async def run_dpkg_query(args: list[str], sudo: bool=False): # export
cmd = ['/usr/bin/dpkg-query']
cmd.extend(args)
if sudo:
return await run_sudo(cmd)
return await run_cmd(cmd)
async def query_packages(names: Iterable[str] = []) -> 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)
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)
return file_list_str.splitlines()