Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable variable modifications to layer changes on the same variable #681

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ramble/ramble/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ def _define_commands(

for mod in self._modifier_instances:
if mod.applies_to_executable(exec_node.key):
exec_vars.update(mod.modded_variables(self))
exec_vars.update(mod.modded_variables(self, exec_vars))

if isinstance(exec_node.attribute, ramble.util.executable.CommandExecutable):
# Process directive defined executables
Expand Down Expand Up @@ -1427,7 +1427,7 @@ def _make_experiments(self, workspace, app_inst=None):
exec_vars = {}

for mod in self._modifier_instances:
exec_vars.update(mod.modded_variables(self))
exec_vars.update(mod.modded_variables(self, exec_vars))

for template_name, template_conf in workspace.all_templates():
expand_path = os.path.join(experiment_run_dir, template_name)
Expand Down
16 changes: 10 additions & 6 deletions lib/ramble/ramble/modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,21 +244,25 @@ def format_doc(self, **kwargs):
results.write((" " * indent) + line + "\n")
return results.getvalue()

def modded_variables(self, app):
def modded_variables(self, app, extra_vars={}):
mods = {}

if self._usage_mode not in self.variable_modifications:
return mods

for var, var_mod in self.variable_modifications[self._usage_mode].items():
if var_mod["method"] in ["append", "prepend"]:
# var_str = app.expander.expansion_str(var)
# prev_val = app.expander.expand_var(var_str)
prev_val = app.variables[var]
if var in extra_vars:
prev_val = extra_vars[var]
elif var in app.variables:
prev_val = app.variables[var]
else:
prev_val = ""

if var_mod["method"] == "append":
mods[var] = f'{prev_val} {var_mod["modification"]}'
mods[var] = f'{prev_val}{var_mod["modification"]}'
else: # method == prepend
mods[var] = f'{var_mod["modification"]} {prev_val}'
mods[var] = f'{var_mod["modification"]}{prev_val}'
else: # method == set
mods[var] = var_mod["modification"]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2022-2024 The Ramble Authors
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

import os

from ramble.test.dry_run_helpers import dry_run_config, SCOPES
import ramble.test.modifier_functionality.modifier_helpers as modifier_helpers

import ramble.workspace
from ramble.main import RambleCommand

workspace = RambleCommand("workspace")


def test_layered_variable_modifications(
mutable_mock_workspace_path, mutable_applications, mock_modifiers
):
workspace_name = "test_gromacs_dry_run_mock_spack_mod"

test_modifiers = [
(SCOPES.experiment, modifier_helpers.named_modifier("test-mod")),
(SCOPES.experiment, modifier_helpers.named_modifier("test-mod-2")),
]

test_template = """
{test_var_mod}
"""

with ramble.workspace.create(workspace_name) as ws1:
ws1.write()

config_path = os.path.join(ws1.config_dir, ramble.workspace.config_file_name)
template_path = os.path.join(ws1.config_dir, "test.tpl")

with open(template_path, "w+") as f:
f.write(test_template)

dry_run_config("modifiers", test_modifiers, config_path, "gromacs", "water_bare")

ws1._re_read()

workspace("concretize", global_args=["-D", ws1.root])
workspace("setup", "--dry-run", global_args=["-D", ws1.root])

rendered_template = os.path.join(
ws1.experiment_dir, "gromacs", "water_bare", "test_exp", "test"
)
assert os.path.exists(rendered_template)

with open(rendered_template) as f:
data = f.read()
assert "test-mod-2-append" in data
assert "test-mod-append" in data
31 changes: 31 additions & 0 deletions var/ramble/repos/builtin.mock/modifiers/test-mod-2/modifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2022-2024 The Ramble Authors
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

from ramble.modkit import * # noqa: F403


class TestMod2(BasicModifier):
"""Define a test modifier

This modifier contains a variable modification that can be layered on top of
the modifications defined in test-mod
"""

name = "test-mod-2"

tags("test")

mode("test", description="This is a test mode")
default_mode("test")

variable_modification(
"test_var_mod",
" test-mod-2-append",
method="append",
modes=["test"],
)
7 changes: 7 additions & 0 deletions var/ramble/repos/builtin.mock/modifiers/test-mod/modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class TestMod(BasicModifier):
"exp-scope", description="This is a test mode at the experiment scope"
)

variable_modification(
"test_var_mod",
" test-mod-append",
method="append",
modes=["test"],
)

variable_modification(
"mpi_command",
'echo "prefix_mpi_command" >> {log_file}; ',
Expand Down
2 changes: 1 addition & 1 deletion var/ramble/repos/builtin/modifiers/intel-aps/modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class IntelAps(BasicModifier):
"aps_flags", "-c mpi -r {aps_log_dir}", method="set", modes=["mpi"]
)
variable_modification(
"mpi_command", "aps {aps_flags}", method="append", modes=["mpi"]
"mpi_command", "aps {aps_flags} ", method="append", modes=["mpi"]
)

archive_pattern("aps_*_results_dir/*")
Expand Down