PythonInstalled: Allow formulae to set/append PYTHONPATH

Improve robustness of `PYTHONPATH` by first unsetting it (during
`satisfy`) so that the `PythonInstalled` can get the `python.version`
and so forth and then, after that, setting the `PYTHONPATH` to our
`global_site_packages`.
In the `python_helper` we append to the `PYTHONPATH` so if that var has
been set in a formula, it is respected.
Brew audit does no longer complain about setting the
`ENV['PYTHONPATH']`.
This commit is contained in:
Samuel John 2013-09-03 10:46:06 +02:00
parent dbaac79f17
commit 5515fda59a
2 changed files with 50 additions and 49 deletions

View File

@ -568,13 +568,7 @@ class FormulaAuditor
end
end
if f.requirements.any?{ |r| r.kind_of?(PythonInstalled) }
# Don't check this for all formulae, because some are allowed to set the
# PYTHONPATH. E.g. python.rb itself needs to set it.
if text =~ /ENV\.append.*PYTHONPATH/ || text =~ /ENV\[['"]PYTHONPATH['"]\]\s*=[^=]/
problem "Don't set the PYTHONPATH, instead declare `depends_on :python`"
end
else
unless f.requirements.any?{ |r| r.kind_of?(PythonInstalled) }
# So if there is no PythonInstalled requirement, we can check if the
# formula still uses python and should add a `depends_on :python`
unless f.name.to_s =~ /(pypy[0-9]*)|(python[0-9]*)/

View File

@ -80,6 +80,9 @@ class PythonInstalled < Requirement
# We look for a brewed python or an external Python and store the loc of
# that binary for later usage. (See Formula#python)
satisfy :build_env => false do
begin
# Unset the PYTHONPATH during these tests, but later ensure it is restored.
pythonpath = ENV['PYTHONPATH']
ENV['PYTHONPATH'] = nil
@unsatisfied_because = ''
if binary.nil? || !binary.executable?
@ -113,6 +116,9 @@ class PythonInstalled < Requirement
end
end
end
ensure
ENV['PYTHONPATH'] = pythonpath
end
end
def importable? module_name
@ -196,7 +202,7 @@ class PythonInstalled < Requirement
end
end
# Is the brewed Python installed
# Is the brewed Python installed?
def brewed?
@brewed ||= begin
require 'formula'
@ -245,6 +251,18 @@ class PythonInstalled < Requirement
# Most methods fail if we don't have a binary.
return if binary.nil?
ENV['PYTHONHOME'] = nil # to avoid fuck-ups.
ENV['PYTHONPATH'] = nil # first unset, because global_site_packages may fail
ENV['PYTHONPATH'] = if brewed? then nil; else global_site_packages.to_s; end
# For non-system python's we add the opt_prefix/bin of python to the path.
ENV.prepend_path 'PATH', binary.dirname unless from_osx?
ENV.append_path 'CMAKE_INCLUDE_PATH', incdir
ENV.append_path 'PKG_CONFIG_PATH', pkg_config_path if pkg_config_path
# We don't set the -F#{framework} here, because if Python 2.x and 3.x are
# used, `Python.framework` is ambiguous. However, in the `python do` block
# we can set LDFLAGS+="-F#{framework}" because only one is temporarily set.
# Write our sitecustomize.py
file = global_site_packages/"sitecustomize.py"
ohai "Writing #{file}" if ARGV.verbose? && ARGV.debug?
@ -256,17 +274,6 @@ class PythonInstalled < Requirement
file.write(sitecustomize)
# For non-system python's we add the opt_prefix/bin of python to the path.
ENV.prepend_path 'PATH', binary.dirname unless from_osx?
ENV['PYTHONHOME'] = nil # to avoid fuck-ups.
ENV['PYTHONPATH'] = if brewed? then nil; else global_site_packages.to_s; end
ENV.append_path 'CMAKE_INCLUDE_PATH', incdir
ENV.append_path 'PKG_CONFIG_PATH', pkg_config_path if pkg_config_path
# We don't set the -F#{framework} here, because if Python 2.x and 3.x are
# used, `Python.framework` is ambiguous. However, in the `python do` block
# we can set LDFLAGS+="-F#{framework}" because only one is temporarily set.
# Udpate distutils.cfg (later we can remove this, but people still have
# their old brewed pythons and we have to update it here)
# Todo: If Jack's formula revisions arrive, we can get rid of this here!