Created
June 13, 2010 03:28
-
-
Save braveulysses/436318 to your computer and use it in GitHub Desktop.
Simple wrapper script for Python virtualenv
This file contains hidden or 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 | |
############################################################################### | |
# | |
# venv: A simple wrapper for Python virtualenv | |
# | |
############################################################################### | |
VIRTUALENV_ROOT_DIR=~/Projects/Virtualenv | |
function usage () { | |
echo "Usage" | |
echo | |
echo " venv Action [env_name]" | |
echo | |
echo "Action" | |
echo | |
echo " usage - show this usage information" | |
echo " list - show all virtual envs" | |
echo " use [env_name] - set current shell to use a specific env" | |
echo " create [env_name] - create new Python virtual env" | |
echo | |
} | |
function list_environments () { | |
if [[ -n $VIRTUAL_ENV ]]; then | |
current_env=`echo $VIRTUAL_ENV | cut -d "/" -f 6` | |
else | |
current_env="" | |
fi | |
envs=(`ls $VIRTUALENV_ROOT_DIR`) | |
echo | |
echo "Virtualenv Pythons" | |
echo | |
for v in ${envs[@]}; do | |
if [[ $v == $current_env ]]; then | |
echo "=> $v" | |
else | |
echo " $v" | |
fi | |
done | |
echo | |
} | |
function switch_environment () { | |
env_name=$1 | |
if [[ -d $VIRTUALENV_ROOT_DIR/$env_name ]]; then | |
# Um, not sure how to do this right... | |
echo "Run this: source $VIRTUALENV_ROOT_DIR/$env_name/bin/activate" | |
else | |
echo "Error loading environment: $env_name" | |
fi | |
} | |
function create_environment () { | |
env_name=$1 | |
if [[ -d $VIRTUALENV_ROOT_DIR/$env_name ]]; then | |
echo "Error: $env_name already exists" | |
else | |
virtualenv --no-site-packages $VIRTUALENV_ROOT_DIR/$env_name | |
fi | |
} | |
# Main | |
if [[ $# -eq 1 ]]; then | |
if [[ $1 == "list" ]]; then | |
list_environments | |
else | |
usage | |
fi | |
elif [[ $# -eq 2 ]]; then | |
case $1 in | |
use ) | |
switch_environment $2 | |
;; | |
create ) | |
create_environment $2 | |
;; | |
* ) | |
usage | |
;; | |
esac | |
else | |
usage | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment