47
47
# Locate the global Python where virtualenvwrapper is installed.
48
48
if [ " $VIRTUALENVWRAPPER_PYTHON " = " " ]
49
49
then
50
- VIRTUALENVWRAPPER_PYTHON=" $( \w hich python) "
50
+ VIRTUALENVWRAPPER_PYTHON=" $( command \w hich python) "
51
51
fi
52
52
53
53
# Set the name of the virtualenv app to use.
78
78
export VIRTUALENVWRAPPER_PROJECT_FILENAME=" .project"
79
79
fi
80
80
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
+
81
104
function virtualenvwrapper_expandpath {
82
105
if [ " $1 " = " " ]; then
83
106
return 1
@@ -99,7 +122,7 @@ function virtualenvwrapper_derive_workon_home {
99
122
100
123
# If the path is relative, prefix it with $HOME
101
124
# (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)
103
126
then
104
127
workon_home_dir=" $HOME /$WORKON_HOME "
105
128
fi
@@ -108,7 +131,7 @@ function virtualenvwrapper_derive_workon_home {
108
131
# path might contain stuff to expand.
109
132
# (it might be possible to do this in shell, but I don't know a
110
133
# 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)
112
135
then
113
136
# This will normalize the path by:
114
137
# - Removing extra slashes (e.g., when TMPDIR ends in a slash)
@@ -141,13 +164,19 @@ function virtualenvwrapper_verify_workon_home {
141
164
142
165
# HOOK_VERBOSE_OPTION="-q"
143
166
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
+
144
173
# Expects 1 argument, the suffix for the new file.
145
174
function virtualenvwrapper_tempfile {
146
175
# Note: the 'X's must come last
147
176
typeset suffix=${1:- hook}
148
177
typeset file
149
178
150
- file=" ` \m ktemp -t virtualenvwrapper-$suffix -XXXXXXXXXX` "
179
+ file=" $( __virtualenvwrapper_mktemp -t virtualenvwrapper-$suffix -XXXXXXXXXX) "
151
180
if [ $? -ne 0 ] || [ -z " $file " ] || [ ! -f " $file " ]
152
181
then
153
182
echo " ERROR: virtualenvwrapper could not create a temporary file name." 1>&2
@@ -167,7 +196,7 @@ function virtualenvwrapper_run_hook {
167
196
if [ -z " $VIRTUALENVWRAPPER_LOG_DIR " ]
168
197
then
169
198
echo " ERROR: VIRTUALENVWRAPPER_LOG_DIR is not set." 1>&2
170
- \r m -f " $hook_script "
199
+ command \r m -f " $hook_script "
171
200
return 1
172
201
fi
173
202
" $VIRTUALENVWRAPPER_PYTHON " -c ' from virtualenvwrapper.hook_loader import main; main()' $HOOK_VERBOSE_OPTION --script " $hook_script " " $@ "
@@ -178,7 +207,7 @@ function virtualenvwrapper_run_hook {
178
207
if [ ! -f " $hook_script " ]
179
208
then
180
209
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 "
182
211
return 2
183
212
fi
184
213
# cat "$hook_script"
@@ -194,7 +223,7 @@ VIRTUALENVWRAPPER_PYTHON=$VIRTUALENVWRAPPER_PYTHON and that PATH is
194
223
set properly.
195
224
EOF
196
225
fi
197
- \r m -f " $hook_script "
226
+ command \r m -f " $hook_script "
198
227
return $result
199
228
}
200
229
@@ -263,7 +292,7 @@ function virtualenvwrapper_initialize {
263
292
264
293
# Verify that the passed resource is in path and exists
265
294
function 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" ))
267
296
if [ " $exe_path " = " " ]
268
297
then
269
298
echo " ERROR: virtualenvwrapper could not find $1 in your path" >&2
@@ -395,7 +424,7 @@ function mkvirtualenv {
395
424
virtualenvwrapper_verify_virtualenv || return 1
396
425
(
397
426
[ -n " $ZSH_VERSION " ] && setopt SH_WORD_SPLIT
398
- \c d " $WORKON_HOME " &&
427
+ __virtualenvwrapper_cd " $WORKON_HOME " &&
399
428
" $VIRTUALENVWRAPPER_VIRTUALENV " $VIRTUALENVWRAPPER_VIRTUALENV_ARGS " $@ " &&
400
429
[ -d " $WORKON_HOME /$envname " ] && \
401
430
virtualenvwrapper_run_hook " pre_mkvirtualenv" " $envname "
@@ -456,16 +485,16 @@ function rmvirtualenv {
456
485
# Move out of the current directory to one known to be
457
486
# safe, in case we are inside the environment somewhere.
458
487
typeset prior_dir=" $( pwd) "
459
- \c d " $WORKON_HOME "
488
+ __virtualenvwrapper_cd " $WORKON_HOME "
460
489
461
490
virtualenvwrapper_run_hook " pre_rmvirtualenv" " $env_name "
462
- \r m -rf " $env_dir "
491
+ command \r m -rf " $env_dir "
463
492
virtualenvwrapper_run_hook " post_rmvirtualenv" " $env_name "
464
493
465
494
# If the directory we used to be in still exists, move back to it.
466
495
if [ -d " $prior_dir " ]
467
496
then
468
- \c d " $prior_dir "
497
+ __virtualenvwrapper_cd " $prior_dir "
469
498
fi
470
499
done
471
500
}
@@ -476,9 +505,7 @@ function virtualenvwrapper_show_workon_options {
476
505
# NOTE: DO NOT use ls here because colorized versions spew control characters
477
506
# into the output list.
478
507
# 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 ' ^\*$' )
482
509
}
483
510
484
511
function _lsvirtualenv_usage {
@@ -725,14 +752,14 @@ function cdsitepackages {
725
752
virtualenvwrapper_verify_workon_home || return 1
726
753
virtualenvwrapper_verify_active_environment || return 1
727
754
typeset site_packages=" ` virtualenvwrapper_get_site_packages_dir` "
728
- \c d " $site_packages " /$1
755
+ __virtualenvwrapper_cd " $site_packages " /$1
729
756
}
730
757
731
758
# Does a ``cd`` to the root of the currently-active virtualenv.
732
759
function cdvirtualenv {
733
760
virtualenvwrapper_verify_workon_home || return 1
734
761
virtualenvwrapper_verify_active_environment || return 1
735
- \c d $VIRTUAL_ENV /$1
762
+ __virtualenvwrapper_cd $VIRTUAL_ENV /$1
736
763
}
737
764
738
765
# Shows the content of the site-packages directory of the currently-active
@@ -820,7 +847,7 @@ function cpvirtualenv {
820
847
echo " Copying $src_name as $trg_name ..."
821
848
(
822
849
[ -n " $ZSH_VERSION " ] && setopt SH_WORD_SPLIT
823
- \c d " $WORKON_HOME " &&
850
+ __virtualenvwrapper_cd " $WORKON_HOME " &&
824
851
" $VIRTUALENVWRAPPER_VIRTUALENV_CLONE " " $src " " $trg "
825
852
[ -d " $trg " ] &&
826
853
virtualenvwrapper_run_hook " pre_cpvirtualenv" " $src " " $trg_name " &&
0 commit comments