From 1593b24b0f4d251bcbfcb8b1e6a31b512a8b7e11 Mon Sep 17 00:00:00 2001
From: "Tim D. Smith" <git@tim-smith.us>
Date: Sun, 2 Nov 2014 10:33:03 -0800
Subject: [PATCH] caveats: recommend adding python site-packages with
 site.addsitedir

Closes Homebrew/homebrew#33364. Fixes Homebrew/homebrew#30844.
---
 Library/Homebrew/caveats.rb | 53 ++++++++++++++++++++++++++++---------
 1 file changed, 40 insertions(+), 13 deletions(-)

diff --git a/Library/Homebrew/caveats.rb b/Library/Homebrew/caveats.rb
index d3e8928ffe..94096b4631 100644
--- a/Library/Homebrew/caveats.rb
+++ b/Library/Homebrew/caveats.rb
@@ -48,21 +48,48 @@ class Caveats
   def python_caveats
     return unless keg
     return unless keg.python_site_packages_installed?
-    return if Formula["python"].installed?
-    site_packages = if f.keg_only?
-      "#{f.opt_prefix}/lib/python2.7/site-packages"
-    else
-      "#{HOMEBREW_PREFIX}/lib/python2.7/site-packages"
+
+    s = nil
+    homebrew_site_packages = Language::Python.homebrew_site_packages
+    user_site_packages = Language::Python.user_site_packages "python"
+    pth_file = user_site_packages/"homebrew.pth"
+    instructions = <<-EOS.undent.gsub(/^/, "  ")
+      mkdir -p #{user_site_packages}
+      echo 'import site; site.addsitedir("#{homebrew_site_packages}")' >> #{pth_file}
+    EOS
+
+    if f.keg_only?
+      keg_site_packages = f.opt_prefix/"lib/python2.7/site-packages"
+      unless Language::Python.in_sys_path?("python", keg_site_packages)
+        s = <<-EOS.undent
+          If you need Python to find bindings for this keg-only formula, run:
+            echo #{keg_site_packages} >> #{homebrew_site_packages/f.name}.pth
+        EOS
+        s += instructions unless Language::Python.reads_brewed_pth_files?("python")
+      end
+      return s
     end
-    dir = "~/Library/Python/2.7/lib/python/site-packages"
-    dir_path = Pathname.new(dir).expand_path
-    file = "#{dir}/homebrew.pth"
-    file_path = Pathname.new(file).expand_path
-    if !file_path.readable? || !file_path.read.include?(site_packages)
-      s = "If you need Python to find the installed site-packages:\n"
-      s += "  mkdir -p #{dir}\n" unless dir_path.exist?
-      s += "  echo '#{site_packages}' > #{file}"
+
+    return if Language::Python.reads_brewed_pth_files?("python")
+
+    if !Language::Python.in_sys_path?("python", homebrew_site_packages)
+      s = <<-EOS.undent
+        Python modules have been installed and Homebrew's site-packages is not
+        in your Python sys.path, so you will not be able to import the modules
+        this formula installed. If you plan to develop with these modules,
+        please run:
+      EOS
+      s += instructions
+    elsif keg.python_pth_files_installed?
+      s = <<-EOS.undent
+        This formula installed .pth files to Homebrew's site-packages and your
+        Python isn't configured to process them, so you will not be able to
+        import the modules this formula installed. If you plan to develop
+        with these modules, please run:
+      EOS
+      s += instructions
     end
+    s
   end
 
   def app_caveats
-- 
GitLab