4747# Locate the global Python where virtualenvwrapper is installed.
4848if [ " $VIRTUALENVWRAPPER_PYTHON " = " " ]
4949then
50- VIRTUALENVWRAPPER_PYTHON=" $( \w hich python) "
50+ VIRTUALENVWRAPPER_PYTHON=" $( command \w hich python) "
5151fi
5252
5353# Set the name of the virtualenv app to use.
7878 export VIRTUALENVWRAPPER_PROJECT_FILENAME=" .project"
7979fi
8080
81+ # Portable shell scripting is hard, let's go shopping.
82+ #
83+ # People insist on aliasing commands like 'cd', either with a real
84+ # alias or even a shell function. Under bash and zsh, "builtin" forces
85+ # the use of a command that is part of the shell itself instead of an
86+ # alias, function, or external command, while "command" does something
87+ # similar but allows external commands. Under ksh "builtin" registers
88+ # a new command from a shared library, but "command" will pick up
89+ # existing builtin commands. We need to use a builtin for cd because
90+ # we are trying to change the state of the current shell, so we use
91+ # "builtin" for bash and zsh but "command" under ksh.
92+ function __virtualenvwrapper_cd {
93+ if [ -n " $BASH " ]
94+ then
95+ builtin \c d " $@ "
96+ elif [ -n " $ZSH_VERSION " ]
97+ then
98+ builtin \c d " $@ "
99+ else
100+ command \c d " $@ "
101+ fi
102+ }
103+
81104function virtualenvwrapper_expandpath {
82105 if [ " $1 " = " " ]; then
83106 return 1
@@ -99,7 +122,7 @@ function virtualenvwrapper_derive_workon_home {
99122
100123 # If the path is relative, prefix it with $HOME
101124 # (note: for compatibility)
102- if echo " $workon_home_dir " | (unset GREP_OPTIONS; \g rep ' ^[^/~]' > /dev/null)
125+ if echo " $workon_home_dir " | (unset GREP_OPTIONS; command \g rep ' ^[^/~]' > /dev/null)
103126 then
104127 workon_home_dir=" $HOME /$WORKON_HOME "
105128 fi
@@ -108,7 +131,7 @@ function virtualenvwrapper_derive_workon_home {
108131 # path might contain stuff to expand.
109132 # (it might be possible to do this in shell, but I don't know a
110133 # cross-shell-safe way of doing it -wolever)
111- if echo " $workon_home_dir " | (unset GREP_OPTIONS; \e grep ' ([\$~]|//)' > /dev/null)
134+ if echo " $workon_home_dir " | (unset GREP_OPTIONS; command \e grep ' ([\$~]|//)' > /dev/null)
112135 then
113136 # This will normalize the path by:
114137 # - Removing extra slashes (e.g., when TMPDIR ends in a slash)
@@ -141,13 +164,19 @@ function virtualenvwrapper_verify_workon_home {
141164
142165# HOOK_VERBOSE_OPTION="-q"
143166
167+ # Function to wrap mktemp so tests can replace it for error condition
168+ # testing.
169+ function __virtualenvwrapper_mktemp {
170+ command \m ktemp " $@ "
171+ }
172+
144173# Expects 1 argument, the suffix for the new file.
145174function virtualenvwrapper_tempfile {
146175 # Note: the 'X's must come last
147176 typeset suffix=${1:- hook}
148177 typeset file
149178
150- file=" ` \m ktemp -t virtualenvwrapper-$suffix -XXXXXXXXXX` "
179+ file=" $( __virtualenvwrapper_mktemp -t virtualenvwrapper-$suffix -XXXXXXXXXX) "
151180 if [ $? -ne 0 ] || [ -z " $file " ] || [ ! -f " $file " ]
152181 then
153182 echo " ERROR: virtualenvwrapper could not create a temporary file name." 1>&2
@@ -167,7 +196,7 @@ function virtualenvwrapper_run_hook {
167196 if [ -z " $VIRTUALENVWRAPPER_LOG_DIR " ]
168197 then
169198 echo " ERROR: VIRTUALENVWRAPPER_LOG_DIR is not set." 1>&2
170- \r m -f " $hook_script "
199+ command \r m -f " $hook_script "
171200 return 1
172201 fi
173202 " $VIRTUALENVWRAPPER_PYTHON " -c ' from virtualenvwrapper.hook_loader import main; main()' $HOOK_VERBOSE_OPTION --script " $hook_script " " $@ "
@@ -178,7 +207,7 @@ function virtualenvwrapper_run_hook {
178207 if [ ! -f " $hook_script " ]
179208 then
180209 echo " ERROR: virtualenvwrapper_run_hook could not find temporary file $hook_script " 1>&2
181- \r m -f " $hook_script "
210+ command \r m -f " $hook_script "
182211 return 2
183212 fi
184213 # cat "$hook_script"
@@ -194,7 +223,7 @@ VIRTUALENVWRAPPER_PYTHON=$VIRTUALENVWRAPPER_PYTHON and that PATH is
194223set properly.
195224EOF
196225 fi
197- \r m -f " $hook_script "
226+ command \r m -f " $hook_script "
198227 return $result
199228}
200229
@@ -263,7 +292,7 @@ function virtualenvwrapper_initialize {
263292
264293# Verify that the passed resource is in path and exists
265294function virtualenvwrapper_verify_resource {
266- typeset exe_path=$( \w hich " $1 " | (unset GREP_OPTIONS; \g rep -v " not found" ))
295+ typeset exe_path=$( command \w hich " $1 " | (unset GREP_OPTIONS; command \g rep -v " not found" ))
267296 if [ " $exe_path " = " " ]
268297 then
269298 echo " ERROR: virtualenvwrapper could not find $1 in your path" >&2
@@ -395,7 +424,7 @@ function mkvirtualenv {
395424 virtualenvwrapper_verify_virtualenv || return 1
396425 (
397426 [ -n " $ZSH_VERSION " ] && setopt SH_WORD_SPLIT
398- \c d " $WORKON_HOME " &&
427+ __virtualenvwrapper_cd " $WORKON_HOME " &&
399428 " $VIRTUALENVWRAPPER_VIRTUALENV " $VIRTUALENVWRAPPER_VIRTUALENV_ARGS " $@ " &&
400429 [ -d " $WORKON_HOME /$envname " ] && \
401430 virtualenvwrapper_run_hook " pre_mkvirtualenv" " $envname "
@@ -456,16 +485,16 @@ function rmvirtualenv {
456485 # Move out of the current directory to one known to be
457486 # safe, in case we are inside the environment somewhere.
458487 typeset prior_dir=" $( pwd) "
459- \c d " $WORKON_HOME "
488+ __virtualenvwrapper_cd " $WORKON_HOME "
460489
461490 virtualenvwrapper_run_hook " pre_rmvirtualenv" " $env_name "
462- \r m -rf " $env_dir "
491+ command \r m -rf " $env_dir "
463492 virtualenvwrapper_run_hook " post_rmvirtualenv" " $env_name "
464493
465494 # If the directory we used to be in still exists, move back to it.
466495 if [ -d " $prior_dir " ]
467496 then
468- \c d " $prior_dir "
497+ __virtualenvwrapper_cd " $prior_dir "
469498 fi
470499 done
471500}
@@ -476,9 +505,7 @@ function virtualenvwrapper_show_workon_options {
476505 # NOTE: DO NOT use ls here because colorized versions spew control characters
477506 # into the output list.
478507 # echo seems a little faster than find, even with -depth 3.
479- (\c d " $WORKON_HOME " ; for f in * /$VIRTUALENVWRAPPER_ENV_BIN_DIR /activate; do echo $f ; done) 2> /dev/null | \s ed ' s|^\./||' | \s ed " s|/$VIRTUALENVWRAPPER_ENV_BIN_DIR /activate||" | \s ort | (unset GREP_OPTIONS; \e grep -v ' ^\*$' )
480-
481- # (\cd "$WORKON_HOME"; find -L . -depth 3 -path '*/bin/activate') | sed 's|^\./||' | sed 's|/bin/activate||' | sort
508+ (__virtualenvwrapper_cd " $WORKON_HOME " ; for f in * /$VIRTUALENVWRAPPER_ENV_BIN_DIR /activate; do echo $f ; done) 2> /dev/null | command \s ed ' s|^\./||' | command \s ed " s|/$VIRTUALENVWRAPPER_ENV_BIN_DIR /activate||" | command \s ort | (unset GREP_OPTIONS; command \e grep -v ' ^\*$' )
482509}
483510
484511function _lsvirtualenv_usage {
@@ -725,14 +752,14 @@ function cdsitepackages {
725752 virtualenvwrapper_verify_workon_home || return 1
726753 virtualenvwrapper_verify_active_environment || return 1
727754 typeset site_packages=" ` virtualenvwrapper_get_site_packages_dir` "
728- \c d " $site_packages " /$1
755+ __virtualenvwrapper_cd " $site_packages " /$1
729756}
730757
731758# Does a ``cd`` to the root of the currently-active virtualenv.
732759function cdvirtualenv {
733760 virtualenvwrapper_verify_workon_home || return 1
734761 virtualenvwrapper_verify_active_environment || return 1
735- \c d $VIRTUAL_ENV /$1
762+ __virtualenvwrapper_cd $VIRTUAL_ENV /$1
736763}
737764
738765# Shows the content of the site-packages directory of the currently-active
@@ -820,7 +847,7 @@ function cpvirtualenv {
820847 echo " Copying $src_name as $trg_name ..."
821848 (
822849 [ -n " $ZSH_VERSION " ] && setopt SH_WORD_SPLIT
823- \c d " $WORKON_HOME " &&
850+ __virtualenvwrapper_cd " $WORKON_HOME " &&
824851 " $VIRTUALENVWRAPPER_VIRTUALENV_CLONE " " $src " " $trg "
825852 [ -d " $trg " ] &&
826853 virtualenvwrapper_run_hook " pre_cpvirtualenv" " $src " " $trg_name " &&
0 commit comments