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
Instance Method Summary collapse
-
#needs_python?(python) ⇒ Boolean
private
Returns true if a formula option for the specified python is currently active or if the specified python is required by the formula.
- #python_names ⇒ Object
-
#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.
-
#virtualenv_install_with_resources(options = {}) ⇒ Object
Helper method for the common case of installing a Python application.
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", "python2", and :python3. Note that "with-python", "without-python", "[email protected]", and "[email protected]" formula options are handled correctly even if not associated with any corresponding depends_on statement.
189 190 191 192 193 |
# File 'Library/Homebrew/language/python.rb', line 189 def needs_python?(python) return true if build.with?(python) (requirements.to_a | deps).any? { |r| r.name.split("/").last == python && r.required? } end |
#python_names ⇒ Object
218 219 220 |
# File 'Library/Homebrew/language/python.rb', line 218 def python_names %w[python python3 pypy pypy3] + Formula.names.select { |name| name.start_with? "[email protected]" } 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.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'Library/Homebrew/language/python.rb', line 156 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 pth_contents = formula_deps.map do |d| next if d.build? || d.test? # Do not add the main site-package provided by the brewed # Python formula, to keep the virtual-env's site-package pristine next if python_names.include? d.name dep_site_packages = Formula[d.name].opt_prefix/Language::Python.site_packages(python) next unless dep_site_packages.exist? "import site; site.addsitedir('#{dep_site_packages}')\n" end.compact unless pth_contents.empty? (venv_root/Language::Python.site_packages(python)/"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 => "python"
) to override the default, guessed
formula preference for python or [email protected], or to resolve an ambiguous
case where it's not clear whether python or [email protected] should be the
default guess.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'Library/Homebrew/language/python.rb', line 202 def virtualenv_install_with_resources( = {}) python = [:using] if python.nil? wanted = python_names.select { |py| needs_python?(py) } raise FormulaUnknownPythonError, self if wanted.empty? raise FormulaAmbiguousPythonError, self if wanted.size > 1 python = wanted.first python = "python3" if python == "python" end venv = virtualenv_create(libexec, python.delete("@")) venv.pip_install resources venv.pip_install_and_link buildpath venv end |