From fea28292c11cba7f2449544e14ac90ad53f10e46 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Oriol=20Tint=C3=B3=20Prims?= <oriol.tinto@lmu.de>
Date: Fri, 24 Mar 2023 13:10:07 +0100
Subject: [PATCH] Adapt build_python_environment.sh to use the new
 spack_utils.sh functions.

---
 templates/common/build_python_environment.sh | 27 +++++---------------
 1 file changed, 7 insertions(+), 20 deletions(-)

diff --git a/templates/common/build_python_environment.sh b/templates/common/build_python_environment.sh
index 043dc54..9908eb8 100644
--- a/templates/common/build_python_environment.sh
+++ b/templates/common/build_python_environment.sh
@@ -6,33 +6,20 @@ SPACK_URL=%spack.url%
 SPACK_BRANCH=%spack.branch%
 SPACK_COMPILER=%spack.compiler%
 
-# If the workdir directory does not exist create it
-if [ ! -d ${WORKDIR} ]; then
-  mkdir -p ${WORKDIR}
-fi
-
 # Go to the working directory
 cd ${WORKDIR}
 
-# Check if experiment's spack installation already exists, if it doesn't, clone it.
-SPACK_ENV=spack/share/spack/setup-env.sh
-if [ ! -f ${SPACK_ENV} ]; then
-  echo "Spack folder not found!"
-  exit 1
-fi
+. ${WORKDIR}/proj/platforms/common/spack_utils.sh
+load_spack "%spack.init%" "%spack.root%" "%spack.url%" "%spack.branch%" "%spack.compiler%" "%spack.disable_local_config%" "%spack.user_cache_path%" "%spack.user_config_path%"
 
-# Setup the spack environment
-source ${SPACK_ENV}
 
 # Use spack to get a recent enough version of python3
-if [ $( spack find python@3.8: &>/dev/null
-  echo $?
-) -ne 0 ]; then
+if [ $(spack find python@3.8: &>/dev/null ) ]; then
+  echo "python@3.8: already installed!"
+else
   echo "Installing a version of python3"
   # Compile openmpi with schedulers=slurm
   spack install python@3.8:
-else
-  echo "python@3.8: already installed!"
 fi
 
 # Load the python module
@@ -56,7 +43,7 @@ ln -sf $(which python3) ${WORKDIR}/python3
 requirements="%python_environment.requirements%"
 
 # Convert list with python format to a bash array
-requirements=($( echo ${requirements} | sed "s/'//g" | tr -d '[],'))
+requirements=($(echo ${requirements} | sed "s/'//g" | tr -d '[],'))
 
 #TODO: Shouldn't be necessary but it is for now to overcome an issue with a repetition of the requirements.
 # Use sort and uniq to get the unique elements
@@ -66,6 +53,6 @@ unique_requirements=($(printf "%s\n" "${requirements[@]}" | sort -u))
 echo "${unique_requirements[@]}"
 
 # Install requirements.
-for requirement in ${unique_requirements[@]} ; do
+for requirement in "${unique_requirements[@]}"; do
   python -m pip install ${requirement}
 done
-- 
GitLab