|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2010 New Relic, Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +set -e |
| 17 | + |
| 18 | +SED=$(which gsed || which sed) |
| 19 | + |
| 20 | +SCRIPT_DIR=$(dirname "$0") |
| 21 | +PIP_REQUIREMENTS=$(cat /requirements.txt) |
| 22 | + |
| 23 | +main() { |
| 24 | + # Coerce space separated string to array |
| 25 | + if [[ ${#PYTHON_VERSIONS[@]} -eq 1 ]]; then |
| 26 | + PYTHON_VERSIONS=($PYTHON_VERSIONS) |
| 27 | + fi |
| 28 | + |
| 29 | + if [[ -z "${PYTHON_VERSIONS[@]}" ]]; then |
| 30 | + echo "No python versions specified. Make sure PYTHON_VERSIONS is set." 1>&2 |
| 31 | + exit 1 |
| 32 | + fi |
| 33 | + |
| 34 | + # Find all latest pyenv supported versions for requested python versions |
| 35 | + PYENV_VERSIONS=() |
| 36 | + for v in "${PYTHON_VERSIONS[@]}"; do |
| 37 | + LATEST=$(pyenv latest -k "$v" || get_latest_patch_version "$v") |
| 38 | + if [[ -z "$LATEST" ]]; then |
| 39 | + echo "Latest version could not be found for ${v}." 1>&2 |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | + PYENV_VERSIONS+=($LATEST) |
| 43 | + done |
| 44 | + |
| 45 | + # Install each specific version |
| 46 | + for v in "${PYENV_VERSIONS[@]}"; do |
| 47 | + pyenv install "$v" & |
| 48 | + done |
| 49 | + wait |
| 50 | + |
| 51 | + # Set all installed versions as globally accessible |
| 52 | + pyenv global ${PYENV_VERSIONS[@]} |
| 53 | + |
| 54 | + # Install dependencies for main python installation |
| 55 | + pyenv exec pip install --upgrade $PIP_REQUIREMENTS |
| 56 | +} |
| 57 | + |
| 58 | +get_latest_patch_version() { |
| 59 | + pyenv install --list | # Get all python versions |
| 60 | + $SED 's/^ *//g' | # Remove leading whitespace |
| 61 | + grep -E "^$1" | # Find specified version by matching start of line |
| 62 | + grep -v -- "-c-jit-latest" | # Filter out pypy JIT versions |
| 63 | + $SED -E '/(-[a-zA-Z]+$)|(a[0-9]+)|(b[0-9]+)|(rc[0-9]+)/!{s/$/_/}' | # Append trailing _ to any non development versions to place them lower when sorted |
| 64 | + sort -V | # Sort using version sorting |
| 65 | + $SED 's/_$//' | # Remove any added trailing underscores to correct version names |
| 66 | + tail -1 # Grab last result as latest version |
| 67 | +} |
| 68 | + |
| 69 | +main |
0 commit comments