cmds.distro: Move all modules to lib

Functions abstracting the distribution are not only needed in the
context of the distro subcommand, but also by other code, so make the
bulk of the code abstracting the distribution available in some place
more universally useful than below cmds.distro.

This commit leaves the source files mostly unchanged. They are only
patched to fix import paths, so that functionality is preserved.
Refactoring the code from command-line API to library API will be
done by the next commit.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-03-05 11:38:29 +01:00
commit 7e7cee6d11
45 changed files with 44 additions and 42 deletions

View file

@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
from typing import Iterable
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': '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()