Skip to content

Commit b7fcb79

Browse files
authored
Merge pull request #3633 from Flamefire/mlUnuse
Avoid module call for unuse() for Lmod and set $MODULEPATH directly
2 parents 57b59bf + 21083d3 commit b7fcb79

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
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: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,11 +1157,11 @@ 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))
11651165

11661166
# make sure the right test module is loaded
11671167
self.modtool.load(['test'])
@@ -1193,8 +1193,9 @@ def test_module_use_unuse(self):
11931193
self.assertEqual(os.getenv('TEST123'), 'two')
11941194
self.modtool.unload(['test'])
11951195

1196-
# check whether prepend with priority actually works (only for Lmod)
1196+
# Tests for Lmod only
11971197
if isinstance(self.modtool, Lmod):
1198+
# check whether prepend with priority actually works (priority is specific to Lmod)
11981199
self.modtool.use(test_dir1, priority=100)
11991200
self.modtool.use(test_dir3)
12001201
self.assertTrue(os.environ['MODULEPATH'].startswith('%s:%s:%s:' % (test_dir2, test_dir1, test_dir3)))
@@ -1212,6 +1213,28 @@ def test_module_use_unuse(self):
12121213
self.assertEqual(os.getenv('TEST123'), 'three')
12131214
self.modtool.unload(['test'])
12141215

1216+
# Check load and unload for a single path when it is the only one
1217+
# Only for Lmod as we have some shortcuts for avoiding the module call there
1218+
old_module_path = os.environ['MODULEPATH']
1219+
del os.environ['MODULEPATH']
1220+
self.modtool.use(test_dir1)
1221+
self.assertEqual(os.environ['MODULEPATH'], test_dir1)
1222+
self.modtool.unuse(test_dir1)
1223+
self.assertFalse('MODULEPATH' in os.environ)
1224+
os.environ['MODULEPATH'] = old_module_path # Restore
1225+
1226+
# Using an empty path still works (technically) (Lmod only, ignored by Tcl)
1227+
old_module_path = os.environ['MODULEPATH']
1228+
self.modtool.use('')
1229+
self.assertEqual(os.environ['MODULEPATH'], ':' + old_module_path)
1230+
self.modtool.unuse('')
1231+
self.assertEqual(os.environ['MODULEPATH'], old_module_path)
1232+
# Even works when the whole path is empty
1233+
os.environ['MODULEPATH'] = ''
1234+
self.modtool.unuse('')
1235+
self.assertFalse('MODULEPATH' in os.environ)
1236+
os.environ['MODULEPATH'] = old_module_path # Restore
1237+
12151238
def test_module_use_bash(self):
12161239
"""Test whether effect of 'module use' is preserved when a new bash session is started."""
12171240
# this test is here as check for a nasty bug in how the modules tool is deployed

0 commit comments

Comments
 (0)