From 18c16917b22ca9b03a9a505620e4f6861dd689b1 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sat, 7 Mar 2026 11:08:14 +0100 Subject: [PATCH] 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 --- src/python/jw/pkg/App.py | 15 +++++++++- src/python/jw/pkg/cmds/Cmd.py | 10 +++++++ src/python/jw/pkg/cmds/CmdDistro.py | 3 +- src/python/jw/pkg/cmds/DistroBase.py | 45 ---------------------------- src/python/jw/pkg/cmds/distro/Cmd.py | 14 +-------- 5 files changed, 27 insertions(+), 60 deletions(-) delete mode 100644 src/python/jw/pkg/cmds/DistroBase.py diff --git a/src/python/jw/pkg/App.py b/src/python/jw/pkg/App.py index 10b76a53..41366969 100644 --- a/src/python/jw/pkg/App.py +++ b/src/python/jw/pkg/App.py @@ -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) diff --git a/src/python/jw/pkg/cmds/Cmd.py b/src/python/jw/pkg/cmds/Cmd.py index 783886ae..86f66aa4 100644 --- a/src/python/jw/pkg/cmds/Cmd.py +++ b/src/python/jw/pkg/cmds/Cmd.py @@ -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 diff --git a/src/python/jw/pkg/cmds/CmdDistro.py b/src/python/jw/pkg/cmds/CmdDistro.py index 4747a778..fe58cdc5 100644 --- a/src/python/jw/pkg/cmds/CmdDistro.py +++ b/src/python/jw/pkg/cmds/CmdDistro.py @@ -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) diff --git a/src/python/jw/pkg/cmds/DistroBase.py b/src/python/jw/pkg/cmds/DistroBase.py deleted file mode 100644 index 26f7d3fd..00000000 --- a/src/python/jw/pkg/cmds/DistroBase.py +++ /dev/null @@ -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) diff --git a/src/python/jw/pkg/cmds/distro/Cmd.py b/src/python/jw/pkg/cmds/distro/Cmd.py index b6da5d51..da31059c 100644 --- a/src/python/jw/pkg/cmds/distro/Cmd.py +++ b/src/python/jw/pkg/cmds/distro/Cmd.py @@ -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