From bdd466f83866e335d016ce251aca4eb8d19fd44a Mon Sep 17 00:00:00 2001
From: Kenneth Reitz <me@kennethreitz.org>
Date: Mon, 19 Dec 2016 22:04:51 -0500
Subject: [PATCH] cleanup pip install steps, caching src

---
 bin/compile           | 10 +++++++++-
 bin/steps/pip-install |  9 +++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/bin/compile b/bin/compile
index 005eb4de..1d9058af 100755
--- a/bin/compile
+++ b/bin/compile
@@ -148,11 +148,14 @@ bpwatch start restore_cache
     cp -R $CACHE_DIR/.heroku/python-version .heroku/ &> /dev/null || true
     cp -R $CACHE_DIR/.heroku/vendor .heroku/ &> /dev/null || true
     cp -R $CACHE_DIR/.heroku/venv .heroku/ &> /dev/null || true
+    if [[ -d $CACHE_DIR/.heroku/src ]]; then
+      cp -R $CACHE_DIR/.heroku/src .heroku/ &> /dev/null || true
+    fi
 
 bpwatch stop restore_cache
 
 mkdir -p $(dirname $PROFILE_PATH)
-mkdir /app/.heroku/src
+mkdir -p /app/.heroku/src
 
 if [[ $BUILD_DIR != '/app' ]]; then
     # python expects to reside in /app, so set up symlinks
@@ -160,6 +163,7 @@ if [[ $BUILD_DIR != '/app' ]]; then
     ln -s $BUILD_DIR/.heroku/python /app/.heroku/python
     ln -s $BUILD_DIR/.heroku/vendor /app/.heroku/vendor
     ln -s $BUILD_DIR/.heroku/venv /app/.heroku/venv
+    # Note: .heroku/src is copied in later.
 fi
 
 # Install Python.
@@ -228,6 +232,7 @@ bpwatch start dump_cache
   rm -rf $CACHE_DIR/.heroku/python-stack
   rm -rf $CACHE_DIR/.heroku/vendor
   rm -rf $CACHE_DIR/.heroku/venv
+  rm -rf $CACHE_DIR/.heroku/src
 
   mkdir -p $CACHE_DIR/.heroku
   cp -R .heroku/python $CACHE_DIR/.heroku/
@@ -235,6 +240,9 @@ bpwatch start dump_cache
   cp -R .heroku/python-stack $CACHE_DIR/.heroku/ &> /dev/null || true
   cp -R .heroku/vendor $CACHE_DIR/.heroku/ &> /dev/null || true
   cp -R .heroku/venv $CACHE_DIR/.heroku/ &> /dev/null || true
+  if [[ -d .heroku/src ]]; then
+    cp -R .heroku/src $CACHE_DIR/.heroku/ &> /dev/null || true
+  fi
 
 bpwatch stop dump_cache
 
diff --git a/bin/steps/pip-install b/bin/steps/pip-install
index 7dea3cea..ab02c105 100755
--- a/bin/steps/pip-install
+++ b/bin/steps/pip-install
@@ -1,6 +1,10 @@
 # Install dependencies with Pip.
 puts-cmd "pip install -r requirements.txt"
 
+# rewrite any existing egg links to point to the build dir so pip does not fail with a mismatch error
+find .heroku/python/lib/python*/site-packages/ -name "*.egg-link" -print0 | xargs -r -0 -n 1 sed -i -e "s#/app/#$(pwd)/#"
+find .heroku/python/lib/python*/site-packages/ -name "*.pth" -print0 | xargs -r -0 -n 1 sed -i -e "s#/app/#/$(pwd)/#"
+
 [ ! "$FRESH_PYTHON" ] && bpwatch start pip_install
 [ "$FRESH_PYTHON" ] && bpwatch start pip_install_first
 
@@ -23,4 +27,9 @@ cp requirements.txt .heroku/python/requirements-declared.txt
 [ ! "$FRESH_PYTHON" ] && bpwatch stop pip_install
 [ "$FRESH_PYTHON" ] && bpwatch stop pip_install_first
 
+# rewrite build dir in egg links to /app so things are found at runtime
+find .heroku/python/lib/python*/site-packages/ -name "*.egg-link" -print0 | xargs -r -0 -n 1 sed -i -e "s#$(pwd)/#/app/#"
+find .heroku/python/lib/python*/site-packages/ -name "*.pth" -print0 | xargs -r -0 -n 1 sed -i -e "s#$(pwd)/#/app/#"
+
+
 echo
-- 
GitLab