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

Search templates up the object inheritance chain #811

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
26 changes: 21 additions & 5 deletions lib/ramble/ramble/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2275,20 +2275,36 @@ def evaluate_success(self):
def _object_templates(self):
"""Return templates defined from different objects associated with the app_inst"""

def _get_template_config(obj, tpl_config):
src_path = os.path.join(os.path.dirname(obj._file_path), tpl_config["src_name"])
if not os.path.isfile(src_path):
def _get_template_config(
obj, tpl_config, obj_type=ramble.repository.ObjectTypes.applications
):
found = False
# Search up the object chain
object_paths = [e[1] for e in ramble.repository.list_object_files(obj, obj_type)]
src_name = tpl_config["src_name"]
for obj_path in object_paths:
src_path = os.path.join(os.path.dirname(obj_path), src_name)
if os.path.isfile(src_path):
found = True
break
if not found:
raise ApplicationError(f"Object {obj.name} is missing template file at {src_path}")
return {**tpl_config, "src_path": src_path}

for tpl_config in self.templates.values():
yield _get_template_config(self, tpl_config)
for mod in self._modifier_instances:
for tpl_config in mod.templates.values():
yield _get_template_config(mod, tpl_config)
yield _get_template_config(
mod, tpl_config, obj_type=ramble.repository.ObjectTypes.modifiers
)
if self.package_manager is not None:
for tpl_config in self.package_manager.templates.values():
yield _get_template_config(self.package_manager, tpl_config)
yield _get_template_config(
self.package_manager,
tpl_config,
obj_type=ramble.repository.ObjectTypes.package_managers,
)

def _render_object_templates(self, extra_vars):
run_dir = self.expander.experiment_run_dir
Expand Down
36 changes: 36 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 @@ -55,3 +55,39 @@ def test_template():
with open(execute_path) as f:
content = f.read()
assert script_path in content


def test_template_inherited():
test_config = """
ramble:
variables:
mpi_command: mpirun -n {n_ranks}
batch_submit: 'batch_submit {execute_experiment}'
processes_per_node: 1
n_nodes: 1
applications:
template-inherited:
workloads:
test_template:
experiments:
test: {}
"""
workspace_name = "test_template_inherited"
ws = ramble.workspace.create(workspace_name)
ws.write()
config_path = os.path.join(ws.config_dir, ramble.workspace.config_file_name)
with open(config_path, "w+") as f:
f.write(test_config)
ws._re_read()

workspace("setup", "--dry-run", global_args=["-w", workspace_name])
run_dir = os.path.join(ws.experiment_dir, "template-inherited/test_template/test/")
script_path = os.path.join(run_dir, "bar.sh")
assert os.path.isfile(script_path)
with open(script_path) as f:
content = f.read()
assert "echo hello world-inherited" in content
execute_path = os.path.join(run_dir, "execute_experiment")
with open(execute_path) as f:
content = f.read()
assert script_path in content
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2022-2025 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.appkit import *

from ramble.app.builtin.mock.template import Template as TemplateBase


class TemplateInherited(TemplateBase):
"""An app for testing object templates inheritance."""

name = "template-inherited"

workload_variable(
"hello_name",
default="world-inherited",
description="hello name",
workload="test_template",
)
Loading