Common Executables

This commit is contained in:
Jörn-Michael Miehe 2021-11-13 03:26:32 +01:00
parent e7e6e5867a
commit 2241aa500f
2 changed files with 16 additions and 12 deletions

View file

@ -3,7 +3,7 @@ import logging
import os import os
import subprocess import subprocess
from pathlib import Path from pathlib import Path
from typing import Optional, List, Any from typing import Optional, List
import attr import attr
@ -17,7 +17,7 @@ class Executable:
@staticmethod @staticmethod
@functools.lru_cache(maxsize=None) @functools.lru_cache(maxsize=None)
def __find_exe_file(exe_name: str) -> Optional[Path]: 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) exe_file = Path(path).joinpath(exe_name)
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK): if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
return exe_file return exe_file
@ -46,15 +46,19 @@ class Executable:
**kwargs **kwargs
) )
def run_less(self, process_args, **kwargs) -> Optional[subprocess.CompletedProcess]: def run_with_pager(self, process_args, **kwargs) -> Optional[subprocess.CompletedProcess]:
kwargs['stdout'] = subprocess.PIPE kwargs["stdout"] = subprocess.PIPE
kwargs['stderr'] = subprocess.DEVNULL kwargs["stderr"] = subprocess.DEVNULL
with self.Popen(process_args, **kwargs) as process: with self.Popen(process_args, **kwargs) as process:
less_process = Executable('less').run([ less_process = Executable("less").run([
'-R', '+G' "-R", "+G"
], stdin=process.stdout) ], stdin=process.stdout)
process.communicate() process.communicate()
return less_process return less_process
DOCKER_EXE = Executable("docker")
COMPOSE_EXE = Executable("docker-compose")

View file

@ -7,7 +7,7 @@ from typing import Optional, TypeVar, Union, List
import attr import attr
from ._constants import IMAGES_DIRECTORY_NAME, LOCAL_IMAGES_NAME, DEFAULT_IMAGE_NAME 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__) _logger = logging.getLogger(__name__)
@ -40,7 +40,7 @@ class Rootkit:
@staticmethod @staticmethod
@functools.lru_cache(maxsize=None) @functools.lru_cache(maxsize=None)
def __exists(image_tag: str) -> bool: def __exists(image_tag: str) -> bool:
ps = Executable('docker').run([ ps = DOCKER_EXE.run([
'images', 'images',
'--filter', f"reference={Rootkit.__image_name(image_tag)}", '--filter', f"reference={Rootkit.__image_name(image_tag)}",
'--format', '{{.Repository}}:{{.Tag}}' '--format', '{{.Repository}}:{{.Tag}}'
@ -54,13 +54,13 @@ class Rootkit:
else: else:
if self.image_tag is None: if self.image_tag is None:
_logger.info(f"Pulling image {Rootkit.__image_name(self.image_tag)}") _logger.info(f"Pulling image {Rootkit.__image_name(self.image_tag)}")
Executable('docker').run([ DOCKER_EXE.run([
'pull', Rootkit.__image_name(self.image_tag) 'pull', Rootkit.__image_name(self.image_tag)
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
else: else:
_logger.info(f"Building image {Rootkit.__image_name(self.image_tag)}") _logger.info(f"Building image {Rootkit.__image_name(self.image_tag)}")
Executable('docker').run([ DOCKER_EXE.run([
'build', 'build',
'-t', Rootkit.__image_name(self.image_tag), '-t', Rootkit.__image_name(self.image_tag),
'-f', f"{IMAGES_DIRECTORY_NAME}/{self.image_tag}.Dockerfile", '-f', f"{IMAGES_DIRECTORY_NAME}/{self.image_tag}.Dockerfile",
@ -69,7 +69,7 @@ class Rootkit:
def run(self, process_args, **kwargs): def run(self, process_args, **kwargs):
self.__build_image() self.__build_image()
Executable('docker').run([ DOCKER_EXE.run([
'run', '--rm', 'run', '--rm',
'-v', '/:/mnt', '-v', '/:/mnt',
'-u', 'root', '-u', 'root',