Merge pull request #1361 from epfl-scitas/features/module_token_expansion_in_environment

module : token expansion in environment
This commit is contained in:
becker33 2016-08-03 09:49:28 -07:00 committed by GitHub
commit 7ae163d436
3 changed files with 38 additions and 6 deletions

View File

@ -37,6 +37,10 @@ def __init__(self, name, **kwargs):
self.args = {'name': name} self.args = {'name': name}
self.args.update(kwargs) self.args.update(kwargs)
def update_args(self, **kwargs):
self.__dict__.update(kwargs)
self.args.update(kwargs)
class NameValueModifier(object): class NameValueModifier(object):
@ -44,7 +48,11 @@ def __init__(self, name, value, **kwargs):
self.name = name self.name = name
self.value = value self.value = value
self.separator = kwargs.get('separator', ':') self.separator = kwargs.get('separator', ':')
self.args = {'name': name, 'value': value, 'delim': self.separator} self.args = {'name': name, 'value': value, 'separator': self.separator}
self.args.update(kwargs)
def update_args(self, **kwargs):
self.__dict__.update(kwargs)
self.args.update(kwargs) self.args.update(kwargs)

View File

@ -272,13 +272,25 @@ def naming_scheme(self):
@property @property
def tokens(self): def tokens(self):
"""Tokens that can be substituted in environment variable values
and naming schemes
"""
tokens = { tokens = {
'name': self.spec.name, 'name': self.spec.name,
'version': self.spec.version, 'version': self.spec.version,
'compiler': self.spec.compiler 'compiler': self.spec.compiler,
'prefix': self.spec.package.prefix
} }
return tokens return tokens
@property
def upper_tokens(self):
"""Tokens that can be substituted in environment variable names"""
upper_tokens = {
'name': self.spec.name.replace('-', '_').upper()
}
return upper_tokens
@property @property
def use_name(self): def use_name(self):
""" """
@ -438,6 +450,11 @@ def prerequisite(self, spec):
def process_environment_command(self, env): def process_environment_command(self, env):
for command in env: for command in env:
# Token expansion from configuration file
name = command.args.get('name', '').format(**self.upper_tokens)
value = str(command.args.get('value', '')).format(**self.tokens)
command.update_args(name=name, value=value)
# Format the line int the module file
try: try:
yield self.environment_modifications_formats[type( yield self.environment_modifications_formats[type(
command)].format(**command.args) command)].format(**command.args)
@ -511,9 +528,9 @@ class TclModule(EnvModule):
name = 'tcl' name = 'tcl'
path = join_path(spack.share_path, "modules") path = join_path(spack.share_path, "modules")
environment_modifications_formats = { environment_modifications_formats = {
PrependPath: 'prepend-path --delim "{delim}" {name} \"{value}\"\n', PrependPath: 'prepend-path --delim "{separator}" {name} \"{value}\"\n',
AppendPath: 'append-path --delim "{delim}" {name} \"{value}\"\n', AppendPath: 'append-path --delim "{separator}" {name} \"{value}\"\n',
RemovePath: 'remove-path --delim "{delim}" {name} \"{value}\"\n', RemovePath: 'remove-path --delim "{separator}" {name} \"{value}\"\n',
SetEnv: 'setenv {name} \"{value}\"\n', SetEnv: 'setenv {name} \"{value}\"\n',
UnsetEnv: 'unsetenv {name}\n' UnsetEnv: 'unsetenv {name}\n'
} }

View File

@ -89,7 +89,10 @@ def mock_open(filename, mode):
'enable': ['tcl'], 'enable': ['tcl'],
'tcl': { 'tcl': {
'all': { 'all': {
'filter': {'environment_blacklist': ['CMAKE_PREFIX_PATH']} 'filter': {'environment_blacklist': ['CMAKE_PREFIX_PATH']},
'environment': {
'set': {'{name}_ROOT': '{prefix}'}
}
}, },
'platform=test target=x86_64': { 'platform=test target=x86_64': {
'environment': { 'environment': {
@ -248,6 +251,8 @@ def test_alter_environment(self):
self.assertEqual( self.assertEqual(
len([x for x in content if 'setenv FOO "foo"' in x]), 1) len([x for x in content if 'setenv FOO "foo"' in x]), 1)
self.assertEqual(len([x for x in content if 'unsetenv BAR' in x]), 1) self.assertEqual(len([x for x in content if 'unsetenv BAR' in x]), 1)
self.assertEqual(
len([x for x in content if 'setenv MPILEAKS_ROOT' in x]), 1)
spec = spack.spec.Spec('libdwarf %clang platform=test target=x86_32') spec = spack.spec.Spec('libdwarf %clang platform=test target=x86_32')
content = self.get_modulefile_content(spec) content = self.get_modulefile_content(spec)
@ -262,6 +267,8 @@ def test_alter_environment(self):
len([x for x in content if 'is-loaded foo/bar' in x]), 1) len([x for x in content if 'is-loaded foo/bar' in x]), 1)
self.assertEqual( self.assertEqual(
len([x for x in content if 'module load foo/bar' in x]), 1) len([x for x in content if 'module load foo/bar' in x]), 1)
self.assertEqual(
len([x for x in content if 'setenv LIBDWARF_ROOT' in x]), 1)
def test_blacklist(self): def test_blacklist(self):
spack.modules.CONFIGURATION = configuration_blacklist spack.modules.CONFIGURATION = configuration_blacklist