|
#!/usr/bin/env bash |
|
|
|
set -e |
|
|
|
# This is being run as root and so sudo is not needed |
|
|
|
CXX_VERSION="$(which gcc)" |
|
|
|
function download_cpython () { |
|
# 1: the version tag |
|
printf "\n### Downloading CPython source as Python-${1}.tgz\n" |
|
wget "https://www.python.org/ftp/python/${1}/Python-${1}.tgz" &> /dev/null |
|
tar -xvzf "Python-${1}.tgz" > /dev/null |
|
rm "Python-${1}.tgz" |
|
} |
|
|
|
function set_num_processors { |
|
# Set the number of processors used for build |
|
# to be 1 less than are available |
|
if [[ -f "$(which nproc)" ]]; then |
|
NPROC="$(nproc)" |
|
else |
|
NPROC="$(grep -c '^processor' /proc/cpuinfo)" |
|
fi |
|
echo `expr "${NPROC}" - 1` |
|
} |
|
|
|
function build_cpython () { |
|
# 1: the prefix to be passed to configure |
|
# c.f. https://docs.python.org/3/using/unix.html#python-related-paths-and-files |
|
# 2: the path to the version of gcc to be used |
|
|
|
# https://docs.python.org/3/using/unix.html#building-python |
|
# https://github.com/python/cpython/blob/3.6/README.rst |
|
printf "\n### ./configure\n" |
|
./configure --prefix="${1}" \ |
|
--exec_prefix="${1}" \ |
|
--with-cxx-main="${2}" \ |
|
--enable-optimizations \ |
|
--with-lto \ |
|
--enable-loadable-sqlite-extensions \ |
|
CXX="${2}" |
|
printf "\n### make -j${NPROC}\n" |
|
make -j${NPROC} |
|
printf "\n### make install\n" |
|
make install |
|
} |
|
|
|
function update_pip { |
|
# Update pip, setuptools, and wheel |
|
if [[ "$(id -u)" -eq 0 ]]; then |
|
# If root |
|
printf "\n### pip3 install --upgrade --no-cache-dir pip setuptools wheel\n" |
|
pip3 install --upgrade --no-cache-dir pip setuptools wheel |
|
else |
|
printf "\n### pip3 install --user --upgrade --no-cache-dir pip setuptools wheel\n" |
|
pip3 install --user --upgrade --no-cache-dir pip setuptools wheel |
|
fi |
|
} |
|
|
|
function symlink_python_to_python3 { |
|
local python_version="$(python3 --version)" |
|
local which_python="$(which python3)${python_version:8:-2}" |
|
local which_pip="$(which pip3)" |
|
|
|
# symlink python to python3 |
|
printf "\n### ln -s -f ${which_python} ${which_python::-3}\n" |
|
ln -s -f "${which_python}" "${which_python::-3}" |
|
|
|
# symlink pip to pip3 if no pip exists or it is a different version than pip3 |
|
if [[ ! -z "$(which pip)" ]]; then |
|
if [[ "$(pip --version)" = "$(pip3 --version)" ]]; then |
|
return 0 |
|
fi |
|
fi |
|
printf "\n### ln -s -f ${which_pip} ${which_pip::-1}\n" |
|
ln -s -f "${which_pip}" "${which_pip::-1}" |
|
return 0 |
|
} |
|
|
|
function main() { |
|
# 1: the Python version tag |
|
# 2: bool of if should symlink python and pip to python3 versions |
|
|
|
PYTHON_VERSION_TAG=3.6.8 # Switch to 3.7 once Tensorflow is out for it |
|
LINK_PYTHON_TO_PYTHON3=0 # By default don't link so as to reserve python for Python 2 |
|
if [[ $# -gt 0 ]]; then |
|
PYTHON_VERSION_TAG="${1}" |
|
|
|
if [[ $# -gt 1 ]]; then |
|
LINK_PYTHON_TO_PYTHON3="${2}" |
|
fi |
|
fi |
|
|
|
NPROC="$(set_num_processors)" |
|
download_cpython "${PYTHON_VERSION_TAG}" |
|
cd Python-"${PYTHON_VERSION_TAG}" |
|
build_cpython /usr "${CXX_VERSION}" |
|
update_pip |
|
|
|
if [[ "${LINK_PYTHON_TO_PYTHON3}" -eq 1 ]]; then |
|
symlink_python_to_python3 |
|
fi |
|
} |
|
|
|
main "$@" || exit 1 |