Skip to content

Instantly share code, notes, and snippets.

@stek29 stek29/fixvenvopt.sh
Created Mar 27, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.