From 0fa112b4de2ba4e969d36d38db8e9c47e6be5fa7 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Mon, 30 Nov 2020 17:23:12 +0000 Subject: [PATCH] RF: Only create wrappers if env vars look correct - $FSLDIR == $PREFIX, and FSL_CREATE_WRAPPER_SCRIPTS is non-empty. Use --- etc/fslconf/createFSLWrapper.sh | 114 ++++++++++++++++++-------------- etc/fslconf/removeFSLWrapper.sh | 2 +- 2 files changed, 65 insertions(+), 51 deletions(-) diff --git a/etc/fslconf/createFSLWrapper.sh b/etc/fslconf/createFSLWrapper.sh index c80f753..20ed0a8 100755 --- a/etc/fslconf/createFSLWrapper.sh +++ b/etc/fslconf/createFSLWrapper.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/usr/bin/env bash # # Create wrapper scripts in $FSLDIR/share/fsl/bin/ which invoke commands that # are installed in $FSLDIR/bin/. @@ -12,6 +12,13 @@ # fslinstaller.py script - it is not intended to be used when individual # FSL projects are explicitly installed into a custom conda environment. # +# Wrapper scripts will only be created if the following conditions are met: +# - The $FSLDIR and $PREFIX environment variables are set, and +# are equivalent +# - The $FSL_CREATE_WRAPPER_SCRIPTS environment variable is set and is not +# empty. +# - +# # Early versions of this script would create symlinks from $FSLDIR/bin/ into # $FSLDIR/fslpython/envs/fslpython/bin/ (and, later, into # $FSLDIR/share/fsl/bin/). However, wrapper scripts are now used instead to @@ -30,64 +37,71 @@ # to have a shebang line pointing to $FSLDIR/bin/python which does not # exceed 127 characters. +# Only create wrappers if the FSL_CREATE_WRAPPER_SCRIPTS +# environment variable is set +if [ -z "$FSL_CREATE_WRAPPER_SCRIPTS" ]; then + exit 0 +fi + +# Only create wrappers if FSLDIR exists +if [ ! -d "$FSLDIR" ]; then + exit 0 +fi + +# and if FSLDIR == PREFIX +if [ "$FSLDIR" != "$PREFIX" ]; then + exit 0 +fi + # Names of all executables for which wrapper # scripts are to be created are passed as # arguments targets=$@ -# Only create wrappers if scripts -# are in subdirectory of FSLDIR -if [ -z "$FSLDIR" ]; then - exit 1 -fi - -case "$PREFIX" in "$FSLDIR"*) - - for script in $targets; do - sourceScript="${PREFIX}/bin/$script" - targetScript="${FSLDIR}/share/fsl/bin/$script" +for script in $targets; do + sourceScript="${PREFIX}/bin/$script" + targetScript="${FSLDIR}/share/fsl/bin/$script" - if [ ! -f "$sourceScript" ]; then - continue - fi + if [ ! -f "$sourceScript" ]; then + continue + fi - # create share/fsl/bin/ if it doesn't - # already exist - if [ ! -e "$FSLDIR/share/fsl/bin" ]; then - mkdir -p "$FSLDIR/share/fsl/bin" - fi + # create share/fsl/bin/ if it doesn't + # already exist + if [ ! -e "$FSLDIR/share/fsl/bin" ]; then + mkdir -p "$FSLDIR/share/fsl/bin" + fi - # remove target script if it already exists - if [ -e "$targetScript" ]; then - rm "$targetScript" - fi + # remove target script if it already exists + if [ -e "$targetScript" ]; then + rm "$targetScript" + fi - # Figure out whether this is a python - # executable or some other type of - # executable. We search for these strings - # in source file headers to ID them as - # python scripts. We need to check both - # match "#!/usr/bin/env python" and - # "#!<fsldir>/bin/python...", because - # conda might generate one or the other - # in different scenarios. - id1=$(head -c 1024 "$sourceScript" | grep "#!/usr/bin/env python") - id2=$(head -c 1024 "$sourceScript" | grep "/fslpython/bin/python") + # Figure out whether this is a python + # executable or some other type of + # executable. We search for these strings + # in source file headers to ID them as + # python scripts. We need to check both + # match "#!/usr/bin/env python" and + # "#!<fsldir>/bin/python...", because + # conda might generate one or the other + # in different scenarios. + id1=$(head -c 1024 "$sourceScript" | grep "#!/usr/bin/env python") + id2=$(head -c 1024 "$sourceScript" | grep "#!${FSLDIR%/}/bin/python") - # Non-python executable - use a - # pass-through script - if [ -z "$id1" ] && [ -z "$id2" ]; then - echo "#!/bin/sh" > "$targetScript" - echo "$FSLDIR/bin/$script \"$@\"" >> "$targetScript" - else + # Non-python executable - use a + # pass-through script + if [ -z "$id1" ] && [ -z "$id2" ]; then + echo "#!/usr/bin/env bash" > "$targetScript" + echo "$FSLDIR/bin/$script "'"$@"' >> "$targetScript" + else - # Python executable - run it via - # $FSLDIR/bin/python in isolated mode - echo "#!/bin/sh" > "$targetScript" - echo "$FSLDIR/bin/python -I $sourceScript \"$@\"" >> "$targetScript" - fi + # Python executable - run it via + # $FSLDIR/bin/python in isolated mode + echo "#!/usr/bin/env bash" > "$targetScript" + echo "$FSLDIR/bin/python -I $sourceScript "'"$@"' >> "$targetScript" + fi - # Preserve file permissions - chmod --reference="$sourceScript" "$targetScript" - done -esac + # Preserve file permissions + chmod --reference="$sourceScript" "$targetScript" +done diff --git a/etc/fslconf/removeFSLWrapper.sh b/etc/fslconf/removeFSLWrapper.sh index 675e598..08f079e 100755 --- a/etc/fslconf/removeFSLWrapper.sh +++ b/etc/fslconf/removeFSLWrapper.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/usr/bin/env bash # # Remove wrapper script/links in $FSLDIR/share/fsl/bin/ which invoke commands # that are installed in $FSLDIR/bin/. See createFSLWrapper.sh for more -- GitLab