Skip to content

Commit

Permalink
Allow templates registered into other workspace paths
Browse files Browse the repository at this point in the history
This commit allows workspace path variables to be replaced in
destination paths for templates that are registered in objects.

This allows a template to be rendered into the shared directory of a
workspace, as an example.
  • Loading branch information
douglasjacobsen committed Jan 15, 2025
1 parent 61acc6c commit 2a6f7e8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/ramble/ramble/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import ramble.util.stats
import ramble.util.graph
import ramble.util.class_attributes
import ramble.util.path
import ramble.util.lock as lk
from ramble.util.foms import FomType
from ramble.util.logger import logger
Expand Down Expand Up @@ -1389,7 +1390,7 @@ def _make_experiments(self, workspace, app_inst=None):
)
os.chmod(expand_path, _DEFAULT_CONTENT_PERM)

self._render_object_templates(exec_vars)
self._render_object_templates(exec_vars, replacement_vars=workspace.workspace_paths())

experiment_script = workspace.experiments_script
experiment_script.write(self.expander.expand_var("{batch_submit}\n"))
Expand Down Expand Up @@ -2311,8 +2312,11 @@ def _get_template_config(obj, tpl_config, obj_type):
for tpl_conf in obj.templates.values():
yield _get_template_config(obj, tpl_conf, obj_type=obj_type)

def _render_object_templates(self, extra_vars):
def _render_object_templates(self, extra_vars, replacement_vars=None):
run_dir = self.expander.experiment_run_dir
replacements = {}
if replacement_vars is not None:
replacements = replacement_vars
for obj, tpl_config in self._object_templates():
src_path = tpl_config["src_path"]
with open(src_path) as f_in:
Expand All @@ -2325,7 +2329,11 @@ def _render_object_templates(self, extra_vars):
extra_vars_func = getattr(obj, extra_vars_func_name)
extra_vars.update(extra_vars_func())
rendered = self.expander.expand_var(content, extra_vars=extra_vars)
out_path = os.path.join(run_dir, tpl_config["dest_name"])
out_path = ramble.util.path.substitute_path_variables(
tpl_config["dest_name"], local_replacements=replacements
)
if not os.path.isabs(out_path):
out_path = os.path.join(run_dir, out_path)
perm = tpl_config.get("content_perm", _DEFAULT_CONTENT_PERM)
with open(out_path, "w+") as f_out:
f_out.write(rendered)
Expand Down
6 changes: 6 additions & 0 deletions lib/ramble/ramble/test/end_to_end/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def test_template():
content = f.read()
assert script_path in content

script_path = os.path.join(ws.shared_dir, "script.sh")
assert os.path.isfile(script_path)


def test_template_inherited():
test_config = """
Expand Down Expand Up @@ -93,3 +96,6 @@ def test_template_inherited():
with open(execute_path) as f:
content = f.read()
assert script_path in content

script_path = os.path.join(ws.shared_dir, "script.sh")
assert os.path.isfile(script_path)
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ def _bar_vars(self):
expander = self.expander
val = expander.expand_var('"hello {hello_name}"')
return {"dynamic_hello_world": val}

register_template(
name="test",
src_name="script.sh",
dest_name="$workspace_shared/script.sh",
output_perm="755",
)
12 changes: 12 additions & 0 deletions var/ramble/repos/builtin.mock/applications/template/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# This is a test script that should end up in a rendered location

if [ ! -f file_list ]; then
exit 1
fi

for FILE in `cat file_list`
do
wc -l $FILE
done

0 comments on commit 2a6f7e8

Please sign in to comment.