Doug Hellmann avatar Doug Hellmann committed 29359ae

minor code cleanup

Comments (0)

Files changed (3)

docsource/history.rst

 Release History
 ===============
 
+1.20
+
+  - Incorporate lssitepackages feature from Sander Smits.
+  - Refactor some of the functions that were using copy-and-paste code to build path names.
+  - Add a few tests.
+
 1.19
 
   - Fix problem with add2virtualenv and relative paths. Thanks to Doug Latornell for the bug report James Bennett for the suggested fix.
     assertTrue "Environment directory was not created" "[ -d $WORKON_HOME/env1 ]"
 }
 
+test_get_python_version() {
+    expected=$(python -V 2>&1 | cut -f2 -d' ' | cut -f-2 -d.)
+    actual=$(virtualenvwrapper_get_python_version)
+    assertSame "$expected" "$actual"
+}
+
 test_cdvirtual() {
     pushd "$(pwd)" >/dev/null
     cdvirtualenv
 
 test_lssitepackages () {
     mkvirtualenv "lssitepackagestest"
-    assertTrue "lssitepackages"
+    contents="$(lssitepackages)"    
+    assertTrue "No easy-install.pth in $contents" "echo $contents | grep easy-install.pth"
 }
 
+test_lssitepackages_add2virtualenv () {
+    mkvirtualenv "lssitepackagestest"
+    parent_dir=$(dirname $(pwd))
+    base_dir=$(basename $(pwd))
+    add2virtualenv "../$base_dir"
+    contents="$(lssitepackages)"    
+    assertTrue "No $base_dir in $contents" "echo $contents | grep $base_dir"
+}
+
+
 . "$test_dir/shunit2"

virtualenvwrapper_bashrc

     return 0
 }
 
+# Run a hook script in the current shell
 function virtualenvwrapper_source_hook () {
     scriptname="$1"
     if [ -f "$scriptname" ]
     fi
 }
 
+# Run a hook script in its own shell
 function virtualenvwrapper_run_hook () {
     scriptname="$1"
     shift
     compctl -g "`virtualenvwrapper_show_workon_options`" workon rmvirtualenv
 fi
 
+# Prints the Python version string for the current interpreter.
+function virtualenvwrapper_get_python_version () {
+    python -c 'import sys; print ".".join(str(p) for p in sys.version_info[:2])'
+}
+
+# Prints the path to the site-packages directory for the current environment.
+function virtualenvwrapper_get_site_packages_dir () {
+    echo "$VIRTUAL_ENV/lib/python`virtualenvwrapper_get_python_version`/site-packages"    
+}
+
 # Path management for packages outside of the virtual env.
 # Based on a contribution from James Bennett and Jannis Leidel.
 #
 # "virtualenv_path_extensions.pth" inside the virtualenv's
 # site-packages directory; if this file does not exist, it will be
 # created first.
-#
 function add2virtualenv () {
 
     virtualenvwrapper_verify_active_environment || return 1
     
-    pyvers="`python -c 'import sys; print sys.version[:3]'`"
-    site_packages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
+    site_packages="`virtualenvwrapper_get_site_packages_dir`"
     
     if [ ! -d "${site_packages}" ]
     then
     return 0
 }
 
-#
-# cdsitepackages
-#
 # Does a ``cd`` to the site-packages directory of the currently-active
 # virtualenv.
-#
-
 function cdsitepackages () {
     virtualenvwrapper_verify_active_environment || return 1
-    pyvers="`python -c 'import sys; print sys.version[:3]'`"
-    site_packages="lib/python${pyvers}/site-packages"
-    cdvirtualenv $site_packages
+    site_packages="`virtualenvwrapper_get_site_packages_dir`"
+    cd "$site_packages"
 }
 
-#
-# cdvirtualenv
-#
 # Does a ``cd`` to the root of the currently-active virtualenv.
-#
-
 function cdvirtualenv () {
     virtualenvwrapper_verify_active_environment || return 1
     cd $VIRTUAL_ENV/$1
 }
 
-#
-# lssitepackages
-# 
 # Shows the content of the site-packages directory of the currently-active
 # virtualenv
-#
-
 function lssitepackages () {
     virtualenvwrapper_verify_active_environment || return 1
-    pyvers="`python -c 'import sys; print sys.version[:3]'`"
-    site_packages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
-    echo "$site_packages"
-    ls -l $site_packages
+    site_packages="`virtualenvwrapper_get_site_packages_dir`"
+    ls $@ $site_packages
     
     path_file="$site_packages/virtualenv_path_extensions.pth"
     if [ -f "$path_file" ]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.