From 70b04f039a637bb30a9b8b3d566e941372007687 Mon Sep 17 00:00:00 2001 From: ldericher Date: Wed, 12 Aug 2020 17:39:55 +0200 Subject: [PATCH] DockerCommand now is an Executable --- src/kiwi/subcommands/utils/dockercommand.py | 24 ++++++++++++-------- src/kiwi/subcommands/utils/executable.py | 25 +++++++++++---------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/kiwi/subcommands/utils/dockercommand.py b/src/kiwi/subcommands/utils/dockercommand.py index c063930..0a52b4e 100644 --- a/src/kiwi/subcommands/utils/dockercommand.py +++ b/src/kiwi/subcommands/utils/dockercommand.py @@ -3,33 +3,39 @@ import subprocess from .executable import Executable -class DockerCommand: +class DockerCommand(Executable): __requires_root = None - __exe = None def __init__(self, exe_name): + super().__init__(exe_name) + if DockerCommand.__requires_root is None: try: Executable('docker').run( - ['ps'], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL + ['ps'], + check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) DockerCommand.__requires_root = False except subprocess.CalledProcessError: DockerCommand.__requires_root = True - self.__exe = Executable(exe_name, DockerCommand.__requires_root) - - def __getattr__(self, item): - return getattr(self.__exe, item) + def run(self, args, **kwargs): + # equivalent to 'super().run' but agnostic of nested class construct + super().__getattr__("run")( + args, DockerCommand.__requires_root, + **kwargs + ) def run_less(self, args, **kwargs): process = self.Popen( - args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, + args, DockerCommand.__requires_root, + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, **kwargs ) less_process = Executable('less').run( - ['-R', '+G'], stdin=process.stdout + ['-R', '+G'], + stdin=process.stdout ) process.communicate() diff --git a/src/kiwi/subcommands/utils/executable.py b/src/kiwi/subcommands/utils/executable.py index 8d793d9..f71802b 100644 --- a/src/kiwi/subcommands/utils/executable.py +++ b/src/kiwi/subcommands/utils/executable.py @@ -20,35 +20,36 @@ def find_exe_file(exe_name): class Executable: - __exe_name = None - __instances = {} - class __Executable: - __cmd = None + __exe_path = None def __init__(self, exe_name, requires_root=False): - self.__cmd = [find_exe_file(exe_name)] + self.__exe_path = find_exe_file(exe_name) + + def __build_cmd(self, args, requires_root=False, **kwargs): + cmd = [self.__exe_path, *args] if requires_root: - self.__cmd = [find_exe_file("sudo"), *self.__cmd] + self.__exe_path = [find_exe_file("sudo"), self.__exe_path] - def __build_cmd(self, args, **kwargs): - cmd = [*self.__cmd, *args] logging.debug(f"Executable cmd{cmd}, kwargs{kwargs}") return cmd - def run(self, args, **kwargs): + def run(self, args, requires_root=False, **kwargs): return subprocess.run( - self.__build_cmd(args, **kwargs), + self.__build_cmd(args, requires_root, **kwargs), **kwargs ) - def Popen(self, args, **kwargs): + def Popen(self, args, requires_root=False, **kwargs): return subprocess.Popen( - self.__build_cmd(args, **kwargs), + self.__build_cmd(args, requires_root, **kwargs), **kwargs ) + __exe_name = None + __instances = {} + def __init__(self, exe_name, requires_root=False): self.__exe_name = exe_name