Last active
August 29, 2015 13:57
-
-
Save JSONOrona/9419489 to your computer and use it in GitHub Desktop.
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
# ======================================== # | |
# Speed up sandbox manipulation for python # | |
# ======================================== # | |
# Add these scripts to your .bashrc in order to help you using virtualenv and automate the creation and activation processes. | |
rel2abs() { | |
#from http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2005-01/0206.html | |
[ "$#" -eq 1 ] || return 1 | |
ls -Ld -- "$1" > /dev/null || return | |
dir=$(dirname -- "$1" && echo .) || return | |
dir=$(cd -P -- "${dir%??}" && pwd -P && echo .) || return | |
dir=${dir%??} | |
file=$(basename -- "$1" && echo .) || return | |
file=${file%??} | |
case $dir in | |
/) printf '%s\n' "/$file";; | |
/*) printf '%s\n' "$dir/$file";; | |
*) return 1;; | |
esac | |
return 0 | |
} | |
function activate(){ | |
if [[ "$1" == "--help" ]]; then | |
echo -e "usage: activate PATH\n" | |
echo -e "Activate the sandbox where PATH points inside of.\n" | |
return | |
fi | |
if [[ "$1" == '' ]]; then | |
local target=$(pwd) | |
else | |
local target=$(rel2abs "$1") | |
fi | |
until [[ "$target" == '/' ]]; do | |
if test -e "$target/bin/activate"; then | |
source "$target/bin/activate" | |
echo "$target sandbox activated" | |
return | |
fi | |
target=$(dirname "$target") | |
done | |
echo 'no sandbox found' | |
} | |
function mksandbox(){ | |
if [[ "$1" == "--help" ]]; then | |
echo -e "usage: mksandbox NAME\n" | |
echo -e "Create and activate a highly isaolated sandbox named NAME.\n" | |
return | |
fi | |
local name='sandbox' | |
if [[ "$1" != "" ]]; then | |
name="$1" | |
fi | |
if [[ -e "$1/bin/activate" ]]; then | |
echo "$1 is already a sandbox" | |
return | |
fi | |
virtualenv --no-site-packages --clear --distribute "$name" | |
sed -i '9i PYTHONPATH="$_OLD_PYTHON_PATH" | |
9i export PYTHONPATH | |
9i unset _OLD_PYTHON_PATH | |
40i _OLD_PYTHON_PATH="$PYTHONPATH" | |
40i PYTHONPATH="." | |
40i export PYTHONPATH' "$name/bin/activate" | |
activate "$name" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment