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.
This commit is contained in:
Massimiliano Culpo 2018-01-28 18:35:00 +01:00 committed by Todd Gamblin
parent 0457c1fbee
commit 6ab57571c2
2 changed files with 38 additions and 12 deletions

View file

@ -35,7 +35,8 @@
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 @@
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):

View file

@ -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 @@
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
)