Browse code

Teach pip_install() about virtual envs

Set PIP_VIRTUAL_ENV to install the package(s) into an existing virtual
environment. This works by simply using the pip command already
in the venv, and not using sudo.

Change-Id: I910e1752e58a666174f83b4f97e547851e66e655

Dean Troyer authored on 2015/02/12 08:01:02
Showing 1 changed files
... ...
@@ -31,6 +31,13 @@ function get_pip_command {
31 31
 # Get the path to the direcotry where python executables are installed.
32 32
 # get_python_exec_prefix
33 33
 function get_python_exec_prefix {
34
+    local xtrace=$(set +o | grep xtrace)
35
+    set +o xtrace
36
+    if [[ -z "$os_PACKAGE" ]]; then
37
+        GetOSVersion
38
+    fi
39
+    $xtrace
40
+
34 41
     if is_fedora || is_suse; then
35 42
         echo "/usr/bin"
36 43
     else
... ...
@@ -39,8 +46,8 @@ function get_python_exec_prefix {
39 39
 }
40 40
 
41 41
 # Wrapper for ``pip install`` to set cache and proxy environment variables
42
-# Uses globals ``INSTALL_TESTONLY_PACKAGES``, ``OFFLINE``, ``TRACK_DEPENDS``,
43
-# ``*_proxy``
42
+# Uses globals ``INSTALL_TESTONLY_PACKAGES``, ``OFFLINE``, ``PIP_VIRTUAL_ENV``,
43
+# ``TRACK_DEPENDS``, ``*_proxy``
44 44
 # pip_install package [package ...]
45 45
 function pip_install {
46 46
     local xtrace=$(set +o | grep xtrace)
... ...
@@ -62,8 +69,13 @@ function pip_install {
62 62
         local cmd_pip=$DEST/.venv/bin/pip
63 63
         local sudo_pip="env"
64 64
     else
65
-        local cmd_pip=$(get_pip_command)
66
-        local sudo_pip="sudo -H"
65
+        if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
66
+            local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
67
+            local sudo_pip="env"
68
+        else
69
+            local cmd_pip=$(get_pip_command)
70
+            local sudo_pip="sudo -H"
71
+        fi
67 72
     fi
68 73
 
69 74
     local pip_version=$(python -c "import pip; \
... ...
@@ -93,6 +105,7 @@ function pip_install {
93 93
                 -r $test_req
94 94
         fi
95 95
     fi
96
+    $xtrace
96 97
 }
97 98
 
98 99
 # should we use this library from their git repo, or should we let it