mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-24 09:13:37 +02:00
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:
parent
f58e9fe298
commit
e2e1b3388c
1 changed files with 36 additions and 0 deletions
36
src/python/jw/pkg/cmds/distro/CmdInfo.py
Normal file
36
src/python/jw/pkg/cmds/distro/CmdInfo.py
Normal 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))
|
||||
Loading…
Add table
Add a link
Reference in a new issue