aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike McQuaid2016-10-03 08:10:12 +0100
committerGitHub2016-10-03 08:10:12 +0100
commit1e1903e4cc0ef928081ebab6b7a8e478de493b2a (patch)
tree033208ce4cad06bad16d4e105631c117023cd39d
parent35ee2831086e923e7fcaf75fb440b01312e3f9c5 (diff)
parent23a38e0ff6beb26f86eca0c56b25d6100375b60d (diff)
downloadbrew-1e1903e4cc0ef928081ebab6b7a8e478de493b2a.tar.bz2
Merge pull request #636 from ilovezfs/spooky_python
virtualenv_install_with_resources: select correct python
-rw-r--r--Library/Homebrew/exceptions.rb11
-rw-r--r--Library/Homebrew/language/python.rb28
2 files changed, 36 insertions, 3 deletions
diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb
index e01a60c75..1b2b24691 100644
--- a/Library/Homebrew/exceptions.rb
+++ b/Library/Homebrew/exceptions.rb
@@ -294,6 +294,17 @@ class FormulaConflictError < RuntimeError
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
attr_reader :formula, :env
diff --git a/Library/Homebrew/language/python.rb b/Library/Homebrew/language/python.rb
index 95259c841..b28b1ca60 100644
--- a/Library/Homebrew/language/python.rb
+++ b/Library/Homebrew/language/python.rb
@@ -139,11 +139,33 @@ module Language
venv
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.
# Creates a virtualenv in `libexec`, installs all `resource`s defined
- # on the formula, and then installs the formula.
- def virtualenv_install_with_resources
- venv = virtualenv_create(libexec)
+ # on the formula, and then installs the formula. An options hash may be
+ # passed (e.g., :using => "python3") to override the default, guessed
+ # 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_and_link buildpath
venv