Utilisation de pathlib

This commit is contained in:
2023-01-31 11:25:20 +01:00
parent f10834b002
commit 201e6be255
2 changed files with 51 additions and 44 deletions

View File

@ -1,4 +1,4 @@
import os, glob, subprocess, configparser, codecs, sys
import os, glob, subprocess, configparser, codecs, sys, pathlib
def run_profile_hooks(profile, step, **kwargs):
hooks_dir = get_hooks_dir()
@ -9,51 +9,57 @@ def run_profile_hooks(profile, step, **kwargs):
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)
hook_path = hooks_dir.joinpath(trimmed_hook_name)
run([hook_path], **kwargs)
def get_base_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/..")
base_dir = pathlib.Path(__file__).absolute().parent.parent.resolve()
return base_dir
def get_hooks_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../hooks")
hooks_dir = get_base_dir().joinpath('hooks')
return hooks_dir
def get_lib_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../lib")
lib_dir = get_base_dir().joinpath('lib')
return lib_dir
def get_utils_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../utils")
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()
with codecs.open(profile_file, encoding = 'utf-8', mode = 'r') as handle:
config.read_file(handle)
return config
return None
utils_dir = get_base_dir().joinpath('utils')
return utils_dir
def get_profiles_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../profiles")
def get_available_profiles():
return glob.glob(get_profiles_dir() + '/*.conf')
def get_available_profile_names():
profile_files = get_available_profiles()
return [os.path.splitext(os.path.basename(f))[0] for f in profile_files]
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 = os.environ["HOME"]
return os.path.join(os.sep, 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"""
dir_path = os.path.join(os.sep, get_workspace_dir(), subdir)
os.makedirs(dir_path, exist_ok=True)
return dir_path
subdir_path = get_workspace_dir().joinpath(subdir)
subdir_path.mkdir(parents=True, exist_ok=True)
return subdir_path
def load_profile(profile_name, debug=False):
profile_filename = profile_name+".conf"
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
return None
def get_available_profiles():
return get_profiles_dir().glob('*.conf')
def get_available_profile_names():
profile_files = get_available_profiles()
return [p.stem for p in profile_files]
def run(cmd, captureOutput=False, pty=False, debug=False, **kwargs):
"""Execute an arbitrary command on the system"""