From f12a5386363c93ad69e06b40f0c64a7ef480a169 Mon Sep 17 00:00:00 2001 From: Ryan Tock Date: Thu, 29 May 2025 01:30:52 -0700 Subject: [PATCH] improved bash scripting for conda setup --- .gitignore | 3 ++ setup/activate_conda.sh | 7 ++++ setup/checks/check_for_conda.sh | 7 ++-- setup/export_versions.sh | 3 ++ setup/setup.sh | 5 +-- setup/setup_conda.sh | 63 +++++++++++++++++++++++---------- setup/teardown.sh | 3 +- setup/teardown_conda.sh | 8 ++--- 8 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 setup/activate_conda.sh create mode 100644 setup/export_versions.sh diff --git a/.gitignore b/.gitignore index ad8685f1..3d040f3c 100644 --- a/.gitignore +++ b/.gitignore @@ -73,6 +73,9 @@ docs/_build/ # PyBuilder target/ +# Conda +miniconda.sh + # Jupyter Notebook .ipynb_checkpoints diff --git a/setup/activate_conda.sh b/setup/activate_conda.sh new file mode 100644 index 00000000..e796cb11 --- /dev/null +++ b/setup/activate_conda.sh @@ -0,0 +1,7 @@ +source setup/export_versions.sh + +INSTALL_PREFIX=$HOME/miniconda-$EXP_CONDA_VER +SOURCE_SCRIPT="$HOME/miniconda-$EXP_CONDA_VER/etc/profile.d/conda.sh" + +source $SOURCE_SCRIPT +conda activate emissioneval \ No newline at end of file diff --git a/setup/checks/check_for_conda.sh b/setup/checks/check_for_conda.sh index 722e6053..1bf8f12d 100644 --- a/setup/checks/check_for_conda.sh +++ b/setup/checks/check_for_conda.sh @@ -1,9 +1,10 @@ +source setup/export_versions.sh CURR_CONDA_VER=`conda --version | cut -d " " -f 2` -EXP_CONDA_VER=23.1.0 -if [ $CURR_CONDA_VER == $EXP_CONDA_VER ]; then +if [[ $CURR_CONDA_VER == $EXP_CONDA_VER ]]; then echo "For conda, found $CURR_CONDA_VER, expected $EXP_CONDA_VER, all is good!" else - echo "For conda, found $CURR_CONDA_VER, expected $EXP_CONDA_VER, run 'bash setup/setup_conda.sh $EXP_CONDA_VER ' to get the correct version" + echo "For conda, found $CURR_CONDA_VER, expected $EXP_CONDA_VER, run 'bash setup/setup_conda.sh ' to get the correct version" echo "Or install manually after downloading from https://repo.anaconda.com/miniconda/" fi + diff --git a/setup/export_versions.sh b/setup/export_versions.sh new file mode 100644 index 00000000..dfc4557d --- /dev/null +++ b/setup/export_versions.sh @@ -0,0 +1,3 @@ +export EXP_CONDA_VER=23.1.0 +export EXP_CONDA_VER_SUFFIX=1 +export EXP_LIBMAMBA_VER=1.0.0 \ No newline at end of file diff --git a/setup/setup.sh b/setup/setup.sh index a0d3af9d..893432c9 100755 --- a/setup/setup.sh +++ b/setup/setup.sh @@ -5,10 +5,7 @@ # - on OSX: /Users//miniconda3/bin/conda # - on Windows: C:/Users//Miniconda3/Scripts/conda -set -e - source setup/checks/check_for_conda.sh -conda create -n emissioneval +echo "Installing using conda now" conda env update --name emissioneval --file setup/environment.yml -conda activate emissioneval diff --git a/setup/setup_conda.sh b/setup/setup_conda.sh index de6b0179..b67f05d8 100644 --- a/setup/setup_conda.sh +++ b/setup/setup_conda.sh @@ -1,21 +1,48 @@ -EXP_CONDA_VER=$1 -PLATFORM=$2 -echo "Installing for version $EXP_CONDA_VER and platform $PLATFORM" +if [[ ! -f setup/export_versions.sh ]]; then + echo "Error: setup/export_versions.sh file is missing. Please ensure it exists before running this script." + exit 1 +fi + +source setup/export_versions.sh +if [[ $? -ne 0 ]]; then + echo "Error: Failed to source setup/export_versions.sh. Please check the file for errors." + exit 1 +fi + +if [[ -z $EXP_CONDA_VER ]]; then + echo "Error: Requires the EXP_CONDA_VER variable to be set" + echo "Please check the 'export_versions.sh' script and ensure that it works" + exit 1 +fi -if [[ -z $EXP_CONDA_VER || -z $PLATFORM ]]; then - echo "Usage: setup_conda.sh " - echo " Platform options are Linux-x86_64, MacOSX-x86_64" - echo " For Windows, manually download and install https://repo.anaconda.com/miniconda/Miniconda3-$EXP_CONDA_VER-Windows-x86_64.exe" -else +PLATFORM=$1 +INSTALL_PREFIX=$2 + +if [[ -z $PLATFORM ]]; then + echo "Usage: setup_conda.sh [install_prefix]" + echo " Platform options are Linux-x86_64, MacOSX-x86_64, MacOSX-arm64" + WINDOWS_INSTALLER_URL="https://repo.anaconda.com/miniconda/Miniconda3-py39_$EXP_CONDA_VER-$EXP_CONDA_VER_SUFFIX-Windows-x86_64.exe" + echo " For Windows, manually download and install $WINDOWS_INSTALLER_URL" + exit 2 +fi + +if [[ -z $INSTALL_PREFIX ]]; then INSTALL_PREFIX=$HOME/miniconda-$EXP_CONDA_VER - SOURCE_SCRIPT="$HOME/miniconda-$EXP_CONDA_VER/etc/profile.d/conda.sh" - - curl -o miniconda.sh -L https://repo.continuum.io/miniconda/Miniconda3-py39_$EXP_CONDA_VER-1-$PLATFORM.sh; - bash miniconda.sh -b -p $INSTALL_PREFIX - source $SOURCE_SCRIPT - hash -r - conda config --set always_yes yes --set changeps1 no - # Useful for debugging any issues with conda - conda info -a - echo "Successfully installed at $INSTALL_PREFIX. Please run 'source $SOURCE_SCRIPT' in every terminal where you want to use conda" fi +SOURCE_SCRIPT="$INSTALL_PREFIX/etc/profile.d/conda.sh" + +echo "Installing for version $EXP_CONDA_VER and platform $PLATFORM at $INSTALL_PREFIX" +curl -o miniconda.sh -L https://repo.anaconda.com/miniconda/Miniconda3-py39_$EXP_CONDA_VER-$EXP_CONDA_VER_SUFFIX-$PLATFORM.sh +bash miniconda.sh -b -p $INSTALL_PREFIX +source $SOURCE_SCRIPT +hash -r +conda config --set auto_update_conda false +conda install conda-libmamba-solver=$EXP_CONDA_VER libmamba=$EXP_LIBMAMBA_VER +conda config --set solver libmamba +conda config --set always_yes yes +# Useful for debugging any issues with conda +conda info -a +echo "Successfully installed at $INSTALL_PREFIX" +echo "Please initalize with '$HOME/miniconda-23.1.0/bin/conda init ' (bash, zsh, etc.) and restart your terminal" +echo "Please run 'source setup/setup.sh' to set up the necessary conda environment" +echo "Please activate with 'source setup/activate_conda.sh' in every terminal where you want to use conda" diff --git a/setup/teardown.sh b/setup/teardown.sh index c0ed9860..00bd812c 100644 --- a/setup/teardown.sh +++ b/setup/teardown.sh @@ -1,2 +1 @@ -conda activate base -conda env remove --name emissioneval +conda deactivate && conda env remove --name emissioneval diff --git a/setup/teardown_conda.sh b/setup/teardown_conda.sh index af303207..4bce35f3 100644 --- a/setup/teardown_conda.sh +++ b/setup/teardown_conda.sh @@ -1,8 +1,4 @@ EXP_CONDA_VER=$1 +INSTALL_PREFIX=$HOME/miniconda-$EXP_CONDA_VER -if [ -z $EXP_CONDA_VER ]; then - echo "Usage: teardown_conda.sh " -else - INSTALL_PREFIX=$HOME/miniconda-$EXP_CONDA_VER - rm -rf $INSTALL_PREFIX -fi +rm -rf $INSTALL_PREFIX