Skip to content

Commit 59af049

Browse files
committed
Add force_module_command parameter
1 parent addd81c commit 59af049

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
@@ -353,12 +353,14 @@ def set_mod_paths(self, mod_paths=None):
353353

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

356-
def use(self, path, priority=None):
356+
def use(self, path, priority=None, force_module_command=False):
357357
"""
358358
Add path to $MODULEPATH via 'module use'.
359359
360360
:param path: path to add to $MODULEPATH
361361
:param priority: priority for this path in $MODULEPATH (Lmod-specific)
362+
:param force_module_command: If False running the module command may be skipped which is faster
363+
but does not reload modules
362364
"""
363365
if priority:
364366
self.log.info("Ignoring specified priority '%s' when running 'module use %s' (Lmod-specific)",
@@ -372,8 +374,14 @@ def use(self, path, priority=None):
372374
mkdir(path, parents=True)
373375
self.run_module(['use', path])
374376

375-
def unuse(self, path):
376-
"""Remove module path via 'module unuse'."""
377+
def unuse(self, path, force_module_command=False):
378+
"""
379+
Remove a module path (usually) via 'module unuse'.
380+
381+
:param path: path to remove from $MODULEPATH
382+
:param force_module_command: If False running the module command may be skipped which is faster
383+
but does not reload modules
384+
"""
377385
self.run_module(['unuse', path])
378386

379387
def add_module_path(self, path, set_mod_paths=True):
@@ -1464,12 +1472,14 @@ def _has_module_paths_with_priority(self):
14641472
# See https://github.com/TACC/Lmod/issues/509
14651473
return bool(os.environ.get('__LMOD_Priority_MODULEPATH'))
14661474

1467-
def use(self, path, priority=None):
1475+
def use(self, path, priority=None, force_module_command=False):
14681476
"""
14691477
Add path to $MODULEPATH via 'module use'.
14701478
14711479
:param path: path to add to $MODULEPATH
14721480
:param priority: priority for this path in $MODULEPATH (Lmod-specific)
1481+
:param force_module_command: If False running the module command may be skipped which is faster
1482+
but does not reload modules
14731483
"""
14741484
if not path:
14751485
raise EasyBuildError("Cannot add empty path to $MODULEPATH")
@@ -1483,17 +1493,26 @@ def use(self, path, priority=None):
14831493
else:
14841494
# LMod allows modifying MODULEPATH directly. So do that to avoid the costly module use
14851495
# unless priorities are in use already
1486-
if self._has_module_paths_with_priority():
1496+
if force_module_command or self._has_module_paths_with_priority():
14871497
self.run_module(['use', path])
14881498
else:
14891499
path = normalize_path(path)
14901500
self._set_module_path([path] + [p for p in curr_module_paths(clean=False) if normalize_path(p) != path])
14911501

1492-
def unuse(self, path):
1493-
"""Remove a module path"""
1494-
# We can simply remove the path from MODULEPATH to avoid the costly module call
1495-
path = normalize_path(path)
1496-
self._set_module_path(p for p in curr_module_paths(clean=False) if normalize_path(p) != path)
1502+
def unuse(self, path, force_module_command=False):
1503+
"""
1504+
Remove a module path
1505+
1506+
:param path: path to remove from $MODULEPATH
1507+
:param force_module_command: If False running the module command may be skipped which is faster
1508+
but does not reload modules
1509+
"""
1510+
if force_module_command:
1511+
super(Lmod, self).unuse(path)
1512+
else:
1513+
# We can simply remove the path from MODULEPATH to avoid the costly module call
1514+
path = normalize_path(path)
1515+
self._set_module_path(p for p in curr_module_paths(clean=False) if normalize_path(p) != path)
14971516

14981517
def prepend_module_path(self, path, set_mod_paths=True, priority=None):
14991518
"""

test/framework/modules.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,19 @@ def get_resolved_module_path():
13331333

13341334
os.environ['MODULEPATH'] = old_module_path # Restore
13351335

1336+
# Forcing to use the module command reloads modules (Only Lmod does this)
1337+
old_module_path = os.environ['MODULEPATH']
1338+
os.environ['MODULEPATH'] = test_dir1
1339+
self.assertFalse('TEST123' in os.environ)
1340+
self.modtool.load(['test'])
1341+
self.assertEqual(os.getenv('TEST123'), 'one')
1342+
self.modtool.use(test_dir2, force_module_command=True)
1343+
self.assertEqual(os.getenv('TEST123'), 'two') # Module reloaded
1344+
self.modtool.unuse(test_dir2, force_module_command=True)
1345+
self.assertEqual(os.getenv('TEST123'), 'one') # Module reloaded
1346+
self.modtool.unload(['test'])
1347+
os.environ['MODULEPATH'] = old_module_path # Restore
1348+
13361349
def test_module_use_bash(self):
13371350
"""Test whether effect of 'module use' is preserved when a new bash session is started."""
13381351
# this test is here as check for a nasty bug in how the modules tool is deployed

0 commit comments

Comments
 (0)