mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-25 09:35:54 +02:00
App, .cmds.Cmd: Add .distro property
DistroBase's option --id is now redundant to the new global option --distro-id in the App class, so remove --id. The only added value DistroBase then brings to the table is its .distro property, which can be provided by App just fine at this point, given that App has all it needs to construct a Distro object, so add .distro to App and remove the entire DistroBase class. For convenience, also make App.distro available as a newly added cmds.Cmd.distro property. This also obviates the need for the distro-related properties in the .distro.Cmd class, remove all that. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
bd38700f67
commit
18c16917b2
5 changed files with 27 additions and 60 deletions
|
|
@ -5,7 +5,11 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TypeAlias
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import TypeAlias
|
||||
import os, sys, pwd, re
|
||||
|
||||
import os, sys, argparse, pwd, re
|
||||
from functools import lru_cache
|
||||
|
|
@ -13,6 +17,7 @@ from enum import Enum, auto
|
|||
|
||||
from .lib.App import App as Base
|
||||
from .lib.log import *
|
||||
from .lib.Distro import Distro
|
||||
|
||||
# Meaning of pkg.requires.xxx variables
|
||||
# build: needs to be built and installed before this can be built
|
||||
|
|
@ -218,6 +223,7 @@ class App(Base):
|
|||
self.__distro_id: str|None = None
|
||||
self.__distro_name: str|None = None
|
||||
self.__distro_codename: str|None = None
|
||||
self.__distro: Distro|None = None
|
||||
self.__os_cascade: list[str]|None = None
|
||||
self.__res_cache = ResultCache()
|
||||
self.__topdir: str|None = None
|
||||
|
|
@ -380,6 +386,13 @@ class App(Base):
|
|||
|
||||
raise RuntimeError('Failed to get GNU triplet from running machine')
|
||||
|
||||
@property
|
||||
def distro(self) -> Distro:
|
||||
if self.__distro is None:
|
||||
ret = Distro.instantiate(self.distro_id, ec=self.exec_context)
|
||||
self.__distro = ret
|
||||
return self.__distro
|
||||
|
||||
def find_dir(self, name: str, search_subdirs: list[str]=[], search_absdirs: list[str]=[], pretty: bool=True):
|
||||
return self.__find_dir(name, search_subdirs, search_absdirs, pretty)
|
||||
|
||||
|
|
|
|||
|
|
@ -18,3 +18,13 @@ class Cmd(Base): # export
|
|||
# the module path of the calling module itself
|
||||
cmds_module = type(self).__module__.replace('Cmd', '').lower()
|
||||
self.add_subcommands(LoadTypes([cmds_module], type_name_filter=r'Cmd[^.]'))
|
||||
|
||||
async def _run(self, args):
|
||||
import sys
|
||||
# Missing subcommand
|
||||
self.parser.print_help()
|
||||
sys.exit(1)
|
||||
|
||||
@property
|
||||
def distro(self) -> Distro:
|
||||
return self.app.distro
|
||||
|
|
|
|||
|
|
@ -3,12 +3,13 @@
|
|||
from argparse import ArgumentParser
|
||||
|
||||
from ..App import App
|
||||
from .DistroBase import DistroBase as CmdBase
|
||||
from .Cmd import Cmd as CmdBase
|
||||
|
||||
class CmdDistro(CmdBase): # export
|
||||
|
||||
def __init__(self, parent: App) -> None:
|
||||
super().__init__(parent, 'distro', help="System package manager wrapper")
|
||||
self._add_subcommands()
|
||||
|
||||
def add_arguments(self, p: ArgumentParser) -> None:
|
||||
super().add_arguments(p)
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import re, sys
|
||||
from argparse import ArgumentParser
|
||||
|
||||
from ..lib.Distro import Distro
|
||||
from ..App import App
|
||||
from .Cmd import Cmd as Base
|
||||
|
||||
class DistroBase(Base): # export
|
||||
|
||||
def __init__(self, parent: App|Base, name: str, help: str) -> None:
|
||||
self.__id = None
|
||||
super().__init__(parent, name, help)
|
||||
self._add_subcommands()
|
||||
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):
|
||||
if self.__id is None:
|
||||
if self.app.args.id is not None:
|
||||
# The distribution ID requested by the command line
|
||||
self.__id = self.app.args.id
|
||||
else:
|
||||
# The ID of the distribution we run on
|
||||
self.__id = self.app.distro_id
|
||||
return self.__id
|
||||
|
||||
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)')
|
||||
|
||||
async def _run(self, args):
|
||||
# Missing subcommand
|
||||
self.parser.print_help()
|
||||
sys.exit(1)
|
||||
|
|
@ -4,25 +4,13 @@ from __future__ import annotations
|
|||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import os, importlib
|
||||
|
||||
from ...lib.log import *
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ...lib.Distro import Distro
|
||||
from ..CmdDistro import CmdDistro
|
||||
|
||||
from ..Cmd import Cmd as Base
|
||||
from ..CmdDistro import CmdDistro
|
||||
|
||||
class Cmd(Base): # export
|
||||
|
||||
def __init__(self, parent: CmdDistro, name: str, help: str) -> None:
|
||||
super().__init__(parent, name, help)
|
||||
|
||||
@property
|
||||
def distro(self) -> Distro:
|
||||
return self.parent.distro
|
||||
|
||||
@property
|
||||
def distro_id(self) -> str:
|
||||
return self.parent.distro_id
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue