utils 2.12 KB
Newer Older
Kenneth Reitz's avatar
Kenneth Reitz committed
1
#!/usr/bin/env bash
Kenneth Reitz's avatar
test  
Kenneth Reitz committed
2
shopt -s extglob
Kenneth Reitz's avatar
Kenneth Reitz committed
3
shopt -s nullglob
Kenneth Reitz's avatar
test  
Kenneth Reitz committed
4

Kenneth Reitz's avatar
Kenneth Reitz committed
5 6
# The standard library.
if [[ ! -f  /tmp/stdlib.sh ]]; then
Kenneth Reitz's avatar
Kenneth Reitz committed
7
  curl --retry 3 -s https://lang-common.s3.amazonaws.com/buildpack-stdlib/v8/stdlib.sh > /tmp/stdlib.sh
Kenneth Reitz's avatar
Kenneth Reitz committed
8
fi
Kenneth Reitz's avatar
Kenneth Reitz committed
9
# shellcheck source=/dev/null
Kenneth Reitz's avatar
Kenneth Reitz committed
10 11
source /tmp/stdlib.sh

Kenneth Reitz's avatar
Kenneth Reitz committed
12
if [ "$(uname)" == Darwin ]; then
13 14 15 16
    sed() { command sed -l "$@"; }
else
    sed() { command sed -u "$@"; }
fi
Kenneth Reitz's avatar
Kenneth Reitz committed
17

Kenneth Reitz's avatar
Kenneth Reitz committed
18 19
# Syntax sugar.
indent() {
20
  sed "s/^/       /"
Kenneth Reitz's avatar
Kenneth Reitz committed
21 22
}

Kenneth Reitz's avatar
Kenneth Reitz committed
23

Kenneth Reitz's avatar
Kenneth Reitz committed
24 25
# Clean up pip output
cleanup() {
Kenneth Reitz's avatar
Kenneth Reitz committed
26
  sed -e 's/\.\.\.\+/.../g' | sed -e '/already satisfied/Id' | sed -e '/No files were found to uninstall/Id' | sed -e '/Overwriting/Id' | sed -e '/python executable/Id' | sed -e '/no previously-included files/Id'
Kenneth Reitz's avatar
Kenneth Reitz committed
27 28 29
}

# Buildpack Steps.
30
puts-step() {
Kenneth Reitz's avatar
Kenneth Reitz committed
31
  echo "-----> $*"
Kenneth Reitz's avatar
Kenneth Reitz committed
32 33 34
}

# Buildpack Warnings.
35
puts-warn() {
Kenneth Reitz's avatar
Kenneth Reitz committed
36
  echo " !     $*"
Kenneth Reitz's avatar
Kenneth Reitz committed
37 38
}

Kenneth Reitz's avatar
Kenneth Reitz committed
39
# Does some serious copying.
40
deep-cp() {
41 42 43 44 45 46
  declare source="$1" target="$2"

  mkdir -p "$target"

  # cp doesn't like being called without source params,
  # so make sure they expand to something first.
47
  # subshell to avoid surprising caller with shopts.
48
  (
49 50
    shopt -s nullglob dotglob
    set -- "$source"/!(tmp|.|..)
51 52
    [[ $# == 0 ]] || cp -a "$@" "$target"
  )
Kenneth Reitz's avatar
Kenneth Reitz committed
53 54
}

Kenneth Reitz's avatar
Kenneth Reitz committed
55

Kenneth Reitz's avatar
Kenneth Reitz committed
56 57
# Measure the size of the Python installation.
measure-size() {
Kenneth Reitz's avatar
Kenneth Reitz committed
58
  echo "$(du -s .heroku/python 2>/dev/null || echo 0) | awk '{print $1}')"
Kenneth Reitz's avatar
Kenneth Reitz committed
59 60
}

Terence Lee's avatar
Terence Lee committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
# Python version operator >
version_gt() {
  test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1";
}

# Python verison operator >=
version_gte() {
  if [ "$1" == "$2" ]; then
    return 0
  fi

  version_gt "$1" "$2"
}

# Check if Python 2
python2_check() {
  VERSION="$1"

  version_gte "$VERSION" "python-2.7.0" && version_gt "python-3.0.0" "$VERSION"
}

# Check if Python 3
python3_check() {
  VERSION="$1"

  version_gte "$VERSION" "python-3.0.0" && version_gt "python-4.0.0" "$VERSION"
}
88 89 90 91 92 93 94 95 96 97 98

# Check if Python version needs to install SQLite3
python_sqlite3_check() {
  VERSION="$1"
  MIN_PYTHON_3="python-3.6.6"
  MIN_PYTHON_2="python-2.7.15"

  ( python2_check "$VERSION" && version_gte "$VERSION" "$MIN_PYTHON_2" ) \
    || ( python3_check "$VERSION" && version_gte "$VERSION" "$MIN_PYTHON_3" ) \
    || ( version_gte "$VERSION" "3.7.0" )
}