Tamarin/lib/tamarin.py

81 lines
2.7 KiB
Python
Raw Normal View History

import os, glob, subprocess, configparser, codecs, sys
def run_profile_hooks(profile, step, **kwargs):
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
hook_path = os.path.join(hooks_dir, trimmed_hook_name)
run([hook_path], **kwargs)
2021-02-25 08:42:54 +01:00
def get_base_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/..")
def get_hooks_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../hooks")
def get_lib_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../lib")
2020-09-02 15:48:05 +02:00
def get_utils_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../utils")
2017-03-10 21:51:57 +01:00
def load_profile(profile_name, debug=False):
profile_filename = profile_name+".conf"
for profile_file in get_available_profiles():
if profile_filename == os.path.basename(profile_file):
config = configparser.ConfigParser()
2017-02-09 21:53:24 +01:00
with codecs.open(profile_file, encoding = 'utf-8', mode = 'r') as handle:
config.read_file(handle)
return config
return None
def get_profiles_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../profiles")
2017-01-19 17:39:55 +01:00
def get_available_profiles():
return glob.glob(get_profiles_dir() + '/*.conf')
def get_available_profile_names():
profile_files = get_available_profiles()
2017-01-19 17:39:55 +01:00
return [os.path.splitext(os.path.basename(f))[0] for f in profile_files]
def get_workspace_dir():
"""Return the absolute path to the tamarin workspace ($HOME/.tamarin)"""
home = os.environ["HOME"]
return os.path.join(os.sep, home, '.tamarin')
def get_workspace_subdir(subdir):
"""Return the absolute path to a subdirectory in tamarin workspace"""
dir_path = os.path.join(os.sep, get_workspace_dir(), subdir)
os.makedirs(dir_path, exist_ok=True)
return dir_path
def get_datasets_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../datasets")
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)
stdin=subprocess.PIPE
2017-07-11 17:39:47 +02:00
kwargs['shell'] = False if isinstance(cmd, list) else True
if pty:
kwargs['stdin'] = sys.stdin
if captureOutput:
return subprocess.check_output(cmd, **kwargs)
else:
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)