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

Add resolve_template function to easyconfig instances #4677

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions easybuild/framework/easyconfig/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,10 @@ def _generate_template_values(self, ignore=None):
if self.template_values[key] is None:
del self.template_values[key]

def resolve_template(self, value):
"""Resolve all templates in the given value using this easyconfig"""
return resolve_template(value, self.template_values)

@handle_deprecated_or_replaced_easyconfig_parameters
def __contains__(self, key):
"""Check whether easyconfig parameter is defined"""
Expand All @@ -1770,9 +1774,7 @@ def __getitem__(self, key):
raise EasyBuildError("Use of unknown easyconfig parameter '%s' when getting parameter value", key)

if self.enable_templating:
if self.template_values is None or len(self.template_values) == 0:
self.generate_template_values()
value = resolve_template(value, self.template_values)
value = self.resolve_template(value)

return value

Expand Down Expand Up @@ -1848,9 +1850,7 @@ def asdict(self):
for key, tup in self._config.items():
value = tup[0]
if self.enable_templating:
if not self.template_values:
self.generate_template_values()
value = resolve_template(value, self.template_values)
value = self.resolve_template(value)
res[key] = value
return res

Expand Down
23 changes: 23 additions & 0 deletions test/framework/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,29 @@ def test_templating_constants(self):
ec = EasyConfig(test_ec)
self.assertEqual(ec['sanity_check_commands'], ['mpiexec -np 1 -- toy'])

def test_ec_method_resolve_template(self):
"""Test the `resolve_template` method of easyconfig instances."""
# don't use any escaping insanity here, since it is templated itself
self.contents = textwrap.dedent("""
easyblock = "ConfigureMake"
name = "PI"
version = "3.14"
homepage = "http://example.com"
description = "test easyconfig %(name)s version %(version_major)s"
toolchain = SYSTEM
""")
self.prep()
ec = EasyConfig(self.eb_file, validate=False)

# We can resolve anything with values from the EC
self.assertEqual(ec.resolve_template('%(namelower)s %(version_major)s begins with %(nameletterlower)s'),
'pi 3 begins with p')

# `resolve_template` does basically the same resolving any value on acccess
description = ec.get('description', resolve=False)
self.assertIn('%', description, 'Description needs a template for the next test')
self.assertEqual(ec.resolve_template(description), ec['description'])

def test_templating_cuda_toolchain(self):
"""Test templates via toolchain component, like setting %(cudaver)s with fosscuda toolchain."""

Expand Down
Loading