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 <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-03-01 17:08:12 +01:00
commit e2e1b3388c

View file

@ -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))