Module: Language::Python::Virtualenv Private

Extended by:
T::Helpers
Defined in:
language/python.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Mixin module for Formula adding virtualenv support features.

Defined Under Namespace

Classes: Virtualenv

Instance Method Summary collapse

Instance Method Details

#needs_python?(python) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

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", "with-python@2" and "without-python@2" formula options are handled correctly even if not associated with any corresponding depends_on statement.

Parameters:

Returns:

  • (Boolean)


205
206
207
208
209
# File 'language/python.rb', line 205

def needs_python?(python)
  return true if build.with?(python)

  (requirements.to_a | deps).any? { |r| Utils.name_from_full_name(r.name) == python && r.required? }
end

#python_namesArray<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



261
262
263
# File 'language/python.rb', line 261

def python_names
  %w[python python3 pypy pypy3] + Formula.names.select { |name| name.start_with? "python@" }
end

#virtualenv_create(venv_root, python = "python", formula = T.cast(self, Formula), system_site_packages: true, without_pip: true) ⇒ Virtualenv

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

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 (String, Pathname)

    the path to the root of the virtualenv (often libexec/"venv")

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

    which interpreter to use (e.g. "python3" or "python3.x")

  • formula (Formula) (defaults to: T.cast(self, Formula))

    the active Formula

  • system_site_packages (Boolean) (defaults to: true)
  • without_pip (Boolean) (defaults to: true)

Returns:



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'language/python.rb', line 159

def virtualenv_create(venv_root, python = "python", formula = T.cast(self, Formula),
                      system_site_packages: true, without_pip: true)
  # Limit deprecation to 3.12+ for now (or if we can't determine the version).
  # Some used this argument for `setuptools`, which we no longer bundle since 3.12.
  unless without_pip
    python_version = Language::Python.major_minor_version(python)
    if python_version.nil? || python_version.null? || python_version >= "3.12"
      raise ArgumentError, "virtualenv_create's without_pip is deprecated starting with Python 3.12"
    end
  end

  ENV.refurbish_args
  venv = Virtualenv.new formula, venv_root, python
  venv.create(system_site_packages:, without_pip:)

  # Find any Python bindings provided by recursive dependencies
  pth_contents = []
  formula.recursive_dependencies do |dependent, dep|
    next Dependable::PRUNE if dep.build? || dep.test?
    # Apply default filter
    next Dependable::PRUNE if (dep.optional? || dep.recommended?) && !T.cast(dependent,
                                                                             Formula).build.with?(dep)
    # Do not add the main site-package provided by the brewed
    # Python formula, to keep the virtual-env's site-package pristine
    next Dependable::PRUNE if python_names.include? dep.name
    # Skip uses_from_macos dependencies as these imply no Python bindings
    next Dependable::PRUNE if dep.uses_from_macos?

    dep_site_packages = dep.to_formula.opt_prefix/Language::Python.site_packages(python)
    next Dependable::PRUNE unless dep_site_packages.exist?

    pth_contents << "import site; site.addsitedir('#{dep_site_packages}')\n"
    nil # Return nil to satisfy T.nilable(Symbol) block sig (Array from << would violate it).
  end
  (venv.site_packages/"homebrew_deps.pth").write pth_contents.join unless pth_contents.empty?

  venv
end

#virtualenv_install_with_resources(using: nil, system_site_packages: true, without_pip: true, link_manpages: true, without: nil, start_with: nil, end_with: nil) ⇒ Virtualenv

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Helper method for the common case of installing a Python application. Creates a virtualenv in libexec, installs all resources 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 python@x.y, or to resolve an ambiguous case where it's not clear whether python or python@x.y should be the default guess.

Parameters:

  • using (String, nil) (defaults to: nil)
  • system_site_packages (Boolean) (defaults to: true)
  • without_pip (Boolean) (defaults to: true)
  • link_manpages (Boolean) (defaults to: true)
  • without (String, Array<String>, nil) (defaults to: nil)
  • start_with (String, Array<String>, nil) (defaults to: nil)
  • end_with (String, Array<String>, nil) (defaults to: nil)

Returns:



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'language/python.rb', line 229

def virtualenv_install_with_resources(using: nil, system_site_packages: true, without_pip: true,
                                      link_manpages: true, without: nil, start_with: nil, end_with: nil)
  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.fetch(0)
    python = "python3" if python == "python"
  end

  venv_resources = if without.nil? && start_with.nil? && end_with.nil?
    resources
  else
    remaining_resources = resources.to_h { |resource| [resource.name, resource] }

    slice_resources!(remaining_resources, Array(without))
    start_with_resources = slice_resources!(remaining_resources, Array(start_with))
    end_with_resources = slice_resources!(remaining_resources, Array(end_with))

    start_with_resources + remaining_resources.values + end_with_resources
  end

  venv = virtualenv_create(libexec, python.delete("@"), system_site_packages:,
                                                        without_pip:)
  venv.pip_install venv_resources
  venv.pip_install_and_link(T.must(buildpath), link_manpages:)
  venv
end