Skip to content

Instantly share code, notes, and snippets.

@kbambz
Last active December 30, 2015 10:19
Show Gist options
  • Select an option

  • Save kbambz/7815246 to your computer and use it in GitHub Desktop.

Select an option

Save kbambz/7815246 to your computer and use it in GitHub Desktop.
virtualenvwrapper postactivate and predeactivate scripts for dumping a .env file's declarations (foreman style -- located in your project root) into your bash environment on activation, then cleaning them up on deactivate. Also tells npm and rvm to install packages directly into the active virtual environment.
#!/bin/bash
# This hook should be run after every virtualenv is activated.
# See `predeactivate-env-cleanup` to configure automatic clean-up.
#
# INSTALLATION
# Save this file in $VIRTUALENVWRAPPER_HOOK_DIR (e.g., ~/.virtualenvs)
# in a file called `postactivate-env-setup`, then do:
#
# $ chmod +x $VIRTUALENVWRAPPER_HOOK_DIR/postactivate-env-setup
# $ echo "source \$VIRTUALENVWRAPPER_HOOK_DIR/postactivate-env-setup" >> $VIRTUALENVWRAPPER_HOOK_DIR/postactivate
# @author Kate Bambino (kbambz)
echo ""
echo -e "\033[1;32mActivating virtualenv \033[0;35m$(basename $VIRTUAL_ENV)\033[0m"
# Check if a project is associated with this virtual environment
if [ -e "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME" ]; then
VIRTUAL_ENV_PROJECT_PATH=`cat "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME"`
echo -e " \033[1;32m@ \033[0;35m$VIRTUAL_ENV_PROJECT_PATH\033[0m"
echo ""
# Read project .env file declarations into bash environment
if [ -e "$VIRTUAL_ENV_PROJECT_PATH/.env" ]; then
echo -e "\033[1;37mReading project's .env file declarations into bash environment...\033[0m"
while read line; do
[ -z "$line" ] && continue
[ -z "${line%%#*}" ] && continue
export ${line%%=*}="${line#*=}"
echo " > ${line%%=*}"
done < "$VIRTUAL_ENV_PROJECT_PATH/.env"
else
echo -e "\033[1;33mWARNING:\033[0m Project root does not contain an .env file."
fi
else
echo -e "\033[1;33mWARNING:\033[0m There is no project directory associated with this virtualenv. To set one, cd into the project root and run \`setvirtualenvproject \$VIRTUAL_ENV \$(pwd) && workon \$(basename \$VIRTUAL_ENV)'"
fi
echo ""
# Make it so npm install -g <package> and rvm install <package>
# install directly into the active virtual environment
# Save existing values for cleanup in predeactivate.sh
if [[ -n $GEM_HOME ]]; then
export OLD_GEM_HOME=$GEM_HOME
fi
if [[ -n $GEM_PATH ]]; then
export OLD_GEM_PATH=$GEM_PATH
fi
if [[ -n $npm_config_prefix ]]; then
export OLD_npm_config_prefix=$npm_config_prefix
fi
export GEM_HOME="$VIRTUAL_ENV/lib/gems"
export GEM_PATH=""
PATH="$GEM_HOME/bin:$PATH"
export npm_config_prefix=$VIRTUAL_ENV
export PATH
#!/bin/bash
# This hook is run before every virtualenv is deactivated.
# It was designed to clean up any bash environment modifications
# made by postactivate hook `postactivate-env-setup`.
#
# INSTALLATION
# Save this file in $VIRTUALENVWRAPPER_HOOK_DIR (e.g., ~/.virtualenvs)
# in a file called `predeactivate-env-cleanup`, then do:
#
# $ chmod +x $VIRTUALENVWRAPPER_HOOK_DIR/predeactivate-env-cleanup
# $ echo "source \$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate-env-cleanup" >> $VIRTUALENVWRAPPER_HOOK_DIR/predeactivate
#
# @author Kate Bambino (kbambz)
# Check if a project is associated with this virtual environment
if [ -e "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME" ]; then
VIRTUAL_ENV_PROJECT_PATH=`cat "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME"`
if [ -e "$VIRTUAL_ENV_PROJECT_PATH/.env" ]; then
echo -e "\033[1;37mCleaning up .env file associated bash assignments...\033[0m"
# Clear .env file declarations from your bash environment
while read line; do
[ -z "$line" ] && continue
[ -z "${line%%#*}" ] && continue
if [[ -n ${line%%=*} ]]; then
unset ${line%%=*}
echo " > ${line%%=*}"
fi
done < "$VIRTUAL_ENV_PROJECT_PATH/.env"
fi
fi
# Make it so npm install -g <package> and rvm install <package>
# install to wherever they were set up to before this virtualenv
# was activated
if [[ -n $OLD_GEM_HOME ]]; then
export GEM_HOME=$OLD_GEM_HOME
unset OLD_GEM_HOME
else
unset GEM_HOME
fi
if [[ -n $OLD_GEM_PATH ]]; then
export GEM_PATH=$OLD_GEM_PATH
unset OLD_GEM_PATH
else
unset GEM_PATH
fi
if [[ -n $OLD_npm_config_prefix ]]; then
export npm_config_prefix=$OLD_npm_config_prefix
unset OLD_npm_config_prefix
else
unset npm_config_prefix
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment