Skip to content

Commit

Permalink
python: create venv's --without-pip
Browse files Browse the repository at this point in the history
  • Loading branch information
branchvincent committed Jul 30, 2023
1 parent c531a35 commit 9333a56
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
19 changes: 13 additions & 6 deletions Library/Homebrew/language/python.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ module Virtualenv
# or "python3.x")
# @param formula [Formula] the active {Formula}
# @return [Virtualenv] a {Virtualenv} instance
def virtualenv_create(venv_root, python = "python", formula = self, system_site_packages: true)
def virtualenv_create(venv_root, python = "python", formula = self, system_site_packages: true,
without_pip: true)
ENV.refurbish_args
venv = Virtualenv.new formula, venv_root, python
venv.create(system_site_packages: system_site_packages)
venv.create(system_site_packages: system_site_packages, without_pip: without_pip)

Check warning on line 141 in Library/Homebrew/language/python.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/language/python.rb#L141

Added line #L141 was not covered by tests

# Find any Python bindings provided by recursive dependencies
formula_deps = formula.recursive_dependencies
Expand Down Expand Up @@ -180,7 +181,8 @@ def needs_python?(python)
# 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.
def virtualenv_install_with_resources(using: nil, system_site_packages: true, link_manpages: false)
def virtualenv_install_with_resources(using: nil, system_site_packages: true, without_pip: true,
link_manpages: false)
python = using
if python.nil?
wanted = python_names.select { |py| needs_python?(py) }
Expand All @@ -190,7 +192,8 @@ def virtualenv_install_with_resources(using: nil, system_site_packages: true, li
python = wanted.first
python = "python3" if python == "python"
end
venv = virtualenv_create(libexec, python.delete("@"), system_site_packages: system_site_packages)
venv = virtualenv_create(libexec, python.delete("@"), system_site_packages: system_site_packages,

Check warning on line 195 in Library/Homebrew/language/python.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/language/python.rb#L195

Added line #L195 was not covered by tests
without_pip: without_pip)
venv.pip_install resources
venv.pip_install_and_link(buildpath, link_manpages: link_manpages)
venv
Expand Down Expand Up @@ -221,11 +224,12 @@ def initialize(formula, venv_root, python)
# Obtains a copy of the virtualenv library and creates a new virtualenv on disk.
#
# @return [void]
def create(system_site_packages: true)
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
Expand All @@ -250,6 +254,9 @@ def create(system_site_packages: true)
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

# Installs packages represented by `targets` into the virtualenv.
Expand Down Expand Up @@ -302,7 +309,7 @@ def pip_install_and_link(targets, link_manpages: false, build_isolation: true)
def do_install(targets, build_isolation: true)
targets = Array(targets)
args = @formula.std_pip_args(prefix: false, build_isolation: build_isolation)
@formula.system @venv_root/"bin/pip", "install", *args, *targets
@formula.system @python, "-m", "pip", "--python=#{@venv_root}/bin/python", "install", *args, *targets
end
end
end
Expand Down
19 changes: 12 additions & 7 deletions Library/Homebrew/test/language/python/virtualenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@

describe "#create" do
it "creates a venv" do
expect(formula).to receive(:system).with("python", "-m", "venv", "--system-site-packages", dir)
expect(formula).to receive(:system).with("python", "-m", "venv", "--system-site-packages", "--without-pip", dir)
virtualenv.create
end

it "creates a venv with pip" do
expect(formula).to receive(:system).with("python", "-m", "venv", "--system-site-packages", dir)
virtualenv.create(without_pip: false)
end
end

describe "#pip_install" do
it "accepts a string" do
expect(formula).to receive(:std_pip_args).with(prefix: false,
build_isolation: true).and_return(["--std-pip-args"])
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "--std-pip-args", "foo")
.with("python", "-m", "pip", "--python=#{dir}/bin/python", "install", "--std-pip-args", "foo")
.and_return(true)
virtualenv.pip_install "foo"
end
Expand All @@ -34,7 +39,7 @@
expect(formula).to receive(:std_pip_args).with(prefix: false,
build_isolation: true).and_return(["--std-pip-args"])
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "--std-pip-args", "foo", "bar")
.with("python", "-m", "pip", "--python=#{dir}/bin/python", "install", "--std-pip-args", "foo", "bar")
.and_return(true)

virtualenv.pip_install <<~EOS
Expand All @@ -47,13 +52,13 @@
expect(formula).to receive(:std_pip_args).with(prefix: false,
build_isolation: true).and_return(["--std-pip-args"])
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "--std-pip-args", "foo")
.with("python", "-m", "pip", "--python=#{dir}/bin/python", "install", "--std-pip-args", "foo")
.and_return(true)

expect(formula).to receive(:std_pip_args).with(prefix: false,
build_isolation: true).and_return(["--std-pip-args"])
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "--std-pip-args", "bar")
.with("python", "-m", "pip", "--python=#{dir}/bin/python", "install", "--std-pip-args", "bar")
.and_return(true)

virtualenv.pip_install ["foo", "bar"]
Expand All @@ -66,7 +71,7 @@
expect(formula).to receive(:std_pip_args).with(prefix: false,
build_isolation: true).and_return(["--std-pip-args"])
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "--std-pip-args", Pathname.pwd)
.with("python", "-m", "pip", "--python=#{dir}/bin/python", "install", "--std-pip-args", Pathname.pwd)
.and_return(true)

virtualenv.pip_install res
Expand All @@ -76,7 +81,7 @@
expect(formula).to receive(:std_pip_args).with(prefix: false,
build_isolation: false).and_return(["--std-pip-args"])
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "--std-pip-args", "foo")
.with("python", "-m", "pip", "--python=#{dir}/bin/python", "install", "--std-pip-args", "foo")
.and_return(true)
virtualenv.pip_install("foo", build_isolation: false)
end
Expand Down

0 comments on commit 9333a56

Please sign in to comment.