python 4.05 KB
Newer Older
Kenneth Reitz's avatar
Kenneth Reitz committed
1 2
#!/usr/bin/env bash

3
set +e
Kenneth Reitz's avatar
Kenneth Reitz committed
4
runtime-fixer runtime.txt
5 6
PYTHON_VERSION=$(cat runtime.txt)

7 8 9
# The location of the pre-compiled python binary.
VENDORED_PYTHON="${VENDOR_URL}/runtimes/$PYTHON_VERSION.tar.gz"

Kenneth Reitz's avatar
Kenneth Reitz committed
10 11 12 13 14
if [[ $PYTHON_VERSION =~ ^python-2 ]]; then
  if [[ "$PYTHON_VERSION" != "$LATEST_2" ]]; then
    puts-warn "The latest version of Python 2 is $LATEST_2 (you are using $PYTHON_VERSION, which is unsupported)."
    puts-warn "We recommend upgrading by specifying the latest version ($LATEST_2)."
    echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
15 16
  else
    echo "       Using supported version of Python 2 ($PYTHON_VERSION)"
Kenneth Reitz's avatar
Kenneth Reitz committed
17 18
  fi
else
19 20 21 22 23
  if [[ $PYTHON_VERSION =~ ^python-3 ]]; then
    if [[ $PYTHON_VERSION =~ ^python-3.7 ]]; then
      if [[ "$PYTHON_VERSION" != "$LATEST_37" ]]; then
      puts-warn "The latest version of Python 3.7 is $LATEST_37 (you are using $PYTHON_VERSION, which is unsupported)."
      puts-warn "We recommend upgrading by specifying the latest version ($LATEST_37)."
24
      echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
25 26 27 28 29 30 31 32 33 34 35 36
      else
        echo "       Using supported version of Python 3.7 ($PYTHON_VERSION)"
      fi
    else
      if [[ $PYTHON_VERSION =~ ^python-3.6 ]]; then
        if [[ "$PYTHON_VERSION" != "$LATEST_36" ]]; then
          puts-warn "The latest version of Python 3.6 is $LATEST_36 (you are using $PYTHON_VERSION, which is unsupported)."
          puts-warn "We recommend upgrading by specifying the latest version ($LATEST_36)."
          echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
        else
          echo "       Using supported version of Python 3.6 ($PYTHON_VERSION)"
        fi
37 38 39 40
      else
        puts-warn "Heroku supports the latest version of Python 2 $LATEST_37, Python 3.6 $LATEST_36 and Python 3.7 $LATEST_2." 
        puts-warn "You are using $PYTHON_VERSION, which is unsupported."
        puts-warn "We recommend upgrading by specifying the default supported version ($LATEST_36)."
41
      fi
42
    fi
Kenneth Reitz's avatar
Kenneth Reitz committed
43 44 45
  fi
fi

46 47
if [[ "$STACK" != "$CACHED_PYTHON_STACK" ]]; then
    puts-step "Stack has changed from $CACHED_PYTHON_STACK to $STACK, clearing cache"
48 49 50 51 52 53 54 55 56
    rm -fr .heroku/python-stack .heroku/python-version .heroku/python .heroku/vendor .heroku/python .heroku/python-sqlite3-version
fi

# need to clear the cache for first time installing SQLite3,
# since the version is changing and could lead to runtime errors
# with compiled extensions.
if [ -d .heroku/python ] && [ ! -f .heroku/python-sqlite3-version ] && python_sqlite3_check "$PYTHON_VERSION"; then
  puts-step "Need to update SQLite3, clearing cache"
  rm -fr .heroku/python-stack .heroku/python-version .heroku/python .heroku/vendor
57 58
fi

59
if [ -f .heroku/python-version ]; then
Kenneth Reitz's avatar
Kenneth Reitz committed
60
  if [ ! "$(cat .heroku/python-version)" = "$PYTHON_VERSION" ]; then
Kenneth Reitz's avatar
v100  
Kenneth Reitz committed
61 62
      puts-step "Found $(cat .heroku/python-version), removing"
      rm -fr .heroku/python
63 64 65 66 67
  else
    SKIP_INSTALL=1
  fi
fi

68

69
if [ ! "$SKIP_INSTALL" ]; then
Kenneth Reitz's avatar
Kenneth Reitz committed
70
    puts-step "Installing $PYTHON_VERSION"
71 72 73 74

    # Prepare destination directory.
    mkdir -p .heroku/python

Kenneth Reitz's avatar
Kenneth Reitz committed
75 76
    mcount "version.python.$PYTHON_VERSION"

77
    if ! curl "${VENDORED_PYTHON}" -s | tar zxv -C .heroku/python &> /dev/null; then
78 79 80 81 82 83
      puts-warn "Requested runtime ($PYTHON_VERSION) is not available for this stack ($STACK)."
      puts-warn "Aborting.  More info: https://devcenter.heroku.com/articles/python-support"
      exit 1
    fi

  # Record for future reference.
Kenneth Reitz's avatar
Kenneth Reitz committed
84 85
  echo "$PYTHON_VERSION" > .heroku/python-version
  echo "$STACK" > .heroku/python-stack
86 87 88 89 90
  FRESH_PYTHON=true

  hash -r
fi

91

92
# If Pip isn't up to date:
Kenneth Reitz's avatar
Kenneth Reitz committed
93
if [ "$FRESH_PYTHON" ] || [[ ! $(pip --version) == *$PIP_UPDATE* ]]; then
94

Kenneth Reitz's avatar
Kenneth Reitz committed
95 96
  puts-step "Installing pip"

Kenneth Reitz's avatar
Kenneth Reitz committed
97
  # Remove old installations.
Kenneth Reitz's avatar
Kenneth Reitz committed
98 99
  rm -fr /app/.heroku/python/lib/python*/site-packages/pip-*
  rm -fr /app/.heroku/python/lib/python*/site-packages/setuptools-*
Kenneth Reitz's avatar
Kenneth Reitz committed
100

Kenneth Reitz's avatar
Kenneth Reitz committed
101
  /app/.heroku/python/bin/python "$ROOT_DIR/vendor/get-pip.py" pip=="$PIP_UPDATE" &> /dev/null
102
  /app/.heroku/python/bin/pip install "$ROOT_DIR/vendor/setuptools-39.0.1-py2.py3-none-any.whl" &> /dev/null
Kenneth Reitz's avatar
Kenneth Reitz committed
103

104 105 106
fi

set -e
107
hash -r