lib.Distro, ExecContext: Add classes, refactor lib.distro
The code below lib.distro, as left behind by the previous commit, is
geared towards being directly used as a command-line API. This commit
introduces the abstract base class Distro, a proxy for
distribution-specific interactions. The proxy abstracts distro
specifics into an API with proper method prototypes, not
argparse.Namespace contents, and can thus be more easily driven by
arbitrary code.
The Distro class is initialized with a member variable of type
ExecContext, another new class introduced by this commit. It is
designed to abstract the communication channel to the distribution
instance. Currently only one specialization exists, Local, which
interacts with the distribution and root file system it is running
in, but is planned to be subclassed to support interaction via SSH,
serial, chroot, or chains thereof.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-05 17:33:52 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
from ...log import *
|
|
|
|
|
from ...Distro import Distro as Base
|
|
|
|
|
from ...pm.dpkg import run_dpkg, run_dpkg_query, query_packages, list_files
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from typing import Iterable
|
2026-04-16 11:23:05 +02:00
|
|
|
from ...base import Result
|
lib.Distro, ExecContext: Add classes, refactor lib.distro
The code below lib.distro, as left behind by the previous commit, is
geared towards being directly used as a command-line API. This commit
introduces the abstract base class Distro, a proxy for
distribution-specific interactions. The proxy abstracts distro
specifics into an API with proper method prototypes, not
argparse.Namespace contents, and can thus be more easily driven by
arbitrary code.
The Distro class is initialized with a member variable of type
ExecContext, another new class introduced by this commit. It is
designed to abstract the communication channel to the distribution
instance. Currently only one specialization exists, Local, which
interacts with the distribution and root file system it is running
in, but is planned to be subclassed to support interaction via SSH,
serial, chroot, or chains thereof.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-05 17:33:52 +01:00
|
|
|
from ...Package import Package
|
|
|
|
|
|
|
|
|
|
class Distro(Base):
|
|
|
|
|
|
2026-03-16 11:07:41 +01:00
|
|
|
async def apt_get(self, args: list[str], verbose: bool=True, sudo: bool=True):
|
lib.Distro, ExecContext: Add classes, refactor lib.distro
The code below lib.distro, as left behind by the previous commit, is
geared towards being directly used as a command-line API. This commit
introduces the abstract base class Distro, a proxy for
distribution-specific interactions. The proxy abstracts distro
specifics into an API with proper method prototypes, not
argparse.Namespace contents, and can thus be more easily driven by
arbitrary code.
The Distro class is initialized with a member variable of type
ExecContext, another new class introduced by this commit. It is
designed to abstract the communication channel to the distribution
instance. Currently only one specialization exists, Local, which
interacts with the distribution and root file system it is running
in, but is planned to be subclassed to support interaction via SSH,
serial, chroot, or chains thereof.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-05 17:33:52 +01:00
|
|
|
cmd = ['/usr/bin/apt-get']
|
|
|
|
|
mod_env = None
|
|
|
|
|
if not self.interactive:
|
|
|
|
|
cmd.extend(['--yes', '--quiet'])
|
|
|
|
|
mod_env = { 'DEBIAN_FRONTEND': 'noninteractive' }
|
|
|
|
|
cmd.extend(args)
|
2026-03-16 11:07:41 +01:00
|
|
|
if sudo:
|
|
|
|
|
return await self.sudo(cmd, verbose=verbose, mod_env=mod_env)
|
|
|
|
|
return await self.run(cmd, verbose=verbose)
|
lib.Distro, ExecContext: Add classes, refactor lib.distro
The code below lib.distro, as left behind by the previous commit, is
geared towards being directly used as a command-line API. This commit
introduces the abstract base class Distro, a proxy for
distribution-specific interactions. The proxy abstracts distro
specifics into an API with proper method prototypes, not
argparse.Namespace contents, and can thus be more easily driven by
arbitrary code.
The Distro class is initialized with a member variable of type
ExecContext, another new class introduced by this commit. It is
designed to abstract the communication channel to the distribution
instance. Currently only one specialization exists, Local, which
interacts with the distribution and root file system it is running
in, but is planned to be subclassed to support interaction via SSH,
serial, chroot, or chains thereof.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-05 17:33:52 +01:00
|
|
|
|
|
|
|
|
async def dpkg(self, *args, **kwargs):
|
2026-03-06 17:25:19 +01:00
|
|
|
return await run_dpkg(*args, ec=self.ctx, **kwargs)
|
lib.Distro, ExecContext: Add classes, refactor lib.distro
The code below lib.distro, as left behind by the previous commit, is
geared towards being directly used as a command-line API. This commit
introduces the abstract base class Distro, a proxy for
distribution-specific interactions. The proxy abstracts distro
specifics into an API with proper method prototypes, not
argparse.Namespace contents, and can thus be more easily driven by
arbitrary code.
The Distro class is initialized with a member variable of type
ExecContext, another new class introduced by this commit. It is
designed to abstract the communication channel to the distribution
instance. Currently only one specialization exists, Local, which
interacts with the distribution and root file system it is running
in, but is planned to be subclassed to support interaction via SSH,
serial, chroot, or chains thereof.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-05 17:33:52 +01:00
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
async def _ref(self) -> None:
|
|
|
|
|
return await self.apt_get(['update'])
|
|
|
|
|
|
|
|
|
|
async def _dup(self, download_only: bool) -> None:
|
|
|
|
|
args: list[str] = []
|
|
|
|
|
if download_only:
|
|
|
|
|
args.append('--download-only')
|
|
|
|
|
args.append('upgrade')
|
|
|
|
|
return await self.apt_get(args)
|
|
|
|
|
|
|
|
|
|
async def _reboot_required(self, verbose: bool) -> bool:
|
|
|
|
|
reboot_required = '/run/reboot_required'
|
|
|
|
|
if os.path.exists(reboot_required):
|
|
|
|
|
if verbose:
|
|
|
|
|
log(NOTICE, f'Yes. {reboot_required} exists.')
|
|
|
|
|
required_pkgs = '/run/reboot-required.pkgs'
|
|
|
|
|
if os.path.exists(required_pkgs):
|
|
|
|
|
with open(required_pkgs, 'r') as f:
|
|
|
|
|
content = f.read()
|
|
|
|
|
print(f'-- From {required_pkgs}:')
|
|
|
|
|
print(content.strip())
|
|
|
|
|
return True
|
|
|
|
|
if verbose:
|
|
|
|
|
log(NOTICE, f'No. {reboot_required} doesn\'t exist.')
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
async def _select(self, names: Iterable[str]) -> Iterable[Package]:
|
2026-03-06 17:25:19 +01:00
|
|
|
return await query_packages(names, ec=self.ctx)
|
lib.Distro, ExecContext: Add classes, refactor lib.distro
The code below lib.distro, as left behind by the previous commit, is
geared towards being directly used as a command-line API. This commit
introduces the abstract base class Distro, a proxy for
distribution-specific interactions. The proxy abstracts distro
specifics into an API with proper method prototypes, not
argparse.Namespace contents, and can thus be more easily driven by
arbitrary code.
The Distro class is initialized with a member variable of type
ExecContext, another new class introduced by this commit. It is
designed to abstract the communication channel to the distribution
instance. Currently only one specialization exists, Local, which
interacts with the distribution and root file system it is running
in, but is planned to be subclassed to support interaction via SSH,
serial, chroot, or chains thereof.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-03-05 17:33:52 +01:00
|
|
|
|
|
|
|
|
async def _install(self, names: Iterable[str], only_update: bool) -> None:
|
|
|
|
|
args = ['install']
|
|
|
|
|
if only_update:
|
|
|
|
|
args.append('--only-upgrade')
|
|
|
|
|
args.append('--no-install-recommends')
|
|
|
|
|
args.extend(names)
|
|
|
|
|
return await self.apt_get(args)
|
|
|
|
|
|
|
|
|
|
async def _delete(self, names: Iterable[str]) -> None:
|
|
|
|
|
return await self.dpkg(['-P', *names], sudo=True)
|
|
|
|
|
|
|
|
|
|
async def _pkg_files(self, name: str) -> Iterable[str]:
|
2026-03-06 17:25:19 +01:00
|
|
|
return await list_files(name, ec=self.ctx)
|