Skip to content

Commit

Permalink
Added caching of the 'module' detection result.
Browse files Browse the repository at this point in the history
  • Loading branch information
skosukhin committed Nov 30, 2017
1 parent 5935bf3 commit 590a255
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
14 changes: 7 additions & 7 deletions lib/spack/spack/test/module_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_get_module_cmd_returns_the_same(tmpdir):
with open('module_list.txt') as f:
module_list = f.read()

module_cmd = get_module_cmd()
module_cmd = get_module_cmd(True)
module_cmd_list = module_cmd('list', output=str, error=str)

# Lmod command reprints some env variables on every invocation.
Expand Down Expand Up @@ -287,7 +287,7 @@ def test_get_module_cmd_detect_order(backup_restore_env, tmpdir):
'echo $0 $*')
which_executable.chmod(0o770)

module_cmd = get_module_cmd()
module_cmd = get_module_cmd(True)
module_cmd_list = module_cmd('list', output=str)

assert module_cmd_list == (str(user_definition) + ' python list\n')
Expand All @@ -296,7 +296,7 @@ def test_get_module_cmd_detect_order(backup_restore_env, tmpdir):
# definition from the bash initialization scripts.
user_definition.write('exit 1')

module_cmd = get_module_cmd()
module_cmd = get_module_cmd(True)
module_cmd_list = module_cmd('list', output=str)

assert module_cmd_list == (str(init_definition) + ' python list\n')
Expand All @@ -305,7 +305,7 @@ def test_get_module_cmd_detect_order(backup_restore_env, tmpdir):
# incorrectly, Spack should use the 'modulecmd' executable.
init_definition.write('exit 1')

module_cmd = get_module_cmd()
module_cmd = get_module_cmd(True)
module_cmd_list = module_cmd('list', output=str)

assert module_cmd_list == (str(which_executable) + ' python list\n')
Expand All @@ -315,7 +315,7 @@ def test_get_module_cmd_detect_order(backup_restore_env, tmpdir):
'exit 1')

with pytest.raises(ModuleError) as e:
get_module_cmd()
get_module_cmd(True)
assert str(e.value).startswith('Spack requires \'modulecmd\' executable ')


Expand Down Expand Up @@ -358,12 +358,12 @@ def write_modulecmd(f, version):

# The script must be modified.
write_modulecmd(modulecmd, '3.3.0')
module_cmd = get_module_cmd()
module_cmd = get_module_cmd(True)
script = module_cmd('load', 'mod', output=str)
assert script == 'exec(open(\'modulescript_12345_00\').read())'

# The script must not be modified.
write_modulecmd(modulecmd, '4.0.0')
module_cmd = get_module_cmd()
module_cmd = get_module_cmd(True)
script = module_cmd('load', 'mod', output=str)
assert script == 'exec \'modulescript_12345_00\''
27 changes: 18 additions & 9 deletions lib/spack/spack/util/module_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@
import llnl.util.tty as tty
from spack.util.executable import which

_module_cmd_cache = None


def get_module_cmd(update_cache=False):
global _module_cmd_cache

if not update_cache and _module_cmd_cache:
return _module_cmd_cache

def get_module_cmd():
result = None

# Unset BASH_ENV to prevent possible redefinition of the function.
Expand All @@ -45,10 +52,10 @@ def get_module_cmd():
os.environ.pop('BASH_ENV')

result = get_module_cmd_from_bash()
except ModuleError:
tty.warn('Could not detect module function as a shell function in the '
'current environment. Trying to detect in the interactive '
'login environment.')
except ModuleError as e:
tty.warn('Failed to detect module function as a shell function in the '
'current environment: ' + str(e) + ' Trying to detect in the '
'interactive login shell.')
finally:
if env_bu:
os.environ.clear()
Expand All @@ -60,10 +67,10 @@ def get_module_cmd():
# all, it should be available at least in the case of the
# interactive login shell.
result = get_module_cmd_from_bash(['-i', '-l'])
except ModuleError:
tty.warn('Could not detect module function in the interactive '
'login environment. Trying to find \'modulecmd\' in the '
'$PATH.')
except ModuleError as e:
tty.warn('Failed to detect module function in the interactive '
'login shell: ' + str(e) + ' Trying to find '
'\'modulecmd\' in the $PATH.')

if result is None:
try:
Expand All @@ -81,6 +88,8 @@ def get_module_cmd():
result(error=str)) is not None:
result.post_processor = old_tcl_postprocessor

_module_cmd_cache = result

return result


Expand Down

0 comments on commit 590a255

Please sign in to comment.