From 6ab57571c20ed50889f4960cdec97b00ba999793 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Sun, 28 Jan 2018 18:35:00 +0100 Subject: 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. --- lib/spack/spack/cmd/common/modules.py | 13 +++--------- lib/spack/spack/cmd/lmod.py | 37 +++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/lib/spack/spack/cmd/common/modules.py b/lib/spack/spack/cmd/common/modules.py index 901eb060c5..964d89d668 100644 --- a/lib/spack/spack/cmd/common/modules.py +++ b/lib/spack/spack/cmd/common/modules.py @@ -35,7 +35,8 @@ from llnl.util import filesystem, tty import spack.cmd import spack.modules import spack.repo -from spack.cmd.common import arguments + +import spack.cmd.common.arguments as arguments description = "manipulate module files" section = "environment" @@ -51,15 +52,6 @@ level = "short" callbacks = {} -def subcommand(subparser_name): - """Registers a function in the callbacks dictionary""" - def decorator(callback): - callbacks[subparser_name] = callback - return callback - return decorator -from . import arguments - - def setup_parser(subparser): sp = subparser.add_subparsers(metavar='SUBCOMMAND', dest='subparser_name') @@ -105,6 +97,7 @@ def setup_parser(subparser): arguments.add_common_arguments( loads_parser, ['constraint', 'recurse_dependencies'] ) + return sp class MultipleSpecsMatch(Exception): 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 ) -- cgit v1.2.3-70-g09d2