Skip to content

Commit

Permalink
commitin --- problems with eclrun_path and SETENV
Browse files Browse the repository at this point in the history
  • Loading branch information
berland committed Dec 10, 2024
1 parent 48b8944 commit 236da1f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
9 changes: 7 additions & 2 deletions src/ert/config/ert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,10 +766,15 @@ def _create_list_of_forward_model_steps_to_run(
cls,
installed_steps: Dict[str, ForwardModelStep],
substitutions: Substitutions,
config_dict,
config_dict: dict,
) -> List[ForwardModelStep]:
errors = []
fm_steps = []

env_vars = {}
for key, val in config_dict.get("SETENV", []):
env_vars[key] = val

for fm_step_description in config_dict.get(ConfigKeys.FORWARD_MODEL, []):
if len(fm_step_description) > 1:
unsubstituted_step_name, args = fm_step_description
Expand Down Expand Up @@ -836,7 +841,7 @@ def _create_list_of_forward_model_steps_to_run(
skip_pre_experiment_validation=True,
)
job_json = substituted_json["jobList"][0]
fm_step.validate_pre_experiment(job_json)
fm_step.validate_pre_experiment(job_json, env_vars)
except ForwardModelStepValidationError as err:
errors.append(
ConfigValidationError.with_context(
Expand Down
4 changes: 3 additions & 1 deletion src/ert/config/forward_model_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ def convert_to_substitutions(cls, v: Dict[str, str]) -> Substitutions:
return v
return Substitutions(v)

def validate_pre_experiment(self, fm_step_json: ForwardModelStepJSON) -> None:
def validate_pre_experiment(
self, fm_step_json: ForwardModelStepJSON, env_vars: dict[str, str]
) -> None:
"""
Raise errors pertaining to the environment not being
as the forward model step requires it to be. For example
Expand Down
39 changes: 27 additions & 12 deletions src/ert/plugins/hook_implementations/forward_model_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
ForwardModelStepValidationError,
plugin,
)
from ert.plugins import ErtPluginManager


class CarefulCopyFile(ForwardModelStepPlugin):
Expand Down Expand Up @@ -217,13 +216,17 @@ def __init__(self) -> None:
default_mapping={"<NUM_CPU>": 1, "<OPTS>": ""},
)

def validate_pre_experiment(self, _: ForwardModelStepJSON) -> None:
def validate_pre_experiment(
self, _: ForwardModelStepJSON, env_vars: dict[str, str]
) -> None:
if "<VERSION>" not in self.private_args:
raise ForwardModelStepValidationError(
"Forward model step ECLIPSE100 must be given a VERSION argument"
)
version = self.private_args["<VERSION>"]
available_versions = _available_eclrun_versions(simulator="eclipse")
available_versions = _available_eclrun_versions(
simulator="eclipse", env_vars=env_vars
)

if available_versions and version not in available_versions:
raise ForwardModelStepValidationError(
Expand Down Expand Up @@ -275,13 +278,17 @@ def __init__(self) -> None:
default_mapping={"<NUM_CPU>": 1, "<OPTS>": "", "<VERSION>": "version"},
)

def validate_pre_experiment(self, _: ForwardModelStepJSON) -> None:
def validate_pre_experiment(
self, _: ForwardModelStepJSON, env_vars: dict[str, str]
) -> None:
if "<VERSION>" not in self.private_args:
raise ForwardModelStepValidationError(
"Forward model step ECLIPSE300 must be given a VERSION argument"
)
version = self.private_args["<VERSION>"]
available_versions = _available_eclrun_versions(simulator="e300")
available_versions = _available_eclrun_versions(
simulator="e300", env_vars=env_vars
)
if available_versions and version not in available_versions:
raise ForwardModelStepValidationError(
f"Unavailable ECLIPSE300 version {version}. "
Expand Down Expand Up @@ -625,19 +632,27 @@ def installable_forward_model_steps() -> List[Type[ForwardModelStepPlugin]]:
return [*_UpperCaseFMSteps, *_LowerCaseFMSteps]


def _available_eclrun_versions(simulator: Literal["eclipse", "e300"]) -> List[str]:
pm = ErtPluginManager()
def _available_eclrun_versions(
simulator: Literal["eclipse", "e300"], env_vars: dict[str, str]
) -> List[str]:
# pm = ErtPluginManager()
eclrun_env = os.environ.copy()
_simulator_to_fm_stepname = {"eclipse": "ECLIPSE100", "e300": "ECLIPSE300"}
eclrun_path = (
pm.get_forward_model_configuration()
.get(_simulator_to_fm_stepname[simulator], {})
.get("ECLRUN_PATH", "")
)
eclrun_path = env_vars.get("ECLRUN_PATH", "")
# pm.get_forward_model_configuration()
# .get(_simulator_to_fm_stepname[simulator], {})
# .get("ECLRUN_PATH", "")
# )
eclrun_env["PATH"] = os.pathsep.join(
[eclrun_path, *os.getenv("PATH", "").split(os.pathsep)]
)
try:
import shutil

print(eclrun_path)
print(eclrun_env["PATH"])

print(shutil.which("eclrun"))
return (
subprocess.check_output(
["eclrun", simulator, "--report-versions"],
Expand Down
26 changes: 26 additions & 0 deletions tests/ert/unit_tests/config/test_forward_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,32 @@ def test_that_eclipse_fm_step_check_version_availability(eclipse_v):
_ = ErtConfig.with_plugins().from_file(config_file_name)


@pytest.mark.parametrize("eclipse_v", ["ECLIPSE100", "ECLIPSE300"])
@pytest.mark.usefixtures("use_tmpdir")
def test_that_we_can_point_to_a_custom_eclrun_when_checking_versions(
eclipse_v, tmp_path
):
eclrun_bin = Path("bin/eclrun")
eclrun_bin.parent.mkdir()
eclrun_bin.write_text("#!/bin/sh\necho 2036.1 2036.2 2037.1", encoding="utf-8")
eclrun_bin.chmod(eclrun_bin.stat().st_mode | stat.S_IEXEC)
config_file_name = "test.ert"
Path(config_file_name).write_text(
dedent(
f"""
NUM_REALIZATIONS 1
SETENV ECLRUN_PATH {eclrun_bin.absolute().parent}
FORWARD_MODEL {eclipse_v}(<VERSION>=2034.1)"""
),
encoding="utf-8",
)
with pytest.raises(
ConfigValidationError,
match=rf".*Unavailable {eclipse_v} version 2034.1. Available versions: \[\'2036.1.*",
):
_ = ErtConfig.with_plugins().from_file(config_file_name)


@pytest.mark.skipif(shutil.which("eclrun") is not None, reason="eclrun is present")
@pytest.mark.parametrize("eclipse_v", ["ECLIPSE100", "ECLIPSE300"])
@pytest.mark.usefixtures("use_tmpdir")
Expand Down

0 comments on commit 236da1f

Please sign in to comment.