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 support for 'modunloadmsg' easyconfig parameter #4223

Merged
merged 2 commits into from
Mar 15, 2023
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
7 changes: 7 additions & 0 deletions easybuild/framework/easyblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,13 @@ def make_module_extra(self, altroot=None, altversion=None):
modloadmsg += '\n'
lines.append(self.module_generator.msg_on_load(modloadmsg))

modunloadmsg = self.cfg['modunloadmsg']
if modunloadmsg:
# add trailing newline to prevent that shell prompt is 'glued' to module unload/purge message
if not modunloadmsg.endswith('\n'):
modunloadmsg += '\n'
lines.append(self.module_generator.msg_on_unload(modunloadmsg))

for (key, value) in self.cfg['modaliases'].items():
lines.append(self.module_generator.set_alias(key, value))

Expand Down
1 change: 1 addition & 0 deletions easybuild/framework/easyconfig/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
'modextrapaths': [{}, "Extra paths to be prepended in module file", MODULES],
'modextravars': [{}, "Extra environment variables to be added to module file", MODULES],
'modloadmsg': [{}, "Message that should be printed when generated module is loaded", MODULES],
'modunloadmsg': [{}, "Message that should be printed when generated module is unloaded", MODULES],
'modluafooter': ["", "Footer to include in generated module file (Lua syntax)", MODULES],
'modaltsoftname': [None, "Module name to use (rather than using software name", MODULES],
'modtclfooter': ["", "Footer to include in generated module file (Tcl syntax)", MODULES],
Expand Down
23 changes: 23 additions & 0 deletions easybuild/tools/module_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,12 @@ def msg_on_load(self, msg):
"""
raise NotImplementedError

def msg_on_unload(self, msg):
"""
Add a message that should be printed when unloading the module.
"""
raise NotImplementedError

def set_alias(self, key, value):
"""
Generate set-alias statement in modulefile for the given key/value pair.
Expand Down Expand Up @@ -951,6 +957,15 @@ def msg_on_load(self, msg):
print_cmd = "puts stderr %s" % quote_str(msg, tcl=True)
return '\n'.join(['', self.conditional_statement("module-info mode load", print_cmd, indent=False)])

def msg_on_unload(self, msg):
"""
Add a message that should be printed when unloading the module.
"""
# escape any (non-escaped) characters with special meaning by prefixing them with a backslash
msg = re.sub(r'((?<!\\)[%s])' % ''.join(self.CHARS_TO_ESCAPE), r'\\\1', msg)
print_cmd = "puts stderr %s" % quote_str(msg, tcl=True)
return '\n'.join(['', self.conditional_statement("module-info mode unload", print_cmd, indent=False)])

def update_paths(self, key, paths, prepend=True, allow_abs=False, expand_relpaths=True):
"""
Generate prepend-path or append-path statements for the given list of paths.
Expand Down Expand Up @@ -1384,6 +1399,14 @@ def msg_on_load(self, msg):
stmt = 'io.stderr:write(%s%s%s)' % (self.START_STR, self.check_str(msg), self.END_STR)
return '\n' + self.conditional_statement('mode() == "load"', stmt, indent=False)

def msg_on_unload(self, msg):
"""
Add a message that should be printed when loading the module.
"""
# take into account possible newlines in messages by using [==...==] (requires Lmod 5.8)
stmt = 'io.stderr:write(%s%s%s)' % (self.START_STR, self.check_str(msg), self.END_STR)
return '\n' + self.conditional_statement('mode() == "unload"', stmt, indent=False)

def modulerc(self, module_version=None, filepath=None, modulerc_txt=None):
"""
Generate contents of .modulerc(.lua) file, in Lua syntax (but only if Lmod is recent enough, i.e. >= 7.7.38)
Expand Down
10 changes: 10 additions & 0 deletions test/framework/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,13 @@ def test_templating_constants(self):
'Perl: %%(perlver)s, %%(perlmajver)s, %%(perlminver)s, %%(perlshortver)s',
'R: %%(rver)s, %%(rmajver)s, %%(rminver)s, %%(rshortver)s',
]),
'modunloadmsg = "%s"' % '; '.join([
'CUDA: %%(cudaver)s, %%(cudamajver)s, %%(cudaminver)s, %%(cudashortver)s',
'Java: %%(javaver)s, %%(javamajver)s, %%(javaminver)s, %%(javashortver)s',
'Python: %%(pyver)s, %%(pymajver)s, %%(pyminver)s, %%(pyshortver)s',
'Perl: %%(perlver)s, %%(perlmajver)s, %%(perlminver)s, %%(perlshortver)s',
'R: %%(rver)s, %%(rmajver)s, %%(rminver)s, %%(rshortver)s',
]),
'modextrapaths = {"PI_MOD_NAME": "%%(module_name)s"}',
'license_file = HOME + "/licenses/PI/license.txt"',
"github_account = 'easybuilders'",
Expand Down Expand Up @@ -1139,6 +1146,7 @@ def test_templating_constants(self):
"Perl: 5.22.0, 5, 22, 5.22; "
"R: 3.2.3, 3, 2, 3.2")
self.assertEqual(ec['modloadmsg'], expected)
self.assertEqual(ec['modunloadmsg'], expected)
self.assertEqual(ec['modextrapaths'], {'PI_MOD_NAME': 'PI/3.04-Python-2.7.10'})
self.assertEqual(ec['license_file'], os.path.join(os.environ['HOME'], 'licenses', 'PI', 'license.txt'))

Expand Down Expand Up @@ -1225,6 +1233,7 @@ def test_java_wrapper_templating(self):
'toolchain = {"name":"GCC", "version": "4.6.3"}',
'dependencies = [("Java", "11", "", SYSTEM)]',
'modloadmsg = "Java: %(javaver)s, %(javamajver)s, %(javashortver)s"',
'modunloadmsg = "Java: %(javaver)s, %(javamajver)s, %(javashortver)s"',
])
self.prep()
eb = EasyConfig(self.eb_file)
Expand All @@ -1236,6 +1245,7 @@ def test_java_wrapper_templating(self):
self.assertNotIn('javaminver', eb.template_values)

self.assertEqual(eb['modloadmsg'], "Java: 11, 11, 11")
self.assertEqual(eb['modunloadmsg'], "Java: 11, 11, 11")

def test_python_whl_templating(self):
"""test templating for Python wheels"""
Expand Down
30 changes: 30 additions & 0 deletions test/framework/module_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,36 @@ def test_load_msg(self):
])
self.assertEqual(lua_load_msg, self.modgen.msg_on_load('test $test \\$test\ntest $foo \\$bar'))

def test_unload_msg(self):
"""Test including an unload message in the module file."""
if self.MODULE_GENERATOR_CLASS == ModuleGeneratorTcl:
expected = "\nif { [ module-info mode unload ] } {\nputs stderr \"test\"\n}\n"
self.assertEqual(expected, self.modgen.msg_on_unload('test'))

tcl_unload_msg = '\n'.join([
'',
"if { [ module-info mode unload ] } {",
"puts stderr \"test \\$test \\$test",
"test \\$foo \\$bar\"",
"}",
'',
])
self.assertEqual(tcl_unload_msg, self.modgen.msg_on_unload('test $test \\$test\ntest $foo \\$bar'))

else:
expected = '\nif mode() == "unload" then\nio.stderr:write([==[test]==])\nend\n'
self.assertEqual(expected, self.modgen.msg_on_unload('test'))

lua_unload_msg = '\n'.join([
'',
'if mode() == "unload" then',
'io.stderr:write([==[test $test \\$test',
'test $foo \\$bar]==])',
'end',
'',
])
self.assertEqual(lua_unload_msg, self.modgen.msg_on_unload('test $test \\$test\ntest $foo \\$bar'))

def test_module_naming_scheme(self):
"""Test using default module naming scheme."""
all_stops = [x[0] for x in EasyBlock.get_steps()]
Expand Down