From be25b7e849f811174729b6b54ecdfc3ceb6af61c Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 5 Dec 2023 21:04:32 +0100 Subject: [PATCH] use dynamically created RUNNER_DICT --- abratest/coordinator.py | 37 +++++++++++++++++++++++++++++++------ abratest/env_manager.py | 8 ++++---- abratest/runner_dict.py | 16 ---------------- abratest/runner_manager.py | 28 ---------------------------- 4 files changed, 35 insertions(+), 54 deletions(-) delete mode 100644 abratest/runner_dict.py delete mode 100644 abratest/runner_manager.py diff --git a/abratest/coordinator.py b/abratest/coordinator.py index 9699a09..11e4b7f 100644 --- a/abratest/coordinator.py +++ b/abratest/coordinator.py @@ -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 diff --git a/abratest/env_manager.py b/abratest/env_manager.py index 31a0634..18086e2 100644 --- a/abratest/env_manager.py +++ b/abratest/env_manager.py @@ -5,7 +5,7 @@ from typing import NamedTuple from dotenv import dotenv_values from abratest.dir_manager import DirManager -from abratest.runner_dict import RUNNER_DICT +from abratest.runner import Runner class EnvFile(NamedTuple): @@ -23,9 +23,9 @@ class DependencyRule(NamedTuple): class EnvManager: - def __init__(self, env_paths_list: list[Path]): + def __init__(self, env_paths_list: list[Path], RUNNER_DICT: dict[str, type["Runner"]]): self.env_files: list[EnvFile] = self._get_env_files(env_paths_list) - self.dependency_rules: list[DependencyRule] = self._get_dependency_rules(self.env_files) + self.dependency_rules: list[DependencyRule] = self._get_dependency_rules(self.env_files, RUNNER_DICT) self.env_files = self.sort_env_files_by_rule(self.env_files, self.dependency_rules) @staticmethod @@ -41,7 +41,7 @@ class EnvManager: return env_files @staticmethod - def _get_dependency_rules(env_files: list[EnvFile]) -> list[DependencyRule]: + def _get_dependency_rules(env_files: list[EnvFile], RUNNER_DICT: dict[str, type["Runner"]]) -> list[DependencyRule]: dependency_rules: list[DependencyRule] = [] for env_file in env_files: child_runner_class = RUNNER_DICT[env_file.env_type] diff --git a/abratest/runner_dict.py b/abratest/runner_dict.py deleted file mode 100644 index e4b694d..0000000 --- a/abratest/runner_dict.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import TYPE_CHECKING - -from authentik.tests_authentik.runner_authentik import RunnerAuthentik -from nextcloud.tests_nextcloud.runner_nextcloud import RunnerNextcloud -from wordpress.tests_wordpress.runner_wordpress import RunnerWordpress - -if TYPE_CHECKING: - from abratest.runner import Runner - -# Register all runners here. Each .env file with TYPE=authentik will be run with RunnerAuthentik - -RUNNER_DICT: dict[str, type["Runner"]] = { - "authentik": RunnerAuthentik, - "wordpress": RunnerWordpress, - "nextcloud": RunnerNextcloud, -} diff --git a/abratest/runner_manager.py b/abratest/runner_manager.py deleted file mode 100644 index 4e87735..0000000 --- a/abratest/runner_manager.py +++ /dev/null @@ -1,28 +0,0 @@ -# should replace static RUNNER_DICT - -import importlib -import re -from pathlib import Path - -from icecream import ic - -from abratest.runner import Runner - -PATTERN = re.compile("Runner.+") - - -class RunnerManager: - def __init__(self, recipes_dir: Path): - RUNNER_DICT_NEW = dict() - - for module_path in recipes_dir.rglob("*/runner*.py"): - rel_path = module_path.relative_to(recipes_dir).as_posix().replace("/", ".").replace(".py", "") - ic(rel_path) - module = importlib.import_module(rel_path) - runner_class_names = [name for name in dir(module) if 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_NEW[RunnerClass.name] = RunnerClass - print(RUNNER_DICT_NEW) - exit()