Reduce nesting and remove extra whitespace in python_helper

This commit is contained in:
Adam Vandenberg 2013-07-02 10:19:31 -07:00
parent 5db5740cc2
commit eafa7a141d

View File

@ -1,4 +1,3 @@
# This is used in the Formula class when the user calls # This is used in the Formula class when the user calls
# `python`, `python2` or `python3`. # `python`, `python2` or `python3`.
@ -19,73 +18,73 @@ def python_helper(options={:allowed_major_versions => [2, 3]}, &block)
# We are already inside of a `python do ... end` block, so just return # We are already inside of a `python do ... end` block, so just return
# the current_python or false if the version.major is not allowed. # the current_python or false if the version.major is not allowed.
if options[:allowed_major_versions].include?(@current_python.version.major) if options[:allowed_major_versions].include?(@current_python.version.major)
@current_python return @current_python
else else
false return false
end end
else end
# Look for PythonInstalled requirements for this formula
python_reqs = requirements.select{ |r| r.kind_of?(PythonInstalled) } # Look for PythonInstalled requirements for this formula
if python_reqs.empty? python_reqs = requirements.select{ |r| r.kind_of?(PythonInstalled) }
raise "If you use python in the formula, you have to add `depends_on :python` (or :python3)!" if python_reqs.empty?
end raise "If you use python in the formula, you have to add `depends_on :python` (or :python3)!"
# Now select those that are satisfied and matching the version.major and end
# check that no two python binaries are the same (which could be the case # Now select those that are satisfied and matching the version.major and
# because more than one `depends_on :python => 'module_name' may be present). # check that no two python binaries are the same (which could be the case
filtered_python_reqs = [] # because more than one `depends_on :python => 'module_name' may be present).
while !python_reqs.empty? filtered_python_reqs = []
py = python_reqs.shift while !python_reqs.empty?
# this is ulgy but Ruby 1.8 has no `uniq! { }` py = python_reqs.shift
if !filtered_python_reqs.map{ |fpr| fpr.binary }.include?(py.binary) && # this is ulgy but Ruby 1.8 has no `uniq! { }`
py.satisfied? && if !filtered_python_reqs.map{ |fpr| fpr.binary }.include?(py.binary) &&
options[:allowed_major_versions].include?(py.version.major) && py.satisfied? &&
self.build.with?(py.name) || !(py.optional? || py.recommended?) options[:allowed_major_versions].include?(py.version.major) &&
then self.build.with?(py.name) || !(py.optional? || py.recommended?)
filtered_python_reqs << py then
end filtered_python_reqs << py
end end
end
# Allow to use an else-branch like so: `if python do ... end; else ... end` # Allow to use an else-branch like so: `if python do ... end; else ... end`
return false if filtered_python_reqs.empty? return false if filtered_python_reqs.empty?
# Sort by version, so the older 2.x will be used first and if no # Sort by version, so the older 2.x will be used first and if no
# block_given? then 2.x is preferred because it is returned. # block_given? then 2.x is preferred because it is returned.
# Further note, having 3.x last allows us to run `2to3 --write .` # Further note, having 3.x last allows us to run `2to3 --write .`
# which modifies the sources in-place (for some packages that need this). # which modifies the sources in-place (for some packages that need this).
filtered_python_reqs.sort_by{ |py| py.version }.map do |py| filtered_python_reqs.sort_by{ |py| py.version }.map do |py|
# Now is the time to set the site_packages to the correct value # Now is the time to set the site_packages to the correct value
py.site_packages = lib/py.xy/'site-packages' py.site_packages = lib/py.xy/'site-packages'
if !block_given? if !block_given?
return py return py
else else
puts "brew: Python block (#{py.binary})..." if ARGV.verbose? && ARGV.debug? puts "brew: Python block (#{py.binary})..." if ARGV.verbose? && ARGV.debug?
# Ensure env changes are only temporary # Ensure env changes are only temporary
begin begin
old_env = ENV.to_hash old_env = ENV.to_hash
# In order to install into the Cellar, the dir must exist and be in the # In order to install into the Cellar, the dir must exist and be in the
# PYTHONPATH. This will be executed in the context of the formula # PYTHONPATH. This will be executed in the context of the formula
# so that lib points to the HOMEBREW_PREFIX/Cellar/<formula>/<version>/lib # so that lib points to the HOMEBREW_PREFIX/Cellar/<formula>/<version>/lib
puts "brew: Appending to PYTHONPATH: #{py.site_packages}" if ARGV.verbose? puts "brew: Appending to PYTHONPATH: #{py.site_packages}" if ARGV.verbose?
mkdir_p py.site_packages mkdir_p py.site_packages
ENV.append 'PYTHONPATH', py.site_packages, ':' ENV.append 'PYTHONPATH', py.site_packages, ':'
ENV['PYTHON'] = py.binary ENV['PYTHON'] = py.binary
ENV.prepend 'CMAKE_INCLUDE_PATH', py.incdir, ':' ENV.prepend 'CMAKE_INCLUDE_PATH', py.incdir, ':'
ENV.prepend 'PKG_CONFIG_PATH', py.pkg_config_path, ':' if py.pkg_config_path ENV.prepend 'PKG_CONFIG_PATH', py.pkg_config_path, ':' if py.pkg_config_path
ENV.prepend 'PATH', py.binary.dirname, ':' unless py.from_osx? ENV.prepend 'PATH', py.binary.dirname, ':' unless py.from_osx?
#Note: Don't set LDFLAGS to point to the Python.framework, because #Note: Don't set LDFLAGS to point to the Python.framework, because
# it breaks builds (for example scipy.) # it breaks builds (for example scipy.)
# Track the state of the currently selected python for this block, # Track the state of the currently selected python for this block,
# so if this python_helper is called again _inside_ the block, # so if this python_helper is called again _inside_ the block,
# we can just return the right python (see `else`-branch a few lines down): # we can just return the right python (see `else`-branch a few lines down):
@current_python = py @current_python = py
res = instance_eval(&block) res = instance_eval(&block)
@current_python = nil @current_python = nil
res res
ensure ensure
ENV.replace(old_env) ENV.replace(old_env)
end
end end
end end
end end
end # enf of python_helper method end