Skip to content

Instantly share code, notes, and snippets.

@fbudin69500
Created September 7, 2018 02:37
Show Gist options
  • Save fbudin69500/52f1e098ef91f9151ad4f2c6c5600392 to your computer and use it in GitHub Desktop.
Save fbudin69500/52f1e098ef91f9151ad4f2c6c5600392 to your computer and use it in GitHub Desktop.
zsh functions to activate and deactivate configurations for specific workspaces
# Configure environment per workspace
# The configuration is defined in a file called .folder.config
# that is contained in any folder. If such a file is found
# in the current folder or any of its parents, this file
# is sourced. The following variables can be defined in a
#`.folder.config` file:
# * `folder_virtual_env`: This variable defines the Python virtual
# environment that is automatically loaded when working in
# the current workspace.
# * `folder_spack_packages`: This variable defines the list of
# spack packages that are automatically loaded when working in
# the current workspace.
# These variables should be defined as `local` variables to avoid
# polluting the environment.
# These variables are convenient as they are understood by the
# functions defined in this file, and will automatically
# load the correct modules when entering in the workspace,
# unload the modules when leaving the workspace, activate the
# current virtual environment when entering in the workspace,
# and deactivating the virtual environment when leaving the workspace.
# Limitation:
# There is currently no clean way to detect if `.folder.config` is called
# to activate or deactivate a workspace.
# TODO: Add a mechanism to let `.folder.config` know that it is called to
# activate or deactivate the workspace, possibly using a variable
# such as `local folder_deactivate`.
# Search for project config file in current directory and
# recursively in parent directories until root directory is found.
# Use `FOLDER_CONFIG_LOOK_UP` environment variable to avoid
# calling the function recursively when changing folder inside
# the folder configuration file.
function _function_lock {
export FOLDER_CONFIG_LOOK_UP=1
$@
unset FOLDER_CONFIG_LOOK_UP
}
# Deactivate folder
# Deactivate virtual environment, unload packages, remove
# environment variables.
function deactivate_folder {
if [[ -f "$CURRENT_FOLDER/$config_filename" ]]; then
source "$CURRENT_FOLDER/$config_filename"
if [[ -n "$folder_virtual_env" ]]; then
deactivate
fi
if [[ -n "$folder_spack_packages" ]]; then
for package in ${folder_spack_packages[@]}; do
spack unload $package
done
fi
fi
unset CURRENT_FOLDER
}
# Activate folder
# Activate virtual environment, load packages.
function activate_folder {
source "$current_path/$config_filename"
export CURRENT_FOLDER=`pwd`
if [[ -n "$folder_virtual_env" ]]; then
echo $folder_virtual_env
workon $folder_virtual_env
fi
if [[ -n "$folder_spack_packages" ]]; then
for package in ${folder_spack_packages[@]}; do
spack load $package
done
fi
}
# Function called when one changes folder.
function chpwd {
if [[ -n $FOLDER_CONFIG_LOOK_UP ]]; then
return 0
fi
current_path=`pwd`
config_filename=".folder.config"
while [[ $current_path != / ]];
do
res=`find "$current_path" -maxdepth 1 -mindepth 1 -name $config_filename`
if [[ -f $res ]]
then
# First deactivate previous folder
_function_lock deactivate_folder
# Then activate new folder
_function_lock activate_folder
return 0
fi
current_path="$(readlink -f "$current_path"/..)"
done
_function_lock deactivate_folder
unset FOLDER_CONFIG_LOOK_UP
}
# Force call chpwd when sourcing this file.
chpwd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment