-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython-upgrade-all
executable file
·54 lines (46 loc) · 1.85 KB
/
python-upgrade-all
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# Script to upgrade Python in all pyenv virtualenvs
newVersion=`pyenv install --list | egrep "^\s*[0-9]*\.[0-9]*(\.[0-9]*)?$" | tail -1 | xargs`
oldVersions=()
packageListPathPrefix="$HOME/Google Drive/System/Package Lists/python-"
packageListPathSuffix=".txt"
forceReinstall="false"
# Force reinstall will rebuild virtualenvs even if they use the latest Python
if [ "$1" == "--force-reinstall" ]; then
forceReinstall="true"
fi
# Switch pyenv to system Python
pyenv global system
# Install latest Python
echo "Installing latest Python version..."
pyenv install "$newVersion" --skip-existing
# Rebuild Pyenv virtualenvs
for virtualenv in `pyenv virtualenvs --bare --skip-aliases`; do
virtualenvVersion=${virtualenv%%/*}
virtualenvName=${virtualenv##*/}
if [ "$virtualenvVersion" != "$newVersion" ] || [ "$forceReinstall" == "true" ]; then
echo "Rebuilding "$virtualenvName"..."
if [ "$virtualenvVersion" != "$newVersion" ] && [[ ! " ${oldVersions[@]} " =~ " $virtualenvVersion " ]]; then
oldVersions+=("$virtualenvVersion")
fi
echo "Removing "$virtualenvName"..."
pyenv uninstall --force "$virtualenv"
echo "Recreating "$virtualenvName"..."
pyenv virtualenv "$newVersion" "$virtualenvName"
pyenv global "$virtualenvName"
echo "Upgrading base packages..."
pip-upgrade
packageListPath="${packageListPathPrefix}${virtualenvName}${packageListPathSuffix}"
if [ -f "$packageListPath" ]; then
echo "Reinstalling packages for "$virtualenvName" from "$packageListPath"..."
pip install -r "$packageListPath"
fi
fi
done
# Switch pyenv to system Python
pyenv global system
# Uninstall old Python versions
for version in "${oldVersions[@]}"; do
echo "Uninstalling "$version"..."
pyenv uninstall --force "$version"
done