blob: 065fc79c345020b32d8f84955fbf0299223af959 [file] [log] [blame]
#!/bin/sh
# Copyright 2016 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
SCRIPT="$(readlink -f $0)"
UPDATABLE_DIR="$(realpath $(dirname ${SCRIPT})/../updatable)"
SRC_PYTHON_DIR="${UPDATABLE_DIR}/python2.7"
PIP_DIR="${UPDATABLE_DIR}/pip"
PIP_BOOTSTRAP="get-pip.py"
if [ "${PY_VERSION}" = python3 ]; then
DST_PYTHON_DIR="/usr/lib/python3"
else
DST_PYTHON_DIR="/usr/lib/python2.7"
fi
set_time() {
echo Current chameleon time: $(date --set="$NOW")
}
validate_package_installation() {
PACKAGE_NAME=$1
# dpkg shows installation by showing "ii" next to the entry
if ! dpkg -l ${PACKAGE_NAME} 2>&1 | grep -qE '^ii'; then
printf "Installing package %s...\n" "${PACKAGE_NAME}"
eval "apt-get update > /dev/null 2>&1"
eval "apt-get -y install ${PACKAGE_NAME} > /dev/null 2>&1"
printf "Package %s installed\n" "${PACKAGE_NAME}"
else
printf "Package %s already installed\n" "${PACKAGE_NAME}"
fi
}
use_python_version() {
target_python_version=$1
curr_python_version="$(readlink "$(which python)")"
if [ "${curr_python_version}" != "python2" ] && \
[ "${curr_python_version}" != "python3" ]; then
die "Python version should be python2 or python3."
fi
if [ "${curr_python_version}" = "${target_python_version}" ]; then
echo "Python version is already ${target_python_version}"
else
echo "Update Python version from ${curr_python_version}" \
"to ${target_python_version}"
ln -fs "${target_python_version}" /usr/bin/python || \
die update "${target_python_version}"
ln -fs "${target_python_version}-config" /usr/bin/python-config || \
die update "${target_python_version}-config"
fi
}
install_pip() {
if [ "${PY_VERSION}" = python3 ]; then
validate_package_installation "python3-pip"
else
validate_package_installation "python-pip"
fi
}
install_setuptools() {
if [ "${PY_VERSION}" = python3 ]; then
pip3 install setuptools==54.1.1
else
pip install setuptools
fi
}
uninstall_old_chameleond_from_pip() {
# On the first installation of PIP, Chameleond was already there,
# i.e /usr/lib/python2.7/site-packages/chameleond.
# Since then, the new Chameleond is installed in a different path,
# i.e. /usr/lib/python2.7/site-packages/chameleond-0.0.2-py2.7.egg.
# So check the package directory and uninstall the old Chameleond.
pip show chameleond | grep 'Location: .*\.egg' > /dev/null
if [ $? -ne 0 ]; then
echo PIP still has the old chameleond. Uninstall it...
pip uninstall -y chameleond
rm -rf /usr/lib/python2.7/site-packages/chameleond
fi
}
# Set the correct time. Otherwise, installation of pip may fail.
set_time
# Switch python version for Raspberry Pi.
if [ "${PLATFORM}" = "raspberrypi" ] || [ "${PLATFORM}" = "x86-generic" ]; then
echo deploy_pip: use PY_VERSION "${PY_VERSION}"
use_python_version "${PY_VERSION}"
fi
install_pip
install_setuptools
# Uninstall old chameleond package from pip
uninstall_old_chameleond_from_pip