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 { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 41 | local xtrace |
| 42 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | 2b56476 | 2015-02-11 17:01:02 -0600 | [diff] [blame] | 43 | set +o xtrace |
| 44 | if [[ -z "$os_PACKAGE" ]]; then |
| 45 | GetOSVersion |
| 46 | fi |
| 47 | $xtrace |
| 48 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 49 | if is_fedora || is_suse; then |
| 50 | echo "/usr/bin" |
| 51 | else |
| 52 | echo "/usr/local/bin" |
| 53 | fi |
| 54 | } |
| 55 | |
Sean Dague | 60996b1 | 2015-04-08 09:06:49 -0400 | [diff] [blame] | 56 | # Wrapper for ``pip install`` that only installs versions of libraries |
| 57 | # from the global-requirements specification. |
| 58 | # |
| 59 | # Uses globals ``REQUIREMENTS_DIR`` |
| 60 | # |
| 61 | # pip_install_gr packagename |
| 62 | function pip_install_gr { |
| 63 | local name=$1 |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 64 | local clean_name |
| 65 | clean_name=$(get_from_global_requirements $name) |
Sean Dague | 60996b1 | 2015-04-08 09:06:49 -0400 | [diff] [blame] | 66 | pip_install $clean_name |
| 67 | } |
| 68 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 69 | # Wrapper for ``pip install`` to set cache and proxy environment variables |
Dean Troyer | 41d6f85 | 2015-03-25 22:42:46 -0500 | [diff] [blame] | 70 | # Uses globals ``OFFLINE``, ``PIP_VIRTUAL_ENV``, |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 71 | # ``PIP_UPGRADE``, ``TRACK_DEPENDS``, ``*_proxy``, |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 72 | # pip_install package [package ...] |
| 73 | function pip_install { |
Ian Wienand | 433a9b1 | 2015-10-07 13:29:31 +1100 | [diff] [blame] | 74 | local xtrace |
| 75 | xtrace=$(set +o | grep xtrace) |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 76 | set +o xtrace |
Chris Dent | ebdd9ac | 2015-03-04 12:35:14 +0000 | [diff] [blame] | 77 | local upgrade="" |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 78 | local offline=${OFFLINE:-False} |
| 79 | if [[ "$offline" == "True" || -z "$@" ]]; then |
| 80 | $xtrace |
| 81 | return |
| 82 | fi |
| 83 | |
Sean Dague | cb658fa | 2015-10-08 17:12:03 -0400 | [diff] [blame] | 84 | time_start "pip_install" |
| 85 | |
Chris Dent | ebdd9ac | 2015-03-04 12:35:14 +0000 | [diff] [blame] | 86 | PIP_UPGRADE=$(trueorfalse False PIP_UPGRADE) |
| 87 | if [[ "$PIP_UPGRADE" = "True" ]] ; then |
| 88 | upgrade="--upgrade" |
| 89 | fi |
| 90 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 91 | if [[ -z "$os_PACKAGE" ]]; then |
| 92 | GetOSVersion |
| 93 | fi |
| 94 | if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then |
| 95 | # TRACK_DEPENDS=True installation creates a circular dependency when |
| 96 | # we attempt to install virtualenv into a virualenv, so we must global |
| 97 | # that installation. |
| 98 | source $DEST/.venv/bin/activate |
| 99 | local cmd_pip=$DEST/.venv/bin/pip |
| 100 | local sudo_pip="env" |
| 101 | else |
Dean Troyer | 2b56476 | 2015-02-11 17:01:02 -0600 | [diff] [blame] | 102 | if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then |
| 103 | local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip |
| 104 | local sudo_pip="env" |
| 105 | else |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 106 | local cmd_pip |
| 107 | cmd_pip=$(get_pip_command) |
Dean Troyer | 2b56476 | 2015-02-11 17:01:02 -0600 | [diff] [blame] | 108 | local sudo_pip="sudo -H" |
| 109 | fi |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 110 | fi |
| 111 | |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 112 | cmd_pip="$cmd_pip install" |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 113 | # Always apply constraints |
| 114 | cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt" |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 115 | |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 116 | local pip_version |
| 117 | pip_version=$(python -c "import pip; \ |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 118 | print(pip.__version__.strip('.')[0])") |
| 119 | if (( pip_version<6 )); then |
| 120 | die $LINENO "Currently installed pip version ${pip_version} does not" \ |
| 121 | "meet minimum requirements (>=6)." |
| 122 | fi |
| 123 | |
| 124 | $xtrace |
| 125 | $sudo_pip \ |
Eli Qiao | 6a83c42 | 2015-03-17 16:54:16 +0800 | [diff] [blame] | 126 | http_proxy="${http_proxy:-}" \ |
| 127 | https_proxy="${https_proxy:-}" \ |
| 128 | no_proxy="${no_proxy:-}" \ |
Joe Gordon | cd8824a | 2015-03-04 16:40:19 -0800 | [diff] [blame] | 129 | PIP_FIND_LINKS=$PIP_FIND_LINKS \ |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 130 | $cmd_pip $upgrade \ |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 131 | $@ |
| 132 | |
Sean Dague | eeb7bda | 2015-03-25 11:55:32 -0400 | [diff] [blame] | 133 | # Also install test requirements |
Sirushti Murugesan | 713fd2f | 2015-09-30 15:12:50 +0530 | [diff] [blame] | 134 | local test_req="${!#}/test-requirements.txt" |
Sean Dague | eeb7bda | 2015-03-25 11:55:32 -0400 | [diff] [blame] | 135 | if [[ -e "$test_req" ]]; then |
| 136 | echo "Installing test-requirements for $test_req" |
| 137 | $sudo_pip \ |
| 138 | http_proxy=${http_proxy:-} \ |
| 139 | https_proxy=${https_proxy:-} \ |
| 140 | no_proxy=${no_proxy:-} \ |
| 141 | PIP_FIND_LINKS=$PIP_FIND_LINKS \ |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 142 | $cmd_pip $upgrade \ |
Sean Dague | eeb7bda | 2015-03-25 11:55:32 -0400 | [diff] [blame] | 143 | -r $test_req |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 144 | fi |
Sean Dague | cb658fa | 2015-10-08 17:12:03 -0400 | [diff] [blame] | 145 | |
| 146 | time_stop "pip_install" |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 147 | } |
| 148 | |
Joe Gordon | d5ac785 | 2015-02-06 19:29:23 -0800 | [diff] [blame] | 149 | # get version of a package from global requirements file |
| 150 | # get_from_global_requirements <package> |
| 151 | function get_from_global_requirements { |
| 152 | local package=$1 |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 153 | local required_pkg |
| 154 | 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] | 155 | if [[ $required_pkg == "" ]]; then |
| 156 | die $LINENO "Can't find package $package in requirements" |
| 157 | fi |
| 158 | echo $required_pkg |
| 159 | } |
| 160 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 161 | # should we use this library from their git repo, or should we let it |
| 162 | # get pulled in via pip dependencies. |
| 163 | function use_library_from_git { |
| 164 | local name=$1 |
| 165 | local enabled=1 |
| 166 | [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0 |
| 167 | return $enabled |
| 168 | } |
| 169 | |
Sean Dague | c71973e | 2015-09-08 07:12:48 -0400 | [diff] [blame] | 170 | # determine if a package was installed from git |
| 171 | function lib_installed_from_git { |
| 172 | local name=$1 |
| 173 | pip freeze 2>/dev/null | grep -- "$name" | grep -q -- '-e git' |
| 174 | } |
| 175 | |
| 176 | # check that everything that's in LIBS_FROM_GIT was actually installed |
| 177 | # correctly, this helps double check issues with library fat fingering. |
| 178 | function check_libs_from_git { |
| 179 | local lib="" |
| 180 | local not_installed="" |
| 181 | for lib in $(echo ${LIBS_FROM_GIT} | tr "," " "); do |
| 182 | if ! lib_installed_from_git "$lib"; then |
| 183 | not_installed+=" $lib" |
| 184 | fi |
| 185 | done |
| 186 | # if anything is not installed, say what it is. |
| 187 | if [[ -n "$not_installed" ]]; then |
| 188 | die $LINENO "The following LIBS_FROM_GIT were not installed correct: $not_installed" |
| 189 | fi |
| 190 | } |
| 191 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 192 | # setup a library by name. If we are trying to use the library from |
| 193 | # git, we'll do a git based install, otherwise we'll punt and the |
| 194 | # library should be installed by a requirements pull from another |
| 195 | # project. |
| 196 | function setup_lib { |
| 197 | local name=$1 |
| 198 | local dir=${GITDIR[$name]} |
| 199 | setup_install $dir |
| 200 | } |
| 201 | |
| 202 | # setup a library by name in editiable mode. If we are trying to use |
| 203 | # the library from git, we'll do a git based install, otherwise we'll |
| 204 | # punt and the library should be installed by a requirements pull from |
| 205 | # another project. |
| 206 | # |
| 207 | # use this for non namespaced libraries |
| 208 | function setup_dev_lib { |
| 209 | local name=$1 |
| 210 | local dir=${GITDIR[$name]} |
| 211 | setup_develop $dir |
| 212 | } |
| 213 | |
| 214 | # this should be used if you want to install globally, all libraries should |
| 215 | # use this, especially *oslo* ones |
| 216 | function setup_install { |
| 217 | local project_dir=$1 |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 218 | setup_package_with_constraints_edit $project_dir |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | # this should be used for projects which run services, like all services |
| 222 | function setup_develop { |
| 223 | local project_dir=$1 |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 224 | setup_package_with_constraints_edit $project_dir -e |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | # determine if a project as specified by directory is in |
| 228 | # projects.txt. This will not be an exact match because we throw away |
| 229 | # the namespacing when we clone, but it should be good enough in all |
| 230 | # practical ways. |
| 231 | function is_in_projects_txt { |
| 232 | local project_dir=$1 |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 233 | local project_name |
| 234 | project_name=$(basename $project_dir) |
Ihar Hrachyshka | 2ba4a72 | 2015-06-26 10:45:44 +0200 | [diff] [blame] | 235 | grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | # ``pip install -e`` the package, which processes the dependencies |
| 239 | # using pip before running `setup.py develop` |
| 240 | # |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 241 | # Updates the constraints from REQUIREMENTS_DIR to reflect the |
| 242 | # future installed state of this package. This ensures when we |
| 243 | # install this package we get the from source version. |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 244 | # |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 245 | # Uses globals ``REQUIREMENTS_DIR`` |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 246 | # setup_develop directory |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 247 | function setup_package_with_constraints_edit { |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 248 | local project_dir=$1 |
| 249 | local flags=$2 |
| 250 | |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 251 | if [ -n "$REQUIREMENTS_DIR" ]; then |
| 252 | # Constrain this package to this project directory from here on out. |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 253 | local name |
| 254 | name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg) |
Robert Collins | 7c83861 | 2015-07-03 13:28:09 +1200 | [diff] [blame] | 255 | $REQUIREMENTS_DIR/.venv/bin/edit-constraints \ |
| 256 | $REQUIREMENTS_DIR/upper-constraints.txt -- $name \ |
| 257 | "$flags file://$project_dir#egg=$name" |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 258 | fi |
| 259 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 260 | setup_package $project_dir $flags |
| 261 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | # ``pip install -e`` the package, which processes the dependencies |
| 265 | # using pip before running `setup.py develop` |
| 266 | # Uses globals ``STACK_USER`` |
| 267 | # setup_develop_no_requirements_update directory |
| 268 | function setup_package { |
| 269 | local project_dir=$1 |
| 270 | local flags=$2 |
| 271 | |
| 272 | pip_install $flags $project_dir |
| 273 | # ensure that further actions can do things like setup.py sdist |
| 274 | if [[ "$flags" == "-e" ]]; then |
| 275 | safe_chown -R $STACK_USER $1/*.egg-info |
| 276 | fi |
| 277 | } |
| 278 | |
| 279 | |
| 280 | # Restore xtrace |
| 281 | $INC_PY_TRACE |
| 282 | |
| 283 | # Local variables: |
| 284 | # mode: shell-script |
| 285 | # End: |