Demo entry 5647416

Download, Compile, Install Python

   

Submitted by anonymous on Jul 06, 2016 at 21:29
Language: Bash. Code size: 4.0 kB.

#!/bin/bash

# -----------------------------------------
# DCIPy (Download, Compile, Install Python)
# -----------------------------------------

# This script was written in order to download, compile and install Python on a Raspberry Pi.
# So this should work on most Debian/Ubuntu distribs.
# Use at your own risks, because idk wtf I'm doing most of the time.

# If you notice any issue, please let me know.

# Usage :
#   DCIPy <python version>


# TODO : Check already installed python versions:
#        sudo find / -type f -executable -iname 'python*' -exec file -i '{}' \; | awk -F: '/x-executable; charset=binary/ {print $1}' | xargs readlink -f | sort -u | xargs -I % sh -c 'echo -n "%: "; % -V'



# The version numbers comparison function
# The helper function below will usually be the one to use, as it's more user friendly
# Parameters :
#   * $1 : a version number delimited by dots
#   * $2 : a version number delimited by dots
# Returns :
#   * 0 : $1 and $2 are equal
#   * 1 : $1 is bigger than $2
#   * 2 ! $1 is lower than $2
version_compare_impl () {
  #ver1 == ver2 ?
  if [[ $1 == $2 ]]
  then
    return 0
  fi

  local IFS=.
  local i ver1=($1) ver2=($2)

  # Fill empty fields in ver1 with zeros
  for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
  do
    ver1[i]=0
  done

  for ((i=0; i<${#ver1[@]}; i++))
  do
    if [[ -z ${ver2[i]} ]]
    then
      # Fill empty fields in ver2 with zeros
      ver2[i]=0
    fi

    # ver1 > ver2 ?
    if ((10#${ver1[i]} > 10#${ver2[i]}))
    then
      return 1
    fi

    # ver1 < ver2 ?
    if ((10#${ver1[i]} < 10#${ver2[i]}))
    then
      return 2
    fi
  done

  # Then ver1 == ver2
  return 0
}

# Version numbers comparison helper
# Parameters :
#   * $1 : a version number delimited by dots
#   * $2 : the comparison operator, '<', '=' or '>'
#   * $3 : a version number delimited by dots
# Returns :
#   * 0 : the comparison is False
#   * 1 : the comparison is True
version_compare () {
  version_compare_impl $1 $3

  case $? in
    0) op='=';;
    1) op='>';;
    2) op='<';;
  esac

  if [[ $op != $2 ]]
  then
    return 1
  else
    return 0
  fi
}

# ---------------------- #
# Let's start the work !
# ---------------------- #

# Check args
if [ $# != 1 ]
then
  echo -e "Usage :\n    $0 <python version number>"
  exit 1
fi

version=$1
remote_directory="https://www.python.org/ftp/python/$version"

IFS=. read -r major minor micro <<< "$version"
bin_ver=$major.$minor
cmd_ver=$major

# Check version's existence
if ! wget -S --spider $remote_directory  2>&1 | grep -q 'HTTP[^\n\0]\{1,\} 200 OK'
then
  echo Python $version not found.
  exit 0
fi

# Update system
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade

# Install components needed for the build
sudo apt-get install -y build-essential libncursesw5-dev libgdbm-dev libc6-dev
sudo apt-get install -y zlib1g-dev libsqlite3-dev tk-dev
sudo apt-get install -y libssl-dev openssl

# Retreive and unpack sources
cd ~
wget $remote_directory/Python-$version.tgz
tar -zxvf Python-$version.tgz

# Configure, build and install Python
cd Python-$version
./configure
make
sudo make install
cd ~

# Cleanup
sudo rm -rf Python-$version
rm Python-$version.tgz

# Check if we need to instal pip (py2.4.9, py3.4 and higher come with it)
if version_compare $version '<' 2.4.9 || \
   version_compare $version '=' 3 ||
   (version_compare $version '>' 3 && version_compare $version '<' 3.4)
then
  install_pip=True
fi

# Download and install pip
if [ $install_pip ]
then
  wget https://bootstrap.pypa.io/get-pip.py
  sudo python$bin_ver get-pip.py
fi

# Version checks
echo
echo python$bin_ver: $(python$bin_ver --version)
echo python$cmd_ver: $(python$cmd_ver --version)
echo
echo pip$bin_ver: $(pip$bin_ver --version)
echo pip$cmd_ver: $(pip$cmd_ver --version)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).