|
#!/bin/bash |
|
|
|
# Install Python in users local directory linked from ~/bin |
|
# Specify version on command line or defaults to 2.7.11 |
|
|
|
VERSION=${1:-2.7.11} |
|
ENV_BASE=${2:-"/opt/$USER"} |
|
|
|
BUILD_ESSENTIALS="dpkg-dev g++ gcc libc-dev make" |
|
PYTHON_REQUIREMENTS="libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev" |
|
PYTHON_BINS="python python2.7 2to3 idle pydoc python2 python2-config python2.7-config python-config" |
|
PYTHON_BINS_EXTRAS="easy_install pip pip2 pip2.7 easy_install-2.7" |
|
|
|
echo "Installing Python $VERSION" |
|
|
|
function _func_checkroot { |
|
if [ $EUID -eq 0 ]; then |
|
echo "Don't run as root"; |
|
exit 1; |
|
fi |
|
} |
|
|
|
function _func_cansudo { |
|
_CAN_SUDO=`sudo -n uptime 2>&1|grep "load"|wc -l` |
|
if [ ${_CAN_SUDO} -gt 0 ] |
|
then |
|
return 0 |
|
else |
|
return 1 |
|
fi |
|
} |
|
|
|
function _func_package_exists { |
|
return dpkg -l $1 2>/dev/null | grep 'ii' | wc -l |
|
} |
|
|
|
function _func_ifinstall { |
|
if ! _func_package_exists $@ ; then |
|
echo "Found $@" |
|
else |
|
echo "Installing $@" |
|
sudo apt-get install -y $@ |
|
fi |
|
} |
|
|
|
function _func_ifclean { |
|
if _func_package_exists $@ ; then |
|
echo "Removing $@" |
|
sudo apt-get purge -y $@ |
|
fi |
|
} |
|
|
|
function _sys_installreq { |
|
for pkg in $BUILD_ESSENTIALS $PYTHON_REQUIREMENTS; do |
|
if dpkg --get-selections | grep -q "^$pkg[[:space:]]*install$" >/dev/null; then |
|
echo -e "$pkg is already installed" |
|
else |
|
# if ! _func_cansudo; then |
|
# echo "Could not sudo please run as user who can" |
|
# exit 1 |
|
# fi |
|
if sudo apt-get -qq install $pkg; then |
|
echo "Successfully installed $pkg" |
|
else |
|
echo "Error installing $pkg" |
|
fi |
|
fi |
|
done |
|
} |
|
|
|
function _sys_update { |
|
echo "Running system update commands" |
|
sudo apt-get -y update |
|
} |
|
|
|
function _sys_clean { |
|
_func_ifclean $BUILD_ESSENTIALS $PYTHON_REQUIREMENTS |
|
} |
|
|
|
function _sys_prep { |
|
echo " * Preparing environment in $ENV_BASE" |
|
sudo mkdir -p $ENV_BASE/{packages,builds}/python |
|
sudo chown -R $USER:$USER $ENV_BASE |
|
} |
|
|
|
function _sys_link { |
|
if [ ! -d "$HOME/bin" ] ; then |
|
mkdir "$HOME/bin" |
|
fi |
|
for bin in $@; do |
|
if [ -h "$HOME/bin/$bin" ] ; then |
|
rm "$HOME/bin/$bin" |
|
fi |
|
if [ -f $ENV_BASE/builds/python/current/bin/$bin ] ; then |
|
ln -s $ENV_BASE/builds/python/current/bin/$bin "$HOME/bin/$bin" |
|
else |
|
echo "Could not link $bin not found: $ENV_BASE/builds/python/current/bin/$bin" |
|
fi |
|
done |
|
source ~/.profile |
|
} |
|
|
|
function _py_download { |
|
echo " * Downloading and unpacking Python $VERSION" |
|
wget -nv https://python.org/ftp/python/$VERSION/Python-$VERSION.tgz -O $ENV_BASE/packages/python/Python-$VERSION.tgz |
|
if [ ! -f $ENV_BASE/packages/python/Python-$VERSION.tgz ] ; then |
|
echo "Could not download - file not at $ENV_BASE/packages/python/Python-$VERSION.tgz" |
|
exit 0 |
|
fi |
|
tar -xf $ENV_BASE/packages/python/Python-$VERSION.tgz -C $ENV_BASE/packages/python/ |
|
if [ -h "$ENV_BASE/packages/python/current" ] ; then |
|
rm $ENV_BASE/packages/python/current |
|
fi |
|
ln -s $ENV_BASE/packages/python/Python-$VERSION $ENV_BASE/packages/python/current |
|
} |
|
|
|
function _py_build { |
|
echo " * Building Python in $ENV_BASE/builds/python/$VERSION" |
|
# compile python source to new directory |
|
cd $ENV_BASE/packages/python/current |
|
./configure --prefix="$ENV_BASE/builds/python/$VERSION" 1>/dev/null |
|
make install 1>/dev/null |
|
if [ -h "$ENV_BASE/builds/python/current" ] ; then |
|
rm $ENV_BASE/builds/python/current |
|
fi |
|
if [ ! -d "$ENV_BASE/builds/python/$VERSION/bin" ] ; then |
|
echo "Didn't install properly something went wrong" |
|
exit 1 |
|
fi |
|
ln -s $ENV_BASE/builds/python/$VERSION $ENV_BASE/builds/python/current |
|
} |
|
|
|
function _py_install_setuptools { |
|
wget -nv https://bootstrap.pypa.io/ez_setup.py -O - | $ENV_BASE/builds/python/current/bin/python |
|
} |
|
|
|
function _py_install_pip { |
|
# use setuptools to install pip |
|
$ENV_BASE/builds/python/current/bin/easy_install -s $ENV_BASE/builds/python/current/bin/ -d $ENV_BASE/builds/python/current/lib/python2.7/site-packages/ pip |
|
$ENV_BASE/builds/python/current/bin/pip -V |
|
} |
|
|
|
function _py_install_virtualenv { |
|
# install virtualenv |
|
$ENV_BASE/builds/python/current/bin/pip install virtualenv |
|
$ENV_BASE/builds/python/current/bin/pip install virtualenvwrapper |
|
export WORKON_HOME=~/.environments |
|
mkdir -p $WORKON_HOME |
|
echo "export WORKON_HOME=~/.environments" >> ~/.profile |
|
. ~/.profile |
|
} |
|
|
|
function _py_unlink { |
|
for bin in $PYTHON_BINS; do |
|
if [ -h "$HOME/bin/$bin" ] ; then |
|
echo "Unlinked $HOME/bin/$bin" |
|
rm "$HOME/bin/$bin" |
|
fi |
|
done |
|
} |
|
|
|
function _py_test { |
|
PYVER=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:3])))') |
|
echo "Running Python from `which python` version "$PYVER |
|
} |
|
|
|
function _main { |
|
_func_checkroot |
|
_sys_installreq |
|
_sys_prep |
|
_py_download |
|
_py_build |
|
_py_unlink |
|
_sys_link $PYTHON_BINS |
|
_py_test |
|
read -p "Install setuptools and pip? " -n 1 -r |
|
echo # (optional) move to a new line |
|
if [[ ! $REPLY =~ ^[Yy]$ ]] |
|
then |
|
_py_install_setuptools |
|
_py_install_pip |
|
_sys_link $PYTHON_BINS_EXTRAS |
|
read -p "Install virtualenv and virtualenvwrapper? " -n 1 -r |
|
echo # (optional) move to a new line |
|
if [[ ! $REPLY =~ ^[Yy]$ ]] |
|
then |
|
_py_install_virtualenv |
|
fi |
|
fi |
|
} |
|
|
|
_main |
|
|