mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-12-25 18:42:59 +00:00
subprocess environment consistency
This commit is contained in:
parent
4c70b7b2c1
commit
20f6131cfc
2 changed files with 52 additions and 31 deletions
|
@ -8,25 +8,25 @@ from .executable import Executable
|
|||
|
||||
|
||||
def _update_kwargs(config, args, **kwargs):
|
||||
project_name = args.project
|
||||
project_marker = config['markers:project']
|
||||
project_dir = f'{project_name}{project_marker}'
|
||||
kwargs['cwd'] = project_dir
|
||||
if 'project' in args:
|
||||
# command affects a project in this instance
|
||||
|
||||
if 'env' not in kwargs:
|
||||
kwargs['env'] = {}
|
||||
project_name = args.project
|
||||
project_marker = config['markers:project']
|
||||
project_dir = f'{project_name}{project_marker}'
|
||||
kwargs['cwd'] = project_dir
|
||||
|
||||
if config['runtime:env'] is not None:
|
||||
kwargs['env'].update(config['runtime:env'])
|
||||
if 'env' not in kwargs:
|
||||
kwargs['env'] = {}
|
||||
|
||||
kwargs['env'].update({
|
||||
'KIWI_HUB_NAME': config['network:name'],
|
||||
'COMPOSE_PROJECT_NAME': project_name,
|
||||
'CONFDIR': os.path.join(config['runtime:storage'], 'conf'),
|
||||
'TARGETDIR': os.path.join(config['runtime:storage'], project_dir)
|
||||
})
|
||||
kwargs['env'].update({
|
||||
'COMPOSE_PROJECT_NAME': project_name,
|
||||
'CONFDIR': os.path.join(config['runtime:storage'], 'conf'),
|
||||
'TARGETDIR': os.path.join(config['runtime:storage'], project_dir)
|
||||
})
|
||||
|
||||
logging.debug(f"kwargs updated: {kwargs}")
|
||||
|
||||
logging.debug(f"kwargs updated: {kwargs}")
|
||||
return kwargs
|
||||
|
||||
|
||||
|
@ -50,15 +50,15 @@ class DockerCommand(Executable):
|
|||
kwargs = _update_kwargs(config, args, **kwargs)
|
||||
|
||||
# equivalent to 'super().run' but agnostic of nested class construct
|
||||
super().__getattr__("run")(
|
||||
process_args, DockerCommand.__requires_root,
|
||||
return super().__getattr__("run")(
|
||||
process_args, config, DockerCommand.__requires_root,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
def run_less(self, config, args, process_args, **kwargs):
|
||||
kwargs = _update_kwargs(config, args, **kwargs)
|
||||
|
||||
super().__getattr__("run_less")(
|
||||
process_args, DockerCommand.__requires_root,
|
||||
return super().__getattr__("run_less")(
|
||||
process_args, config, DockerCommand.__requires_root,
|
||||
**kwargs
|
||||
)
|
||||
|
|
|
@ -4,17 +4,34 @@ import os
|
|||
import subprocess
|
||||
|
||||
|
||||
def is_executable(filename):
|
||||
def _update_kwargs(config, **kwargs):
|
||||
if config is not None:
|
||||
if config['runtime:env'] is not None:
|
||||
kwargs['env'].update(config['runtime:env'])
|
||||
|
||||
if 'env' not in kwargs:
|
||||
kwargs['env'] = {}
|
||||
|
||||
kwargs['env'].update({
|
||||
'KIWI_HUB_NAME': config['network:name']
|
||||
})
|
||||
|
||||
logging.debug(f"kwargs updated: {kwargs}")
|
||||
|
||||
return kwargs
|
||||
|
||||
|
||||
def _is_executable(filename):
|
||||
if filename is None:
|
||||
return False
|
||||
|
||||
return os.path.isfile(filename) and os.access(filename, os.X_OK)
|
||||
|
||||
|
||||
def find_exe_file(exe_name):
|
||||
def _find_exe_file(exe_name):
|
||||
for path in os.environ['PATH'].split(os.pathsep):
|
||||
exe_file = os.path.join(path, exe_name)
|
||||
if is_executable(exe_file):
|
||||
if _is_executable(exe_file):
|
||||
return exe_file
|
||||
|
||||
raise FileNotFoundError(f"Executable '{exe_name}' not found in $PATH!")
|
||||
|
@ -24,36 +41,40 @@ class Executable:
|
|||
class __Executable:
|
||||
__exe_path = None
|
||||
|
||||
def __init__(self, exe_name, requires_root=False):
|
||||
self.__exe_path = find_exe_file(exe_name)
|
||||
def __init__(self, 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.__exe_path = [find_exe_file("sudo"), self.__exe_path]
|
||||
self.__exe_path = [_find_exe_file("sudo"), self.__exe_path]
|
||||
|
||||
logging.debug(f"Executable cmd{cmd}, kwargs{kwargs}")
|
||||
return cmd
|
||||
|
||||
def run(self, process_args, requires_root=False, **kwargs):
|
||||
def run(self, process_args, config=None, requires_root=False, **kwargs):
|
||||
kwargs = _update_kwargs(config, **kwargs)
|
||||
|
||||
return subprocess.run(
|
||||
self.__build_cmd(process_args, requires_root, **kwargs),
|
||||
**kwargs
|
||||
)
|
||||
|
||||
def Popen(self, process_args, requires_root=False, **kwargs):
|
||||
def Popen(self, process_args, config=None, requires_root=False, **kwargs):
|
||||
kwargs = _update_kwargs(config, **kwargs)
|
||||
|
||||
return subprocess.Popen(
|
||||
self.__build_cmd(process_args, requires_root, **kwargs),
|
||||
**kwargs
|
||||
)
|
||||
|
||||
def run_less(self, process_args, requires_root=False, **kwargs):
|
||||
def run_less(self, process_args, config=None, requires_root=False, **kwargs):
|
||||
kwargs['stdout'] = subprocess.PIPE
|
||||
kwargs['stderr'] = subprocess.DEVNULL
|
||||
|
||||
process = self.Popen(
|
||||
process_args, requires_root,
|
||||
process_args, config, requires_root,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
|
@ -68,11 +89,11 @@ class Executable:
|
|||
__exe_name = None
|
||||
__instances = {}
|
||||
|
||||
def __init__(self, exe_name, requires_root=False):
|
||||
def __init__(self, exe_name):
|
||||
self.__exe_name = exe_name
|
||||
|
||||
if exe_name not in Executable.__instances:
|
||||
Executable.__instances[exe_name] = Executable.__Executable(exe_name, requires_root)
|
||||
Executable.__instances[exe_name] = Executable.__Executable(exe_name)
|
||||
|
||||
def __getattr__(self, item):
|
||||
return getattr(self.__instances[self.__exe_name], item)
|
||||
|
|
Loading…
Reference in a new issue