Created
June 25, 2011 18:08
-
-
Save durden/1046719 to your computer and use it in GitHub Desktop.
Shell script wrapper for easily starting/populating a virtual env
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# This is a function so that we can use 'workon' to switch since it will | |
# refresh the environment and pass it onto the caller. Thus, no reason to run | |
# another shell after this command or calling workon manually, etc. | |
start_virtualenv_main() { | |
# Assume your working in the directory of the project name | |
curr=`pwd` | |
proj=`basename $curr` | |
# Assume curr directory has requirements.pip | |
requirements="$curr/requirements.pip" | |
# Default base virtualenv dir to environment variable, but allow argument | |
# override | |
env | grep -q VIRTUALENV_DIR | |
[ $? -eq 0 ] && virtualenv_base=$VIRTUALENV_DIR | |
while getopts "p:b:r:" opt; do | |
case $opt in | |
p) | |
proj=$OPTARG ;; | |
b) | |
virtualenv_base=$OPTARG ;; | |
r) | |
requirements=$OPTARG ;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
echo "Usage: start_virtualenv [-p <project name>] [-b <virtualenv base directory>] [-r <requirements file>]" | |
return | |
;; | |
esac | |
done | |
python="/System/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python" | |
virtualenv_dir="$virtualenv_base/$proj/" | |
echo "Creating virtual environment for $proj in $virtualenv_dir with $requirements" | |
mkdir -p $virtualenv_dir | |
virtualenv --no-site-packages -v -p $python $virtualenv_dir | |
workon $proj | |
if [ -e $requirements ]; then | |
pip install -E $virtualenv_dir -v -r $requirements | |
else | |
pip install -E $virtualenv_dir -v | |
fi | |
} | |
# Pass all arguments to function (necessary to use getopts) | |
start_virtualenv_main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pretty simple, but I really had remembering the arguments, etc. to virtualenv and pip.
This assumes you have virtualenvwrapper installed (no excuse for you if you don't).
TODO: