mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-28 21:54:34 +02:00
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>
This commit is contained in:
parent
f24541dbe4
commit
3e897f4df8
55 changed files with 426 additions and 720 deletions
|
|
@ -3,6 +3,7 @@
|
|||
import re, sys
|
||||
from argparse import ArgumentParser
|
||||
|
||||
from ..lib.Distro import Distro
|
||||
from ..App import App
|
||||
from .Cmd import Cmd as Base
|
||||
|
||||
|
|
@ -12,7 +13,16 @@ class DistroBase(Base): # export
|
|||
self.__id = None
|
||||
super().__init__(parent, name, help)
|
||||
self._add_subcommands()
|
||||
self.__interactive: bool|None = None
|
||||
self.__distro: Distro|None = None
|
||||
|
||||
@property
|
||||
def distro(self) -> Distro:
|
||||
if self.__distro is None:
|
||||
ret = Distro.instantiate(self.distro_id, ec=self.app.exec_context)
|
||||
self.__distro = ret
|
||||
return self.__distro
|
||||
|
||||
# --------------- legacy methods
|
||||
|
||||
@property
|
||||
def distro_id(self):
|
||||
|
|
@ -28,19 +38,6 @@ class DistroBase(Base): # export
|
|||
def add_arguments(self, p: ArgumentParser) -> None:
|
||||
super().add_arguments(p)
|
||||
p.add_argument('--id', default=None, help='Distribution ID (default is taken from /etc/os-release)')
|
||||
p.add_argument('--interactive', choices=['true', 'false', 'auto'], default='true', help="Wait for user input or try to proceed unattended")
|
||||
|
||||
@property
|
||||
def interactive(self) -> bool:
|
||||
if self.__interactive is None:
|
||||
match self.app.args.interactive:
|
||||
case 'true':
|
||||
self.__interactive = True
|
||||
case 'false':
|
||||
self.__interactive = False
|
||||
case 'auto':
|
||||
self.__interactive = sys.stdin.isatty()
|
||||
return self.__interactive
|
||||
|
||||
async def _run(self, args):
|
||||
# Missing subcommand
|
||||
|
|
|
|||
|
|
@ -1,64 +1,28 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import os, importlib
|
||||
|
||||
from ...lib.log import *
|
||||
from ...lib.distros.Util import Util
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ...lib.Distro import Distro
|
||||
|
||||
from ..Cmd import Cmd as Base
|
||||
from ..CmdDistro import CmdDistro
|
||||
|
||||
class Cmd(Base): # export
|
||||
|
||||
from ...lib.distros.Backend import Backend
|
||||
|
||||
def __init__(self, parent: CmdDistro, name: str, help: str) -> None:
|
||||
super().__init__(parent, name, help)
|
||||
self.__backend_path: str|None = None
|
||||
self.__util: Util|None = None
|
||||
self.__backend: Backend|None = None
|
||||
|
||||
@property
|
||||
def distro(self) -> Distro:
|
||||
return self.parent.distro
|
||||
|
||||
@property
|
||||
def distro_id(self) -> str:
|
||||
return self.parent.distro_id
|
||||
|
||||
@property
|
||||
def interactive(self) -> bool:
|
||||
return self.parent.interactive
|
||||
|
||||
@property
|
||||
def _backend_path(self):
|
||||
if self.__backend_path is None:
|
||||
backend_id = self.parent.distro_id.lower().replace('-', '_')
|
||||
match backend_id:
|
||||
case 'ubuntu' | 'raspbian' | 'kali':
|
||||
backend_id = 'debian'
|
||||
case 'centos':
|
||||
backend_id = 'redhat'
|
||||
case 'opensuse' | 'suse':
|
||||
backend_id = 'suse'
|
||||
self.__backend_path = 'jw.pkg.lib.distros.' + backend_id + '.'
|
||||
return self.__backend_path
|
||||
|
||||
def _instantiate(self, name: str, *args, **kwargs):
|
||||
module_path = self._backend_path + name
|
||||
try:
|
||||
module = importlib.import_module(module_path)
|
||||
except Exception as e:
|
||||
log(ERR, f'Failed to import module {module_path} ({str(e)})')
|
||||
raise
|
||||
cls = getattr(module, name)
|
||||
return cls(self, *args, **kwargs)
|
||||
|
||||
@property
|
||||
def util(self) -> Util:
|
||||
if self.__util is None:
|
||||
self.__util = self._instantiate('Util')
|
||||
return self.__util
|
||||
|
||||
@property
|
||||
def _backend(self) -> Backend:
|
||||
if self.__backend is None:
|
||||
name = self.__class__.__name__[3:] # Get rid of "Cmd"
|
||||
self.__backend = self._instantiate(name)
|
||||
return self.__backend
|
||||
|
|
|
|||
|
|
@ -15,4 +15,4 @@ class CmdDelete(Cmd): # export
|
|||
parser.add_argument("names", nargs="*", help="Names of packages to be deleted")
|
||||
|
||||
async def _run(self, args: Namespace) -> None:
|
||||
return await self._backend.run(args)
|
||||
return await self.distro.delete(args.names)
|
||||
|
|
|
|||
|
|
@ -14,6 +14,5 @@ class CmdDup(Cmd): # export
|
|||
super().add_arguments(parser)
|
||||
parser.add_argument('--download-only', default=False, action='store_true',
|
||||
help='Only download packages from the repos, don\'t install them, yet')
|
||||
|
||||
async def _run(self, args: Namespace) -> None:
|
||||
return await self._backend.run(args)
|
||||
return await self.distro.dup(download_only=args.download_only)
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ class CmdInstall(Cmd): # export
|
|||
|
||||
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||
super().add_arguments(parser)
|
||||
parser.add_argument("packages", nargs="*", help="Packages to be installed")
|
||||
parser.add_argument("names", nargs="*", help="Packages to be installed")
|
||||
parser.add_argument('--only-update', default=False, action='store_true', help='Only update the listed packages, don\'t install them')
|
||||
|
||||
async def _run(self, args: Namespace) -> None:
|
||||
return await self._backend.run(args)
|
||||
return await self.distro.install(args.names, only_update=args.only_update)
|
||||
|
|
|
|||
|
|
@ -15,4 +15,4 @@ class CmdRebootRequired(Cmd): # export
|
|||
parser.add_argument('--verbose', default=False, action='store_true', help='Be chatty about the check')
|
||||
|
||||
async def _run(self, args: Namespace) -> None:
|
||||
return await self._backend.run(args)
|
||||
return await self.distro.reboot_required(verbose=args.verbose)
|
||||
|
|
|
|||
|
|
@ -14,4 +14,4 @@ class CmdRefresh(Cmd): # export
|
|||
super().add_arguments(parser)
|
||||
|
||||
async def _run(self, args: Namespace) -> None:
|
||||
return await self._backend.run(args)
|
||||
return await self.distro.ref()
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ from argparse import Namespace, ArgumentParser
|
|||
import re
|
||||
|
||||
from ...lib.Package import Package
|
||||
from ...lib.distros.BeSelect import BeSelect
|
||||
from ..CmdDistro import CmdDistro
|
||||
from .Cmd import Cmd
|
||||
|
||||
|
|
@ -27,5 +26,5 @@ class CmdSelect(Cmd): # export
|
|||
|
||||
async def _run(self, args: Namespace) -> None:
|
||||
# TODO: Semantics probably change heavily in the future
|
||||
for p in self.filter_packages(args.filter, await self._backend.all_installed_packages):
|
||||
for p in self.filter_packages(args.filter, await self.distro.select()):
|
||||
print(p.name)
|
||||
|
|
|
|||
|
|
@ -7,15 +7,9 @@ from ..CmdPkg import CmdPkg as Parent
|
|||
|
||||
class Cmd(Base): # export
|
||||
|
||||
from ....lib.distros.Backend import Backend
|
||||
|
||||
def __init__(self, parent: Parent, name: str, help: str) -> None:
|
||||
super().__init__(parent, name, help)
|
||||
|
||||
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||
super().add_arguments(parser)
|
||||
parser.add_argument('names', nargs='*', help='Package names')
|
||||
|
||||
@property
|
||||
def _backend(self) -> Backend:
|
||||
return self.parent._backend
|
||||
|
|
|
|||
|
|
@ -15,4 +15,4 @@ class CmdLs(Cmd): # export
|
|||
|
||||
async def _run(self, args: Namespace) -> None:
|
||||
for name in args.names:
|
||||
print('\n'.join(await self._backend.files(name)))
|
||||
print('\n'.join(await self.parent.distro.pkg_files(name)))
|
||||
|
|
|
|||
|
|
@ -14,10 +14,8 @@ class CmdMeta(Cmd): # export
|
|||
super().add_arguments(parser)
|
||||
|
||||
async def _run(self, args: Namespace) -> None:
|
||||
for name in args.names:
|
||||
packages = await self._backend.meta_data([name])
|
||||
if packages:
|
||||
assert len(packages) == 1
|
||||
if len(args.names) > 1:
|
||||
print(f'-- {name}')
|
||||
print(packages[0])
|
||||
packages = await self.distro.select(args.names)
|
||||
for package in packages:
|
||||
if len(args.names) > 1:
|
||||
print(f'-- {name}')
|
||||
print(package)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue