summaryrefslogtreecommitdiff
path: root/lib/spack/spack/cmd/lmod.py
diff options
context:
space:
mode:
authorMassimiliano Culpo <massimiliano.culpo@gmail.com>2018-01-28 18:35:00 +0100
committerTodd Gamblin <tgamblin@llnl.gov>2018-07-24 11:27:17 -0700
commit6ab57571c20ed50889f4960cdec97b00ba999793 (patch)
tree5d8072f37554afc85c2fe374c02afad72c0c2cb3 /lib/spack/spack/cmd/lmod.py
parent0457c1fbeedb02830cfc79009603aea24ea5b3de (diff)
downloadspack-6ab57571c20ed50889f4960cdec97b00ba999793.tar.gz
spack-6ab57571c20ed50889f4960cdec97b00ba999793.tar.bz2
spack-6ab57571c20ed50889f4960cdec97b00ba999793.tar.xz
spack-6ab57571c20ed50889f4960cdec97b00ba999793.zip
Added 'setdefault' subcommand to 'spack lmod'
Added a subcommand that sets the default module to be loaded, if multiple installations of the same package are present.
Diffstat (limited to 'lib/spack/spack/cmd/lmod.py')
-rw-r--r--lib/spack/spack/cmd/lmod.py37
1 files changed, 35 insertions, 2 deletions
diff --git a/lib/spack/spack/cmd/lmod.py b/lib/spack/spack/cmd/lmod.py
index 620ce38209..ea3cbd7a61 100644
--- a/lib/spack/spack/cmd/lmod.py
+++ b/lib/spack/spack/cmd/lmod.py
@@ -22,6 +22,10 @@
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
+import os
+
+import llnl.util.filesystem
+import spack.cmd.common.arguments
import spack.cmd.common.modules
description = "manipulate hierarchical module files"
@@ -33,10 +37,39 @@ _module_type = 'lmod'
def setup_parser(subparser):
- spack.cmd.common.modules.setup_parser(subparser)
+ sp = spack.cmd.common.modules.setup_parser(subparser)
+
+ # Set default module file for a package
+ setdefault = sp.add_parser(
+ 'setdefault', help='set the default module file for a package'
+ )
+ spack.cmd.common.arguments.add_common_arguments(
+ setdefault, ['constraint']
+ )
+
+
+def setdefault(module_type, specs, args):
+ """Set the default module file, when multiple are present"""
+ # For details on the underlying mechanism see:
+ #
+ # https://lmod.readthedocs.io/en/latest/060_locating.html#marking-a-version-as-default
+ #
+ spack.cmd.common.modules.one_spec_or_raise(specs)
+ writer = spack.modules.module_types[_module_type](specs[0])
+
+ module_folder = os.path.dirname(writer.layout.filename)
+ module_basename = os.path.basename(writer.layout.filename)
+ with llnl.util.filesystem.working_dir(module_folder):
+ if os.path.exists('default') and os.path.islink('default'):
+ os.remove('default')
+ os.symlink(module_basename, 'default')
+
+
+callbacks = dict(spack.cmd.common.modules.callbacks.items())
+callbacks['setdefault'] = setdefault
def lmod(parser, args):
spack.cmd.common.modules.modules_cmd(
- parser, args, module_type=_module_type
+ parser, args, module_type=_module_type, callbacks=callbacks
)