Skip to content

Commit 02e9c6d

Browse files
committed
Add force_module_command parameter
1 parent 01e337f commit 02e9c6d

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

easybuild/tools/modules.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,14 @@ def set_mod_paths(self, mod_paths=None):
361361

362362
self.log.debug("$MODULEPATH after set_mod_paths: %s" % os.environ.get('MODULEPATH', ''))
363363

364-
def use(self, path, priority=None):
364+
def use(self, path, priority=None, force_module_command=False):
365365
"""
366366
Add path to $MODULEPATH via 'module use'.
367367
368368
:param path: path to add to $MODULEPATH
369369
:param priority: priority for this path in $MODULEPATH (Lmod-specific)
370+
:param force_module_command: If False running the module command may be skipped which is faster
371+
but does not reload modules
370372
"""
371373
if priority:
372374
self.log.info("Ignoring specified priority '%s' when running 'module use %s' (Lmod-specific)",
@@ -380,8 +382,14 @@ def use(self, path, priority=None):
380382
mkdir(path, parents=True)
381383
self.run_module(['use', path])
382384

383-
def unuse(self, path):
384-
"""Remove module path via 'module unuse'."""
385+
def unuse(self, path, force_module_command=False):
386+
"""
387+
Remove a module path (usually) via 'module unuse'.
388+
389+
:param path: path to remove from $MODULEPATH
390+
:param force_module_command: If False running the module command may be skipped which is faster
391+
but does not reload modules
392+
"""
385393
self.run_module(['unuse', path])
386394

387395
def add_module_path(self, path, set_mod_paths=True):
@@ -1473,12 +1481,14 @@ def _has_module_paths_with_priority(self):
14731481
# See https://github.com/TACC/Lmod/issues/509
14741482
return bool(os.environ.get('__LMOD_Priority_MODULEPATH'))
14751483

1476-
def use(self, path, priority=None):
1484+
def use(self, path, priority=None, force_module_command=False):
14771485
"""
14781486
Add path to $MODULEPATH via 'module use'.
14791487
14801488
:param path: path to add to $MODULEPATH
14811489
:param priority: priority for this path in $MODULEPATH (Lmod-specific)
1490+
:param force_module_command: If False running the module command may be skipped which is faster
1491+
but does not reload modules
14821492
"""
14831493
if not path:
14841494
raise EasyBuildError("Cannot add empty path to $MODULEPATH")
@@ -1492,17 +1502,26 @@ def use(self, path, priority=None):
14921502
else:
14931503
# LMod allows modifying MODULEPATH directly. So do that to avoid the costly module use
14941504
# unless priorities are in use already
1495-
if self._has_module_paths_with_priority():
1505+
if force_module_command or self._has_module_paths_with_priority():
14961506
self.run_module(['use', path])
14971507
else:
14981508
path = normalize_path(path)
14991509
self._set_module_path([path] + [p for p in curr_module_paths(clean=False) if normalize_path(p) != path])
15001510

1501-
def unuse(self, path):
1502-
"""Remove a module path"""
1503-
# We can simply remove the path from MODULEPATH to avoid the costly module call
1504-
path = normalize_path(path)
1505-
self._set_module_path(p for p in curr_module_paths(clean=False) if normalize_path(p) != path)
1511+
def unuse(self, path, force_module_command=False):
1512+
"""
1513+
Remove a module path
1514+
1515+
:param path: path to remove from $MODULEPATH
1516+
:param force_module_command: If False running the module command may be skipped which is faster
1517+
but does not reload modules
1518+
"""
1519+
if force_module_command:
1520+
super(Lmod, self).unuse(path)
1521+
else:
1522+
# We can simply remove the path from MODULEPATH to avoid the costly module call
1523+
path = normalize_path(path)
1524+
self._set_module_path(p for p in curr_module_paths(clean=False) if normalize_path(p) != path)
15061525

15071526
def prepend_module_path(self, path, set_mod_paths=True, priority=None):
15081527
"""

test/framework/modules.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,19 @@ def test_module_use_unuse(self):
12851285
self.assertNotIn('MODULEPATH', os.environ)
12861286
os.environ['MODULEPATH'] = old_module_path # Restore
12871287

1288+
# Forcing to use the module command reloads modules (Only Lmod does this)
1289+
old_module_path = os.environ['MODULEPATH']
1290+
os.environ['MODULEPATH'] = test_dir1
1291+
self.assertFalse('TEST123' in os.environ)
1292+
self.modtool.load(['test'])
1293+
self.assertEqual(os.getenv('TEST123'), 'one')
1294+
self.modtool.use(test_dir2, force_module_command=True)
1295+
self.assertEqual(os.getenv('TEST123'), 'two') # Module reloaded
1296+
self.modtool.unuse(test_dir2, force_module_command=True)
1297+
self.assertEqual(os.getenv('TEST123'), 'one') # Module reloaded
1298+
self.modtool.unload(['test'])
1299+
os.environ['MODULEPATH'] = old_module_path # Restore
1300+
12881301
def test_add_and_remove_module_path(self):
12891302
"""Test add_module_path and whether remove_module_path undoes changes of add_module_path"""
12901303
test_dir1 = tempfile.mkdtemp(suffix="_dir1")

0 commit comments

Comments
 (0)