Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # **inc/python** - Python-related functions |
| 4 | # |
| 5 | # Support for pip/setuptools interfaces and virtual environments |
| 6 | # |
| 7 | # External functions used: |
| 8 | # - GetOSVersion |
| 9 | # - is_fedora |
| 10 | # - is_suse |
| 11 | # - safe_chown |
| 12 | |
| 13 | # Save trace setting |
| 14 | INC_PY_TRACE=$(set +o | grep xtrace) |
| 15 | set +o xtrace |
| 16 | |
| 17 | |
| 18 | # Python Functions |
| 19 | # ================ |
| 20 | |
| 21 | # Get the path to the pip command. |
| 22 | # get_pip_command |
| 23 | function get_pip_command { |
| 24 | which pip || which pip-python |
| 25 | |
| 26 | if [ $? -ne 0 ]; then |
| 27 | die $LINENO "Unable to find pip; cannot continue" |
| 28 | fi |
| 29 | } |
| 30 | |
| 31 | # Get the path to the direcotry where python executables are installed. |
| 32 | # get_python_exec_prefix |
| 33 | function get_python_exec_prefix { |
Dean Troyer | 2b56476 | 2015-02-11 17:01:02 -0600 | [diff] [blame^] | 34 | local xtrace=$(set +o | grep xtrace) |
| 35 | set +o xtrace |
| 36 | if [[ -z "$os_PACKAGE" ]]; then |
| 37 | GetOSVersion |
| 38 | fi |
| 39 | $xtrace |
| 40 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 41 | if is_fedora || is_suse; then |
| 42 | echo "/usr/bin" |
| 43 | else |
| 44 | echo "/usr/local/bin" |
| 45 | fi |
| 46 | } |
| 47 | |
| 48 | # Wrapper for ``pip install`` to set cache and proxy environment variables |
Dean Troyer | 2b56476 | 2015-02-11 17:01:02 -0600 | [diff] [blame^] | 49 | # Uses globals ``INSTALL_TESTONLY_PACKAGES``, ``OFFLINE``, ``PIP_VIRTUAL_ENV``, |
| 50 | # ``TRACK_DEPENDS``, ``*_proxy`` |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 51 | # pip_install package [package ...] |
| 52 | function pip_install { |
| 53 | local xtrace=$(set +o | grep xtrace) |
| 54 | set +o xtrace |
| 55 | local offline=${OFFLINE:-False} |
| 56 | if [[ "$offline" == "True" || -z "$@" ]]; then |
| 57 | $xtrace |
| 58 | return |
| 59 | fi |
| 60 | |
| 61 | if [[ -z "$os_PACKAGE" ]]; then |
| 62 | GetOSVersion |
| 63 | fi |
| 64 | if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then |
| 65 | # TRACK_DEPENDS=True installation creates a circular dependency when |
| 66 | # we attempt to install virtualenv into a virualenv, so we must global |
| 67 | # that installation. |
| 68 | source $DEST/.venv/bin/activate |
| 69 | local cmd_pip=$DEST/.venv/bin/pip |
| 70 | local sudo_pip="env" |
| 71 | else |
Dean Troyer | 2b56476 | 2015-02-11 17:01:02 -0600 | [diff] [blame^] | 72 | if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then |
| 73 | local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip |
| 74 | local sudo_pip="env" |
| 75 | else |
| 76 | local cmd_pip=$(get_pip_command) |
| 77 | local sudo_pip="sudo -H" |
| 78 | fi |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 79 | fi |
| 80 | |
| 81 | local pip_version=$(python -c "import pip; \ |
| 82 | print(pip.__version__.strip('.')[0])") |
| 83 | if (( pip_version<6 )); then |
| 84 | die $LINENO "Currently installed pip version ${pip_version} does not" \ |
| 85 | "meet minimum requirements (>=6)." |
| 86 | fi |
| 87 | |
| 88 | $xtrace |
| 89 | $sudo_pip \ |
| 90 | http_proxy=${http_proxy:-} \ |
| 91 | https_proxy=${https_proxy:-} \ |
| 92 | no_proxy=${no_proxy:-} \ |
| 93 | $cmd_pip install \ |
| 94 | $@ |
| 95 | |
| 96 | INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES) |
| 97 | if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then |
| 98 | local test_req="$@/test-requirements.txt" |
| 99 | if [[ -e "$test_req" ]]; then |
| 100 | $sudo_pip \ |
| 101 | http_proxy=${http_proxy:-} \ |
| 102 | https_proxy=${https_proxy:-} \ |
| 103 | no_proxy=${no_proxy:-} \ |
| 104 | $cmd_pip install \ |
| 105 | -r $test_req |
| 106 | fi |
| 107 | fi |
Dean Troyer | 2b56476 | 2015-02-11 17:01:02 -0600 | [diff] [blame^] | 108 | $xtrace |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | # should we use this library from their git repo, or should we let it |
| 112 | # get pulled in via pip dependencies. |
| 113 | function use_library_from_git { |
| 114 | local name=$1 |
| 115 | local enabled=1 |
| 116 | [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0 |
| 117 | return $enabled |
| 118 | } |
| 119 | |
| 120 | # setup a library by name. If we are trying to use the library from |
| 121 | # git, we'll do a git based install, otherwise we'll punt and the |
| 122 | # library should be installed by a requirements pull from another |
| 123 | # project. |
| 124 | function setup_lib { |
| 125 | local name=$1 |
| 126 | local dir=${GITDIR[$name]} |
| 127 | setup_install $dir |
| 128 | } |
| 129 | |
| 130 | # setup a library by name in editiable mode. If we are trying to use |
| 131 | # the library from git, we'll do a git based install, otherwise we'll |
| 132 | # punt and the library should be installed by a requirements pull from |
| 133 | # another project. |
| 134 | # |
| 135 | # use this for non namespaced libraries |
| 136 | function setup_dev_lib { |
| 137 | local name=$1 |
| 138 | local dir=${GITDIR[$name]} |
| 139 | setup_develop $dir |
| 140 | } |
| 141 | |
| 142 | # this should be used if you want to install globally, all libraries should |
| 143 | # use this, especially *oslo* ones |
| 144 | function setup_install { |
| 145 | local project_dir=$1 |
| 146 | setup_package_with_req_sync $project_dir |
| 147 | } |
| 148 | |
| 149 | # this should be used for projects which run services, like all services |
| 150 | function setup_develop { |
| 151 | local project_dir=$1 |
| 152 | setup_package_with_req_sync $project_dir -e |
| 153 | } |
| 154 | |
| 155 | # determine if a project as specified by directory is in |
| 156 | # projects.txt. This will not be an exact match because we throw away |
| 157 | # the namespacing when we clone, but it should be good enough in all |
| 158 | # practical ways. |
| 159 | function is_in_projects_txt { |
| 160 | local project_dir=$1 |
| 161 | local project_name=$(basename $project_dir) |
| 162 | return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null |
| 163 | } |
| 164 | |
| 165 | # ``pip install -e`` the package, which processes the dependencies |
| 166 | # using pip before running `setup.py develop` |
| 167 | # |
| 168 | # Updates the dependencies in project_dir from the |
| 169 | # openstack/requirements global list before installing anything. |
| 170 | # |
| 171 | # Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS`` |
| 172 | # setup_develop directory |
| 173 | function setup_package_with_req_sync { |
| 174 | local project_dir=$1 |
| 175 | local flags=$2 |
| 176 | |
| 177 | # Don't update repo if local changes exist |
| 178 | # Don't use buggy "git diff --quiet" |
| 179 | # ``errexit`` requires us to trap the exit code when the repo is changed |
| 180 | local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed") |
| 181 | |
| 182 | if [[ $update_requirements != "changed" ]]; then |
| 183 | if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then |
| 184 | if is_in_projects_txt $project_dir; then |
| 185 | (cd $REQUIREMENTS_DIR; \ |
| 186 | python update.py $project_dir) |
| 187 | else |
| 188 | # soft update projects not found in requirements project.txt |
| 189 | (cd $REQUIREMENTS_DIR; \ |
| 190 | python update.py -s $project_dir) |
| 191 | fi |
| 192 | else |
| 193 | (cd $REQUIREMENTS_DIR; \ |
| 194 | python update.py $project_dir) |
| 195 | fi |
| 196 | fi |
| 197 | |
| 198 | setup_package $project_dir $flags |
| 199 | |
| 200 | # We've just gone and possibly modified the user's source tree in an |
| 201 | # automated way, which is considered bad form if it's a development |
| 202 | # tree because we've screwed up their next git checkin. So undo it. |
| 203 | # |
| 204 | # However... there are some circumstances, like running in the gate |
| 205 | # where we really really want the overridden version to stick. So provide |
| 206 | # a variable that tells us whether or not we should UNDO the requirements |
| 207 | # changes (this will be set to False in the OpenStack ci gate) |
| 208 | if [ $UNDO_REQUIREMENTS = "True" ]; then |
| 209 | if [[ $update_requirements != "changed" ]]; then |
| 210 | (cd $project_dir && git reset --hard) |
| 211 | fi |
| 212 | fi |
| 213 | } |
| 214 | |
| 215 | # ``pip install -e`` the package, which processes the dependencies |
| 216 | # using pip before running `setup.py develop` |
| 217 | # Uses globals ``STACK_USER`` |
| 218 | # setup_develop_no_requirements_update directory |
| 219 | function setup_package { |
| 220 | local project_dir=$1 |
| 221 | local flags=$2 |
| 222 | |
| 223 | pip_install $flags $project_dir |
| 224 | # ensure that further actions can do things like setup.py sdist |
| 225 | if [[ "$flags" == "-e" ]]; then |
| 226 | safe_chown -R $STACK_USER $1/*.egg-info |
| 227 | fi |
| 228 | } |
| 229 | |
| 230 | |
| 231 | # Restore xtrace |
| 232 | $INC_PY_TRACE |
| 233 | |
| 234 | # Local variables: |
| 235 | # mode: shell-script |
| 236 | # End: |