1. barberj
  2. virtualenvwrapper

Commits

barberj  committed ac7f9d2

Deduped code between cpvirtualenv and importvirtualenv

  • Participants
  • Parent commits 73d217d
  • Branches importvirtualenv

Comments (0)

Files changed (1)

File virtualenvwrapper.sh

View file
  • Ignore whitespace
 
 function expandpath {
         if [ "$1" == "" ]; then
-            echo "Nothing passed"
             return 1
         else
-            echo
             "$VIRTUALENVWRAPPER_PYTHON" -c "import os,sys; sys.stdout.write(os.path.realpath(os.path.expandvars(os.path.expanduser(\"$1\")))+'\n')"
             return 0
         fi
     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 "$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";
 # 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 targetenv="$2"
+    typeset envname="$2"
 
     # validate we got a source path
     if [ "$srcvirtualenv" = "" ] || [ ! -e "$srcvirtualenv" ]
     fi
 
     # get the base name for the envname to setup
-    typeset envname=$(basename $srcvirtualenv)
-    if [ "$targetenv" = "" ]; then
-        targetenv="$envname"
-    else
-        envname=$targetenv
-        targetenv=$WORKON_HOME/$envname
+    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..."
 
-    virtualenvwrapper_verify_virtualenv || return 1
-    \cp -r "$srcvirtualenv" "$targetenv"
-    for script in $( \ls $targetenv/$VIRTUALENVWRAPPER_ENV_BIN_DIR/* )
-    do
-        newscript="$script-new"
-        \sed "s|$srcvirtualenv|$targetenv|g" < "$script" > "$newscript"
-        \mv "$newscript" "$script"
-        \chmod a+x "$script"
-    done
-    "$VIRTUALENVWRAPPER_VIRTUALENV" "$srcvirtualenv" --relocatable 
-    \sed "s|$srcvirtualenv|$targetenv|g" < "$srcvirtualenv/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate" > "$targetenv/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate"
+    copy_virtualenv_directory "$srcvirtualenv" "$targetenv"
 
     # need to add user scripts
     virtualenvwrapper_run_hook "pre_mkvirtualenv" "$envname"