Skip to content

Instantly share code, notes, and snippets.

@gmaze
Last active June 21, 2023 10:31
Show Gist options
  • Save gmaze/ff2485feb158d29d89c341f6a8f03bd4 to your computer and use it in GitHub Desktop.
Save gmaze/ff2485feb158d29d89c341f6a8f03bd4 to your computer and use it in GitHub Desktop.
Clean cloning of a conda env (and add it to jupyter kernel list)
#!/bin/sh
#
# Clone a conda env and add it to jupyter kernel list
#
# This is basicaly a nice shortcut for:
# mamba create --name argopy-pull208 --clone argopy-py38-all-pinned
# python -m ipykernel install --user --name=argopy-pull208
#
#
# Created by G. Maze on 2023-06-21.
#########################
# FUNCTIONS
#########################
Help() {
# Display Help
echo "Clone Conda environments nicely"
echo
echo "Syntax: clone -o <origin> -c <clone>"
echo "options:"
echo "h Print this Help"
echo "o Name of the environment to clone, default is 'argopy-py38-all-pinned'"
echo "c Name of the cloned environment"
echo
}
fix_kernel_path (){
ENV=${1}
kernelspec="$(jupyter kernelspec list | grep ${ENV})"
echo "${kernelspec}"
IFS=' ' read -ra ADDR <<< "$kernelspec"
kernel_path="${ADDR[1]}"
kernel_cfg_file="${kernel_path}/kernel.json"
python_path="$(more ${kernel_cfg_file} | jq .argv[0] | sed 's/\"//g')"
DIR="$(dirname ${python_path})"
FILE="$(basename ${python_path})"
#echo "[${DIR}] [${FILE}]"
IFS='/' read -r -a parts <<< "$DIR"
# We expect: /Users/gmaze/miniconda3/envs/<ENV>/bin
if [ "${parts[-2]}" = ${ENV} ]; then
echo "kernel spec OK"
else
parts[-2]=${ENV} # update with appropriate python path
NEW_DIR=$(IFS='/' ; echo "${parts[*]}")
new_python_path="${NEW_DIR}/${FILE}"
while true; do
printf "Replace: \n${python_path} \nwith: \n${new_python_path}\n"
read -p "Do you confirm ? [Yy/Nn] " yn
case $yn in
[Yy]* )
cp -f ${kernel_cfg_file} "${kernel_cfg_file}.bak"
tmp=$(mktemp)
jq --arg a "$new_python_path" '.argv[0] = $a' ${kernel_cfg_file} > "$tmp" && mv "$tmp" ${kernel_cfg_file}
break;;
[Nn]* ) exit;;
* ) echo "Please answer yes [Yy] or no [Nn]";;
esac
done
fi
}
add_ipykernel () {
# Possibly add it the Jupyter kernels:
python -m ipykernel install --user --name=$1
# Check installation of the kernel:
fix_kernel_path ${1}
}
clone_conda_env () {
# $1 is the name of the new environment
# $2 is the name of the environment to clone
# Eventually remove environment if exists:
if conda env list | grep $1; then
printf "$1 already exists, removing this environment...\n"
mamba remove --quiet --name $1 --all --yes --json > /dev/null 2>&1
fi
# Create the environment from file
printf "Creating conda environment $1 ...\n"
mamba create --name $1 --clone $2 > /dev/null 2>&1
}
#########################
# PROCESS OPTIONS
#########################
# Set variables
envname_to_clone="argopy-py38-all-pinned"
envname_to_install=""
# Get the options
while getopts "ho:c:" option; do
case $option in
h) # display Help
Help
exit 0
;;
o) # Enter an action to perform
envname_to_clone=$OPTARG
;;
c) # Enter an action to perform
envname_to_install=$OPTARG
;;
esac
done
#########################
# ACTIONS
#########################
if [ -n "$envname_to_install" ]; then
clone_conda_env $envname_to_install $envname_to_clone
add_ipykernel $envname_to_clone
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment