diff options
| author | Samuel John | 2013-01-21 10:33:56 +0100 |
|---|---|---|
| committer | Samuel John | 2013-06-03 17:29:43 +0200 |
| commit | c5248956662e06094b7c4f7f7475d66771771ff6 (patch) | |
| tree | 084b05b98d35f7ded16d6f83a8905c4a8a2830ce /Library/Homebrew/python_helper.rb | |
| parent | 9647954a603dcb39cc8bb3f5fd0028670c2db0de (diff) | |
| download | brew-c5248956662e06094b7c4f7f7475d66771771ff6.tar.bz2 | |
Python 2.x and 3.x support
New `depends_on :python` Dependency.
New `depends_on :python3` Dependency.
To avoid having multiple formulae with endings -py2 and -py3,
we will handle support for different pythons (2.x vs. 3.x)
in the same formula.
Further brewed vs. external python will be transparently supported.
The formula also gets a new object `python`, which is false if
no Python is available or the user has disabled it. Otherwise
it is defined and provides several support methods:
python.site_packages # the site-packages in the formula's Cellar
python.global_site_packages
python.binary # the full path to the python binary
python.prefix
python.version
python.version.major
python.version.minor
python.xy # => e.g. "python2.7"
python.incdir # includes of python
python.libdir # the python dylib library
python.pkg_config_path # used internally by brew
python.from_osx?
python.framework?
python.universal?
python.pypy?
python.standard_caveats # Text to set PYTHONPATH for python.from_osx?
python.if3then3 # => "" for 2.x and to "3" for 3.x.
Further, to avoid code duplication, `python` takes an optional
block that is run twice if the formula defines depends_on
:python AND :python3.
python do
system python, 'setup.py', "--prefix=#{prefix}"
end
Read more in the Homebrew wiki.
Diffstat (limited to 'Library/Homebrew/python_helper.rb')
| -rw-r--r-- | Library/Homebrew/python_helper.rb | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/Library/Homebrew/python_helper.rb b/Library/Homebrew/python_helper.rb new file mode 100644 index 000000000..ef71b2a30 --- /dev/null +++ b/Library/Homebrew/python_helper.rb @@ -0,0 +1,80 @@ + +# The python_helper is used in the Formula class when the user calls +# `python`, `python2` or `python3`. + +# This method has a dual nature. For one, it takes a &block and sets up +# the ENV such that a Python, as defined in the requirements, is the default. +# If there are multiple `PythonInstalled` requirements, the block is evaluated +# once for each Python. This makes it possible to easily support 2.x and +# 3.x Python bindings without code duplication in formulae. +# If you need to special case stuff, set :allowed_major_versions. +# Second, inside the block, a formula author may call this method to access +# certain convienience methods for the currently selected Python, e.g. +# `python.site_packages`. +def python_helper(options={:allowed_major_versions => [2, 3]}, &block) + if !block_given? and !@current_python.nil? + # 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. + if options[:allowed_major_versions].include?(@current_python.version.major) + @current_python + else + false + end + else + # Look for PythonInstalled requirements for this formula + python_reqs = requirements.select{ |r| r.kind_of?(PythonInstalled) } + if python_reqs.empty? + raise "If you use python in the formula, you have to add `depends_on :python` (or :python3)!" + end + # Now select those that are satisfied and matching the version.major + python_reqs = python_reqs.select do |p| + p.satisfied? && + options[:allowed_major_versions].include?(p.version.major) && + if p.optional? || p.recommended? + self.build.with?(p.name) + else + true + end + end + + # Allow to use an else-branch like so: `if python do ... end; else ... end` + return false if python_reqs.empty? + + # 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. + # Further note, having 3.x last allows us to run `2to3 --write .` + # which modifies the sources in-place (for some packages that need this). + python_reqs.sort_by{ |py| py.version }.map do |py| + # Now is the time to set the site_packages to the correct value + py.site_packages = lib/py.xy/'site-packages' + if block_given? + puts "brew: Python block (#{py.binary})..." if ARGV.verbose? + require 'superenv' + # Ensure env changes are only temporary by using `with_build_environment` + ENV.with_build_environment do + # 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 + # so that lib points to the HOMEBREW_PREFIX/Cellar/<formula>/<version>/lib + puts "brew: Setting PYTHONPATH=#{py.site_packages}" if ARGV.verbose? + mkdir_p py.site_packages + ENV.append 'PYTHONPATH', py.site_packages, ':' + ENV['PYTHON'] = py.binary + ENV.prepend 'CMAKE_INCLUDE_PATH', py.incdir, ':' + ENV.prepend 'PKG_CONFIG_PATH', py.pkg_config_path, ':' if py.pkg_config_path + ENV.prepend 'PATH', py.binary.dirname, ':' unless py.from_osx? + # Track the state of the currently selected python for this 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): + @current_python = py + res = instance_eval(&block) + @current_python = nil + res + end + else + puts "brew: Using #{py.binary}" if ARGV.verbose? + # We return here with intention, because no block_given? + return py + end + end + end +end |
