From 3126ed5f212e12464d349118a56b2a0ac0bf9308 Mon Sep 17 00:00:00 2001
From: alalazo <massimiliano.culpo@googlemail.com>
Date: Mon, 25 Jul 2016 18:10:27 +0200
Subject: modules : permit token expansion in configuration files

---
 lib/spack/spack/environment.py  |  9 ++++++++-
 lib/spack/spack/modules.py      | 25 +++++++++++++++++++++----
 lib/spack/spack/test/modules.py |  7 ++++++-
 3 files changed, 35 insertions(+), 6 deletions(-)

(limited to 'lib')

diff --git a/lib/spack/spack/environment.py b/lib/spack/spack/environment.py
index 30c6228ca4..9cb3f2575d 100644
--- a/lib/spack/spack/environment.py
+++ b/lib/spack/spack/environment.py
@@ -37,6 +37,9 @@ class NameModifier(object):
         self.args = {'name': name}
         self.args.update(kwargs)
 
+    def update_args(self, **kwargs):
+        self.__dict__.update(kwargs)
+        self.args.update(kwargs)
 
 class NameValueModifier(object):
 
@@ -44,7 +47,11 @@ class NameValueModifier(object):
         self.name = name
         self.value = value
         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)
 
 
diff --git a/lib/spack/spack/modules.py b/lib/spack/spack/modules.py
index db8b20ae42..7d35839570 100644
--- a/lib/spack/spack/modules.py
+++ b/lib/spack/spack/modules.py
@@ -272,13 +272,25 @@ class EnvModule(object):
 
     @property
     def tokens(self):
+        """Tokens that can be substituted in environment variable values
+        and naming schemes
+        """
         tokens = {
             'name': self.spec.name,
             'version': self.spec.version,
-            'compiler': self.spec.compiler
+            'compiler': self.spec.compiler,
+            'prefix': self.spec.package.prefix
         }
         return tokens
 
+    @property
+    def upper_tokens(self):
+        """Tokens that can be substituted in environment variable names"""
+        upper_tokens = {
+            'name': self.spec.name.upper()
+        }
+        return upper_tokens
+
     @property
     def use_name(self):
         """
@@ -438,6 +450,11 @@ class EnvModule(object):
 
     def process_environment_command(self, 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:
                 yield self.environment_modifications_formats[type(
                     command)].format(**command.args)
@@ -511,9 +528,9 @@ class TclModule(EnvModule):
     name = 'tcl'
     path = join_path(spack.share_path, "modules")
     environment_modifications_formats = {
-        PrependPath: 'prepend-path --delim "{delim}" {name} \"{value}\"\n',
-        AppendPath: 'append-path   --delim "{delim}" {name} \"{value}\"\n',
-        RemovePath: 'remove-path   --delim "{delim}" {name} \"{value}\"\n',
+        PrependPath: 'prepend-path --delim "{separator}" {name} \"{value}\"\n',
+        AppendPath: 'append-path   --delim "{separator}" {name} \"{value}\"\n',
+        RemovePath: 'remove-path   --delim "{separator}" {name} \"{value}\"\n',
         SetEnv: 'setenv {name} \"{value}\"\n',
         UnsetEnv: 'unsetenv {name}\n'
     }
diff --git a/lib/spack/spack/test/modules.py b/lib/spack/spack/test/modules.py
index 135cd028e3..0d33627be3 100644
--- a/lib/spack/spack/test/modules.py
+++ b/lib/spack/spack/test/modules.py
@@ -89,7 +89,10 @@ configuration_alter_environment = {
     'enable': ['tcl'],
     'tcl': {
         'all': {
-            'filter': {'environment_blacklist': ['CMAKE_PREFIX_PATH']}
+            'filter': {'environment_blacklist': ['CMAKE_PREFIX_PATH']},
+            'environment': {
+                'set': {'{name}_ROOT': '{prefix}'}
+            }
         },
         'platform=test target=x86_64': {
             'environment': {
@@ -248,6 +251,7 @@ class TclTests(MockPackagesTest):
         self.assertEqual(
             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 'setenv MPILEAKS_ROOT' in x]), 1)
 
         spec = spack.spec.Spec('libdwarf %clang platform=test target=x86_32')
         content = self.get_modulefile_content(spec)
@@ -262,6 +266,7 @@ class TclTests(MockPackagesTest):
             len([x for x in content if 'is-loaded foo/bar' in x]), 1)
         self.assertEqual(
             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):
         spack.modules.CONFIGURATION = configuration_blacklist
-- 
cgit v1.2.3-70-g09d2