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

Inherits:
Object
  • Object
show all
Defined in:
Library/Homebrew/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) ⇒ Virtualenv

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

Parameters:

  • formula (Formula)

    the active Formula

  • venv_root (Pathname, String)

    the path to the root of the virtualenv

  • python (String)

    which interpreter to use; i.e. “python” or “python3”



157
158
159
160
161
# File 'Library/Homebrew/language/python.rb', line 157

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

Instance Method Details

#createvoid

This method returns an undefined value.

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



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
197
198
# File 'Library/Homebrew/language/python.rb', line 166

def create
  return if (@venv_root/"bin/python").exist?

  @formula.resource("homebrew-virtualenv").stage do |stage|
    old_pythonpath = ENV.delete "PYTHONPATH"
    begin
      xy = Language::Python.major_minor_version(@python)
      staging = Pathname.new(stage.staging.tmpdir)
      ENV.prepend_create_path "PYTHONPATH", staging/"target/lib/python#{xy}/site-packages"
      @formula.system @python, *Language::Python.setup_install_args(staging/"target")
      @formula.system @python, "-s", staging/"target/bin/virtualenv", "-p", @python, @venv_root
    ensure
      ENV["PYTHONPATH"] = old_pythonpath
    end
  end

  # Robustify symlinks to survive python3 patch upgrades
  @venv_root.find do |f|
    next unless f.symlink?
    next unless (rp = f.realpath.to_s).start_with? HOMEBREW_CELLAR
    python = rp.include?("python3") ? "python3" : "python"
    new_target = rp.sub %r{#{HOMEBREW_CELLAR}/#{python}/[^/]+}, Formula[python].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
    python = prefix_path.include?("python3") ? "python3" : "python"
    prefix_path.sub! %r{^#{HOMEBREW_CELLAR}/#{python}/[^/]+}, Formula[python].opt_prefix
    prefix_file.atomic_write prefix_path
  end
end

#pip_install(targets) ⇒ 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`.



209
210
211
212
213
214
215
216
217
218
219
220
# File 'Library/Homebrew/language/python.rb', line 209

def pip_install(targets)
  targets = [targets] unless targets.is_a? Array
  targets.each do |t|
    if t.respond_to? :stage
      next if t.name == "homebrew-virtualenv"
      t.stage { do_install Pathname.pwd }
    else
      t = t.lines.map(&:strip) if t.respond_to?(:lines) && t =~ /\n/
      do_install t
    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:

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



226
227
228
229
230
231
232
233
234
# File 'Library/Homebrew/language/python.rb', line 226

def pip_install_and_link(targets)
  bin_before = Dir[@venv_root/"bin/*"].to_set

  pip_install(targets)

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