From f39a174f5e8559503f57129f7e08c9f95196bf44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Eide?= Date: Thu, 28 Nov 2024 14:28:12 +0100 Subject: [PATCH] Replace everest site config manager with ert --- src/everest/plugins/hook_impl.py | 29 ------ src/everest/plugins/site_config_env.py | 93 ------------------ tests/everest/test_egg_simulation.py | 23 ++--- tests/everest/test_site_config_env.py | 131 ------------------------- 4 files changed, 10 insertions(+), 266 deletions(-) delete mode 100644 src/everest/plugins/site_config_env.py delete mode 100644 tests/everest/test_site_config_env.py diff --git a/src/everest/plugins/hook_impl.py b/src/everest/plugins/hook_impl.py index bd7fd99e8ce..dff69615d70 100644 --- a/src/everest/plugins/hook_impl.py +++ b/src/everest/plugins/hook_impl.py @@ -6,35 +6,6 @@ def visualize_data(api): print("No visualization plugin installed!") -@hookimpl -def default_site_config_lines(): - return [ - "JOB_SCRIPT job_dispatch.py", - "QUEUE_OPTION LOCAL MAX_RUNNING 1", - "", - ] - - -@hookimpl -def site_config_lines(): - return None - - -@hookimpl -def ecl100_config_path(): - return None - - -@hookimpl -def ecl300_config_path(): - return None - - -@hookimpl -def flow_config_path(): - return None - - @hookimpl def lint_forward_model(): return None diff --git a/src/everest/plugins/site_config_env.py b/src/everest/plugins/site_config_env.py deleted file mode 100644 index 18991a015e8..00000000000 --- a/src/everest/plugins/site_config_env.py +++ /dev/null @@ -1,93 +0,0 @@ -import os -import shutil -import tempfile - -from everest.plugins.everest_plugin_manager import EverestPluginManager - - -class PluginSiteConfigEnv: - """ - Allows plugin configuration of site config file. - """ - - def __init__(self): - self.pm = EverestPluginManager() - self.backup_env = os.environ.copy() - self.tmp_dir = None - - def _config_env_vars(self): - config_env_vars = [ - ("ECL100_SITE_CONFIG", self.pm.hook.ecl100_config_path()), - ("ECL300_SITE_CONFIG", self.pm.hook.ecl300_config_path()), - ("FLOW_SITE_CONFIG", self.pm.hook.flow_config_path()), - ] - config_lines = [ - "SETENV {} {}".format(env_var, env_value.data) - for env_var, env_value in config_env_vars - if env_value is not None - ] - - return [*config_lines, ""] - - def _get_temp_site_config_path(self): - self.tmp_dir = tempfile.mkdtemp() - return os.path.join(self.tmp_dir, "site-config") - - def _install_workflow_job_lines(self): - response = self.pm.hook.installable_workflow_jobs() - if response: - job_paths = [] - for item in reversed(response): - job_paths += item.data.values() - return ["LOAD_WORKFLOW_JOB {}".format(path) for path in job_paths] + [""] - return [] - - def _site_config_content(self): - response = self.pm.hook.site_config_lines() - if response: - lines = [] - for item in reversed(response): - lines += item.data - return lines - return None - - def _get_site_config_content(self): - plugin_content = self._site_config_content() - if plugin_content: - site_config_lines = self.pm.hook.default_site_config_lines() - site_config_lines.extend(plugin_content) - site_config_lines.extend(self._config_env_vars()) - site_config_lines.extend(self.pm.hook.install_job_directories() or []) - site_config_lines.extend(self._install_workflow_job_lines()) - - return "\n".join(site_config_lines) + "\n" - return None - - @staticmethod - def _is_site_config_env_set(): - return os.environ.get("ERT_SITE_CONFIG", None) is not None - - @staticmethod - def _write_tmp_site_config_file(path, content): - with open(path, "w", encoding="utf-8") as fh: - fh.write(content) - - def __enter__(self): - if not self._is_site_config_env_set(): - site_config_content = self._get_site_config_content() - if site_config_content is not None: - tmp_site_conf_path = self._get_temp_site_config_path() - self._write_tmp_site_config_file( - tmp_site_conf_path, site_config_content - ) - os.environ["ERT_SITE_CONFIG"] = tmp_site_conf_path - - def __exit__(self, *args): - if ( - self.backup_env.get("ERT_SITE_CONFIG", None) is None - and self._is_site_config_env_set() - ): - del os.environ["ERT_SITE_CONFIG"] - - if self.tmp_dir is not None: - shutil.rmtree(self.tmp_dir) diff --git a/tests/everest/test_egg_simulation.py b/tests/everest/test_egg_simulation.py index b77cbd9e9c5..9ebf96746ab 100644 --- a/tests/everest/test_egg_simulation.py +++ b/tests/everest/test_egg_simulation.py @@ -12,7 +12,6 @@ from everest.config.export_config import ExportConfig from everest.config_keys import ConfigKeys from everest.export import MetaDataColumnNames, export_data -from everest.plugins.site_config_env import PluginSiteConfigEnv from everest.simulator.everest_to_ert import _everest_to_ert_config_dict from tests.everest.utils import ( everest_default_jobs, @@ -680,12 +679,11 @@ def sweetcallbackofmine(self, *args, **kwargs): self.called = True cbtracker = CBTracker() - with PluginSiteConfigEnv(): - run_model = EverestRunModel.create( - config, simulation_callback=cbtracker.sweetcallbackofmine - ) - evaluator_server_config = EvaluatorServerConfig() - run_model.run_experiment(evaluator_server_config) + run_model = EverestRunModel.create( + config, simulation_callback=cbtracker.sweetcallbackofmine + ) + evaluator_server_config = EvaluatorServerConfig() + run_model.run_experiment(evaluator_server_config) assert cbtracker.called # TODO: The comparison is currently disabled because we know it would @@ -812,12 +810,11 @@ def sweetcallbackofmine(self, *args, **kwargs): self.called = True cbtracker = CBTracker() - with PluginSiteConfigEnv(): - run_model = EverestRunModel.create( - config, simulation_callback=cbtracker.sweetcallbackofmine - ) - evaluator_server_config = EvaluatorServerConfig() - run_model.run_experiment(evaluator_server_config) + run_model = EverestRunModel.create( + config, simulation_callback=cbtracker.sweetcallbackofmine + ) + evaluator_server_config = EvaluatorServerConfig() + run_model.run_experiment(evaluator_server_config) assert cbtracker.called diff --git a/tests/everest/test_site_config_env.py b/tests/everest/test_site_config_env.py deleted file mode 100644 index 8d01190969b..00000000000 --- a/tests/everest/test_site_config_env.py +++ /dev/null @@ -1,131 +0,0 @@ -import os -import shutil -from unittest.mock import patch - -import pytest - -from everest.plugins import hook_impl as everest_implementation -from everest.plugins import hookimpl -from everest.plugins.everest_plugin_manager import EverestPluginManager -from everest.plugins.plugin_response import plugin_response -from everest.plugins.site_config_env import PluginSiteConfigEnv - - -class DummyPlugin2: - @hookimpl - @plugin_response(plugin_name="Dummy2") # pylint: disable=no-value-for-parameter - def site_config_lines(self): - return ["-- dummy site config from plugin 2", ""] - - @hookimpl - @plugin_response(plugin_name="Dummy2") # pylint: disable=no-value-for-parameter - def installable_workflow_jobs(self): - return {"dummy_job": "dummy/workflow/job/path"} - - -class DummyPlugin: - @hookimpl - @plugin_response(plugin_name="Dummy") # pylint: disable=no-value-for-parameter - def site_config_lines(self): - return ["-- dummy site config", ""] - - @hookimpl - @plugin_response(plugin_name="Dummy") # pylint: disable=no-value-for-parameter - def ecl100_config_path(self): - return "dummy/ecl100_config_path" - - @hookimpl - @plugin_response(plugin_name="Dummy") # pylint: disable=no-value-for-parameter - def ecl300_config_path(self): - return "dummy/ecl300_config_path" - - @hookimpl - @plugin_response(plugin_name="Dummy") # pylint: disable=no-value-for-parameter - def flow_config_path(self): - return "dummy/flow_config_path" - - -@pytest.fixture -def mocked_env(mocker): - @patch( - "everest.plugins.site_config_env.EverestPluginManager", - return_value=EverestPluginManager( - [DummyPlugin(), DummyPlugin2(), everest_implementation] - ), - ) - def get_env(*args): - return PluginSiteConfigEnv() - - return get_env() - - -expected_env_lines = [ - "SETENV ECL100_SITE_CONFIG dummy/ecl100_config_path", - "SETENV ECL300_SITE_CONFIG dummy/ecl300_config_path", - "SETENV FLOW_SITE_CONFIG dummy/flow_config_path", - "", -] - -expected_site_config_extra = [ - "JOB_SCRIPT job_dispatch.py", - "QUEUE_OPTION LOCAL MAX_RUNNING 1", - "", -] - -expected_site_config_content = ( - "\n".join( - [ - *expected_site_config_extra, - "-- dummy site config", - "", - "-- dummy site config from plugin 2", - "", - *expected_env_lines, - "LOAD_WORKFLOW_JOB dummy/workflow/job/path", - "", - ] - ) - + "\n" -) - - -def test_add_config_env_vars(mocked_env): - result = mocked_env._config_env_vars() - assert result == expected_env_lines - - -def test_get_temp_site_config_path(mocked_env): - result = mocked_env._get_temp_site_config_path() - expected = os.path.join(mocked_env.tmp_dir, "site-config") - assert result == expected - if mocked_env.tmp_dir is not None: - shutil.rmtree(mocked_env.tmp_dir) - - -def test_get_site_config_content(mocked_env): - result = mocked_env._get_site_config_content() - assert result == expected_site_config_content - - -def test_write_tmp_site_config_file(tmpdir, mocked_env): - with tmpdir.as_cwd(): - site_conf_path = "test-site-config" - assert not os.path.exists(site_conf_path) - mocked_env._write_tmp_site_config_file( - path=site_conf_path, content="test content" - ) - assert os.path.exists("test-site-config") - with open(site_conf_path, "r", encoding="utf-8") as f: - assert f.read() == "test content" - - -def test_env_context(mocked_env): - assert os.environ.get("ERT_SITE_CONFIG", "NOT_SET") == "NOT_SET" - with mocked_env: - site_config_path = os.path.join(mocked_env.tmp_dir, "site-config") - assert os.environ.get("ERT_SITE_CONFIG", "NOT_SET") == site_config_path - os.path.exists(site_config_path) - with open(site_config_path, "r", encoding="utf-8") as f: - assert expected_site_config_content == f.read() - assert not os.path.exists(mocked_env.tmp_dir) - assert os.environ.get("ERT_SITE_CONFIG", "NOT_SET") == "NOT_SET"