Skip to content

Commit

Permalink
Also check for module basename in module exist
Browse files Browse the repository at this point in the history
Allows to find Java/whatver-11 from "module show Java/11"
  • Loading branch information
Flamefire committed Mar 16, 2020
1 parent 3274ab5 commit 3ff16d7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
22 changes: 18 additions & 4 deletions easybuild/tools/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,21 @@ def mod_exists_via_show(mod_name):
:param mod_name: module name
"""
mod_exists_regex = mod_exists_regex_template % re.escape(mod_name)
txt = self.show(mod_name)
return bool(re.search(mod_exists_regex, txt, re.M))
res = False
names_to_check = [mod_name]
# The module might be an alias where the target can be arbitrary
# As a compromise we check for the base name of the module so we find
# "Java/whatever-11" when searching for "Java/11" (--> basename="Java")
basename = os.path.dirname(mod_name)
if basename:
names_to_check.append(basename)
for name in names_to_check:
mod_exists_regex = mod_exists_regex_template % re.escape(name)
if re.search(mod_exists_regex, txt, re.M):
res = True
break
return res

if skip_avail:
avail_mod_names = []
Expand Down Expand Up @@ -643,7 +655,7 @@ def show(self, mod_name):
ans = MODULE_SHOW_CACHE[key]
self.log.debug("Found cached result for 'module show %s' with key '%s': %s", mod_name, key, ans)
else:
ans = self.run_module('show', mod_name, check_output=False, return_output=True)
ans = self.run_module('show', mod_name, check_output=False, return_stderr=True)
MODULE_SHOW_CACHE[key] = ans
self.log.debug("Cached result for 'module show %s' with key '%s': %s", mod_name, key, ans)

Expand Down Expand Up @@ -761,7 +773,9 @@ def run_module(self, *args, **kwargs):
if kwargs.get('check_output', True):
self.check_module_output(full_cmd, stdout, stderr)

if kwargs.get('return_output', False):
if kwargs.get('return_stderr', False):
return stderr
elif kwargs.get('return_output', False):
return stdout + stderr
else:
# the module command was run with an outdated selected environment variables (see LD_ENV_VAR_KEYS list)
Expand Down
16 changes: 15 additions & 1 deletion test/framework/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,15 @@ def test_exist(self):
'if {"Java/1.8" eq [module-info version Java/1.8]} {',
' module-version Java/1.8.0_181 1.8',
'}',
'if {"Java/site_default" eq [module-info version Java/site_default]} {',
' module-version Java/1.8.0_181 site_default',
'}',
])
else:
modulerc_tcl_txt = '\n'.join([
'#%Module',
'module-version Java/1.8.0_181 1.8',
'module-version Java/1.8.0_181 site_default',
])

write_file(os.path.join(java_mod_dir, '.modulerc'), modulerc_tcl_txt)
Expand All @@ -188,6 +192,8 @@ def test_exist(self):
if isinstance(self.modtool, Lmod) and StrictVersion(self.modtool.version) >= StrictVersion('7.0'):
self.assertTrue('Java/1.8' in avail_mods)
self.assertEqual(self.modtool.exist(['Java/1.8', 'Java/1.8.0_181']), [True, True])
# Check for an alias with a different version suffix than the base module
self.assertEqual(self.modtool.exist(['Java/site_default']), [True])
self.assertEqual(self.modtool.module_wrapper_exists('Java/1.8'), 'Java/1.8.0_181')

reset_module_caches()
Expand All @@ -199,6 +205,7 @@ def test_exist(self):
self.assertTrue('Core/Java/1.8.0_181' in self.modtool.available())
self.assertEqual(self.modtool.exist(['Core/Java/1.8.0_181']), [True])
self.assertEqual(self.modtool.exist(['Core/Java/1.8']), [True])
self.assertEqual(self.modtool.exist(['Core/Java/site_default']), [True])
self.assertEqual(self.modtool.module_wrapper_exists('Core/Java/1.8'), 'Core/Java/1.8.0_181')

# also check with .modulerc.lua for Lmod 7.8 or newer
Expand All @@ -207,12 +214,18 @@ def test_exist(self):
reset_module_caches()

remove_file(os.path.join(java_mod_dir, '.modulerc'))
write_file(os.path.join(java_mod_dir, '.modulerc.lua'), 'module_version("Java/1.8.0_181", "1.8")')
write_file(os.path.join(java_mod_dir, '.modulerc.lua'),
'\n'.join([
'module_version("Java/1.8.0_181", "1.8")',
'module_version("Java/1.8.0_181", "site_default")',
]))

avail_mods = self.modtool.available()
self.assertTrue('Java/1.8.0_181' in avail_mods)
self.assertTrue('Java/1.8' in avail_mods)
self.assertEqual(self.modtool.exist(['Java/1.8', 'Java/1.8.0_181']), [True, True])
# Check for an alias with a different version suffix than the base module
self.assertEqual(self.modtool.exist(['Java/site_default']), [True])
self.assertEqual(self.modtool.module_wrapper_exists('Java/1.8'), 'Java/1.8.0_181')

reset_module_caches()
Expand All @@ -222,6 +235,7 @@ def test_exist(self):
self.assertTrue('Core/Java/1.8.0_181' in self.modtool.available())
self.assertEqual(self.modtool.exist(['Core/Java/1.8.0_181']), [True])
self.assertEqual(self.modtool.exist(['Core/Java/1.8']), [True])
self.assertEqual(self.modtool.exist(['Core/Java/site_default']), [True])
self.assertEqual(self.modtool.module_wrapper_exists('Core/Java/1.8'), 'Core/Java/1.8.0_181')

def test_load(self):
Expand Down

0 comments on commit 3ff16d7

Please sign in to comment.