mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-24 17:23:36 +02:00
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>
64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import os, importlib
|
|
|
|
from ...lib.log import *
|
|
from ...lib.distros.Util import Util
|
|
|
|
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_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
|