barberj avatar barberj committed ade941b Merge

Add importvirtualenv feature.
Includes documentation and a fix for requirements passing.

Comments (0)

Files changed (3)

docs/en/command_ref.rst

    * :ref:`scripts-premkvirtualenv`
    * :ref:`scripts-postmkvirtualenv`
 
+.. _command-importvirtualenv:
+
+importvirtualenv
+------------
+
+Duplicate an existing virtual environment not already in the WORKON_HOME.
+
+Syntax::
+
+    importvirtualenv ENVPATH [TARGETENVNAME]
+
+TARGETENVNAME
+  If ommited the new virtual environment will match the name of virtual enviroment being imported. Existing virtual environment is not moved during import.
+
+.. note::
+
+   The environment created by the import operation is made `relocatable
+   <http://virtualenv.openplans.org/#making-environments-relocatable>`__.
+
+::
+
+    $ importvirtualenv requests_env requests
+    Importing /Users/jbarber/shootq/src/requests_env as requests...
+    Script /Users/jbarber/shootq/src/requests_env/bin/activate.csh cannot be made relative (it's not a normal script that starts with #!/Users/jbarber/shootq/src/requests_env/bin/python)
+    Script /Users/jbarber/shootq/src/requests_env/bin/activate.fish cannot be made relative (it's not a normal script that starts with #!/Users/jbarber/shootq/src/requests_env/bin/python)
+    Making script /Users/jbarber/shootq/src/requests_env/bin/easy_install relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/easy_install-2.7 relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/ipcluster relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/ipcontroller relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/ipengine relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/iplogger relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/iptest relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/ipython relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/ipython-qtconsole relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/irunner relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/nosetests relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/nosetests-2.7 relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/pip relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/pip-2.7 relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/pycolor relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/pygmentize relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/rst2html.py relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/rst2latex.py relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/rst2man.py relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/rst2odt.py relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/rst2odt_prepstyles.py relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/rst2pseudoxml.py relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/rst2s5.py relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/rst2xetex.py relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/rst2xml.py relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/rstpep2html.py relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/sphinx-apidoc relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/sphinx-autogen relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/sphinx-build relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/sphinx-quickstart relative
+    Making script /Users/jbarber/shootq/src/requests_env/bin/yolk relative
+    virtualenvwrapper.user_scripts creating /Users/jbarber/shootq/env/requests/bin/predeactivate
+    virtualenvwrapper.user_scripts creating /Users/jbarber/shootq/env/requests/bin/postdeactivate
+    virtualenvwrapper.user_scripts creating /Users/jbarber/shootq/env/requests/bin/preactivate
+    virtualenvwrapper.user_scripts creating /Users/jbarber/shootq/env/requests/bin/postactivate
+    virtualenvwrapper.user_scripts creating /Users/jbarber/shootq/env/requests/bin/get_env_details
+    (requests)$
+
 ==================================
 Controlling the Active Environment
 ==================================

tests/test_expandpath.sh

+#!/bin/sh
+
+test_dir=$(cd $(dirname $0) && pwd)
+source "$test_dir/setup.sh"
+TMP_WORKON_HOME="$WORKON_HOME"
+
+oneTimeSetUp() {
+    source "$test_dir/../virtualenvwrapper.sh"
+    echo $PYTHONPATH
+}
+
+oneTimeTearDown() {
+    return 0
+}
+
+test_tilde() {
+    assertSame "$HOME" "$(expandpath ~)"
+}
+
+test_vars() {
+    assertSame "$HOME" "$(expandpath $HOME)"
+}
+
+test_tilde_vars() {
+    assertSame "$HOME" "$(expandpath ~$USER)"
+}
+
+. "$test_dir/shunit2"

virtualenvwrapper.sh

     export VIRTUALENVWRAPPER_PROJECT_FILENAME=".project"
 fi
 
+function expandpath {
+        if [ "$1" = "" ]; then
+            return 1
+        else
+            "$VIRTUALENVWRAPPER_PYTHON" -c "import os,sys; sys.stdout.write(os.path.realpath(os.path.expandvars(os.path.expanduser(\"$1\")))+'\n')"
+            return 0
+        fi
+}
+
 function virtualenvwrapper_derive_workon_home {
     typeset workon_home_dir="$WORKON_HOME"
 
         # - Removing extra slashes (e.g., when TMPDIR ends in a slash)
         # - Expanding variables (e.g., $foo)
         # - Converting ~s to complete paths (e.g., ~/ to /home/brian/ and ~arthur to /home/arthur)
-        workon_home_dir=$("$VIRTUALENVWRAPPER_PYTHON" -c "import os,sys; sys.stdout.write(os.path.expandvars(os.path.expanduser(\"$workon_home_dir\"))+'\n')")
+        workon_home_dir=$(expandpath $workon_home_dir)
     fi
 
     echo "$workon_home_dir"
                 packages="$packages ${in_args[$i]}";;
             -r)
                 i=$(( $i + 1 ));
-                requirements="${in_args[$i]}";;
+                requirements="${in_args[$i]}";
+                requirements=$(expandpath $requirements);;
             *)
                 if [ ${#out_args} -gt 0 ]
                 then
     fi
 }
 
+function copy_virtualenv_directory {
+    typeset source="$1"
+    typeset target="$2"
+
+    \cp -r "$source" "$target"
+    for script in $( \ls $target/$VIRTUALENVWRAPPER_ENV_BIN_DIR/* )
+    do
+        newscript="$script-new"
+        \sed "s|$source|$target|g" < "$script" > "$newscript"
+        \mv "$newscript" "$script"
+        \chmod a+x "$script"
+    done
+
+    "$VIRTUALENVWRAPPER_VIRTUALENV" "$target" --relocatable
+    \sed "s|$source|$target|g" < "$source/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate" > "$target/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate"
+
+    return 0
+}
+
 # Duplicate the named virtualenv to make a new one.
 function cpvirtualenv {
     typeset env_name="$1"
         echo "Please specify target virtualenv"
         return 1
     fi
-    if echo "$WORKON_HOME" | (unset GREP_OPTIONS; \grep "/$" > /dev/null)
-    then
-        typeset env_home="$WORKON_HOME"
-    else
-        typeset env_home="$WORKON_HOME/"
-    fi
-    typeset source_env="$env_home$env_name"
-    typeset target_env="$env_home$new_env"
+
+    typeset source_env="$WORKON_HOME/$env_name"
+    typeset target_env="$WORKON_HOME/$new_env"
 
     if [ ! -e "$source_env" ]
     then
         echo "$env_name virtualenv doesn't exist"
         return 1
     fi
+    # validate new_env does not already exist
+    if [ -e "$target_env" ]
+    then
+        echo "$new_env virtualenv already exists."
+        return 1
+    fi
 
-    \cp -r "$source_env" "$target_env"
-    for script in $( \ls $target_env/$VIRTUALENVWRAPPER_ENV_BIN_DIR/* )
-    do
-        newscript="$script-new"
-        \sed "s|$source_env|$target_env|g" < "$script" > "$newscript"
-        \mv "$newscript" "$script"
-        \chmod a+x "$script"
-    done
-
-    "$VIRTUALENVWRAPPER_VIRTUALENV" "$target_env" --relocatable
-    \sed "s/VIRTUAL_ENV\(.*\)$env_name/VIRTUAL_ENV\1$new_env/g" < "$source_env/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate" > "$target_env/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate"
+    copy_virtualenv_directory "$source_env" "$target_env"
 
     (\cd "$WORKON_HOME" && (
         virtualenvwrapper_run_hook "pre_cpvirtualenv" "$env_name" "$new_env";
     virtualenvwrapper_run_hook "post_cpvirtualenv"
 }
 
+# Import virtualenv into WORKON_HOME
+# adding the hook scripts 
+function importvirtualenv {
+    # no point in doing anything if no virtualenv
+    virtualenvwrapper_verify_virtualenv || return 1
+
+    # source path for the virtualenv to import
+    typeset srcvirtualenv=$(expandpath "$1")
+    # second parameter is optional,
+    # its the target environment name
+    # incase they want it different 
+    # then the orignal
+    typeset envname="$2"
+
+    # validate we got a source path
+    if [ "$srcvirtualenv" = "" ] || [ ! -e "$srcvirtualenv" ]
+    then
+        echo "$srcvirtualenv virtualenv doesn't exist"
+        echo "You must provide a valid path to a virtualenv to import"
+        return 1
+    fi
+
+    # get the base name for the envname to setup
+    if [ "$envname" = "" ]; then
+        envname=$(basename $srcvirtualenv)
+    fi
+    targetenv=$WORKON_HOME/$envname
+
+    # validate envname does not already exist
+    if [ -d "$targetenv" ]
+    then
+        echo "$envname virtualenv already exists." >&2
+        return 1
+    fi
+
+    echo "Importing $srcvirtualenv as $envname..."
+
+    copy_virtualenv_directory "$srcvirtualenv" "$targetenv"
+
+    # need to add user scripts
+    virtualenvwrapper_run_hook "pre_mkvirtualenv" "$envname"
+
+    workon "$envname"
+}
+
 #
 # virtualenvwrapper project functions
 #
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.