use dynamically created RUNNER_DICT

This commit is contained in:
Daniel 2023-12-05 21:04:32 +01:00
parent 7ac62a6e80
commit be25b7e849
4 changed files with 35 additions and 54 deletions

View file

@ -1,3 +1,5 @@
import importlib
import re
from pathlib import Path
from loguru import logger
@ -6,8 +8,6 @@ from abratest.dir_manager import DirManager
from abratest.env_manager import EnvFile, EnvManager
from abratest.html_helper import merge_html_files
from abratest.runner import Runner
from abratest.runner_dict import RUNNER_DICT
from abratest.runner_manager import RunnerManager
from abratest.utils import rmtree
@ -19,9 +19,9 @@ class Coordinator:
out_string += f"session_id = {session_id}"
logger.info(f"initialize Coordinator instance with\nenv_paths_list =\n{out_string}")
self.RUNNER_DICT = self.create_runner_dict(recipes_dir)
self.DIR = DirManager(output_dir=output_dir, session_id=session_id, recipes_dir=recipes_dir)
self.ENV = EnvManager(env_paths_list)
runner_manager = RunnerManager(recipes_dir)
self.ENV = EnvManager(env_paths_list, self.RUNNER_DICT)
def setup_test(self) -> None:
logger.info("calling setup_test()")
@ -43,10 +43,10 @@ class Coordinator:
"""Creates an instance of the correct Runner class for each given env file"""
runners: list[Runner] = []
for env_file in env_files:
RunnerClass = RUNNER_DICT[env_file.config["TYPE"]]
RunnerClass = self.RUNNER_DICT[env_file.config["TYPE"]]
dependency_classes: list[type[Runner]] = []
for dependency in RunnerClass.dependencies:
dependency_classes.append(RUNNER_DICT[dependency])
dependency_classes.append(self.RUNNER_DICT[dependency])
runner_instance = RunnerClass(dotenv_path=env_file.env_path, DIR=self.DIR)
runner_instance._dependency_runners = dependency_classes
runners.append(runner_instance)
@ -81,3 +81,28 @@ class Coordinator:
new_path = get_new_path(self.DIR.RECORDS, f.parent.name)
f.parent.rename(new_path)
rmtree(trace_root_dir)
@staticmethod
def create_runner_dict(recipes_dir: Path) -> dict[str, type["Runner"]]:
"""Creates a dictionary holding all the RunnerClasses that can be discovered in recipes_dir
example:
RUNNER_DICT: dict[str, type["Runner"]] = {
"authentik": RunnerAuthentik,
"wordpress": RunnerWordpress,
"nextcloud": RunnerNextcloud,
}
"""
RUNNER_DICT: dict[str, type["Runner"]] = dict()
runner_discovery_pattern = re.compile("Runner.+")
for module_path in recipes_dir.rglob("*/runner*.py"):
rel_path = module_path.relative_to(recipes_dir).as_posix().replace("/", ".").replace(".py", "")
module = importlib.import_module(rel_path)
runner_class_names = [name for name in dir(module) if runner_discovery_pattern.match(name)]
assert len(runner_class_names) == 1
runner_class_name = runner_class_names[0]
RunnerClass: type[Runner] = getattr(module, runner_class_name)
RUNNER_DICT[RunnerClass.name] = RunnerClass
return RUNNER_DICT