Skip to content

Commit 29359ae

Browse files
committed
minor code cleanup
1 parent 5fc101f commit 29359ae

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

docsource/history.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
Release History
33
===============
44

5+
1.20
6+
7+
- Incorporate lssitepackages feature from Sander Smits.
8+
- Refactor some of the functions that were using copy-and-paste code to build path names.
9+
- Add a few tests.
10+
511
1.19
612

713
- Fix problem with add2virtualenv and relative paths. Thanks to Doug Latornell for the bug report James Bennett for the suggested fix.

tests/test.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ test_mkvirtualenv() {
2626
assertTrue "Environment directory was not created" "[ -d $WORKON_HOME/env1 ]"
2727
}
2828

29+
test_get_python_version() {
30+
expected=$(python -V 2>&1 | cut -f2 -d' ' | cut -f-2 -d.)
31+
actual=$(virtualenvwrapper_get_python_version)
32+
assertSame "$expected" "$actual"
33+
}
34+
2935
test_cdvirtual() {
3036
pushd "$(pwd)" >/dev/null
3137
cdvirtualenv
@@ -174,7 +180,18 @@ test_add2virtualenv_relative () {
174180

175181
test_lssitepackages () {
176182
mkvirtualenv "lssitepackagestest"
177-
assertTrue "lssitepackages"
183+
contents="$(lssitepackages)"
184+
assertTrue "No easy-install.pth in $contents" "echo $contents | grep easy-install.pth"
178185
}
179186

187+
test_lssitepackages_add2virtualenv () {
188+
mkvirtualenv "lssitepackagestest"
189+
parent_dir=$(dirname $(pwd))
190+
base_dir=$(basename $(pwd))
191+
add2virtualenv "../$base_dir"
192+
contents="$(lssitepackages)"
193+
assertTrue "No $base_dir in $contents" "echo $contents | grep $base_dir"
194+
}
195+
196+
180197
. "$test_dir/shunit2"

virtualenvwrapper_bashrc

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function virtualenvwrapper_verify_active_environment () {
6363
return 0
6464
}
6565

66+
# Run a hook script in the current shell
6667
function virtualenvwrapper_source_hook () {
6768
scriptname="$1"
6869
if [ -f "$scriptname" ]
@@ -71,6 +72,7 @@ function virtualenvwrapper_source_hook () {
7172
fi
7273
}
7374

75+
# Run a hook script in its own shell
7476
function virtualenvwrapper_run_hook () {
7577
scriptname="$1"
7678
shift
@@ -219,6 +221,16 @@ elif [ -n "$ZSH_VERSION" ] ; then
219221
compctl -g "`virtualenvwrapper_show_workon_options`" workon rmvirtualenv
220222
fi
221223

224+
# Prints the Python version string for the current interpreter.
225+
function virtualenvwrapper_get_python_version () {
226+
python -c 'import sys; print ".".join(str(p) for p in sys.version_info[:2])'
227+
}
228+
229+
# Prints the path to the site-packages directory for the current environment.
230+
function virtualenvwrapper_get_site_packages_dir () {
231+
echo "$VIRTUAL_ENV/lib/python`virtualenvwrapper_get_python_version`/site-packages"
232+
}
233+
222234
# Path management for packages outside of the virtual env.
223235
# Based on a contribution from James Bennett and Jannis Leidel.
224236
#
@@ -230,13 +242,11 @@ fi
230242
# "virtualenv_path_extensions.pth" inside the virtualenv's
231243
# site-packages directory; if this file does not exist, it will be
232244
# created first.
233-
#
234245
function add2virtualenv () {
235246

236247
virtualenvwrapper_verify_active_environment || return 1
237248

238-
pyvers="`python -c 'import sys; print sys.version[:3]'`"
239-
site_packages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
249+
site_packages="`virtualenvwrapper_get_site_packages_dir`"
240250

241251
if [ ! -d "${site_packages}" ]
242252
then
@@ -271,44 +281,26 @@ function add2virtualenv () {
271281
return 0
272282
}
273283

274-
#
275-
# cdsitepackages
276-
#
277284
# Does a ``cd`` to the site-packages directory of the currently-active
278285
# virtualenv.
279-
#
280-
281286
function cdsitepackages () {
282287
virtualenvwrapper_verify_active_environment || return 1
283-
pyvers="`python -c 'import sys; print sys.version[:3]'`"
284-
site_packages="lib/python${pyvers}/site-packages"
285-
cdvirtualenv $site_packages
288+
site_packages="`virtualenvwrapper_get_site_packages_dir`"
289+
cd "$site_packages"
286290
}
287291

288-
#
289-
# cdvirtualenv
290-
#
291292
# Does a ``cd`` to the root of the currently-active virtualenv.
292-
#
293-
294293
function cdvirtualenv () {
295294
virtualenvwrapper_verify_active_environment || return 1
296295
cd $VIRTUAL_ENV/$1
297296
}
298297

299-
#
300-
# lssitepackages
301-
#
302298
# Shows the content of the site-packages directory of the currently-active
303299
# virtualenv
304-
#
305-
306300
function lssitepackages () {
307301
virtualenvwrapper_verify_active_environment || return 1
308-
pyvers="`python -c 'import sys; print sys.version[:3]'`"
309-
site_packages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
310-
echo "$site_packages"
311-
ls -l $site_packages
302+
site_packages="`virtualenvwrapper_get_site_packages_dir`"
303+
ls $@ $site_packages
312304

313305
path_file="$site_packages/virtualenv_path_extensions.pth"
314306
if [ -f "$path_file" ]

0 commit comments

Comments
 (0)