From e2e1b3388cad924c96af7c5a675da747a90e3627 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sun, 1 Mar 2026 17:08:12 +0100 Subject: [PATCH] jw.pkg.cmds.distro.CmdInfo: Add class CmdInfo provides what "projects os-cascade" provided as "distro info --format '%{cascade}'" plus additional macros: %{id}, %{name}, %{codename} and ${gnu-triplet}. Signed-off-by: Jan Lindemann --- src/python/jw/pkg/cmds/distro/CmdInfo.py | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/python/jw/pkg/cmds/distro/CmdInfo.py diff --git a/src/python/jw/pkg/cmds/distro/CmdInfo.py b/src/python/jw/pkg/cmds/distro/CmdInfo.py new file mode 100644 index 00000000..45353698 --- /dev/null +++ b/src/python/jw/pkg/cmds/distro/CmdInfo.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- + +import re +from argparse import Namespace, ArgumentParser + +from ..Cmd import Cmd +from ..CmdProjects import CmdProjects + +class CmdInfo(Cmd): # export + + def __init__(self, parent: CmdProjects) -> None: + super().__init__(parent, 'info', help='Print information about operating system this script runs on') + + def _expand_macros(self, string: str) -> str: + ret = string + for macro in re.findall("%{([A-Za-z_-]+)}", string): + try: + name = 'distro_' + macro.replace('-', '_') + val = getattr(self.app, name) + patt = r'%{' + macro + r'}' + if ret.find(patt) == -1: + continue + ret = ret.replace(patt, val) + except Exception as e: + raise Exception(f'Failed to expand macro "{macro}" inside "{string}": {e}') + return ret + + def add_arguments(self, parser: ArgumentParser) -> None: + super().add_arguments(parser) + macro_names = ', '.join(['%%{' + name.replace('distro_', '').replace('_', '-') + '}' + for name in dir(self.app) if name.startswith('distro_')]) + parser.add_argument('--format', default='%{cascade}', + help=f'Format string, expanding macros {macro_names}') + + async def _run(self, args: Namespace) -> None: + print(self._expand_macros(args.format))