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

Refactor make_extension_string #4690

Open
wants to merge 2 commits into
base: 5.0.x
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
37 changes: 22 additions & 15 deletions easybuild/framework/easyblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,8 +1520,8 @@ def make_module_extra_extensions(self):
# set environment variable that specifies list of extensions
# We need only name and version, so don't resolve templates
exts_list = self.make_extension_string(ext_sep=',', sort=False)
env_var_name = convert_name(self.name, upper=True)
lines.append(self.module_generator.set_environment('EBEXTSLIST%s' % env_var_name, exts_list))
env_var_name = 'EBEXTSLIST' + convert_name(self.name, upper=True)
lines.append(self.module_generator.set_environment(env_var_name, exts_list))

return ''.join(lines)

Expand Down Expand Up @@ -1800,14 +1800,32 @@ def load_dependency_modules(self):
# EXTENSIONS UTILITY FUNCTIONS
#

def _make_extension_list(self):
def make_extension_string(self, name_version_sep='-', ext_sep=', ', sort=True):
"""
Generate a string with a list of extensions returned by make_extension_list.

The name and version are separated by name_version_sep and each extension is separated by ext_sep.
For customization of extensions the make_extension_list method should be used.
"""
exts_list = (name_version_sep.join(ext) for ext in self.make_extension_list())
if sort:
exts_list = sorted(exts_list, key=str.lower)
return ext_sep.join(exts_list)

def make_extension_list(self):
"""
Return a list of extension names and their versions included in this installation

Each entry should be a (name, version) tuple or just (name, ) if no version exists
Each entry should be a (name, version) tuple or just (name, ) if no version exists.
Custom EasyBlocks may override this to add extensions that cannot be found automatically.
"""
# Each extension in exts_list is either a string or a list/tuple with name, version as first entries
# As name can be a templated value we must resolve templates
if hasattr(self, '_make_extension_list'):
self.log.nosupport("self._make_extension_list is replaced by self.make_extension_list", '5.0')
if type(self).make_extension_string != EasyBlock.make_extension_string:
self.log.nosupport("self.make_extension_string should not be overridden", '5.0')

exts_list = []
for ext in self.cfg.get_ref('exts_list'):
if isinstance(ext, str):
Expand All @@ -1817,17 +1835,6 @@ def _make_extension_list(self):
resolve_template(ext[1], self.cfg.template_values)))
return exts_list

def make_extension_string(self, name_version_sep='-', ext_sep=', ', sort=True):
"""
Generate a string with a list of extensions.

The name and version are separated by name_version_sep and each extension is separated by ext_sep
"""
exts_list = (name_version_sep.join(ext) for ext in self._make_extension_list())
if sort:
exts_list = sorted(exts_list, key=str.lower)
return ext_sep.join(exts_list)

def prepare_for_extensions(self):
"""Ran before installing extensions (eg to set templates)"""

Expand Down
30 changes: 6 additions & 24 deletions easybuild/tools/module_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,21 +610,6 @@ def use(self, paths, prefix=None, guarded=False, user_modpath=None):
"""
raise NotImplementedError

def _generate_extension_list(self):
"""
Generate a string with a list of extensions.

The name and version are separated by name_version_sep and each extension is separated by ext_sep
"""
return self.app.make_extension_string()

def _generate_extensions_list(self):
"""
Generate a list of all extensions in name/version format
"""
exts_str = self.app.make_extension_string(name_version_sep='/', ext_sep=',')
return exts_str.split(',') if exts_str else []

def _generate_help_text(self):
"""
Generate syntax-independent help text used for `module help`.
Expand Down Expand Up @@ -671,7 +656,7 @@ def _generate_help_text(self):
lines.extend(self._generate_section("Compatible modules", compatible_modules_txt))

# Extensions (if any)
extensions = self._generate_extension_list()
extensions = self.app.make_extension_string()
lines.extend(self._generate_section("Included extensions", '\n'.join(wrap(extensions, 78))))

return '\n'.join(lines)
Expand All @@ -686,10 +671,10 @@ def _generate_multi_deps_list(self):
mod_list = []
txt = ''
vlist = self.app.cfg['multi_deps'].get(key)
for idx in range(len(vlist)):
for idx, version in enumerate(vlist):
for deplist in self.app.cfg.multi_deps:
for dep in deplist:
if dep['name'] == key and dep['version'] == vlist[idx]:
if dep['name'] == key and dep['version'] == version:
modname = dep['short_mod_name']
# indicate which version is loaded by default (unless that's disabled)
if idx == 0 and self.app.cfg['multi_deps_load_default']:
Expand Down Expand Up @@ -728,7 +713,7 @@ def _generate_whatis_lines(self):
if multi_deps:
whatis.append("Compatible modules: %s" % ', '.join(multi_deps))

extensions = self._generate_extension_list()
extensions = self.app.make_extension_string()
if extensions:
whatis.append("Extensions: %s" % extensions)

Expand Down Expand Up @@ -1284,12 +1269,9 @@ def get_description(self, conflict=True):
elif conflict:
# conflict on 'name' part of module name (excluding version part at the end)
lines.extend(['', 'conflict("%s")' % os.path.dirname(self.app.short_mod_name)])

if build_option('module_extensions'):
extensions_list = self._generate_extensions_list()

extensions_list = self.app.make_extension_string(name_version_sep='/', ext_sep=',')
if extensions_list:
extensions_stmt = 'extensions("%s")' % ','.join([str(x) for x in extensions_list])
extensions_stmt = 'extensions("%s")' % extensions_list
# put this behind a Lmod version check as 'extensions' is only (well) supported since Lmod 8.2.8,
# see https://lmod.readthedocs.io/en/latest/330_extensions.html#module-extensions and
# https://github.com/TACC/Lmod/issues/428
Expand Down
Loading