aboutsummaryrefslogtreecommitdiffstats
path: root/python_install.sh
blob: c98912551325ab10842073c7371addfd91ad9bd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash

# @author: <sylvain.herledan@hrafnagud.info>
# @date: 2017-06-06

set -o nounset
set -o errexit
set -o pipefail

readonly PROGNAME="$(basename "${0}")";
readonly PROGDIR="$(readlink -f "$(dirname "${0}")")";
readonly ARGS="${@}";

readonly PYTHON_VERSION_PATH="$(mktemp)";
readonly PIP_INSTALL_SCRIPT="$(mktemp)";

cleanup()
{
  local rc=$?;  # Keep original return code

  rm -f "${PYTHON_VERSION_PATH}"
  rm -f "${PIP_INSTALL_SCRIPT}"
  exit ${rc}
}

get_python_version()
{
  local python="${1}"; shift;
  local version_path="${1}"; shift;

  # Redirecting stderr to stdout is necessary because older versions
  # of Python printed to stderr
  "${python}" --version 2>&1 \
    | cut -d\  -f2 \
    > "${version_path}"
}

venv_install_method()
{
  local python="${1}"; shift;
  local env_dir="${1}"; shift;

  "${python}" -m venv "${env_dir}"
}

pip_install_method()
{
  local python="${1}"; shift;
  local env_dir="${1}"; shift;
  local num="${1}"; shift;
  local opts='';

  wget 'https://bootstrap.pypa.io/get-pip.py' \
       -O "${PIP_INSTALL_SCRIPT}"
  PYTHONUSERBASE="${env_dir}" \
  "${python}" "${PIP_INSTALL_SCRIPT}" --isolated \
                                      --user \
                                      --ignore-installed \
                                      --no-cache

  # WARNING: for some reason it is not possible to force
  # the script to build from source (with --no-binary setuptools,pip)
  # due to an unsupported --no-user option in the install procedure.
  # So it is better to check that the installed binary is compatible
  # with the local system (notably the SSL library which may cause
  # issues when downloading packages from HTTPS).
}

install_environment()
{

  get_python_version "${python}" "${PYTHON_VERSION_PATH}"
  local major="$(cat "${PYTHON_VERSION_PATH}" | cut -d. -f1)";
  local minor="$(cat "${PYTHON_VERSION_PATH}" | cut -d. -f2)";
  local num=$((${minor} + 10 * ${major}));

  if [ ${num} -gt 33 ];
  then
    set +o errexit
    "${python}" -m ensurepip --help 2>&1 >/dev/null
    local rc=$?
    set -o errexit

    if [ 0 -eq ${rc} ];
    then
      venv_install_method "${python}" "${env_dir}"
    else
      pip_install_method "${python}" "${env_dir}" "${num}"
      install_virtualenv "${python}" "${env_dir}"
    fi
  elif [ ${num} -eq 27 ];
  then
    pip_install_method "${python}" "${env_dir}" "${num}"
    install_virtualenv "${python}" "${env_dir}"
  else
    >&2 echo "Unsupported Python version"
    exit 1
  fi

}

install_virtualenv()
{
  local python="${1}"; shift;
  local env_dir="${1}"; shift;

  # Install virtualenv
  PYTHONUSERBASE="${env_dir}" \
  "${env_dir}/bin/pip" install --user \
                               --ignore-installed \
                               --no-cache \
                               --no-binary virtualenv \
                               virtualenv

  local _pkg_dir="$(find "${env_dir}/lib" -name "virtualenv.py" \
                                          -exec dirname {} \;)";
  local pkg_dir="$(readlink -f "${_pkg_dir}")";

  mkdir -p "${env_dir}/cache"

  PIP_CACHE_DIR="${env_dir}/cache" \
  PIP_IGNORE_INSTALLED=true \
  PYTHONPATH="${pkg_dir}" \
  "${python}" -m virtualenv "${env_dir}"

  cat > "${env_dir}/pip.conf" << EOF
[global]
cache-dir=${env_dir}/cache
EOF
}

main()
{
  local python="${1}"; shift;
  local _install_dir="${1}"; shift;
  local install_dir="$(readlink -f "${_install_dir}")";
  local env_dir="${install_dir}/env";

  trap 'cleanup' INT TERM EXIT

  install_environment "${python}" "${env_dir}"

  trap - INT TERM EXIT
  echo 'Environment ready'
}

main ${ARGS}
exit 0