Do not re-download already installed Python versions

This commit is contained in:
Zanie 2024-01-26 13:22:04 -06:00
parent 5cc4e5d31e
commit f21c033db8
1 changed files with 27 additions and 10 deletions

View File

@ -63,12 +63,37 @@ interpreter='cpython'
# On macOS, we need a newer version of `realpath` for `--relative-to` support
realpath="$(which grealpath || which realpath)"
# Utility for linking executables for the version being installed
# We need to update executables even if the version is already downloaded and extracted
# to ensure that changes to the precedence of versions are respected
link_executables() {
# Use relative paths for links so if the bin is moved they don't break
local link=$($realpath --relative-to="$bin_dir" "$install_key/install/bin/python3")
local minor=$(jq --arg key "$key" '.[$key] | .minor' -r < "$versions_metadata")
# Link as all version tuples, later versions in the file will take precedence
ln -sf "./$link" "$bin_dir/python$version"
ln -sf "./$link" "$bin_dir/python3.$minor"
ln -sf "./$link" "$bin_dir/python3"
ln -sf "./$link" "$bin_dir/python"
}
# Read requested versions into an array
readarray -t versions < "$versions_file"
# Install each version
for version in "${versions[@]}"; do
key="$interpreter-$version-$os-$arch"
install_key="$install_dir/$interpreter@$version"
if [ -d "$install_key" ]; then
echo "Already installed, skipping download"
link_executables
echo "Updated executables for python$version"
continue
fi
echo "Installing $key"
url=$(jq --arg key "$key" '.[$key] | .url' -r < "$versions_metadata")
@ -91,7 +116,6 @@ for version in "${versions[@]}"; do
echo " OK"
fi
install_key="$install_dir/$interpreter@$version"
rm -rf "$install_key"
echo "Extracting to $($realpath --relative-to="$root_dir" "$install_key")"
mkdir -p "$install_key"
@ -99,16 +123,9 @@ for version in "${versions[@]}"; do
# Setup the installation
mv "$install_key/python/"* "$install_key"
# Use relative paths for links so if the bin is moved they don't break
link=$($realpath --relative-to="$bin_dir" "$install_key/install/bin/python3")
minor=$(jq --arg key "$key" '.[$key] | .minor' -r < "$versions_metadata")
# Link as all version tuples, later versions in the file will take precedence
ln -sf "./$link" "$bin_dir/python$version"
ln -sf "./$link" "$bin_dir/python3.$minor"
ln -sf "./$link" "$bin_dir/python3"
ln -sf "./$link" "$bin_dir/python"
echo "Installed as python$version"
link_executables
echo "Installed executables for python$version"
# Cleanup
rmdir "$install_key/python/"