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