a75b4089cbba267de0e71d1b1a9be883fcf1819d

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

Message

Merge pull request #57 from MichaelAquilina/minor_improvements

Several improvements

Diff

This diff is truncated to protect this page.

  1diff --git a/README.rst b/README.rst
  2index 49332138a41c8f33e6350f9c9d2157e39c71320b..9b093554dc4aecb236f86d3c7aa1520da05be2b5 100644
  3--- a/README.rst
  4+++ b/README.rst
  5@@ -87,10 +87,7 @@ helper command.
  6 
  7     $ cd my-python-project
  8     $ mkvenv
  9-    Using real prefix '/usr'
 10-    New python executable in /home/michael/.virtualenvs/my-python-project/bin/python2
 11-    Also creating executable in /home/michael/.virtualenvs/my-python-project/bin/python
 12-    Installing setuptools, pip, wheel...done.
 13+    Creating my-python-project virtualenv
 14     Found a requirements.txt. Install? [y/N]:
 15     Collecting requests (from -r requirements.txt (line 1))
 16       Using cached requests-2.11.1-py2.py3-none-any.whl
 17@@ -166,6 +163,16 @@ This will delete the virtual environment in ``.venv`` and remove the
 18     $ rmvenv
 19     No .venv file in the current directory!
 20 
 21+disable_autoswitch_virtualenv
 22+'''''''''''''''''''''''''''''
 23+
 24+Temporarily disables autoswitching of virtualenvs when moving between
 25+directories.
 26+
 27+enable_autoswitch_virtualenv
 28+''''''''''''''''''''''''''''
 29+
 30+Re-enable autoswitching of virtualenvs (if it was previously disabled).
 31 
 32 Customising Messages
 33 --------------------
 34@@ -217,6 +224,14 @@ You can prevent verbose messages from being displayed when moving
 35 between directories. You can do this by setting ``AUTOSWITCH_SILENT`` to
 36 a non-empty value.
 37 
 38+**Choosing where virtualenvs are stored**
 39+
 40+By default, virtualenvs created are placed in ``$HOME/.virtualenvs`` - which is
 41+the same location that the ``virtualenvwrapper`` package uses.
 42+
 43+If you wish to change this to another location, simply set the value of the
 44+environment variable ``AUTOSWITCH_VIRTUAL_ENV_DIR``.
 45+
 46 Security Warnings
 47 -----------------
 48 
 49diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 50index 1a01b1c75f98063acc32ff13ad9b9d23af415be7..5b20ca4391be4edb97a307f2cd968511b71c8ecd 100644
 51--- a/autoswitch_virtualenv.plugin.zsh
 52+++ b/autoswitch_virtualenv.plugin.zsh
 53@@ -1,9 +1,5 @@
 54 export AUTOSWITCH_VERSION='1.1.1'
 55 
 56-# TODO: Possibly allow the user to modify this if they wish
 57-VIRTUAL_ENV_DIR="$HOME/.virtualenvs"
 58-mkdir -p "$VIRTUAL_ENV_DIR"
 59-
 60 RED="\e[31m"
 61 GREEN="\e[32m"
 62 PURPLE="\e[35m"
 63@@ -11,7 +7,6 @@ BOLD="\e[1m"
 64 NORMAL="\e[0m"
 65 
 66 
 67-
 68 if ! type "virtualenv" > /dev/null; then
 69     export DISABLE_AUTOSWITCH_VENV="1"
 70     printf "${BOLD}${RED}"
 71@@ -23,6 +18,13 @@ if ! type "virtualenv" > /dev/null; then
 72 fi
 73 
 74 
 75+function _virtual_env_dir() {
 76+    local VIRTUAL_ENV_DIR="${AUTOSWITCH_VIRTUAL_ENV_DIR:-$HOME/.virtualenvs}"
 77+    mkdir -p "$VIRTUAL_ENV_DIR"
 78+    printf "%s" "$VIRTUAL_ENV_DIR"
 79+}
 80+
 81+
 82 function _python_version() {
 83    PYTHON_BIN="$1"
 84    if [[ -f "$PYTHON_BIN" ]] then
 85@@ -42,7 +44,7 @@ function _maybeworkon() {
 86 
 87   if [[ -z "$VIRTUAL_ENV" || "$venv_name" != "$(basename $VIRTUAL_ENV)" ]]; then
 88      if [ -z "$AUTOSWITCH_SILENT" ]; then
 89-        py_version="$(_python_version "$VIRTUAL_ENV_DIR/$venv_name/bin/python")"
 90+        py_version="$(_python_version "$(_virtual_env_dir)/$venv_name/bin/python")"
 91 
 92         message="${AUTOSWITCH_MESSAGE_FORMAT:-"$DEFAULT_MESSAGE_FORMAT"}"
 93         message="${message//\%venv_type/$venv_type}"
 94@@ -52,7 +54,7 @@ function _maybeworkon() {
 95      fi
 96 
 97      # Much faster to source the activate file directly rather than use the `workon` command
 98-     source "$VIRTUAL_ENV_DIR/$venv_name/bin/activate"
 99+     source "$(_virtual_env_dir)/$venv_name/bin/activate"
100   fi
101 }
102 
103@@ -60,7 +62,7 @@ function _maybeworkon() {
104 # Gives the path to the nearest parent .venv file or nothing if it gets to root
105 function _check_venv_path()
106 {
107-    local check_dir=$1
108+    local check_dir="$1"
109 
110     if [[ -f "${check_dir}/.venv" ]]; then
111         printf "${check_dir}/.venv"
112@@ -101,11 +103,11 @@ function check_venv()
113           if [[ "$file_owner" != "$(id -u)" ]]; then
114             printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
115             printf "Reason: Found a .venv file but it is not owned by the current user\n"
116-            printf "Change ownership of $venv_path to '$USER' to fix this\n"
117+            printf "Change ownership of ${PURPLE}$venv_path${NORMAL} to ${PURPLE}'$USER'${NORMAL} to fix this\n"
118           elif ! [[ "$file_permissions" =~ ^[64][04][04]$ ]]; then
119             printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
120             printf "Reason: Found a .venv file with weak permission settings ($file_permissions).\n"
121-            printf "Run the following command to fix this: \"chmod 600 $venv_path\"\n"
122+            printf "Run the following command to fix this: ${PURPLE}\"chmod 600 $venv_path\"${NORMAL}\n"
123           else
124             SWITCH_TO="$(<"$venv_path")"
125           fi
126@@ -150,8 +152,8 @@ function rmvenv()
127         fi
128     fi
129 
130-    printf "Removing %s...\n" "$venv_name"
131-    rm -rf "$VIRTUAL_ENV_DIR/$venv_name"
132+    printf "Removing ${PURPLE}%s${NORMAL}...\n" "$venv_name"
133+    rm -rf "$(_virtual_env_dir)/$venv_name"
134     rm ".venv"
135   else
136     printf "No .venv file in the current directory!\n"
137@@ -168,8 +170,12 @@ function mkvenv()
138     venv_name="$(basename $PWD)"
139 
140     printf "Creating ${PURPLE}%s${NONE} virtualenv\n" "$venv_name"
141-    # TODO: Allow verbose option to remove suppressing details
142-    virtualenv $@ "$VIRTUAL_ENV_DIR/$venv_name" > /dev/null
143+
144+    if [[ ${@[(ie)--verbose]} -eq ${#@} ]]; then
145+        virtualenv $@ "$(_virtual_env_dir)/$venv_name"
146+    else
147+        virtualenv $@ "$(_virtual_env_dir)/$venv_name" > /dev/null
148+    fi
149 
150     printf "$venv_name\n" > ".venv"
151     chmod 600 .venv
152@@ -185,7 +191,7 @@ function install_requirements() {
153diff --git a/tests/test_check_venv.zunit b/tests/test_check_venv.zunit
154index b419c0159c532fd6dfd34d3ed5f9924ee5005f3a..532a7b45438a6bfe2a353c1543a1cca5db2e4ead 100644
155--- a/tests/test_check_venv.zunit
156+++ b/tests/test_check_venv.zunit
157@@ -39,7 +39,7 @@
158     assert "$lines[1]" same_as "AUTOSWITCH WARNING: Virtualenv will not be activated"
159     assert "$lines[2]" is_empty
160     assert "$lines[3]" same_as "Reason: Found a .venv file with weak permission settings (620)."
161-    assert "$lines[4]" same_as "Run the following command to fix this: \"chmod 600 $TARGET/.venv\""
162+    assert "$lines[4]" same_as "Run the following command to fix this: \e[35m\"chmod 600 $TARGET/.venv\"\e[0m"
163 }
164 
165 @test 'check_venv - Security warning for weak writeable by everyone permissions' {
166@@ -55,7 +55,7 @@
167     assert "$lines[1]" same_as "AUTOSWITCH WARNING: Virtualenv will not be activated"
168     assert "$lines[2]" is_empty
169     assert "$lines[3]" same_as "Reason: Found a .venv file with weak permission settings (622)."
170-    assert "$lines[4]" same_as "Run the following command to fix this: \"chmod 600 $TARGET/.venv\""
171+    assert "$lines[4]" same_as "Run the following command to fix this: \e[35m\"chmod 600 $TARGET/.venv\"\e[0m"
172 }
173 
174 @test 'check_venv - No security warning for readable by everyone permission' {
175diff --git a/tests/test_rmvenv.zunit b/tests/test_rmvenv.zunit
176index 2c8d53703304437d752e7a0c33b434e0bd27ccb4..9ef479905b18eedbd79371017b6ac79df7376081 100644
177--- a/tests/test_rmvenv.zunit
178+++ b/tests/test_rmvenv.zunit
179@@ -30,7 +30,7 @@
180     run rmvenv
181 
182     assert $state equals 0
183-    assert "$output" same_as "Removing 12345..."
184+    assert "$output" same_as "Removing \e[35m12345\e[0m..."
185     test ! -f .venv
186     test ! -d "$HOME/.virtualenvs/12345"
187 }
188@@ -42,7 +42,7 @@
189     run rmvenv
190 
191     assert $state equals 0
192-    assert "$output" same_as "Removing $test_venv..."
193+    assert "$output" same_as "Removing \e[35m$test_venv\e[0m..."
194     test ! -f .venv
195     test ! -d "$HOME/.virtualenvs/$test_venv"
196 }