From c4923fe3b3ee28ac47311411a631b228aaefef38 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Fri, 24 Mar 2023 20:38:24 +0100 Subject: [PATCH] modules: add support for append_flags/remove_flags (#36402) Adapt tcl and lmod modulefile templates to generate append-path or remove-path commands in modulefile when respectively append_flags or remove_flags commands are defined in package for run environment. Fixes #10299. --- lib/spack/spack/test/modules/lmod.py | 2 ++ lib/spack/spack/test/modules/tcl.py | 2 ++ share/spack/templates/modules/modulefile.lua | 4 ++-- share/spack/templates/modules/modulefile.tcl | 4 ++-- .../builtin.mock/packages/module-path-separator/package.py | 3 +++ 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/spack/spack/test/modules/lmod.py b/lib/spack/spack/test/modules/lmod.py index f002285ce03..4ac888f2d53 100644 --- a/lib/spack/spack/test/modules/lmod.py +++ b/lib/spack/spack/test/modules/lmod.py @@ -144,6 +144,8 @@ def test_prepend_path_separator(self, modulefile_content, module_configuration): assert len([x for x in content if 'append_path("SEMICOLON", "bar", ";")' in x]) == 1 assert len([x for x in content if 'prepend_path("SEMICOLON", "bar", ";")' in x]) == 1 assert len([x for x in content if 'remove_path("SEMICOLON", "bar", ";")' in x]) == 1 + assert len([x for x in content if 'append_path("SPACE", "qux", " ")' in x]) == 1 + assert len([x for x in content if 'remove_path("SPACE", "qux", " ")' in x]) == 1 @pytest.mark.parametrize("config_name", ["exclude", "blacklist"]) def test_exclude(self, modulefile_content, module_configuration, config_name): diff --git a/lib/spack/spack/test/modules/tcl.py b/lib/spack/spack/test/modules/tcl.py index f289e455e58..f91a55691f6 100644 --- a/lib/spack/spack/test/modules/tcl.py +++ b/lib/spack/spack/test/modules/tcl.py @@ -120,6 +120,8 @@ def test_prepend_path_separator(self, modulefile_content, module_configuration): assert len([x for x in content if 'append-path --delim ";" SEMICOLON "bar"' in x]) == 1 assert len([x for x in content if 'prepend-path --delim ";" SEMICOLON "bar"' in x]) == 1 assert len([x for x in content if 'remove-path --delim ";" SEMICOLON "bar"' in x]) == 1 + assert len([x for x in content if 'append-path --delim " " SPACE "qux"' in x]) == 1 + assert len([x for x in content if 'remove-path --delim " " SPACE "qux"' in x]) == 1 @pytest.mark.parametrize("config_name", ["exclude", "blacklist"]) def test_exclude(self, modulefile_content, module_configuration, config_name): diff --git a/share/spack/templates/modules/modulefile.lua b/share/spack/templates/modules/modulefile.lua index 146a8e882d8..8a8329ecd1e 100644 --- a/share/spack/templates/modules/modulefile.lua +++ b/share/spack/templates/modules/modulefile.lua @@ -70,9 +70,9 @@ depends_on("{{ module }}") {% for command_name, cmd in environment_modifications %} {% if command_name == 'PrependPath' %} prepend_path("{{ cmd.name }}", "{{ cmd.value }}", "{{ cmd.separator }}") -{% elif command_name == 'AppendPath' %} +{% elif command_name in ('AppendPath', 'AppendFlagsEnv') %} append_path("{{ cmd.name }}", "{{ cmd.value }}", "{{ cmd.separator }}") -{% elif command_name == 'RemovePath' %} +{% elif command_name in ('RemovePath', 'RemoveFlagsEnv') %} remove_path("{{ cmd.name }}", "{{ cmd.value }}", "{{ cmd.separator }}") {% elif command_name == 'SetEnv' %} setenv("{{ cmd.name }}", "{{ cmd.value }}") diff --git a/share/spack/templates/modules/modulefile.tcl b/share/spack/templates/modules/modulefile.tcl index be53856ba7c..5ea976fbdde 100644 --- a/share/spack/templates/modules/modulefile.tcl +++ b/share/spack/templates/modules/modulefile.tcl @@ -43,9 +43,9 @@ conflict {{ name }} {% for command_name, cmd in environment_modifications %} {% if command_name == 'PrependPath' %} prepend-path --delim "{{ cmd.separator }}" {{ cmd.name }} "{{ cmd.value }}" -{% elif command_name == 'AppendPath' %} +{% elif command_name in ('AppendPath', 'AppendFlagsEnv') %} append-path --delim "{{ cmd.separator }}" {{ cmd.name }} "{{ cmd.value }}" -{% elif command_name == 'RemovePath' %} +{% elif command_name in ('RemovePath', 'RemoveFlagsEnv') %} remove-path --delim "{{ cmd.separator }}" {{ cmd.name }} "{{ cmd.value }}" {% elif command_name == 'SetEnv' %} setenv {{ cmd.name }} "{{ cmd.value }}" diff --git a/var/spack/repos/builtin.mock/packages/module-path-separator/package.py b/var/spack/repos/builtin.mock/packages/module-path-separator/package.py index ffd3c48bebc..65d6e7adeda 100644 --- a/var/spack/repos/builtin.mock/packages/module-path-separator/package.py +++ b/var/spack/repos/builtin.mock/packages/module-path-separator/package.py @@ -20,3 +20,6 @@ def setup_run_environment(self, env): env.append_path("SEMICOLON", "bar", separator=";") env.prepend_path("SEMICOLON", "bar", separator=";") env.remove_path("SEMICOLON", "bar", separator=";") + + env.append_flags("SPACE", "qux") + env.remove_flags("SPACE", "qux")