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 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")

View file

@ -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',