diff --git a/lib/ramble/ramble/application.py b/lib/ramble/ramble/application.py index e6d14a754..a4fb7803c 100644 --- a/lib/ramble/ramble/application.py +++ b/lib/ramble/ramble/application.py @@ -2293,6 +2293,9 @@ def _render_object_templates(self, extra_vars): src_path = tpl_config["src_path"] with open(src_path) as f_in: content = f_in.read() + extra_vars_wm = tpl_config.get("extra_vars") + if extra_vars_wm is not None: + extra_vars.update(extra_vars_wm) extra_vars_func_name = tpl_config.get("extra_vars_func_name") if extra_vars_func_name is not None: extra_vars_func = getattr(obj, extra_vars_func_name) diff --git a/lib/ramble/ramble/language/shared_language.py b/lib/ramble/ramble/language/shared_language.py index 9f386ddae..57ca08aec 100644 --- a/lib/ramble/ramble/language/shared_language.py +++ b/lib/ramble/ramble/language/shared_language.py @@ -486,6 +486,7 @@ def register_template( src_name: str, dest_name: str, define_var: bool = True, + extra_vars: Optional[dict] = None, extra_vars_func: Optional[str] = None, output_perm=None, ): @@ -505,8 +506,12 @@ def register_template( dest_name: The leaf name of the rendered output under the experiment run directory. define_var: Controls if a variable named `name` should be defined. + extra_vars: If present, the variable dict is used as extra variables to + render the template. extra_vars_func: If present, the name of the function to call to return a dict of extra variables used to render the template. + This option is combined together with and takes precedence + over `extra_vars`, if both are present. output_perm: The chmod mask for the rendered output file. """ @@ -517,6 +522,7 @@ def _define_template(obj): "src_name": src_name, "dest_name": dest_name, "var_name": var_name, + "extra_vars": extra_vars, "extra_vars_func_name": extra_vars_func_name, "output_perm": output_perm, } diff --git a/lib/ramble/ramble/test/end_to_end/test_template.py b/lib/ramble/ramble/test/end_to_end/test_template.py index a94b25a08..a2f5ad694 100644 --- a/lib/ramble/ramble/test/end_to_end/test_template.py +++ b/lib/ramble/ramble/test/end_to_end/test_template.py @@ -50,7 +50,9 @@ def test_template(): assert os.path.isfile(script_path) with open(script_path) as f: content = f.read() + assert "echo foobar" in content assert "echo hello santa" in content + assert "echo not_exist" not in content execute_path = os.path.join(run_dir, "execute_experiment") with open(execute_path) as f: content = f.read() diff --git a/var/ramble/repos/builtin.mock/applications/template/application.py b/var/ramble/repos/builtin.mock/applications/template/application.py index 6493f8183..9a48807d8 100644 --- a/var/ramble/repos/builtin.mock/applications/template/application.py +++ b/var/ramble/repos/builtin.mock/applications/template/application.py @@ -29,6 +29,11 @@ class Template(ExecutableApplication): name="bar", src_name="bar.tpl", dest_name="bar.sh", + # The `dynamic_hello_world` will be overridden by `_bar_vars` + extra_vars={ + "dynamic_var1": "foobar", + "dynamic_hello_world": "not_exist", + }, extra_vars_func="bar_vars", ) diff --git a/var/ramble/repos/builtin.mock/applications/template/bar.tpl b/var/ramble/repos/builtin.mock/applications/template/bar.tpl index fc8265c25..7979e2ef8 100644 --- a/var/ramble/repos/builtin.mock/applications/template/bar.tpl +++ b/var/ramble/repos/builtin.mock/applications/template/bar.tpl @@ -1,2 +1,3 @@ #!/bin/bash +echo {dynamic_var1} echo {dynamic_hello_world}