Skip to content

Instantly share code, notes, and snippets.

@Hypnoiku
Last active December 25, 2018 07:59
Show Gist options
  • Save Hypnoiku/0d0109343b74accf4b00b40ca4a39915 to your computer and use it in GitHub Desktop.
Save Hypnoiku/0d0109343b74accf4b00b40ca4a39915 to your computer and use it in GitHub Desktop.
Script to install multiple python package updates at once
#!/bin/sh
# pipUpgrade script to install multiple python package updates at once
# Compatible/tested with msys2 on Windows / Linux
#----------
# If you want to use this script in ConEmu with cmd, follow these steps (assuming both msys2 and python are already installed)
# a. Add a MSYS_HOME env variable pointing to C:\MSYS2_INSTALL_DIR\usr
# b. Add 'alias pipUpgrade=%MSYS_HOME%\bin\sh "C:\path\to\pipUpgrade.sh" $1 $2 $3' to Settings/Startup/Environment (without single quotation marks)
# c. Uncomment the following line
#PATH=$PATH:$MSYS_HOME\\bin
# d. Within ConEmu with the cmd shell, simply run 'pipUpgrade'
# e. profit
#----------
#Help screen
if [ "$1" = "--help" ]; then
echo "pipUpgrade: Install multiple python package updates at once"
printf "Usage: ./pipUpgrade.sh [--help] [-y] [--pythonVer=[2|3]] [-win]\n\n"
printf -- " --help\n Display this help message\n\n"
printf -- " --list-all\n List all installed packages\n\n"
printf -- " --list-outdated\n List outdated packages\n\n"
printf -- " --pythonVer\n Define python version to use (Linux only unless '-win' is used).\n Use --pythonVer=2 for python 2.x or --pythonVer=3 for python 3.x.\n\n"
printf -- " -y\n Answer yes to all questions\n\n"
printf -- " --win\n Uses py executable instead of python2 or python3\n"
exit 0
fi
#Set arguments
YES=0
PYVER=0
WIN=0
if [ "$1" = "-y" ] || [ "$2" = "-y" ] || [ "$3" = "-y" ]; then
YES=1
fi
if [ "$1" = "--pythonVer=2" ] || [ "$2" = "--pythonVer=2" ] || [ "$3" = "--pythonVer=2" ]; then
PYVER=2
fi
if [ "$1" = "--pythonVer=3" ] || [ "$2" = "--pythonVer=3" ] || [ "$3" = "--pythonVer=3" ]; then
PYVER=3
fi
if [ "$1" = "--win" ] || [ "$2" = "--win" ] || [ "$3" = "--win" ]; then
WIN=1
fi
#List packages
if [ "$1" = "--list-all" ] || [ "$2" = "--list-all" ] || [ "$3" = "--list-all" ]; then
if [ "$WIN" = 1 ]; then
case "$PYVER" in
"2") py -2 -m pip list --format freeze | cut -d = -f 1; break;;
"3") py -3 -m pip list --format freeze | cut -d = -f 1; break;;
*) pip list --format freeze | cut -d = -f 1;;
esac
else
case "$PYVER" in
"2") python2 -m pip list --format freeze | cut -d = -f 1; break;;
"3") python3 -m pip list --format freeze | cut -d = -f 1; break;;
*) pip list --format freeze | cut -d = -f 1;;
esac
fi
exit 0
fi
if [ "$1" = "--list-outdated" ] || [ "$2" = "--list-outdated" ] || [ "$3" = "--list-outdated" ]; then
if [ "$WIN" = 1 ]; then
case "$PYVER" in
"2") py -2 -m pip list --outdated --format freeze | cut -d = -f 1; break;;
"3") py -3 -m pip list --outdated --format freeze | cut -d = -f 1; break;;
*) pip list --outdated --format freeze | cut -d = -f 1;;
esac
else
case "$PYVER" in
"2") python2 -m pip list --outdated --format freeze | cut -d = -f 1; break;;
"3") python3 -m pip list --outdated --format freeze | cut -d = -f 1; break;;
*) pip list --outdated --format freeze | cut -d = -f 1;;
esac
fi
exit 0
fi
#Get package list
echo "Python packages to be upgraded:"
if [ "$WIN" = 1 ]; then
case "$PYVER" in
"2") OUTPUT="$(py -2 -m pip list --outdated --format freeze | cut -d = -f 1)"; break;;
"3") OUTPUT="$(py -3 -m pip list --outdated --format freeze | cut -d = -f 1)"; break;;
*) OUTPUT="$(pip list --outdated --format freeze | cut -d = -f 1)";;
esac
else
case "$PYVER" in
"2") OUTPUT="$(python2 -m pip list --outdated --format freeze | cut -d = -f 1)"; break;;
"3") OUTPUT="$(python3 -m pip list --outdated --format freeze | cut -d = -f 1)"; break;;
*) OUTPUT="$(pip list --outdated --format freeze | cut -d = -f 1)";;
esac
fi
echo "${OUTPUT}"
if [ -z "$OUTPUT" ]; then
echo "All python packages are up to date. Nothing to do."
exit 0
fi
if [ "$YES" = 0 ]; then
printf "\n"
read -p "Are you sure you want to update these packages? (Y/N): " yn
case $yn in
[Yy]*) break;;
[Nn]*) exit 0;;
*) exit 0;;
esac
fi
#Update packages
printf "\nStarting upgrade:\n"
if [ "$WIN" = 1 ]; then
case "$PYVER" in
"2") echo "${OUTPUT}" | xargs py -2 -m pip install --upgrade; break;;
"3") echo "${OUTPUT}" | xargs py -3 -m pip install --upgrade; break;;
*) echo "${OUTPUT}" | xargs pip install --upgrade;;
esac
else
case "$PYVER" in
"2") echo "${OUTPUT}" | xargs python2 -m pip install --upgrade; break;;
"3") echo "${OUTPUT}" | xargs python3 -m pip install --upgrade; break;;
*) echo "${OUTPUT}" | xargs pip install --upgrade;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment