33378771429590a7b168c4d9888453ccaab69b06

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

Message

Add AUTOSWITCH_FILE variable

Diff

This diff is truncated to protect this page.

  1diff --git a/README.rst b/README.rst
  2index d43c0b8c2db5b514db6b29e23a291004e7f95cf2..e458ad990acf218a6084fcda6474147d405c8848 100644
  3--- a/README.rst
  4+++ b/README.rst
  5@@ -273,6 +273,18 @@ by setting the value of ``AUTOSWITCH_DEFAULT_PYTHON``. For example:
  6 You may still override this default as usual by passing the --python parameter to
  7 the mkvenv command.
  8 
  9+**Autoswitch file name**
 10+
 11+By default, the `.venv` file is searched for in each directory in order to tell if
 12+a virtualenv should be automatically activated.
 13+
 14+If this needs to be changed (e.g. it conflicts with something else) then it may be
 15+changed by setting the value of ``AUTOSWITCH_FILE``. For example:
 16+
 17+::
 18+
 19+    export AUTOSWITCH_FILE=".autoswitch"
 20+
 21 **Default requirements file**
 22 
 23 You may specify a default requirements file to use when creating a virtualenv by
 24diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 25index 6bdb8ea776e99fcedde524190051e20eca6b6ec7..2ac5d89a233cf30b76d17633e0c72360ddc0596e 100644
 26--- a/autoswitch_virtualenv.plugin.zsh
 27+++ b/autoswitch_virtualenv.plugin.zsh
 28@@ -1,4 +1,5 @@
 29 export AUTOSWITCH_VERSION='1.14.0'
 30+export AUTOSWITCH_FILE=".venv"
 31 
 32 RED="\e[31m"
 33 GREEN="\e[32m"
 34@@ -6,6 +7,7 @@ PURPLE="\e[35m"
 35 BOLD="\e[1m"
 36 NORMAL="\e[0m"
 37 
 38+
 39 function _virtual_env_dir() {
 40     local venv_name="$1"
 41     local VIRTUAL_ENV_DIR="${AUTOSWITCH_VIRTUAL_ENV_DIR:-$HOME/.virtualenvs}"
 42@@ -95,16 +97,16 @@ function _maybeworkon() {
 43 }
 44 
 45 
 46-# Gives the path to the nearest parent .venv file or nothing if it gets to root
 47+# Gives the path to the nearest parent $AUTOSWITCH_FILE or nothing if it gets to root
 48 function _check_venv_path()
 49 {
 50     local check_dir="$1"
 51 
 52-    if [[ -f "${check_dir}/.venv" ]]; then
 53-        printf "${check_dir}/.venv"
 54+    if [[ -f "${check_dir}/$AUTOSWITCH_FILE" ]]; then
 55+        printf "${check_dir}/$AUTOSWITCH_FILE"
 56         return
 57     else
 58-        # Abort search at file system root or HOME directory (latter is a perfomance optimisation).
 59+        # Abort search at file system root or HOME directory (latter is a performance optimisation).
 60         if [[ "$check_dir" = "/" || "$check_dir" = "$HOME" ]]; then
 61             return
 62         fi
 63@@ -113,13 +115,13 @@ function _check_venv_path()
 64 }
 65 
 66 
 67-# Automatically switch virtualenv when .venv file detected
 68+# Automatically switch virtualenv when $AUTOSWITCH_FILE file detected
 69 function check_venv()
 70 {
 71     local file_owner
 72     local file_permissions
 73 
 74-    # Get the .venv file, scanning parent directories
 75+    # Get the $AUTOSWITCH_FILE, scanning parent directories
 76     local venv_path=$(_check_venv_path "$PWD")
 77 
 78     if [[ -n "$venv_path" ]]; then
 79@@ -135,11 +137,11 @@ function check_venv()
 80 
 81         if [[ "$file_owner" != "$(id -u)" ]]; then
 82             printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
 83-            printf "Reason: Found a .venv file but it is not owned by the current user\n"
 84+            printf "Reason: Found a $AUTOSWITCH_FILE file but it is not owned by the current user\n"
 85             printf "Change ownership of ${PURPLE}$venv_path${NORMAL} to ${PURPLE}'$USER'${NORMAL} to fix this\n"
 86         elif ! [[ "$file_permissions" =~ ^[64][04][04]$ ]]; then
 87             printf "AUTOSWITCH WARNING: Virtualenv will not be activated\n\n"
 88-            printf "Reason: Found a .venv file with weak permission settings ($file_permissions).\n"
 89+            printf "Reason: Found a $AUTOSWITCH_FILE file with weak permission settings ($file_permissions).\n"
 90             printf "Run the following command to fix this: ${PURPLE}\"chmod 600 $venv_path\"${NORMAL}\n"
 91         else
 92             local switch_to="$(<"$venv_path")"
 93@@ -186,8 +188,8 @@ function _default_venv()
 94 # remove virtual environment for current directory
 95 function rmvenv()
 96 {
 97-    if [[ -f ".venv" ]]; then
 98-        local venv_name="$(<.venv)"
 99+    if [[ -f "$AUTOSWITCH_FILE" ]]; then
100+        local venv_name="$(<$AUTOSWITCH_FILE)"
101 
102         # detect if we need to switch virtualenv first
103         if [[ -n "$VIRTUAL_ENV" ]]; then
104@@ -203,9 +205,9 @@ function rmvenv()
105         # https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s04.html
106         # https://www.freedesktop.org/wiki/Software/systemd/TheCaseForTheUsrMerge/
107         /bin/rm -rf "$(_virtual_env_dir "$venv_name")"
108-        /bin/rm ".venv"
109+        /bin/rm "$AUTOSWITCH_FILE"
110     else
111-        printf "No .venv file in the current directory!\n"
112+        printf "No $AUTOSWITCH_FILE file in the current directory!\n"
113     fi
114 }
115 
116@@ -223,8 +225,8 @@ function mkvenv()
117         return
118     fi
119 
120-    if [[ -f ".venv" ]]; then
121-        printf ".venv file already exists. If this is a mistake use the rmvenv command\n"
122+    if [[ -f "$AUTOSWITCH_FILE" ]]; then
123+        printf "$AUTOSWITCH_FILE file already exists. If this is a mistake use the rmvenv command\n"
124     else
125         local venv_name="$(basename $PWD)"
126 
127@@ -243,8 +245,8 @@ function mkvenv()