Skip to content

Commit b5e154c

Browse files
committed
Avoid module call for unuse() for LMod and set $MODULEPATH directly
Followup to easybuilders#3557 No check for priorities is required as LMod correctly handles that Also adds a check for an empty path which is technically possible and handled by LMod like any other path
1 parent 15f1d82 commit b5e154c

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

easybuild/tools/modules.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,21 @@ def use(self, path, priority=None):
14431443
('<unset>' if cur_mod_path is None else cur_mod_path, new_mod_path))
14441444
os.environ['MODULEPATH'] = new_mod_path
14451445

1446+
def unuse(self, path):
1447+
"""Remove a module path"""
1448+
# We can simply remove the path from MODULEPATH to avoid the costly module call
1449+
cur_mod_path = os.environ.get('MODULEPATH')
1450+
if cur_mod_path is not None:
1451+
# Removing the last entry unsets the variable
1452+
if cur_mod_path == path:
1453+
self.log.debug('Changing MODULEPATH from %s to <unset>' % cur_mod_path)
1454+
del os.environ['MODULEPATH']
1455+
else:
1456+
new_mod_path = ':'.join(p for p in cur_mod_path.split(':') if p != path)
1457+
if new_mod_path != cur_mod_path:
1458+
self.log.debug('Changing MODULEPATH from %s to %s' % (cur_mod_path, new_mod_path))
1459+
os.environ['MODULEPATH'] = new_mod_path
1460+
14461461
def prepend_module_path(self, path, set_mod_paths=True, priority=None):
14471462
"""
14481463
Prepend given module path to list of module paths, or bump it to 1st place.

test/framework/modules.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,11 +1157,23 @@ def test_module_use_unuse(self):
11571157

11581158
self.assertFalse(test_dir1 in os.environ.get('MODULEPATH', ''))
11591159
self.modtool.use(test_dir1)
1160-
self.assertTrue(os.environ.get('MODULEPATH', '').startswith('%s:' % test_dir1))
1160+
self.assertTrue(os.environ['MODULEPATH'].startswith('%s:' % test_dir1))
11611161
self.modtool.use(test_dir2)
1162-
self.assertTrue(os.environ.get('MODULEPATH', '').startswith('%s:' % test_dir2))
1162+
self.assertTrue(os.environ['MODULEPATH'].startswith('%s:' % test_dir2))
11631163
self.modtool.use(test_dir3)
1164-
self.assertTrue(os.environ.get('MODULEPATH', '').startswith('%s:' % test_dir3))
1164+
self.assertTrue(os.environ['MODULEPATH'].startswith('%s:' % test_dir3))
1165+
1166+
# Using an empty path still works (technically)
1167+
old_module_path = os.environ['MODULEPATH']
1168+
self.modtool.use('')
1169+
self.assertEqual(os.environ['MODULEPATH'], ':' + old_module_path)
1170+
self.modtool.unuse('')
1171+
self.assertEqual(os.environ['MODULEPATH'], old_module_path)
1172+
# Even works when the whole path is empty
1173+
os.environ['MODULEPATH'] = ''
1174+
self.modtool.unuse('')
1175+
self.assertFalse('MODULEPATH' in os.environ)
1176+
os.environ['MODULEPATH'] = old_module_path # Restore
11651177

11661178
# make sure the right test module is loaded
11671179
self.modtool.load(['test'])

0 commit comments

Comments
 (0)