Skip to content

Commit bf38429

Browse files
committed
Use context managers form templating changes in Bundle easyblock
1 parent ac21a74 commit bf38429

File tree

1 file changed

+50
-57
lines changed

1 file changed

+50
-57
lines changed

easybuild/easyblocks/generic/bundle.py

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,27 @@ def __init__(self, *args, **kwargs):
9191
self.cfg = self.cfg.copy()
9292

9393
# disable templating to avoid premature resolving of template values
94-
self.cfg.enable_templating = False
95-
96-
# list of checksums for patches (must be included after checksums for sources)
97-
checksums_patches = []
98-
99-
if self.cfg['sanity_check_components'] and self.cfg['sanity_check_all_components']:
100-
raise EasyBuildError("sanity_check_components and sanity_check_all_components cannot be enabled together")
101-
102-
# backup and reset general sanity checks from main body of ec, if component-specific sanity checks are enabled
103-
# necessary to avoid:
104-
# - duplicating the general sanity check across all components running sanity checks
105-
# - general sanity checks taking precedence over those defined in a component's easyblock
106-
self.backup_sanity_paths = self.cfg['sanity_check_paths']
107-
self.backup_sanity_cmds = self.cfg['sanity_check_commands']
108-
if self.cfg['sanity_check_components'] or self.cfg['sanity_check_all_components']:
109-
# reset general sanity checks, to be restored later
110-
self.cfg['sanity_check_paths'] = {}
111-
self.cfg['sanity_check_commands'] = {}
112-
113-
for comp in self.cfg['components']:
94+
with self.cfg.disable_templating():
95+
# list of checksums for patches (must be included after checksums for sources)
96+
checksums_patches = []
97+
98+
if self.cfg['sanity_check_components'] and self.cfg['sanity_check_all_components']:
99+
raise EasyBuildError("sanity_check_components and sanity_check_all_components"
100+
"cannot be enabled together")
101+
102+
# backup and reset general sanity checks from main body of ec,
103+
# if component-specific sanity checks are enabled necessary to avoid:
104+
# - duplicating the general sanity check across all components running sanity checks
105+
# - general sanity checks taking precedence over those defined in a component's easyblock
106+
self.backup_sanity_paths = self.cfg['sanity_check_paths']
107+
self.backup_sanity_cmds = self.cfg['sanity_check_commands']
108+
if self.cfg['sanity_check_components'] or self.cfg['sanity_check_all_components']:
109+
# reset general sanity checks, to be restored later
110+
self.cfg['sanity_check_paths'] = {}
111+
self.cfg['sanity_check_commands'] = {}
112+
components = self.cfg['components']
113+
114+
for comp in components:
114115
comp_name, comp_version, comp_specs = comp[0], comp[1], {}
115116
if len(comp) == 3:
116117
comp_specs = comp[2]
@@ -162,36 +163,31 @@ def __init__(self, *args, **kwargs):
162163
for key in comp_specs:
163164
comp_cfg[key] = comp_specs[key]
164165

165-
# enable resolving of templates for component-specific EasyConfig instance,
166-
# but don't require that all template values can be resolved at this point;
167-
# this is important to ensure that template values like %(name)s and %(version)s
166+
# Don't require that all template values can be resolved at this point but still resolve them.
167+
# This is important to ensure that template values like %(name)s and %(version)s
168168
# are correctly resolved with the component name/version before values are copied over to self.cfg
169-
comp_cfg.enable_templating = True
170-
comp_cfg.expect_resolved_template_values = False
171-
172-
# 'sources' is strictly required
173-
comp_sources = comp_cfg['sources']
174-
if comp_sources:
175-
# If per-component source URLs are provided, attach them directly to the relevant sources
176-
comp_source_urls = comp_cfg['source_urls']
177-
if comp_source_urls:
178-
for source in comp_sources:
179-
if isinstance(source, str):
180-
self.cfg.update('sources', [{'filename': source, 'source_urls': comp_source_urls[:]}])
181-
elif isinstance(source, dict):
182-
# Update source_urls in the 'source' dict to use the one for the components
183-
# (if it doesn't already exist)
184-
if 'source_urls' not in source:
185-
source['source_urls'] = comp_source_urls[:]
186-
self.cfg.update('sources', [source])
187-
else:
188-
raise EasyBuildError("Source %s for component %s is neither a string nor a dict, cannot "
189-
"process it.", source, comp_cfg['name'])
190-
else:
191-
# add component sources to list of sources
192-
self.cfg.update('sources', comp_sources)
193-
else:
169+
with comp_cfg.allow_unresolved_templates():
170+
comp_sources = comp_cfg['sources']
171+
if not comp_sources:
194172
raise EasyBuildError("No sources specification for component %s v%s", comp_name, comp_version)
173+
# If per-component source URLs are provided, attach them directly to the relevant sources
174+
comp_source_urls = comp_cfg['source_urls']
175+
if comp_source_urls:
176+
for source in comp_sources:
177+
if isinstance(source, str):
178+
self.cfg.update('sources', [{'filename': source, 'source_urls': comp_source_urls[:]}])
179+
elif isinstance(source, dict):
180+
# Update source_urls in the 'source' dict to use the one for the components
181+
# (if it doesn't already exist)
182+
if 'source_urls' not in source:
183+
source['source_urls'] = comp_source_urls[:]
184+
self.cfg.update('sources', [source])
185+
else:
186+
raise EasyBuildError("Source %s for component %s is neither a string nor a dict, cannot "
187+
"process it.", source, comp_cfg['name'])
188+
else:
189+
# add component sources to list of sources
190+
self.cfg.update('sources', comp_sources)
195191

196192
comp_checksums = comp_cfg['checksums']
197193
if comp_checksums:
@@ -203,12 +199,11 @@ def __init__(self, *args, **kwargs):
203199
# add per-component checksums for patches to list of checksums for patches
204200
checksums_patches.extend(comp_checksums[src_cnt:])
205201

206-
comp_patches = comp_cfg['patches']
202+
with comp_cfg.allow_unresolved_templates():
203+
comp_patches = comp_cfg['patches']
207204
if comp_patches:
208205
self.cfg.update('patches', comp_patches)
209206

210-
comp_cfg.expect_resolved_template_values = True
211-
212207
self.comp_instances.append((comp_cfg, comp_cfg.easyblock(comp_cfg, logfile=self.logfile)))
213208

214209
self.cfg.update('checksums', checksums_patches)
@@ -218,8 +213,6 @@ def __init__(self, *args, **kwargs):
218213
self.cfg['sanity_check_paths'] = self.backup_sanity_paths
219214
self.cfg['sanity_check_commands'] = self.backup_sanity_cmds
220215

221-
self.cfg.enable_templating = True
222-
223216
def check_checksums(self):
224217
"""
225218
Check whether a SHA256 checksum is available for all sources & patches (incl. extensions).
@@ -279,9 +272,10 @@ def install_step(self):
279272

280273
comp.src = []
281274

282-
# find match entries in self.src for this component
283-
comp.cfg.expect_resolved_template_values = False
284-
for source in comp.cfg['sources']:
275+
# find matching entries in self.src for this component
276+
with comp.cfg.allow_unresolved_templates():
277+
comp_sources = comp.cfg['sources']
278+
for source in comp_sources:
285279
if isinstance(source, str):
286280
comp_src_fn = source
287281
elif isinstance(source, dict):
@@ -304,7 +298,6 @@ def install_step(self):
304298

305299
# location of first unpacked source is used to determine where to apply patch(es)
306300
comp.src[-1]['finalpath'] = comp.cfg['start_dir']
307-
comp.cfg.expect_resolved_template_values = True
308301

309302
# check if sanity checks are enabled for the component
310303
if self.cfg['sanity_check_all_components'] or comp.cfg['name'] in self.cfg['sanity_check_components']:

0 commit comments

Comments
 (0)