env: add spack env activate/deactivate and shell support

- `spack env activate foo`: sets SPACK_ENV to the current active env name

- `spack env deactivate`: unsets SPACK_ENV, deactivates the environment

- added support to setup_env.sh and setup_env.csh

- other env commands work properly with SPACK_ENV, as with an environment
  arguments.

- command-line --env arguments take precedence over the active
  environment, if given.
This commit is contained in:
Todd Gamblin
2018-10-14 15:21:57 -07:00
parent 15c5c36eaf
commit d14f7b82bb
9 changed files with 280 additions and 65 deletions

View File

@@ -63,6 +63,47 @@ case cd:
cd `\spack location $_sp_arg $_sp_args`
endif
breaksw
case env:
shift _sp_args # get rid of 'env'
set _sp_arg=""
[ $#_sp_args -gt 0 ] && set _sp_arg = ($_sp_args[1])
if ( "$_sp_arg" == "-h" ) then
\spack env -h
else
switch ($_sp_arg)
case activate:
set _sp_env_arg=""
[ $#_sp_args -gt 1 ] && set _sp_env_arg = ($_sp_args[2])
if ( "$_sp_env_arg" == "" || "$_sp_env_arg" =~ "-*" ) then
# no args or does not start with -: just execute
\spack $_sp_flags env $_sp_args
else
shift _sp_args # consume 'activate' or 'deactivate'
# actual call to activate: source the output
eval `\spack $_sp_flags env activate --csh $_sp_args`
endif
breaksw
case deactivate:
set _sp_env_arg=""
[ $#_sp_args -gt 1 ] && set _sp_env_arg = ($_sp_args[2])
if ( "$_sp_env_arg" != "" ) then
# with args: execute the command
\spack $_sp_flags env $_sp_args
else
# no args: source the output
eval `\spack $_sp_flags env deactivate --csh`
endif
breaksw
default:
echo default
\spack $_sp_flags env $_sp_args
breaksw
endsw
endif
case use:
case unuse:
case load:
@@ -113,3 +154,4 @@ endsw
_sp_end:
unset _sp_args _sp_full_spec _sp_modtype _sp_module_args
unset _sp_sh_cmd _sp_spec _sp_subcommand _sp_flags
unset _sp_arg _sp_env_arg

View File

@@ -28,5 +28,6 @@ if ($?SPACK_ROOT) then
_spack_pathadd DK_NODE "$_sp_dotkit_root/$_sp_sys_type"
_spack_pathadd MODULEPATH "$_sp_tcl_root/$_sp_sys_type"
else
echo "ERROR: Sourcing spack setup-env.csh requires setting SPACK_ROOT to the root of your spack installation"
echo "ERROR: Sourcing spack setup-env.csh requires setting SPACK_ROOT to "
echo " the root of your spack installation."
endif

View File

@@ -89,6 +89,42 @@ function spack {
fi
return
;;
"env")
_sp_arg=""
if [ -n "$1" ]; then
_sp_arg="$1"
shift
fi
if [ "$_sp_arg" = "-h" ]; then
command spack env -h
else
case $_sp_arg in
activate)
if [ -z "$1" -o "${1#-}" != "$1" ]; then
# no args or does not start with -: just execute
command spack "${args[@]}"
else
# actual call to activate: source the output
eval $(command spack $_sp_flags env activate --sh "$@")
fi
;;
deactivate)
if [ -n "$1" ]; then
# with args: execute the command
command spack "${args[@]}"
else
# no args: source the output.
eval $(command spack $_sp_flags env deactivate --sh)
fi
;;
*)
command spack "${args[@]}"
;;
esac
fi
return
;;
"use"|"unuse"|"load"|"unload")
# Shift any other args for use off before parsing spec.
_sp_subcommand_args=""