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