From dd8c7767965cdbdc31e1c5240b3ae5500017f4e7 Mon Sep 17 00:00:00 2001 From: Dror Atariah Date: Tue, 16 May 2017 13:23:01 +0200 Subject: [PATCH 01/10] Improved utilization of PROMPT_COMMAND So far, the script overwrote the existing value of `PROMPT_COMMAND`. This broke, for example the behavior of customized prompts. --- conda_auto_env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index 2470c54..9ab24d1 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -31,4 +31,4 @@ function conda_auto_env() { fi } -export PROMPT_COMMAND=conda_auto_env +export PROMPT_COMMAND="conda_auto_env;$PROMPT_COMMAND" From 295850ef6caee21f0f92863ff200292139bb6fe8 Mon Sep 17 00:00:00 2001 From: Dror Atariah Date: Wed, 28 Jun 2017 15:14:30 +0200 Subject: [PATCH 02/10] Disabled the automatic execution of the script Now, you'd have to add a specific export to enable it automatically --- README.md | 16 +++++++++++++--- conda_auto_env.sh | 2 -- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d5c2326..4c1d64b 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # conda-auto-env -Automatically activate a conda environment when entering a folder with an environment.yml file. +(Semi-)Automatically activate a conda environment when entering a folder with an environment.yml file. -If the environment doesn't exist, conda-auto-env creates it and activates it for you. +If the environment doesn't exist, `conda-auto-env` creates it and activates it for you. This functionality was inspired by [conda auto activate](https://github.com/sotte/conda_auto_activate), [virtualenv auto activate](https://gist.github.com/garyjohnson/394c58e22a2adfa103e2) and [autoenv](https://github.com/kennethreitz/autoenv). @@ -10,7 +10,17 @@ This functionality was inspired by [conda auto activate](https://github.com/sott To install add this line to your .bashrc or .bash-profile: - source /path/to/conda_auto_env.sh +```bash +source /path/to/conda_auto_env.sh +``` + +If you also want this script to be ran automatically, whenever you enter a directory add also the following: + +```bash +export PROMPT_COMMAND="conda_auto_env;$PROMPT_COMMAND" +``` + +Otherwise, whenever in a directory containing `environment.yml`, you can execute `conda_auto_env`. ### Remote environments diff --git a/conda_auto_env.sh b/conda_auto_env.sh index 9ab24d1..675e2b7 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -30,5 +30,3 @@ function conda_auto_env() { fi fi } - -export PROMPT_COMMAND="conda_auto_env;$PROMPT_COMMAND" From 94baabd3bd09f324b9889b1d2c014fc5e1b7f606 Mon Sep 17 00:00:00 2001 From: Dror Atariah Date: Wed, 3 Jan 2018 11:42:53 +0100 Subject: [PATCH 03/10] Switched from `source activate` to `conda activate` See the CHANGELOG of conda: https://github.com/conda/conda/blob/a4c4feae404b2b378e106bd25f62cc8be15c768f/CHANGELOG.md#440-2017-12-20 --- README.md | 2 +- conda_auto_env.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4c1d64b..865e241 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # conda-auto-env -(Semi-)Automatically activate a conda environment when entering a folder with an environment.yml file. +(Semi-)Automatically activate a conda environment when entering a folder with an `environment.yml` file. If the environment doesn't exist, `conda-auto-env` creates it and activates it for you. diff --git a/conda_auto_env.sh b/conda_auto_env.sh index 675e2b7..724e616 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -18,14 +18,14 @@ function conda_auto_env() { # Check if you are already in the environment if [[ $PATH != *$ENV* ]]; then # Check if the environment exists - source activate $ENV + conda activate $ENV if [ $? -eq 0 ]; then : else # Create the environment and activate echo "Conda env '$ENV' doesn't exist." conda env create -q - source activate $ENV + conda activate $ENV fi fi fi From 4c896f8680bb8b8f192de4639073eb3f0a143fc5 Mon Sep 17 00:00:00 2001 From: Dror Atariah Date: Wed, 3 Jan 2018 11:49:42 +0100 Subject: [PATCH 04/10] Initial attempt on toggling --- conda_auto_env.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index 724e616..d040b34 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -15,8 +15,11 @@ function conda_auto_env() { if [ -e "environment.yml" ]; then # echo "environment.yml file found" ENV=$(head -n 1 environment.yml | cut -f2 -d ' ') + CURRENT_ENV=$(conda env list | grep \* | cut -f 1 -d " ") # Check if you are already in the environment - if [[ $PATH != *$ENV* ]]; then + if [ $ENV = $CURRENT_ENV]; then + conda deactivate + elif [[ $PATH != *$ENV* ]]; then # Check if the environment exists conda activate $ENV if [ $? -eq 0 ]; then From 6febfa9d20ac8e4809955438b41fa2d570ed0543 Mon Sep 17 00:00:00 2001 From: Dror Atariah Date: Fri, 5 Jan 2018 08:46:35 +0100 Subject: [PATCH 05/10] Typo in code --- conda_auto_env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index d040b34..8998812 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -17,7 +17,7 @@ function conda_auto_env() { ENV=$(head -n 1 environment.yml | cut -f2 -d ' ') CURRENT_ENV=$(conda env list | grep \* | cut -f 1 -d " ") # Check if you are already in the environment - if [ $ENV = $CURRENT_ENV]; then + if [ $ENV = $CURRENT_ENV ]; then conda deactivate elif [[ $PATH != *$ENV* ]]; then # Check if the environment exists From 7ecb693b8699f0e9e0c163fa2aa4231261d077f8 Mon Sep 17 00:00:00 2001 From: Dror Atariah Date: Fri, 5 Jan 2018 09:07:41 +0100 Subject: [PATCH 06/10] Fixed and annotated toggling --- conda_auto_env.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index 8998812..fcca025 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -11,24 +11,29 @@ # source /path/to/conda_auto_env.sh # +ACTIVATED="${ENV} activated!" +DEACTIVATE="${ENV} is already activated. Deactivating..." + function conda_auto_env() { if [ -e "environment.yml" ]; then - # echo "environment.yml file found" + echo "environment.yml file found" ENV=$(head -n 1 environment.yml | cut -f2 -d ' ') CURRENT_ENV=$(conda env list | grep \* | cut -f 1 -d " ") # Check if you are already in the environment if [ $ENV = $CURRENT_ENV ]; then + echo $DEACTIVATE conda deactivate elif [[ $PATH != *$ENV* ]]; then # Check if the environment exists conda activate $ENV if [ $? -eq 0 ]; then - : + echo $ACTIVATED else # Create the environment and activate echo "Conda env '$ENV' doesn't exist." conda env create -q conda activate $ENV + echo $ACTIVATED fi fi fi From 014c7229aaae0c65278aa0543f12b5553a2c8d36 Mon Sep 17 00:00:00 2001 From: Dror Atariah Date: Fri, 5 Jan 2018 09:11:15 +0100 Subject: [PATCH 07/10] Fixed messages inits --- conda_auto_env.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index fcca025..01f4a13 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -11,13 +11,12 @@ # source /path/to/conda_auto_env.sh # -ACTIVATED="${ENV} activated!" -DEACTIVATE="${ENV} is already activated. Deactivating..." - function conda_auto_env() { if [ -e "environment.yml" ]; then echo "environment.yml file found" ENV=$(head -n 1 environment.yml | cut -f2 -d ' ') + ACTIVATED="${ENV} activated!" + DEACTIVATE="${ENV} is already activated. Deactivating..." CURRENT_ENV=$(conda env list | grep \* | cut -f 1 -d " ") # Check if you are already in the environment if [ $ENV = $CURRENT_ENV ]; then From 42e87d298795672178b3ecf77084394979b4e5cf Mon Sep 17 00:00:00 2001 From: Dror Atariah Date: Fri, 12 Jan 2018 08:35:03 +0100 Subject: [PATCH 08/10] Initial attempt to act based on dir name If environment.yml doesn't exist --- conda_auto_env.sh | 55 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index 01f4a13..eb5e452 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -11,29 +11,50 @@ # source /path/to/conda_auto_env.sh # +DEFAULT_PY_VER=3.6 + function conda_auto_env() { + # Determine ENV's name; eitehr from environment.yml + # or based on current dir name if [ -e "environment.yml" ]; then echo "environment.yml file found" ENV=$(head -n 1 environment.yml | cut -f2 -d ' ') - ACTIVATED="${ENV} activated!" - DEACTIVATE="${ENV} is already activated. Deactivating..." - CURRENT_ENV=$(conda env list | grep \* | cut -f 1 -d " ") - # Check if you are already in the environment - if [ $ENV = $CURRENT_ENV ]; then - echo $DEACTIVATE - conda deactivate - elif [[ $PATH != *$ENV* ]]; then - # Check if the environment exists - conda activate $ENV - if [ $? -eq 0 ]; then - echo $ACTIVATED - else - # Create the environment and activate - echo "Conda env '$ENV' doesn't exist." + else + echo "No environment.yml found. Reverting to current dir" + ENV=${PWD##*/} + fi + + echo "Processing the environment: ${ENV}" + + ACTIVATED="${ENV} activated!" + DEACTIVATE="${ENV} is already activated. Deactivating..." + CURRENT_ENV=$(conda env list | grep \* | cut -f 1 -d " ") + + if [ $ENV = $CURRENT_ENV ]; then + # ENV is already activated. Deactivate + echo $DEACTIVATE + conda deactivate + if [[ $? -eq 0 ]]; then + echo "Deactivated successfully" + else + echo "Failed to deactivate" + fi + elif [[ $PATH != *$ENV* ]]; then + # ENV is not activated. Trying to activate + conda activate $ENV + if [ $? -eq 0 ]; then + echo $ACTIVATED + else + # Failed to activate. Creating environment + echo "Conda env '$ENV' doesn't exist." + if [[ -e "environment.yml" ]]; then conda env create -q - conda activate $ENV - echo $ACTIVATED + else + echo "Creating ${ENV} with default settings" + conda create -n ${ENV} python=${DEFAULT_PY_VER} fi + conda activate $ENV + echo $ACTIVATED fi fi } From df0550c3ae805a84137e9997b4cf69d97050bf47 Mon Sep 17 00:00:00 2001 From: Dror Atariah Date: Tue, 16 Jan 2018 11:30:12 +0100 Subject: [PATCH 09/10] Improved calls and return values checks --- conda_auto_env.sh | 51 ++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index eb5e452..a10352c 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -1,7 +1,7 @@ #!/bin/bash -# conda-auto-env automatically activates a conda environment when -# entering a folder with an environment.yml file. +# conda-auto-env is a utility to activate a conda environment in a folder +# with an environment.yml file or based on the current dir's name. # # If the environment doesn't exist, conda-auto-env creates it and # activates it for you. @@ -14,10 +14,31 @@ DEFAULT_PY_VER=3.6 function conda_auto_env() { + + function activate_env() { + conda activate $1 + if [[ $? -eq 0 ]]; then + echo "${1} activated successfully" + else + echo "Failed to activate ${1}" + return 1 + fi + } + + function deactivate_env() { + conda deactivate + if [[ $? -eq 0 ]]; then + echo "Deactivated successfully" + else + echo "Failed to deactivate" + return 1 + fi + } + # Determine ENV's name; eitehr from environment.yml # or based on current dir name if [ -e "environment.yml" ]; then - echo "environment.yml file found" + echo "Found environment.yml" ENV=$(head -n 1 environment.yml | cut -f2 -d ' ') else echo "No environment.yml found. Reverting to current dir" @@ -26,35 +47,29 @@ function conda_auto_env() { echo "Processing the environment: ${ENV}" - ACTIVATED="${ENV} activated!" - DEACTIVATE="${ENV} is already activated. Deactivating..." CURRENT_ENV=$(conda env list | grep \* | cut -f 1 -d " ") if [ $ENV = $CURRENT_ENV ]; then # ENV is already activated. Deactivate - echo $DEACTIVATE - conda deactivate - if [[ $? -eq 0 ]]; then - echo "Deactivated successfully" - else - echo "Failed to deactivate" - fi + deactivate_env elif [[ $PATH != *$ENV* ]]; then # ENV is not activated. Trying to activate - conda activate $ENV - if [ $? -eq 0 ]; then - echo $ACTIVATED - else + activate_env $ENV + if [ $? -ne 0 ]; then # Failed to activate. Creating environment echo "Conda env '$ENV' doesn't exist." if [[ -e "environment.yml" ]]; then + echo "Creating ${ENV} based on environment.yml" conda env create -q else echo "Creating ${ENV} with default settings" conda create -n ${ENV} python=${DEFAULT_PY_VER} fi - conda activate $ENV - echo $ACTIVATED + activate_env $ENV fi fi + + # Make sure utility functions are local + unset -f activate_env + unset -f deactivate_env } From 0d7540e2177e823ed89a44ba0b77b1c055c7cb59 Mon Sep 17 00:00:00 2001 From: Dror Atariah Date: Thu, 31 Jan 2019 09:04:23 +0100 Subject: [PATCH 10/10] Faster fetching of current env name --- conda_auto_env.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index a10352c..84d6bb1 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -47,9 +47,7 @@ function conda_auto_env() { echo "Processing the environment: ${ENV}" - CURRENT_ENV=$(conda env list | grep \* | cut -f 1 -d " ") - - if [ $ENV = $CURRENT_ENV ]; then + if [ $ENV = $CONDA_DEFAULT_ENV ]; then # ENV is already activated. Deactivate deactivate_env elif [[ $PATH != *$ENV* ]]; then @@ -72,4 +70,5 @@ function conda_auto_env() { # Make sure utility functions are local unset -f activate_env unset -f deactivate_env + unset ENV }