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

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

Overview

Convenience wrapper for creating and installing packages into Python virtualenvs.

Instance Method Summary collapse

Constructor Details

#initialize(formula, venv_root, python) ⇒ void

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

Parameters:



255
256
257
258
259
# File 'language/python.rb', line 255

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

Instance Method Details

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

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)


275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'language/python.rb', line 275

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 (rp = f.realpath.to_s).start_with? HOMEBREW_CELLAR

    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

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

#pip_install(targets, build_isolation: true) ⇒ void

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)


326
327
328
329
330
331
332
333
334
335
336
# File 'language/python.rb', line 326

def pip_install(targets, build_isolation: true)
  targets = Array(targets)
  targets.each do |t|
    if t.is_a?(Resource)
      t.stage { do_install(Pathname.pwd, build_isolation: build_isolation) }
    else
      t = t.lines.map(&:strip) if t.is_a?(String) && t.include?("\n")
      do_install(t, build_isolation: build_isolation)
    end
  end
end

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:



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'language/python.rb', line 350

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

Returns:



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

def root
  @venv_root
end

#site_packagesPathname

Returns:



267
268
269
# File 'language/python.rb', line 267

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