setup-env.sh: now compatible with bash, zsh, and dash
- replace use of [[ with [ - replace function foo { .. } with foo() { .. } - wrap some long lines - add lsof and /proc/fd magic so that we can find the sourced file even in dash - only do the complicated shell checks in one place; test $_sp_shell elsewhere.
This commit is contained in:
parent
a7da5dcb19
commit
e625ee3ffd
@ -6,11 +6,11 @@
|
|||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
#
|
#
|
||||||
# This file is part of Spack and sets up the spack environment for
|
# This file is part of Spack and sets up the spack environment for bash,
|
||||||
# bash and zsh. This includes dotkit support, module support, and
|
# zsh, and dash (sh). This includes dotkit support, module support, and
|
||||||
# it also puts spack in your path. The script also checks that
|
# it also puts spack in your path. The script also checks that at least
|
||||||
# at least module support exists, and provides suggestions if it
|
# module support exists, and provides suggestions if it doesn't. Source
|
||||||
# doesn't. Source it like this:
|
# it like this:
|
||||||
#
|
#
|
||||||
# . /path/to/spack/share/spack/setup-env.sh
|
# . /path/to/spack/share/spack/setup-env.sh
|
||||||
#
|
#
|
||||||
@ -39,24 +39,25 @@
|
|||||||
# spack dotfiles.
|
# spack dotfiles.
|
||||||
########################################################################
|
########################################################################
|
||||||
|
|
||||||
function spack {
|
spack() {
|
||||||
# Zsh does not do word splitting by default, this enables it for this function only
|
# Zsh does not do word splitting by default, this enables it for this
|
||||||
|
# function only
|
||||||
if [ -n "${ZSH_VERSION:-}" ]; then
|
if [ -n "${ZSH_VERSION:-}" ]; then
|
||||||
emulate -L sh
|
emulate -L sh
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# save raw arguments into an array before butchering them
|
|
||||||
args=( "$@" )
|
|
||||||
|
|
||||||
# accumulate initial flags for main spack command
|
# accumulate initial flags for main spack command
|
||||||
_sp_flags=""
|
_sp_flags=""
|
||||||
while [[ "$1" =~ ^- ]]; do
|
while [ "${1#-}" != "${1}" ]; do
|
||||||
_sp_flags="$_sp_flags $1"
|
_sp_flags="$_sp_flags $1"
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
# h and V flags don't require further output parsing.
|
# h and V flags don't require further output parsing.
|
||||||
if [[ (! -z "$_sp_flags") && ("$_sp_flags" =~ '.*h.*' || "$_sp_flags" =~ '.*V.*') ]]; then
|
if [ -n "$_sp_flags" ] && \
|
||||||
|
[ "${_sp_flags#*h}" != "${_sp_flags}" ] || \
|
||||||
|
[ "${_sp_flags#*V}" != "${_sp_flags}" ];
|
||||||
|
then
|
||||||
command spack $_sp_flags "$@"
|
command spack $_sp_flags "$@"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
@ -66,7 +67,6 @@ function spack {
|
|||||||
_sp_subcommand="$1"
|
_sp_subcommand="$1"
|
||||||
shift
|
shift
|
||||||
fi
|
fi
|
||||||
_sp_spec=("$@")
|
|
||||||
|
|
||||||
# Filter out use and unuse. For any other commands, just run the
|
# Filter out use and unuse. For any other commands, just run the
|
||||||
# command.
|
# command.
|
||||||
@ -77,11 +77,11 @@ function spack {
|
|||||||
_sp_arg="$1"
|
_sp_arg="$1"
|
||||||
shift
|
shift
|
||||||
fi
|
fi
|
||||||
if [[ "$_sp_arg" = "-h" || "$_sp_arg" = "--help" ]]; then
|
if [ "$_sp_arg" = "-h" ] || [ "$_sp_arg" = "--help" ]; then
|
||||||
command spack cd -h
|
command spack cd -h
|
||||||
else
|
else
|
||||||
LOC="$(spack location $_sp_arg "$@")"
|
LOC="$(spack location $_sp_arg "$@")"
|
||||||
if [[ -d "$LOC" ]] ; then
|
if [ -d "$LOC" ] ; then
|
||||||
cd "$LOC"
|
cd "$LOC"
|
||||||
else
|
else
|
||||||
return 1
|
return 1
|
||||||
@ -96,31 +96,41 @@ function spack {
|
|||||||
shift
|
shift
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$_sp_arg" = "-h" || "$_sp_arg" = "--help" ]]; then
|
if [ "$_sp_arg" = "-h" ] || [ "$_sp_arg" = "--help" ]; then
|
||||||
command spack env -h
|
command spack env -h
|
||||||
else
|
else
|
||||||
case $_sp_arg in
|
case $_sp_arg in
|
||||||
activate)
|
activate)
|
||||||
_a="$@"
|
_a="$@"
|
||||||
if [ -z "$1" -o "${_a#*--sh}" != "$_a" -o "${_a#*--csh}" != "$_a" -o "${_a#*-h}" != "$_a" ]; then
|
if [ -z "$1" ] || \
|
||||||
|
[ "${_a#*--sh}" != "$_a" ] || \
|
||||||
|
[ "${_a#*--csh}" != "$_a" ] || \
|
||||||
|
[ "${_a#*-h}" != "$_a" ];
|
||||||
|
then
|
||||||
# no args or args contain -h/--help, --sh, or --csh: just execute
|
# no args or args contain -h/--help, --sh, or --csh: just execute
|
||||||
command spack "${args[@]}"
|
command spack env activate "$@"
|
||||||
else
|
else
|
||||||
# actual call to activate: source the output
|
# actual call to activate: source the output
|
||||||
eval $(command spack $_sp_flags env activate --sh "$@")
|
eval $(command spack $_sp_flags env activate --sh "$@")
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
deactivate)
|
deactivate)
|
||||||
if [ -n "$1" ]; then
|
_a="$@"
|
||||||
# with args: execute the command
|
if [ "${_a#*--sh}" != "$_a" ] || \
|
||||||
command spack "${args[@]}"
|
[ "${_a#*--csh}" != "$_a" ];
|
||||||
|
then
|
||||||
|
# just execute the command if --sh or --csh are provided
|
||||||
|
command spack env deactivate "$@"
|
||||||
|
elif [ -n "$*" ]; then
|
||||||
|
# any other arguments are an error or help, so just run help
|
||||||
|
command spack env deactivate -h
|
||||||
else
|
else
|
||||||
# no args: source the output.
|
# no args: source the output of the command
|
||||||
eval $(command spack $_sp_flags env deactivate --sh)
|
eval $(command spack $_sp_flags env deactivate --sh)
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
command spack "${args[@]}"
|
command spack env $_sp_arg "$@"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
@ -130,8 +140,11 @@ function spack {
|
|||||||
# Shift any other args for use off before parsing spec.
|
# Shift any other args for use off before parsing spec.
|
||||||
_sp_subcommand_args=""
|
_sp_subcommand_args=""
|
||||||
_sp_module_args=""
|
_sp_module_args=""
|
||||||
while [[ "$1" =~ ^- ]]; do
|
while [ "${1#-}" != "${1}" ]; do
|
||||||
if [ "$1" = "-r" -o "$1" = "--dependencies" ]; then
|
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
|
||||||
|
command spack $_sp_flags $_sp_subcommand $_sp_subcommand_args "$@"
|
||||||
|
return
|
||||||
|
elif [ "$1" = "-r" ] || [ "$1" = "--dependencies" ]; then
|
||||||
_sp_subcommand_args="$_sp_subcommand_args $1"
|
_sp_subcommand_args="$_sp_subcommand_args $1"
|
||||||
else
|
else
|
||||||
_sp_module_args="$_sp_module_args $1"
|
_sp_module_args="$_sp_module_args $1"
|
||||||
@ -139,51 +152,54 @@ function spack {
|
|||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
_sp_spec=("$@")
|
|
||||||
|
|
||||||
# Here the user has run use or unuse with a spec. Find a matching
|
# Here the user has run use or unuse with a spec. Find a matching
|
||||||
# spec using 'spack module find', then use the appropriate module
|
# spec using 'spack module find', then use the appropriate module
|
||||||
# tool's commands to add/remove the result from the environment.
|
# tool's commands to add/remove the result from the environment.
|
||||||
# If spack module command comes back with an error, do nothing.
|
# If spack module command comes back with an error, do nothing.
|
||||||
case $_sp_subcommand in
|
case $_sp_subcommand in
|
||||||
"use")
|
"use")
|
||||||
if _sp_full_spec=$(command spack $_sp_flags module dotkit find $_sp_subcommand_args "${_sp_spec[@]}"); then
|
if _sp_full_spec=$(command spack $_sp_flags module dotkit find $_sp_subcommand_args "$@"); then
|
||||||
use $_sp_module_args $_sp_full_spec
|
use $_sp_module_args $_sp_full_spec
|
||||||
else
|
else
|
||||||
$(exit 1)
|
$(exit 1)
|
||||||
fi ;;
|
fi
|
||||||
|
;;
|
||||||
"unuse")
|
"unuse")
|
||||||
if _sp_full_spec=$(command spack $_sp_flags module dotkit find $_sp_subcommand_args "${_sp_spec[@]}"); then
|
if _sp_full_spec=$(command spack $_sp_flags module dotkit find $_sp_subcommand_args "$@"); then
|
||||||
unuse $_sp_module_args $_sp_full_spec
|
unuse $_sp_module_args $_sp_full_spec
|
||||||
else
|
else
|
||||||
$(exit 1)
|
$(exit 1)
|
||||||
fi ;;
|
fi
|
||||||
|
;;
|
||||||
"load")
|
"load")
|
||||||
if _sp_full_spec=$(command spack $_sp_flags module tcl find $_sp_subcommand_args "${_sp_spec[@]}"); then
|
if _sp_full_spec=$(command spack $_sp_flags module tcl find $_sp_subcommand_args "$@"); then
|
||||||
module load $_sp_module_args $_sp_full_spec
|
module load $_sp_module_args $_sp_full_spec
|
||||||
else
|
else
|
||||||
$(exit 1)
|
$(exit 1)
|
||||||
fi ;;
|
fi
|
||||||
|
;;
|
||||||
"unload")
|
"unload")
|
||||||
if _sp_full_spec=$(command spack $_sp_flags module tcl find $_sp_subcommand_args "${_sp_spec[@]}"); then
|
if _sp_full_spec=$(command spack $_sp_flags module tcl find $_sp_subcommand_args "$@"); then
|
||||||
module unload $_sp_module_args $_sp_full_spec
|
module unload $_sp_module_args $_sp_full_spec
|
||||||
else
|
else
|
||||||
$(exit 1)
|
$(exit 1)
|
||||||
fi ;;
|
fi
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
command spack "${args[@]}"
|
command spack $_sp_flags $_sp_subcommand "$@"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
# Prepends directories to path, if they exist.
|
# Prepends directories to path, if they exist.
|
||||||
# pathadd /path/to/dir # add to PATH
|
# pathadd /path/to/dir # add to PATH
|
||||||
# or pathadd OTHERPATH /path/to/dir # add to OTHERPATH
|
# or pathadd OTHERPATH /path/to/dir # add to OTHERPATH
|
||||||
########################################################################
|
########################################################################
|
||||||
function _spack_pathadd {
|
_spack_pathadd() {
|
||||||
# If no variable name is supplied, just append to PATH
|
# If no variable name is supplied, just append to PATH
|
||||||
# otherwise append to that variable.
|
# otherwise append to that variable.
|
||||||
_pa_varname=PATH
|
_pa_varname=PATH
|
||||||
@ -196,7 +212,10 @@ function _spack_pathadd {
|
|||||||
# Do the actual prepending here.
|
# Do the actual prepending here.
|
||||||
eval "_pa_oldvalue=\${${_pa_varname}:-}"
|
eval "_pa_oldvalue=\${${_pa_varname}:-}"
|
||||||
|
|
||||||
if [ -d "$_pa_new_path" ] && [[ ":$_pa_oldvalue:" != *":$_pa_new_path:"* ]]; then
|
_pa_canonical=":$_pa_oldvalue:"
|
||||||
|
if [ -d "$_pa_new_path" ] && \
|
||||||
|
[ "${_pa_canonical#*:${_pa_new_path}:}" = "${_pa_canonical}" ];
|
||||||
|
then
|
||||||
if [ -n "$_pa_oldvalue" ]; then
|
if [ -n "$_pa_oldvalue" ]; then
|
||||||
eval "export $_pa_varname=\"$_pa_new_path:$_pa_oldvalue\""
|
eval "export $_pa_varname=\"$_pa_new_path:$_pa_oldvalue\""
|
||||||
else
|
else
|
||||||
@ -205,29 +224,11 @@ function _spack_pathadd {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Export spack function so it is available in subshells (only works with bash)
|
|
||||||
if [ -n "${BASH_VERSION:-}" ]; then
|
|
||||||
export -f spack
|
|
||||||
fi
|
|
||||||
|
|
||||||
#
|
|
||||||
# Figure out where this file is. Below code needs to be portable to
|
|
||||||
# bash and zsh.
|
|
||||||
#
|
|
||||||
_sp_source_file="${BASH_SOURCE[0]:-}" # Bash's location of last sourced file.
|
|
||||||
if [ -z "$_sp_source_file" ]; then
|
|
||||||
_sp_source_file="${(%):-%N}" # zsh way to do it
|
|
||||||
if [ -z "$_sp_source_file" ]; then
|
|
||||||
# Not zsh either... bail out with plain old $0,
|
|
||||||
# which WILL NOT work if this is sourced indirectly.
|
|
||||||
_sp_source_file="$0"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Determine which shell is being used
|
# Determine which shell is being used
|
||||||
#
|
#
|
||||||
function _spack_determine_shell() {
|
_spack_determine_shell() {
|
||||||
# This logic is derived from the cea-hpc/modules profile.sh example at
|
# This logic is derived from the cea-hpc/modules profile.sh example at
|
||||||
# https://github.com/cea-hpc/modules/blob/master/init/profile.sh.in
|
# https://github.com/cea-hpc/modules/blob/master/init/profile.sh.in
|
||||||
#
|
#
|
||||||
@ -241,23 +242,71 @@ function _spack_determine_shell() {
|
|||||||
PS_FORMAT= ps -p $$ | tail -n 1 | awk '{print $4}' | sed 's/^-//' | xargs basename
|
PS_FORMAT= ps -p $$ | tail -n 1 | awk '{print $4}' | sed 's/^-//' | xargs basename
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
export SPACK_SHELL=$(_spack_determine_shell)
|
_sp_shell=$(_spack_determine_shell)
|
||||||
|
|
||||||
|
|
||||||
|
# Export spack function so it is available in subshells (only works with bash)
|
||||||
|
if [ "$_sp_shell" = bash ]; then
|
||||||
|
export -f spack
|
||||||
|
fi
|
||||||
|
|
||||||
|
#
|
||||||
|
# Figure out where this file is.
|
||||||
|
#
|
||||||
|
if [ "$_sp_shell" = bash ]; then
|
||||||
|
_sp_source_file="${BASH_SOURCE[0]:-}"
|
||||||
|
elif [ "$_sp_shell" = zsh ]; then
|
||||||
|
_sp_source_file="${(%):-%N}"
|
||||||
|
else
|
||||||
|
# Try to read the /proc filesystem (works on linux without lsof)
|
||||||
|
# In dash, the sourced file is the last one opened (and it's kept open)
|
||||||
|
_sp_source_file_fd="$(\ls /proc/$$/fd 2>/dev/null | sort -n | tail -1)"
|
||||||
|
if ! _sp_source_file="$(readlink /proc/$$/fd/$_sp_source_file_fd)"; then
|
||||||
|
# Last resort: try lsof. This works in dash on macos -- same reason.
|
||||||
|
# macos has lsof installed by default; some linux containers don't.
|
||||||
|
_sp_lsof_output="$(lsof -p $$ -Fn0 | tail -1)"
|
||||||
|
_sp_source_file="${_sp_lsof_output#*n}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If we can't find this script's path after all that, bail out with
|
||||||
|
# plain old $0, which WILL NOT work if this is sourced indirectly.
|
||||||
|
if [ ! -f "$_sp_source_file" ]; then
|
||||||
|
_sp_source_file="$0"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
#
|
#
|
||||||
# Find root directory and add bin to path.
|
# Find root directory and add bin to path.
|
||||||
#
|
#
|
||||||
# We send cd output to /dev/null to avoid because a lot of users set up
|
# We send cd output to /dev/null to avoid because a lot of users set up
|
||||||
# their shell so that cd prints things out to the tty.
|
# their shell so that cd prints things out to the tty.
|
||||||
#
|
|
||||||
_sp_share_dir="$(cd "$(dirname $_sp_source_file)" > /dev/null && pwd)"
|
_sp_share_dir="$(cd "$(dirname $_sp_source_file)" > /dev/null && pwd)"
|
||||||
_sp_prefix="$(cd "$(dirname $(dirname $_sp_share_dir))" > /dev/null && pwd)"
|
_sp_prefix="$(cd "$(dirname $(dirname $_sp_share_dir))" > /dev/null && pwd)"
|
||||||
_spack_pathadd PATH "${_sp_prefix%/}/bin"
|
if [ -x "$_sp_prefix/bin/spack" ]; then
|
||||||
export SPACK_ROOT="${_sp_prefix}"
|
export SPACK_ROOT="${_sp_prefix}"
|
||||||
|
else
|
||||||
|
# If the shell couldn't find the sourced script, fall back to
|
||||||
|
# whatever the user set SPACK_ROOT to.
|
||||||
|
if [ -n "$SPACK_ROOT" ]; then
|
||||||
|
_sp_prefix="$SPACK_ROOT"
|
||||||
|
_sp_share_dir="$_sp_prefix/share/spack"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If SPACK_ROOT didn't work, fail. We should need this rarely, as
|
||||||
|
# the tricks above for finding the sourced file are pretty robust.
|
||||||
|
if [ ! -x "$_sp_prefix/bin/spack" ]; then
|
||||||
|
echo "==> Error: SPACK_ROOT must point to spack's prefix when using $_sp_shell"
|
||||||
|
echo "Run this with the correct prefix before sourcing setup-env.sh:"
|
||||||
|
echo " export SPACK_ROOT=</path/to/spack>"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
_spack_pathadd PATH "${_sp_prefix%/}/bin"
|
||||||
|
|
||||||
#
|
#
|
||||||
# Check whether a function of the given name is defined
|
# Check whether a function of the given name is defined
|
||||||
#
|
#
|
||||||
function _spack_fn_exists() {
|
_spack_fn_exists() {
|
||||||
LANG= type $1 2>&1 | grep -q 'function'
|
LANG= type $1 2>&1 | grep -q 'function'
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,7 +315,6 @@ if ! _spack_fn_exists use && ! _spack_fn_exists module; then
|
|||||||
need_module="yes"
|
need_module="yes"
|
||||||
fi;
|
fi;
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# make available environment-modules
|
# make available environment-modules
|
||||||
#
|
#
|
||||||
@ -284,18 +332,19 @@ if [ "${need_module}" = "yes" ]; then
|
|||||||
fi
|
fi
|
||||||
export MODULE_PREFIX_BIN
|
export MODULE_PREFIX_BIN
|
||||||
_spack_pathadd PATH "${MODULE_PREFIX_BIN}"
|
_spack_pathadd PATH "${MODULE_PREFIX_BIN}"
|
||||||
module() { eval `${MODULE_PREFIX_BIN}/modulecmd ${SPACK_SHELL} $*`; }
|
module() { eval `${MODULE_PREFIX_BIN}/modulecmd ${_sp_shell} $*`; }
|
||||||
fi;
|
fi;
|
||||||
else
|
else
|
||||||
eval `spack --print-shell-vars sh`
|
eval `spack --print-shell-vars sh`
|
||||||
fi;
|
fi;
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# set module system roots
|
# set module system roots
|
||||||
#
|
#
|
||||||
_sp_multi_pathadd() {
|
_sp_multi_pathadd() {
|
||||||
local IFS=':'
|
local IFS=':'
|
||||||
if [[ -n "${ZSH_VERSION:-}" ]]; then
|
if [ "$_sp_shell" = zsh ]; then
|
||||||
setopt sh_word_split
|
setopt sh_word_split
|
||||||
fi
|
fi
|
||||||
for pth in $2; do
|
for pth in $2; do
|
||||||
@ -307,6 +356,6 @@ _sp_multi_pathadd DK_NODE "$_sp_dotkit_roots"
|
|||||||
|
|
||||||
# Add programmable tab completion for Bash
|
# Add programmable tab completion for Bash
|
||||||
#
|
#
|
||||||
if [ -n "${BASH_VERSION:-}" ]; then
|
if [ "$_sp_shell" = bash ]; then
|
||||||
source $_sp_share_dir/spack-completion.bash
|
source $_sp_share_dir/spack-completion.bash
|
||||||
fi
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user