virtualenv_install_with_resources: select correct python

`virtualenv_install_with_resources` will now attempt to guess the
desired Python based on the active build options and based on the
dependencies and requirements. When the situation is ambiguous (e.g.,
`depends_on :python3` and `build.with? "python"` is true) raise
`FormulaAmbiguousPythonError` unless `:using => "python"` or
`:using => "python3"` has been passed to resolve the ambiguity.

In most cases, this will allow

```
    virtualenv_create(libexec, "python3")
    virtualenv_install_with_resources
```

to be changed to just

```
    virtualenv_install_with_resources
```
This commit is contained in:
ilovezfs 2016-08-05 00:20:20 -07:00
parent 7d31a70373
commit 23a38e0ff6
2 changed files with 36 additions and 3 deletions

View File

@ -294,6 +294,17 @@ class FormulaConflictError < RuntimeError
end end
end end
class FormulaAmbiguousPythonError < RuntimeError
def initialize(formula)
super <<-EOS.undent
The version of python to use with the virtualenv in the `#{formula.full_name}` formula
cannot be guessed automatically. If the simultaneous use of python and python3
is intentional, please add `:using => "python"` or `:using => "python3"` to
`virtualenv_install_with_resources` to resolve the ambiguity manually.
EOS
end
end
class BuildError < RuntimeError class BuildError < RuntimeError
attr_reader :formula, :env attr_reader :formula, :env

View File

@ -139,11 +139,33 @@ module Language
venv venv
end end
# Returns true if a formula option for the specified python is currently
# active or if the specified python is required by the formula. Valid
# inputs are "python", "python3", :python, and :python3. Note that
# "with-python", "without-python", "with-python3", and "without-python3"
# formula options are handled correctly even if not associated with any
# corresponding depends_on statement.
# @api private
def needs_python?(python)
return true if build.with?(python)
(requirements.to_a | deps).any? { |r| r.name == python && r.required? }
end
# Helper method for the common case of installing a Python application. # Helper method for the common case of installing a Python application.
# Creates a virtualenv in `libexec`, installs all `resource`s defined # Creates a virtualenv in `libexec`, installs all `resource`s defined
# on the formula, and then installs the formula. # on the formula, and then installs the formula. An options hash may be
def virtualenv_install_with_resources # passed (e.g., :using => "python3") to override the default, guessed
venv = virtualenv_create(libexec) # formula preference for python or python3, or to resolve an ambiguous
# case where it's not clear whether python or python3 should be the
# default guess.
def virtualenv_install_with_resources(options = {})
python = options[:using]
if python.nil?
wanted = %w[python python3].select { |py| needs_python?(py) }
raise FormulaAmbiguousPythonError, self if wanted.size > 1
python = wanted.first || "python"
end
venv = virtualenv_create(libexec, python)
venv.pip_install resources venv.pip_install resources
venv.pip_install_and_link buildpath venv.pip_install_and_link buildpath
venv venv