Class: Language::Python::Virtualenv::Virtualenv
- Inherits:
-
Object
- Object
- Language::Python::Virtualenv::Virtualenv
- Defined in:
- language/python.rb
Overview
Convenience wrapper for creating and installing packages into Python virtualenvs.
Instance Method Summary collapse
-
#create(system_site_packages: true) ⇒ void
Obtains a copy of the virtualenv library and creates a new virtualenv on disk.
-
#initialize(formula, venv_root, python) ⇒ Virtualenv
constructor
Initializes a Virtualenv instance.
-
#pip_install(targets) ⇒ void
Installs packages represented by
targets
into the virtualenv. -
#pip_install_and_link(targets) ⇒ void
Installs packages represented by
targets
into the virtualenv, but unlike #pip_install also links new scripts to Formula#bin.
Constructor Details
#initialize(formula, venv_root, python) ⇒ Virtualenv
Initializes a Virtualenv instance. This does not create the virtualenv on disk; #create does that.
209 210 211 212 213 |
# File 'language/python.rb', line 209 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) ⇒ void
This method returns an undefined value.
Obtains a copy of the virtualenv library and creates a new virtualenv on disk.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'language/python.rb', line 218 def create(system_site_packages: true) return if (@venv_root/"bin/python").exist? args = ["-m", "venv"] args << "--system-site-packages" if system_site_packages @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 end |
#pip_install(targets) ⇒ void
This method returns an undefined value.
Installs packages represented by targets
into the virtualenv.
259 260 261 262 263 264 265 266 267 268 269 |
# File 'language/python.rb', line 259 def pip_install(targets) targets = Array(targets) targets.each do |t| if t.respond_to? :stage t.stage { do_install Pathname.pwd } else t = t.lines.map(&:strip) if t.respond_to?(:lines) && t.include?("\n") do_install t end end end |
#pip_install_and_link(targets) ⇒ void
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.
276 277 278 279 280 281 282 283 284 |
# File 'language/python.rb', line 276 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 |