Class: Language::Python::Virtualenv::Virtualenv Private

Inherits:
Object
  • Object
show all
Defined in:
language/python.rb

Overview

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

Convenience wrapper for creating and installing packages into Python virtualenvs.

Instance Method Summary collapse

Constructor Details

#initialize(formula, venv_root, python) ⇒ void

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.

Initializes a Virtualenv instance. This does not create the virtualenv on disk; #create does that.

Parameters:



302
303
304
305
306
# File 'language/python.rb', line 302

def initialize(formula, venv_root, python)
  @formula = formula
  @venv_root = T.let(Pathname(venv_root), Pathname)
  @python = python
end

Instance Method Details

#create(system_site_packages: true, without_pip: true) ⇒ void

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.

This method returns an undefined value.

Obtains a copy of the virtualenv library and creates a new virtualenv on disk.

Parameters:

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


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'language/python.rb', line 322

def create(system_site_packages: true, without_pip: true)
  return if (@venv_root/"bin/python").exist?

  args = ["-m", "venv"]
  args << "--system-site-packages" if system_site_packages
  args << "--without-pip" if without_pip
  @formula.system @python, *args, @venv_root

  # Robustify symlinks to survive python patch upgrades
  @venv_root.find do |f|
    next unless f.symlink?
    next unless f.readlink.expand_path.to_s.start_with? HOMEBREW_CELLAR

    rp = f.realpath.to_s
    version = rp.match %r{^#{HOMEBREW_CELLAR}/python@(.*?)/}o
    version = "@#{version.captures.first}" unless version.nil?

    new_target = rp.sub %r{#{HOMEBREW_CELLAR}/python#{version}/[^/]+}, Formula["python#{version}"].opt_prefix
    f.unlink
    f.make_symlink new_target
  end

  Pathname.glob(@venv_root/"lib/python*/orig-prefix.txt").each do |prefix_file|
    prefix_path = prefix_file.read

    version = prefix_path.match %r{^#{HOMEBREW_CELLAR}/python@(.*?)/}o
    version = "@#{version.captures.first}" unless version.nil?

    prefix_path.sub! %r{^#{HOMEBREW_CELLAR}/python#{version}/[^/]+}, Formula["python#{version}"].opt_prefix
    prefix_file.atomic_write prefix_path
  end

  # Reduce some differences between macOS and Linux venv
  lib64 = @venv_root/"lib64"
  lib64.make_symlink "lib" unless lib64.exist?
  if (cfg_file = @venv_root/"pyvenv.cfg").exist?
    cfg = cfg_file.read
    framework = "Frameworks/Python.framework/Versions"
    cfg.match(%r{= *(#{HOMEBREW_CELLAR}/(python@[\d.]+)/[^/]+(?:/#{framework}/[\d.]+)?/bin)}) do |match|
      cfg.sub! match[1].to_s, Formula[match[2]].opt_bin
      cfg_file.atomic_write cfg
    end
  end

  # Remove unnecessary activate scripts
  (@venv_root/"bin").glob("[Aa]ctivate*").map(&:unlink)
end

#pip_install(targets, build_isolation: true) ⇒ void

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.

This method returns an undefined value.

Installs packages represented by targets into the virtualenv.

Parameters:

  • targets (String, Pathname, Resource, Array<String, Pathname, Resource>)

    (A) token(s) passed to pip representing the object to be installed. This can be a directory containing a setup.py, a Resource which will be staged and installed, or a package identifier to be fetched from PyPI. Multiline strings are allowed and treated as though they represent the contents of a requirements.txt.

  • build_isolation (Boolean) (defaults to: true)


386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'language/python.rb', line 386

def pip_install(targets, build_isolation: true)
  targets = Array(targets)
  targets.each do |t|
    if t.is_a?(Resource)
      t.stage do
        target = Pathname.pwd
        target /= t.downloader.basename if t.url&.end_with?("-none-any.whl")
        do_install(target, build_isolation:)
      end
    else
      t = t.lines.map(&:strip) if t.is_a?(String) && t.include?("\n")
      do_install(t, build_isolation:)
    end
  end
end

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.

This method returns an undefined value.

Installs packages represented by targets into the virtualenv, but unlike #pip_install also links new scripts to Formula#bin.

Parameters:



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'language/python.rb', line 414

def pip_install_and_link(targets, link_manpages: false, build_isolation: true)
  bin_before = Dir[@venv_root/"bin/*"].to_set
  man_before = Dir[@venv_root/"share/man/man*/*"].to_set if link_manpages

  pip_install(targets, build_isolation:)

  bin_after = Dir[@venv_root/"bin/*"].to_set
  bin_to_link = (bin_after - bin_before).to_a
  @formula.bin.install_symlink(bin_to_link)
  return unless link_manpages

  man_after = Dir[@venv_root/"share/man/man*/*"].to_set
  man_to_link = (man_after - man_before).to_a
  man_to_link.each do |manpage|
    (@formula.man/Pathname.new(manpage).dirname.basename).install_symlink manpage
  end
end

#rootPathname

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:



309
310
311
# File 'language/python.rb', line 309

def root
  @venv_root
end

#site_packagesPathname

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:



314
315
316
# File 'language/python.rb', line 314

def site_packages
  @venv_root/Language::Python.site_packages(@python)
end