05ab38ba93c6d031c4aa087f2acb513c45fd6ad4

Author
Michael Aquilina <michaelaquilina@gmail.com>
Committer
Michael Aquilina <michaelaquilina@gmail.com>
Date

Message

Allow user to customize where virtualenvs are located

Diff

 1diff --git a/README.rst b/README.rst
 2index 49332138a41c8f33e6350f9c9d2157e39c71320b..f6d4bed7d5234bde49a62ecc15082191322e8a59 100644
 3--- a/README.rst
 4+++ b/README.rst
 5@@ -217,6 +217,14 @@ You can prevent verbose messages from being displayed when moving
 6 between directories. You can do this by setting ``AUTOSWITCH_SILENT`` to
 7 a non-empty value.
 8 
 9+**Choosing where virtualenvs are stored**
10+
11+By default, virtualenvs created are placed in ``$HOME/.virtualenvs`` - which is
12+the same location that the ``virtualenvwrapper`` package uses.
13+
14+If you wish to change this to another location, simply set the value of the
15+environment variable ``AUTOSWITCH_VIRTUAL_ENV_DIR``.
16+
17 Security Warnings
18 -----------------
19 
20diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
21index c3703b05d5784b37931b7dc3c8182dfda725a048..66803ab45e5cd3668f0460ad1bdc80721933d202 100644
22--- a/autoswitch_virtualenv.plugin.zsh
23+++ b/autoswitch_virtualenv.plugin.zsh
24@@ -1,9 +1,5 @@
25 export AUTOSWITCH_VERSION='1.1.1'
26 
27-# TODO: Possibly allow the user to modify this if they wish
28-VIRTUAL_ENV_DIR="$HOME/.virtualenvs"
29-mkdir -p "$VIRTUAL_ENV_DIR"
30-
31 RED="\e[31m"
32 GREEN="\e[32m"
33 PURPLE="\e[35m"
34@@ -22,6 +18,13 @@ if ! type "virtualenv" > /dev/null; then
35 fi
36 
37 
38+function _virtual_env_dir() {
39+    local VIRTUAL_ENV_DIR="${AUTOSWITCH_VIRTUAL_ENV_DIR:-$HOME/.virtualenvs}"
40+    mkdir -p "$VIRTUAL_ENV_DIR"
41+    printf "%s" "$VIRTUAL_ENV_DIR"
42+}
43+
44+
45 function _python_version() {
46    PYTHON_BIN="$1"
47    if [[ -f "$PYTHON_BIN" ]] then
48@@ -41,7 +44,7 @@ function _maybeworkon() {
49 
50   if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(basename $VIRTUAL_ENV)" ]]; then
51      if [ -z "$AUTOSWITCH_SILENT" ]; then
52-        py_version="$(_python_version "$VIRTUAL_ENV_DIR/$venv_name/bin/python")"
53+        py_version="$(_python_version "$(_virtual_env_dir)/$venv_name/bin/python")"
54 
55         message="${AUTOSWITCH_MESSAGE_FORMAT:-"$DEFAULT_MESSAGE_FORMAT"}"
56         message="${message//\%venv_type/$venv_type}"
57@@ -51,7 +54,7 @@ function _maybeworkon() {
58      fi
59 
60      # Much faster to source the activate file directly rather than use the `workon` command
61-     source "$VIRTUAL_ENV_DIR/$venv_name/bin/activate"
62+     source "$(_virtual_env_dir)/$venv_name/bin/activate"
63   fi
64 }
65 
66@@ -150,7 +153,7 @@ function rmvenv()
67     fi
68 
69     printf "Removing ${PURPLE}%s${NORMAL}...\n" "$venv_name"
70-    rm -rf "$VIRTUAL_ENV_DIR/$venv_name"
71+    rm -rf "$(_virtual_env_dir)/$venv_name"
72     rm ".venv"
73   else
74     printf "No .venv file in the current directory!\n"
75@@ -168,7 +171,7 @@ function mkvenv()
76 
77     printf "Creating ${PURPLE}%s${NONE} virtualenv\n" "$venv_name"
78     # TODO: Allow verbose option to remove suppressing details
79-    virtualenv $@ "$VIRTUAL_ENV_DIR/$venv_name" > /dev/null
80+    virtualenv $@ "$(_virtual_env_dir)/$venv_name" > /dev/null
81 
82     printf "$venv_name\n" > ".venv"
83     chmod 600 .venv