Skip to content

Instantly share code, notes, and snippets.

@stek29
Created March 27, 2019 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stek29/f1f24c4def689628ff67fa30c4d83ab2 to your computer and use it in GitHub Desktop.
Save stek29/f1f24c4def689628ff67fa30c4d83ab2 to your computer and use it in GitHub Desktop.
Fix Cellar versioned links in virtualenvs
#!/usr/bin/env bash
# Python virtualenvs contain references to base python
# For brew installed python they reference versioned Cellar
# Which breaks even on minor update
# This hacky script rewrites all those references to opt based,
# unversioned ones.
#
# Example usage: fixvenvopt.sh ~/.virtualenvs/jupyter
set -euo pipefail
noversion() {
# Yup, sed spawned on each invokation. I'm not proud of this either.
printf "%s" "$1" | sed 's#Cellar/python/[^/]*/#opt/python/#'
}
if [[ "$#" != 1 ]]; then
echo "Usage: $0 virtualenv_path"
exit 1
fi
cd "$1"
for f in $(find . -type l); do
lnk="$(readlink "$f")"
if [[ $lnk = *Cellar* ]]; then
rm "$f"
ln -s "$(noversion $lnk)" "$f"
fi
done
orig_prefix_v="$(cat lib/python*/orig-prefix.txt)"
# tee is needed to force glob expansion
echo "$(noversion "$orig_prefix_v")" | tee lib/python*/orig-prefix.txt >/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment