mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-28 13:55:24 +02:00
49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
|
||
|
|
import re, sys
|
||
|
|
from argparse import ArgumentParser
|
||
|
|
|
||
|
|
from ..App import App
|
||
|
|
from .Cmd import Cmd as CmdBase
|
||
|
|
|
||
|
|
class CmdDistro(CmdBase): # export
|
||
|
|
|
||
|
|
def __init__(self, parent: App) -> None:
|
||
|
|
self.__id = None
|
||
|
|
super().__init__(parent, 'distro', help="System package manager wrapper")
|
||
|
|
self._add_subcommands()
|
||
|
|
self.__interactive: bool|None = None
|
||
|
|
|
||
|
|
@property
|
||
|
|
def distro_id(self):
|
||
|
|
if self.__id is None:
|
||
|
|
if self.app.args.id is not None:
|
||
|
|
self.__id = self.app.args.id
|
||
|
|
else:
|
||
|
|
os_release = '/etc/os-release'
|
||
|
|
with open(os_release, 'r') as file:
|
||
|
|
matches = re.findall(r'^ID="?([^"]+)"?$', file.read(), re.MULTILINE)
|
||
|
|
if len(matches) != 1:
|
||
|
|
raise Exception(f'Could not read "ID=" from "{os_release}"')
|
||
|
|
self.__id = matches[0]
|
||
|
|
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)')
|
||
|
|
p.add_argument('--interactive', choices=['true', 'false', 'auto'], default='false', help="Wait for user input or try to proceed unattended")
|
||
|
|
|
||
|
|
@property
|
||
|
|
def interactive(self) -> bool:
|
||
|
|
if self.__interactive is None:
|
||
|
|
match self.app.args.interactive:
|
||
|
|
case 'true':
|
||
|
|
self.__interactive = True
|
||
|
|
case 'false':
|
||
|
|
self.__interactive = False
|
||
|
|
case 'auto':
|
||
|
|
self.__interactive = sys.stdin.isatty()
|
||
|
|
return self.__interactive
|
||
|
|
|
||
|
|
async def _run(self, args):
|
||
|
|
raise Exception("Running with args", args)
|