From b5775083a1fe12b3393789ad6b4ea67c67112558 Mon Sep 17 00:00:00 2001 From: Baptiste Darthenay Date: Tue, 27 Dec 2016 19:54:25 +0100 Subject: Using WORKON_HOME instead of envroot. This improves compatibility with virtualenvwrapper. --- README.md | 4 +++- venvworkon.sh | 13 ++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3ddcf66..fc74104 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,9 @@ will be printed instead. ## Where venvs are created When `workon` is called, venvs are created or loaded from the -`$HOME/.venvs` folder. +`$HOME/.venvs` folder by default. +To change this behavior, set the `WORKON_HOME` environment +variable before sourcing `venvworkon.sh`. ## Post-load script If the file `$HOME/.venvs//postload.sh` exists, it is diff --git a/venvworkon.sh b/venvworkon.sh index 1da01bd..adca644 100755 --- a/venvworkon.sh +++ b/venvworkon.sh @@ -1,20 +1,23 @@ workon() { - envroot="$HOME/.venv" + if [ -z "$WORKON_HOME" ] + then + WORKON_HOME="$HOME/.venv" + fi - if [ ! -e "$envroot" ] + if [ ! -e "$WORKON_HOME" ] then - mkdir -p "$envroot" + mkdir -p "$WORKON_HOME" fi if [ "$#" -lt "1" ] then - ls "$envroot" + ls "$WORKON_HOME" return fi envname=$1 - envdir="$envroot/$envname" + envdir="$WORKON_HOME/$envname" if [ ! -e "$envdir" ] then -- cgit v1.2.3 From ef80fa0bec2df62c2ea5469123b4ad794c7c9306 Mon Sep 17 00:00:00 2001 From: Baptiste Darthenay Date: Tue, 27 Dec 2016 20:33:59 +0100 Subject: Rename postload.sh to postactivate.sh. This is closer to the virtualenvwrapper naming. --- README.md | 6 +++--- venvworkon.sh | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fc74104..3376c4b 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,9 @@ When `workon` is called, venvs are created or loaded from the To change this behavior, set the `WORKON_HOME` environment variable before sourcing `venvworkon.sh`. -## Post-load script -If the file `$HOME/.venvs//postload.sh` exists, it is -automatically sourced after the venv is loaded. This is useful +## Post-activate script +If the file `$HOME/.venvs//postactivate.sh` exists, it is +automatically sourced after the venv is activated. This is useful to export environment variable such as `DJANGO_SETTINGS_MODULE`. ## Why bother using this diff --git a/venvworkon.sh b/venvworkon.sh index adca644..f6ac4ef 100755 --- a/venvworkon.sh +++ b/venvworkon.sh @@ -26,8 +26,8 @@ workon() { source "$envdir/bin/activate" - if [ -e "$envdir/postload.sh" ] + if [ -e "$envdir/postactivate.sh" ] then - source "$envdir/postload.sh" + source "$envdir/postactivate.sh" fi } -- cgit v1.2.3 From dbbc7dc2950d6b565488864ee8ebf53a73d6e8eb Mon Sep 17 00:00:00 2001 From: Baptiste Darthenay Date: Wed, 28 Dec 2016 11:35:56 +0100 Subject: Jumps to project directory if exists. --- README.md | 5 +++++ venvworkon.sh | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 3376c4b..acbec3b 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,11 @@ If the file `$HOME/.venvs//postactivate.sh` exists, it is automatically sourced after the venv is activated. This is useful to export environment variable such as `DJANGO_SETTINGS_MODULE`. +## Project directory +If `PROJECT_HOME` is set (e.g. `$HOME/Projects`) and there is +a project folder which match the environment name, venvworkon +change the current working directory to the project directory. + ## Why bother using this `virtualenvwrapper` is certainly much more feature-packed and useful than this. But this wrapper is handy for development and diff --git a/venvworkon.sh b/venvworkon.sh index f6ac4ef..960048b 100755 --- a/venvworkon.sh +++ b/venvworkon.sh @@ -26,6 +26,11 @@ workon() { source "$envdir/bin/activate" + if [ -d "$PROJECT_HOME/$envname" ] + then + cd "$PROJECT_HOME/$envname" + fi + if [ -e "$envdir/postactivate.sh" ] then source "$envdir/postactivate.sh" -- cgit v1.2.3 From 84cf9821e256ee2458568e03a9548702b14af26b Mon Sep 17 00:00:00 2001 From: Baptiste Darthenay Date: Wed, 28 Dec 2016 12:02:43 +0100 Subject: Comments and Oneliners. --- venvworkon.sh | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/venvworkon.sh b/venvworkon.sh index 960048b..92890b6 100755 --- a/venvworkon.sh +++ b/venvworkon.sh @@ -1,38 +1,24 @@ workon() { - if [ -z "$WORKON_HOME" ] - then - WORKON_HOME="$HOME/.venv" - fi + # Sets WORKON_HOME to default if not set already + test -z "$WORKON_HOME" && WORKON_HOME="$HOME/.venv" - if [ ! -e "$WORKON_HOME" ] - then - mkdir -p "$WORKON_HOME" - fi + # Creates WORKON_HOME directory if not exists + test ! -e "$WORKON_HOME" && mkdir -p $_ - if [ "$#" -lt "1" ] - then - ls "$WORKON_HOME" - return - fi + # Quit if there is no arguments, while showing available venvs + test "$#" -lt "1" && ls "$WORKON_HOME"; return - envname=$1 + envdir="$WORKON_HOME/$1" - envdir="$WORKON_HOME/$envname" - - if [ ! -e "$envdir" ] - then - pyvenv "$envdir" - fi + # Creates virtual env if doesn't exists + test ! -e "$envdir" && pyvenv $_ && echo "Virtual env created in $_" source "$envdir/bin/activate" + unset envdir # VIRTUAL_ENV is now set by bin/activate - if [ -d "$PROJECT_HOME/$envname" ] - then - cd "$PROJECT_HOME/$envname" - fi + # Jumps to project directory if exists and PROJECT_HOME is set + test -d "$PROJECT_HOME/$(basename $VIRTUAL_ENV)" && cd $_ - if [ -e "$envdir/postactivate.sh" ] - then - source "$envdir/postactivate.sh" - fi + # Source postactivate.sh if exists in the virtual env + test -e "$VIRTUAL_ENV/postactivate.sh" && source $_ } -- cgit v1.2.3