Module: Language::Python::Virtualenv

Defined in:
Library/Homebrew/language/python.rb

Overview

Mixin module for Formula adding virtualenv support features.

Defined Under Namespace

Classes: Virtualenv

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



76
77
78
79
80
81
82
83
# File 'Library/Homebrew/language/python.rb', line 76

def self.included(base)
  base.class_eval do
    resource "homebrew-virtualenv" do
      url PYTHON_VIRTUALENV_URL
      sha256 PYTHON_VIRTUALENV_SHA256
    end
  end
end

Instance Method Details

#needs_python?(python) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Returns:

  • (Boolean)


122
123
124
125
# File 'Library/Homebrew/language/python.rb', line 122

def needs_python?(python)
  return true if build.with?(python)
  (requirements.to_a | deps).any? { |r| r.name == python && r.required? }
end

#virtualenv_create(venv_root, python = "python", formula = self) ⇒ Virtualenv

Instantiates, creates, and yields a Virtualenv object for use from Formula#install, which provides helper methods for instantiating and installing packages into a Python virtualenv.

Parameters:

  • venv_root (Pathname, String)

    the path to the root of the virtualenv (often `libexec/“venv”`)

  • python (String) (defaults to: "python")

    which interpreter to use (e.g. “python” or “python3”)

  • formula (Formula) (defaults to: self)

    the active Formula

Returns:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'Library/Homebrew/language/python.rb', line 94

def virtualenv_create(venv_root, python = "python", formula = self)
  ENV.refurbish_args
  venv = Virtualenv.new formula, venv_root, python
  venv.create

  # Find any Python bindings provided by recursive dependencies
  formula_deps = formula.recursive_dependencies
  xy = Language::Python.major_minor_version python
  pth_contents = formula_deps.map do |d|
    next if d.build?
    dep_site_packages = Formula[d.name].opt_lib/"python#{xy}/site-packages"
    next unless dep_site_packages.exist?
    "import site; site.addsitedir('#{dep_site_packages}')\n"
  end.compact
  unless pth_contents.empty?
    (venv_root/"lib/python#{xy}/site-packages/homebrew_deps.pth").write pth_contents.join
  end

  venv
end

#virtualenv_install_with_resources(options = {}) ⇒ Object

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. 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.



134
135
136
137
138
139
140
141
142
143
144
145
# File 'Library/Homebrew/language/python.rb', line 134

def virtualenv_install_with_resources(options = {})
  python = options[:using]
  if python.nil?
    wanted = %w[python python@2 python@3 python3].select { |py| needs_python?(py) }
    raise FormulaAmbiguousPythonError, self if wanted.size > 1
    python = wanted.first || "python2.7"
  end
  venv = virtualenv_create(libexec, python.delete("@"))
  venv.pip_install resources
  venv.pip_install_and_link buildpath
  venv
end