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