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``, |
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 | |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 106 | cmd_pip="$cmd_pip install" |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 107 | # Always apply constraints |
| 108 | cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt" |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 109 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 110 | local pip_version=$(python -c "import pip; \ |
| 111 | print(pip.__version__.strip('.')[0])") |
| 112 | if (( pip_version<6 )); then |
| 113 | die $LINENO "Currently installed pip version ${pip_version} does not" \ |
| 114 | "meet minimum requirements (>=6)." |
| 115 | fi |
| 116 | |
| 117 | $xtrace |
| 118 | $sudo_pip \ |
Eli Qiao | 6a83c42 | 2015-03-17 16:54:16 +0800 | [diff] [blame] | 119 | http_proxy="${http_proxy:-}" \ |
| 120 | https_proxy="${https_proxy:-}" \ |
| 121 | no_proxy="${no_proxy:-}" \ |
Joe Gordon | cd8824a | 2015-03-04 16:40:19 -0800 | [diff] [blame] | 122 | PIP_FIND_LINKS=$PIP_FIND_LINKS \ |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 123 | $cmd_pip $upgrade \ |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 124 | $@ |
| 125 | |
Sean Dague | eeb7bda | 2015-03-25 11:55:32 -0400 | [diff] [blame] | 126 | # Also install test requirements |
| 127 | local test_req="$@/test-requirements.txt" |
| 128 | if [[ -e "$test_req" ]]; then |
| 129 | echo "Installing test-requirements for $test_req" |
| 130 | $sudo_pip \ |
| 131 | http_proxy=${http_proxy:-} \ |
| 132 | https_proxy=${https_proxy:-} \ |
| 133 | no_proxy=${no_proxy:-} \ |
| 134 | PIP_FIND_LINKS=$PIP_FIND_LINKS \ |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 135 | $cmd_pip $upgrade \ |
Sean Dague | eeb7bda | 2015-03-25 11:55:32 -0400 | [diff] [blame] | 136 | -r $test_req |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 137 | fi |
| 138 | } |
| 139 | |
Joe Gordon | d5ac785 | 2015-02-06 19:29:23 -0800 | [diff] [blame] | 140 | # get version of a package from global requirements file |
| 141 | # get_from_global_requirements <package> |
| 142 | function get_from_global_requirements { |
| 143 | local package=$1 |
Amrith Kumar | 9860876 | 2015-04-08 15:37:58 -0400 | [diff] [blame] | 144 | 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] | 145 | if [[ $required_pkg == "" ]]; then |
| 146 | die $LINENO "Can't find package $package in requirements" |
| 147 | fi |
| 148 | echo $required_pkg |
| 149 | } |
| 150 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 151 | # should we use this library from their git repo, or should we let it |
| 152 | # get pulled in via pip dependencies. |
| 153 | function use_library_from_git { |
| 154 | local name=$1 |
| 155 | local enabled=1 |
| 156 | [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0 |
| 157 | return $enabled |
| 158 | } |
| 159 | |
| 160 | # setup a library by name. If we are trying to use the library from |
| 161 | # git, we'll do a git based install, otherwise we'll punt and the |
| 162 | # library should be installed by a requirements pull from another |
| 163 | # project. |
| 164 | function setup_lib { |
| 165 | local name=$1 |
| 166 | local dir=${GITDIR[$name]} |
| 167 | setup_install $dir |
| 168 | } |
| 169 | |
| 170 | # setup a library by name in editiable mode. If we are trying to use |
| 171 | # the library from git, we'll do a git based install, otherwise we'll |
| 172 | # punt and the library should be installed by a requirements pull from |
| 173 | # another project. |
| 174 | # |
| 175 | # use this for non namespaced libraries |
| 176 | function setup_dev_lib { |
| 177 | local name=$1 |
| 178 | local dir=${GITDIR[$name]} |
| 179 | setup_develop $dir |
| 180 | } |
| 181 | |
| 182 | # this should be used if you want to install globally, all libraries should |
| 183 | # use this, especially *oslo* ones |
| 184 | function setup_install { |
| 185 | local project_dir=$1 |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 186 | setup_package_with_constraints_edit $project_dir |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | # this should be used for projects which run services, like all services |
| 190 | function setup_develop { |
| 191 | local project_dir=$1 |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 192 | setup_package_with_constraints_edit $project_dir -e |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | # determine if a project as specified by directory is in |
| 196 | # projects.txt. This will not be an exact match because we throw away |
| 197 | # the namespacing when we clone, but it should be good enough in all |
| 198 | # practical ways. |
| 199 | function is_in_projects_txt { |
| 200 | local project_dir=$1 |
| 201 | local project_name=$(basename $project_dir) |
Ihar Hrachyshka | 2ba4a72 | 2015-06-26 10:45:44 +0200 | [diff] [blame] | 202 | grep -q "/$project_name\$" $REQUIREMENTS_DIR/projects.txt |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | # ``pip install -e`` the package, which processes the dependencies |
| 206 | # using pip before running `setup.py develop` |
| 207 | # |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 208 | # Updates the constraints from REQUIREMENTS_DIR to reflect the |
| 209 | # future installed state of this package. This ensures when we |
| 210 | # install this package we get the from source version. |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 211 | # |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 212 | # Uses globals ``REQUIREMENTS_DIR`` |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 213 | # setup_develop directory |
Clark Boylan | 05aa384 | 2015-08-03 11:14:13 -0700 | [diff] [blame] | 214 | function setup_package_with_constraints_edit { |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 215 | local project_dir=$1 |
| 216 | local flags=$2 |
| 217 | |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 218 | if [ -n "$REQUIREMENTS_DIR" ]; then |
| 219 | # Constrain this package to this project directory from here on out. |
| 220 | local name=$(awk '/^name.*=/ {print $3}' $project_dir/setup.cfg) |
Robert Collins | 7c83861 | 2015-07-03 13:28:09 +1200 | [diff] [blame] | 221 | $REQUIREMENTS_DIR/.venv/bin/edit-constraints \ |
| 222 | $REQUIREMENTS_DIR/upper-constraints.txt -- $name \ |
| 223 | "$flags file://$project_dir#egg=$name" |
Robert Collins | 635a5ba | 2015-06-10 08:48:06 +1200 | [diff] [blame] | 224 | fi |
| 225 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 226 | setup_package $project_dir $flags |
| 227 | |
Dean Troyer | 490430d | 2015-01-30 14:38:35 -0600 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | # ``pip install -e`` the package, which processes the dependencies |
| 231 | # using pip before running `setup.py develop` |
| 232 | # Uses globals ``STACK_USER`` |
| 233 | # setup_develop_no_requirements_update directory |
| 234 | function setup_package { |
| 235 | local project_dir=$1 |
| 236 | local flags=$2 |
| 237 | |
| 238 | pip_install $flags $project_dir |
| 239 | # ensure that further actions can do things like setup.py sdist |
| 240 | if [[ "$flags" == "-e" ]]; then |
| 241 | safe_chown -R $STACK_USER $1/*.egg-info |
| 242 | fi |
| 243 | } |
| 244 | |
| 245 | |
| 246 | # Restore xtrace |
| 247 | $INC_PY_TRACE |
| 248 | |
| 249 | # Local variables: |
| 250 | # mode: shell-script |
| 251 | # End: |