PythonInstalled, name includes modules

If `depends_on :python => ['modulename', :optional]` then the generated
option is now `--with-python-modulename`, so that it is possible to
actually make depending on python modules optional.
Further, `brew options` becomes more meaningful.
This commit is contained in:
Samuel John 2013-08-26 20:00:03 +02:00
parent 77056dc5cd
commit 2e2e46b5b4

View File

@ -17,6 +17,7 @@ class PythonInstalled < Requirement
attr_reader :min_version attr_reader :min_version
attr_reader :if3then3 attr_reader :if3then3
attr_reader :imports attr_reader :imports
attr_reader :python
attr_accessor :site_packages attr_accessor :site_packages
attr_writer :binary # The python.rb formula needs to set the binary attr_writer :binary # The python.rb formula needs to set the binary
@ -47,9 +48,7 @@ class PythonInstalled < Requirement
@if3then3 = "" @if3then3 = ""
end end
# Set name according to the major version. @python = "python"+@if3then3
# The name is used to generate the options like --without-python3
@name = "python" + @if3then3
# Check if any python modules should be importable. We use a hash to store # Check if any python modules should be importable. We use a hash to store
# the corresponding name on PyPi "<import_name>" => "<name_on_PyPi>". # the corresponding name on PyPi "<import_name>" => "<name_on_PyPi>".
@ -63,6 +62,11 @@ class PythonInstalled < Requirement
end end
end end
# Set name according to the major version and optionally python modules:
# Used to generate the options like --without-python3, --with-python-numpy
@name = "python#{@if3then3}"
@name += "-#{@imports.values*'-'}" unless @imports.empty?
# will be set later by the python_helper, because it needs the # will be set later by the python_helper, because it needs the
# formula prefix to set site_packages # formula prefix to set site_packages
@site_packages = nil @site_packages = nil
@ -77,13 +81,13 @@ class PythonInstalled < Requirement
ENV['PYTHONPATH'] = nil ENV['PYTHONPATH'] = nil
@unsatisfied_because = '' @unsatisfied_because = ''
if binary.nil? || !binary.executable? if binary.nil? || !binary.executable?
@unsatisfied_because += "No `#{@name}` found in your PATH! Consider to `brew install #{@name}`." @unsatisfied_because += "No `#{@python}` found in your PATH! Consider to `brew install #{@python}`."
false false
elsif pypy? elsif pypy?
@unsatisfied_because += "Your #{@name} executable appears to be a PyPy, which is not supported." @unsatisfied_because += "Your #{@python} executable appears to be a PyPy, which is not supported."
false false
elsif version.major != @min_version.major elsif version.major != @min_version.major
@unsatisfied_because += "No Python #{@min_version.major}.x found in your PATH! --> `brew install #{@name}`?" @unsatisfied_because += "No Python #{@min_version.major}.x found in your PATH! --> `brew install #{@python}`?"
false false
elsif version < @min_version elsif version < @min_version
@unsatisfied_because += "Python version #{version} is too old (need at least #{@min_version})." @unsatisfied_because += "Python version #{version} is too old (need at least #{@min_version})."
@ -119,9 +123,9 @@ class PythonInstalled < Requirement
if brewed? if brewed?
# If the python is brewed we always prefer it! # If the python is brewed we always prefer it!
# Note, we don't support homebrew/versions/pythonXX.rb, though. # Note, we don't support homebrew/versions/pythonXX.rb, though.
Formula.factory(@name).opt_prefix/"bin/python#{@min_version.major}" Formula.factory(@python).opt_prefix/"bin/python#{@min_version.major}"
else else
which(@name) which(@python)
end end
end end
end end
@ -130,7 +134,7 @@ class PythonInstalled < Requirement
def prefix def prefix
if brewed? if brewed?
# Homebrew since a long while only supports frameworked python # Homebrew since a long while only supports frameworked python
HOMEBREW_PREFIX/"opt/#{name}/Frameworks/Python.framework/Versions/#{version.major}.#{version.minor}" HOMEBREW_PREFIX/"opt/#{python}/Frameworks/Python.framework/Versions/#{version.major}.#{version.minor}"
elsif from_osx? elsif from_osx?
# Python on OS X has been stripped off its includes (unless you install the CLT), therefore we use the MacOS.sdk. # Python on OS X has been stripped off its includes (unless you install the CLT), therefore we use the MacOS.sdk.
Pathname.new("#{MacOS.sdk_path}/System/Library/Frameworks/Python.framework/Versions/#{version.major}.#{version.minor}") Pathname.new("#{MacOS.sdk_path}/System/Library/Frameworks/Python.framework/Versions/#{version.major}.#{version.minor}")
@ -194,7 +198,7 @@ class PythonInstalled < Requirement
def brewed? def brewed?
@brewed ||= begin @brewed ||= begin
require 'formula' require 'formula'
Formula.factory(@name).linked_keg.exist? Formula.factory(@python).linked_keg.exist?
end end
end end
@ -229,7 +233,7 @@ class PythonInstalled < Requirement
"" # empty string, so we can concat this "" # empty string, so we can concat this
else else
<<-EOS.undent <<-EOS.undent
For non-homebrew #{@name} (#{@min_version.major}.x), you need to amend your PYTHONPATH like so: For non-homebrew #{@python} (#{@min_version.major}.x), you need to amend your PYTHONPATH like so:
export PYTHONPATH=#{global_site_packages}:$PYTHONPATH export PYTHONPATH=#{global_site_packages}:$PYTHONPATH
EOS EOS
end end
@ -266,7 +270,7 @@ class PythonInstalled < Requirement
# Todo: If Jack's formula revisions arrive, we can get rid of this here! # Todo: If Jack's formula revisions arrive, we can get rid of this here!
if brewed? if brewed?
require 'formula' require 'formula'
file = Formula.factory(@name).prefix/"Frameworks/Python.framework/Versions/#{version.major}.#{version.minor}/lib/#{xy}/distutils/distutils.cfg" file = Formula.factory(@python).prefix/"Frameworks/Python.framework/Versions/#{version.major}.#{version.minor}/lib/#{xy}/distutils/distutils.cfg"
ohai "Writing #{file}" if ARGV.verbose? && ARGV.debug? ohai "Writing #{file}" if ARGV.verbose? && ARGV.debug?
file.delete if file.exist? file.delete if file.exist?
file.write <<-EOF.undent file.write <<-EOF.undent
@ -314,12 +318,12 @@ class PythonInstalled < Requirement
# Assume Framework style build (default since months in brew) # Assume Framework style build (default since months in brew)
try: try:
from _sysconfigdata import build_time_vars from _sysconfigdata import build_time_vars
build_time_vars['LINKFORSHARED'] = '-u _PyMac_Error #{HOMEBREW_PREFIX}/opt/#{name}/Frameworks/Python.framework/Versions/#{version.major}.#{version.minor}/Python' build_time_vars['LINKFORSHARED'] = '-u _PyMac_Error #{HOMEBREW_PREFIX}/opt/#{python}/Frameworks/Python.framework/Versions/#{version.major}.#{version.minor}/Python'
except: except:
pass # remember: don't print here. Better to fail silently. pass # remember: don't print here. Better to fail silently.
# Set the sys.executable to use the opt_prefix # Set the sys.executable to use the opt_prefix
sys.executable = '#{HOMEBREW_PREFIX}/opt/#{name}/bin/#{xy}' sys.executable = '#{HOMEBREW_PREFIX}/opt/#{python}/bin/#{xy}'
# Tell about homebrew's site-packages location. # Tell about homebrew's site-packages location.
# This is needed for Python to parse *.pth. # This is needed for Python to parse *.pth.
@ -341,13 +345,12 @@ class PythonInstalled < Requirement
end end
# Objects of this class are used to represent dependencies on Python and # Objects of this class are used to represent dependencies on Python and
# dependencies on Python modules, so the combination of name + imports is # dependencies on Python modules. Both are already included in `name`
# enough to identify them uniquely.
def eql?(other) def eql?(other)
instance_of?(other.class) && name == other.name && imports == other.imports instance_of?(other.class) && name == other.name
end end
def hash def hash
[name, *imports].hash name.hash
end end
end end