python 4.27 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"

Casey Faist's avatar
Casey Faist committed
10
SECURITY_UPDATE="Python has released a security update! Please consider upgrading to"
Casey Faist's avatar
Casey Faist committed
11 12

# check if runtime exists
13 14
if curl --output /dev/null --silent --head --fail "$VENDORED_PYTHON"; then
  if [[ "$PYTHON_VERSION" == $PY37* ]]; then
Casey Faist's avatar
Casey Faist committed
15
    # do things to alert the user of security release available
16 17
    if [ "$PYTHON_VERSION" != "$LATEST_37" ]; then
      puts-warn "$SECURITY_UPDATE" "$LATEST_37"
Casey Faist's avatar
Casey Faist committed
18 19
      echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
    fi
Kenneth Reitz's avatar
Kenneth Reitz committed
20
  fi
21
  if [[ "$PYTHON_VERSION" == $PY36* ]]; then
Casey Faist's avatar
Casey Faist committed
22
    # security update note
23 24
    if [ "$PYTHON_VERSION" != "$LATEST_36" ]; then
      puts-warn "$SECURITY_UPDATE" "$LATEST_36"
25 26
      echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
    fi
Kenneth Reitz's avatar
Kenneth Reitz committed
27
  fi
28
  if [[ "$PYTHON_VERSION" == $PY35* ]]; then
Casey Faist's avatar
Casey Faist committed
29
    # security update note
30 31
    if [ "$PYTHON_VERSION" != "$LATEST_35" ]; then
      puts-warn "$SECURITY_UPDATE" "$LATEST_35"
Casey Faist's avatar
Casey Faist committed
32 33 34
      echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
    fi
  fi
Casey Faist's avatar
Casey Faist committed
35 36 37 38 39 40 41
  if [[ "$PYTHON_VERSION" == $PY34* ]]; then
    # security update note
    if [ "$PYTHON_VERSION" != "$LATEST_34" ]; then
      puts-warn "$SECURITY_UPDATE" "$LATEST_34"
      echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
    fi
  fi
42
  if [[ "$PYTHON_VERSION" == $PY27* ]]; then
Casey Faist's avatar
Casey Faist committed
43
    # security update note
44 45
    if [ "$PYTHON_VERSION" != "$LATEST_27" ]; then
      puts-warn "$SECURITY_UPDATE" "$LATEST_27"
Casey Faist's avatar
Casey Faist committed
46 47 48 49 50 51 52
      echo "       Learn More: https://devcenter.heroku.com/articles/python-runtimes"
    fi
  fi
else
  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
Kenneth Reitz's avatar
Kenneth Reitz committed
53 54
fi

55 56
if [[ "$STACK" != "$CACHED_PYTHON_STACK" ]]; then
    puts-step "Stack has changed from $CACHED_PYTHON_STACK to $STACK, clearing cache"
57 58 59 60 61 62 63 64 65
    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
66 67
fi

68
if [ -f .heroku/python-version ]; then
Kenneth Reitz's avatar
Kenneth Reitz committed
69
  if [ ! "$(cat .heroku/python-version)" = "$PYTHON_VERSION" ]; then
Kenneth Reitz's avatar
v100  
Kenneth Reitz committed
70 71
      puts-step "Found $(cat .heroku/python-version), removing"
      rm -fr .heroku/python
72 73 74 75 76
  else
    SKIP_INSTALL=1
  fi
fi

77

78
if [ ! "$SKIP_INSTALL" ]; then
Kenneth Reitz's avatar
Kenneth Reitz committed
79
    puts-step "Installing $PYTHON_VERSION"
80 81 82 83

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

Kenneth Reitz's avatar
Kenneth Reitz committed
84 85
    mcount "version.python.$PYTHON_VERSION"

86
    if ! curl "${VENDORED_PYTHON}" -s | tar zxv -C .heroku/python &> /dev/null; then
87 88 89 90 91 92
      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
93 94
  echo "$PYTHON_VERSION" > .heroku/python-version
  echo "$STACK" > .heroku/python-stack
95 96 97 98 99
  FRESH_PYTHON=true

  hash -r
fi

Casey Faist's avatar
Casey Faist committed
100 101 102 103
# Heroku uses the get-pip utility maintained by the Python community to vendor Pip.
# https://github.com/pypa/get-pip
GETPIP="https://lang-python.s3.amazonaws.com/etc/get-pip.py"

Casey Faist's avatar
Casey Faist committed
104
if ! curl -s "${GETPIP}" -o "$ROOT_DIR/get-pip.py" &> /dev/null; then
105 106 107
    mcount "failure.python.get-pip"
    echo "Failed to pull down get-pip"
    exit 1
Casey Faist's avatar
Casey Faist committed
108
fi
109

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

Kenneth Reitz's avatar
Kenneth Reitz committed
113 114
  puts-step "Installing pip"

Kenneth Reitz's avatar
Kenneth Reitz committed
115
  # Remove old installations.
David Zülke's avatar
David Zülke committed
116 117
  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
118

David Zülke's avatar
David Zülke committed
119 120
  /app/.heroku/python/bin/python "$ROOT_DIR/get-pip.py" pip=="$PIP_UPDATE" &> /dev/null
  /app/.heroku/python/bin/pip install "$ROOT_DIR/vendor/setuptools-39.0.1-py2.py3-none-any.whl" &> /dev/null
121 122 123
fi

set -e
124
hash -r