From 2241aa500f4b813c1a8932061d161a54b7a758de Mon Sep 17 00:00:00 2001 From: ldericher <40151420+ldericher@users.noreply.github.com> Date: Sat, 13 Nov 2021 03:26:32 +0100 Subject: [PATCH] Common Executables --- kiwi_scp/executable.py | 18 +++++++++++------- kiwi_scp/rootkit.py | 10 +++++----- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/kiwi_scp/executable.py b/kiwi_scp/executable.py index 48168d1..184e41e 100644 --- a/kiwi_scp/executable.py +++ b/kiwi_scp/executable.py @@ -3,7 +3,7 @@ import logging import os import subprocess from pathlib import Path -from typing import Optional, List, Any +from typing import Optional, List import attr @@ -17,7 +17,7 @@ class Executable: @staticmethod @functools.lru_cache(maxsize=None) def __find_exe_file(exe_name: str) -> Optional[Path]: - for path in os.environ['PATH'].split(os.pathsep): + for path in os.environ["PATH"].split(os.pathsep): exe_file = Path(path).joinpath(exe_name) if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK): return exe_file @@ -46,15 +46,19 @@ class Executable: **kwargs ) - def run_less(self, process_args, **kwargs) -> Optional[subprocess.CompletedProcess]: - kwargs['stdout'] = subprocess.PIPE - kwargs['stderr'] = subprocess.DEVNULL + def run_with_pager(self, process_args, **kwargs) -> Optional[subprocess.CompletedProcess]: + kwargs["stdout"] = subprocess.PIPE + kwargs["stderr"] = subprocess.DEVNULL with self.Popen(process_args, **kwargs) as process: - less_process = Executable('less').run([ - '-R', '+G' + less_process = Executable("less").run([ + "-R", "+G" ], stdin=process.stdout) process.communicate() return less_process + + +DOCKER_EXE = Executable("docker") +COMPOSE_EXE = Executable("docker-compose") diff --git a/kiwi_scp/rootkit.py b/kiwi_scp/rootkit.py index 7332c43..adef3f1 100644 --- a/kiwi_scp/rootkit.py +++ b/kiwi_scp/rootkit.py @@ -7,7 +7,7 @@ from typing import Optional, TypeVar, Union, List import attr from ._constants import IMAGES_DIRECTORY_NAME, LOCAL_IMAGES_NAME, DEFAULT_IMAGE_NAME -from .executable import Executable +from .executable import DOCKER_EXE _logger = logging.getLogger(__name__) @@ -40,7 +40,7 @@ class Rootkit: @staticmethod @functools.lru_cache(maxsize=None) def __exists(image_tag: str) -> bool: - ps = Executable('docker').run([ + ps = DOCKER_EXE.run([ 'images', '--filter', f"reference={Rootkit.__image_name(image_tag)}", '--format', '{{.Repository}}:{{.Tag}}' @@ -54,13 +54,13 @@ class Rootkit: else: if self.image_tag is None: _logger.info(f"Pulling image {Rootkit.__image_name(self.image_tag)}") - Executable('docker').run([ + DOCKER_EXE.run([ 'pull', Rootkit.__image_name(self.image_tag) ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) else: _logger.info(f"Building image {Rootkit.__image_name(self.image_tag)}") - Executable('docker').run([ + DOCKER_EXE.run([ 'build', '-t', Rootkit.__image_name(self.image_tag), '-f', f"{IMAGES_DIRECTORY_NAME}/{self.image_tag}.Dockerfile", @@ -69,7 +69,7 @@ class Rootkit: def run(self, process_args, **kwargs): self.__build_image() - Executable('docker').run([ + DOCKER_EXE.run([ 'run', '--rm', '-v', '/:/mnt', '-u', 'root',