2023-01-31 11:25:20 +01:00
|
|
|
import os, glob, subprocess, configparser, codecs, sys, pathlib
|
2017-01-18 17:43:46 +01:00
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
def run_profile_hooks(profile, step, **kwargs):
|
2017-01-24 23:15:13 +01:00
|
|
|
hooks_dir = get_hooks_dir()
|
2017-02-09 21:53:24 +01:00
|
|
|
step_hooks = profile[step]["hooks"]
|
|
|
|
if not step_hooks:
|
|
|
|
return
|
|
|
|
for hook_name in step_hooks.split(","):
|
|
|
|
trimmed_hook_name = hook_name.strip(' \t\n\r')
|
|
|
|
if not trimmed_hook_name:
|
|
|
|
continue
|
2023-01-31 11:25:20 +01:00
|
|
|
hook_path = hooks_dir.joinpath(trimmed_hook_name)
|
2017-03-21 23:05:16 +01:00
|
|
|
run([hook_path], **kwargs)
|
2017-01-24 23:15:13 +01:00
|
|
|
|
2021-02-25 08:42:54 +01:00
|
|
|
def get_base_dir():
|
2023-01-31 11:25:20 +01:00
|
|
|
base_dir = pathlib.Path(__file__).absolute().parent.parent.resolve()
|
|
|
|
return base_dir
|
2021-02-25 08:42:54 +01:00
|
|
|
|
2017-01-24 23:15:13 +01:00
|
|
|
def get_hooks_dir():
|
2023-01-31 11:25:20 +01:00
|
|
|
hooks_dir = get_base_dir().joinpath('hooks')
|
|
|
|
return hooks_dir
|
2017-01-24 23:15:13 +01:00
|
|
|
|
2017-01-25 14:49:30 +01:00
|
|
|
def get_lib_dir():
|
2023-01-31 11:25:20 +01:00
|
|
|
lib_dir = get_base_dir().joinpath('lib')
|
|
|
|
return lib_dir
|
2017-01-25 14:49:30 +01:00
|
|
|
|
2020-09-02 15:48:05 +02:00
|
|
|
def get_utils_dir():
|
2023-01-31 11:25:20 +01:00
|
|
|
utils_dir = get_base_dir().joinpath('utils')
|
|
|
|
return utils_dir
|
|
|
|
|
|
|
|
def get_profiles_dir():
|
|
|
|
profiles_dir = get_base_dir().joinpath('profiles')
|
|
|
|
return profiles_dir
|
|
|
|
|
|
|
|
def get_workspace_dir():
|
|
|
|
"""Return the absolute path to the tamarin workspace ($HOME/.tamarin)"""
|
|
|
|
home = pathlib.Path(os.environ["HOME"])
|
|
|
|
workspace_dir = home.joinpath('.tamarin')
|
|
|
|
return workspace_dir
|
|
|
|
|
|
|
|
def get_workspace_subdir(subdir):
|
|
|
|
"""Return the absolute path to a subdirectory in tamarin workspace"""
|
|
|
|
subdir_path = get_workspace_dir().joinpath(subdir)
|
|
|
|
subdir_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
return subdir_path
|
2020-09-02 15:48:05 +02:00
|
|
|
|
2017-03-10 21:51:57 +01:00
|
|
|
def load_profile(profile_name, debug=False):
|
2017-01-19 22:56:17 +01:00
|
|
|
profile_filename = profile_name+".conf"
|
2023-01-31 11:25:20 +01:00
|
|
|
profile_path = get_profiles_dir().joinpath(profile_filename)
|
|
|
|
if profile_path.exists():
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
with codecs.open(profile_path, encoding = 'utf-8', mode = 'r') as handle:
|
|
|
|
config.read_file(handle)
|
|
|
|
return config
|
2017-01-19 22:56:17 +01:00
|
|
|
return None
|
|
|
|
|
2017-01-19 17:39:55 +01:00
|
|
|
def get_available_profiles():
|
2023-01-31 11:25:20 +01:00
|
|
|
return get_profiles_dir().glob('*.conf')
|
2017-01-19 22:56:17 +01:00
|
|
|
|
|
|
|
def get_available_profile_names():
|
|
|
|
profile_files = get_available_profiles()
|
2023-01-31 11:25:20 +01:00
|
|
|
return [p.stem for p in profile_files]
|
2017-01-18 17:43:46 +01:00
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
def run(cmd, captureOutput=False, pty=False, debug=False, **kwargs):
|
|
|
|
"""Execute an arbitrary command on the system"""
|
2017-03-10 21:51:57 +01:00
|
|
|
if debug:
|
2017-07-11 17:39:47 +02:00
|
|
|
print(" ".join(cmd) if isinstance(cmd, list) else cmd)
|
2017-03-21 23:05:16 +01:00
|
|
|
stdin=subprocess.PIPE
|
2017-07-11 17:39:47 +02:00
|
|
|
kwargs['shell'] = False if isinstance(cmd, list) else True
|
2017-03-21 23:05:16 +01:00
|
|
|
if pty:
|
|
|
|
kwargs['stdin'] = sys.stdin
|
2017-01-20 17:22:54 +01:00
|
|
|
if captureOutput:
|
2017-03-21 23:05:16 +01:00
|
|
|
return subprocess.check_output(cmd, **kwargs)
|
2017-01-20 17:22:54 +01:00
|
|
|
else:
|
2017-03-21 23:05:16 +01:00
|
|
|
return subprocess.check_call(cmd, **kwargs)
|
|
|
|
|
|
|
|
def run_docker(args, captureOutput=False, **kwargs):
|
2017-07-11 17:39:47 +02:00
|
|
|
if isinstance(args, list):
|
|
|
|
cmd = ["docker"] + args
|
|
|
|
else:
|
|
|
|
cmd = "docker " + args
|
|
|
|
return run(cmd, captureOutput=captureOutput, **kwargs)
|