2026-01-23 14:54:27 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
import os, importlib
|
|
|
|
|
|
|
|
|
|
from ..Cmd import Cmd as Base
|
|
|
|
|
from ..CmdDistro import CmdDistro
|
2026-02-17 11:51:40 +01:00
|
|
|
from .backend.Util import Util
|
2026-01-23 14:54:27 +01:00
|
|
|
|
|
|
|
|
class Cmd(Base): # export
|
|
|
|
|
|
2026-02-17 11:51:40 +01:00
|
|
|
from .backend.Backend import Backend
|
|
|
|
|
|
2026-01-23 14:54:27 +01:00
|
|
|
def __init__(self, parent: CmdDistro, name: str, help: str) -> None:
|
|
|
|
|
super().__init__(parent, name, help)
|
2026-02-17 11:51:40 +01:00
|
|
|
self.__backend_path: str|None = None
|
|
|
|
|
self.__util: Util|None = None
|
|
|
|
|
self.__backend: Backend|None = None
|
2026-01-23 14:54:27 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def distro_id(self) -> str:
|
|
|
|
|
return self.parent.distro_id
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def interactive(self) -> bool:
|
|
|
|
|
return self.parent.interactive
|
|
|
|
|
|
|
|
|
|
@property
|
2026-02-17 11:51:40 +01:00
|
|
|
def _backend_path(self):
|
|
|
|
|
if self.__backend_path is None:
|
2026-01-23 14:54:27 +01:00
|
|
|
backend_id = self.parent.distro_id.lower().replace('-', '_')
|
|
|
|
|
match backend_id:
|
2026-02-14 20:35:53 +01:00
|
|
|
case 'ubuntu' | 'raspbian' | 'kali':
|
2026-01-23 14:54:27 +01:00
|
|
|
backend_id = 'debian'
|
|
|
|
|
case 'centos':
|
|
|
|
|
backend_id = 'redhat'
|
2026-03-03 05:02:44 +01:00
|
|
|
case 'opensuse' | 'suse':
|
2026-01-23 14:54:27 +01:00
|
|
|
backend_id = 'suse'
|
2026-02-17 11:51:40 +01:00
|
|
|
self.__backend_path = (
|
2026-01-23 14:54:27 +01:00
|
|
|
os.path.splitext(__name__)[0]
|
|
|
|
|
+ '.backend.'
|
|
|
|
|
+ backend_id
|
|
|
|
|
+ '.'
|
|
|
|
|
)
|
2026-02-17 11:51:40 +01:00
|
|
|
return self.__backend_path
|
|
|
|
|
|
|
|
|
|
def _instantiate(self, name: str, *args, **kwargs):
|
|
|
|
|
module = importlib.import_module(self._backend_path + name)
|
|
|
|
|
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)
|
2026-01-23 14:54:27 +01:00
|
|
|
return self.__backend
|