aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Formula
diff options
context:
space:
mode:
authorMike McQuaid2014-01-20 14:40:30 -0800
committerMike McQuaid2014-01-20 15:43:00 -0800
commit0a82b3a90b2cf5979bb843aaae1998a90ac3d1ab (patch)
tree030bd65c7bb5d003b6d7f2ecc2e6b5f669a51ad2 /Library/Formula
parentccc0a3a24d0d96ff035d34586865617c47c74b4a (diff)
downloadhomebrew-0a82b3a90b2cf5979bb843aaae1998a90ac3d1ab.tar.bz2
python: write sitecustomize.py in formula.
Diffstat (limited to 'Library/Formula')
-rw-r--r--Library/Formula/python.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/Library/Formula/python.rb b/Library/Formula/python.rb
index 6dbee657a..e0f8ce3f5 100644
--- a/Library/Formula/python.rb
+++ b/Library/Formula/python.rb
@@ -121,6 +121,10 @@ class Python < Formula
# Symlink the prefix site-packages into the cellar.
ln_s site_packages, site_packages_cellar
+ # Write our sitecustomize.py
+ Dir["#{site_packages}/*.py{,c,o}"].each {|f| Pathname.new(f).unlink }
+ (site_packages/"sitecustomize.py").write(sitecustomize)
+
# Remove old setuptools installations that may still fly around and be
# listed in the easy_install.pth. This can break setuptools build with
# zipimport.ZipImportError: bad local file header
@@ -220,6 +224,56 @@ class Python < Formula
end
end
+ def sitecustomize
+ <<-EOF.undent
+ # This file is created by Homebrew and is executed on each python startup.
+ # Don't print from here, or else python command line scripts may fail!
+ # <https://github.com/Homebrew/homebrew/wiki/Homebrew-and-Python>
+ import os
+ import sys
+
+ if sys.version_info[0] != 2:
+ # This can only happen if the user has set the PYTHONPATH for 3.x and run Python 2.x or vice versa.
+ # Every Python looks at the PYTHONPATH variable and we can't fix it here in sitecustomize.py,
+ # because the PYTHONPATH is evaluated after the sitecustomize.py. Many modules (e.g. PyQt4) are
+ # built only for a specific version of Python and will fail with cryptic error messages.
+ # In the end this means: Don't set the PYTHONPATH permanently if you use different Python versions.
+ exit('Your PYTHONPATH points to a site-packages dir for Python 2.x but you are running Python ' +
+ str(sys.version_info[0]) + '.x!\\n PYTHONPATH is currently: "' + str(os.environ['PYTHONPATH']) + '"\\n' +
+ ' You should `unset PYTHONPATH` to fix this.')
+ else:
+ # Only do this for a brewed python:
+ opt_executable = '#{HOMEBREW_PREFIX}/opt/python/bin/python2.7'
+ if os.path.realpath(sys.executable) == os.path.realpath(opt_executable):
+ # Remove /System site-packages, and the Cellar site-packages
+ # which we moved to lib/pythonX.Y/site-packages. Further, remove
+ # HOMEBREW_PREFIX/lib/python because we later addsitedir(...).
+ sys.path = [ p for p in sys.path
+ if (not p.startswith('/System') and
+ not p.startswith('#{HOMEBREW_PREFIX}/lib/python') and
+ not (p.startswith('#{HOMEBREW_PREFIX}/Cellar/python') and p.endswith('site-packages'))) ]
+
+ # LINKFORSHARED (and python-config --ldflags) return the
+ # full path to the lib (yes, "Python" is actually the lib, not a
+ # dir) so that third-party software does not need to add the
+ # -F/#{HOMEBREW_PREFIX}/Frameworks switch.
+ # Assume Framework style build (default since months in brew)
+ try:
+ from _sysconfigdata import build_time_vars
+ build_time_vars['LINKFORSHARED'] = '-u _PyMac_Error #{HOMEBREW_PREFIX}/opt/python/Frameworks/Python.framework/Versions/2.7/Python'
+ except:
+ pass # remember: don't print here. Better to fail silently.
+
+ # Set the sys.executable to use the opt_prefix
+ sys.executable = opt_executable
+
+ # Tell about homebrew's site-packages location.
+ # This is needed for Python to parse *.pth.
+ import site
+ site.addsitedir('#{HOMEBREW_PREFIX}/lib/python2.7/site-packages')
+ EOF
+ end
+
def caveats
<<-EOS.undent
Python demo