Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: use built-in venv instead of virtualenv when installing formulae #10039

Merged
merged 2 commits into from Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 2 additions & 49 deletions Library/Homebrew/language/python.rb
Expand Up @@ -116,35 +116,6 @@ def detected_python_shebang(formula = self)
module Virtualenv
extend T::Sig

def self.included(base)
base.class_eval do
resource "homebrew-virtualenv" do
url "https://files.pythonhosted.org/packages/c6/3e/d00f1500aa0e8a69323101c33f6e6910bbc68d34df3e8a0b1e510219a956/virtualenv-20.2.2.tar.gz"
sha256 "b7a8ec323ee02fb2312f098b6b4c9de99559b462775bc8fe3627a73706603c1b"
end

resource "homebrew-appdirs" do
url "https://files.pythonhosted.org/packages/d7/d8/05696357e0311f5b5c316d7b95f46c669dd9c15aaeecbb48c7d0aeb88c40/appdirs-1.4.4.tar.gz"
sha256 "7d5d0167b2b1ba821647616af46a749d1c653740dd0d2415100fe26e27afdf41"
end

resource "homebrew-distlib" do
url "https://files.pythonhosted.org/packages/2f/83/1eba07997b8ba58d92b3e51445d5bf36f9fba9cb8166bcae99b9c3464841/distlib-0.3.1.zip"
sha256 "edf6116872c863e1aa9d5bb7cb5e05a022c519a4594dc703843343a9ddd9bff1"
end

resource "homebrew-filelock" do
url "https://files.pythonhosted.org/packages/14/ec/6ee2168387ce0154632f856d5cc5592328e9cf93127c5c9aeca92c8c16cb/filelock-3.0.12.tar.gz"
sha256 "18d82244ee114f543149c66a6e0c14e9c4f8a1044b5cdaadd0f82159d6a6ff59"
end

resource "homebrew-six" do
url "https://files.pythonhosted.org/packages/6b/34/415834bfdafca3c5f451532e8a8d9ba89a21c9743a0c59fbd0205c7f9426/six-1.15.0.tar.gz"
sha256 "30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259"
end
end
end

# Instantiates, creates, and yields a {Virtualenv} object for use from
# {Formula#install}, which provides helper methods for instantiating and
# installing packages into a Python virtualenv.
Expand Down Expand Up @@ -245,25 +216,7 @@ def initialize(formula, venv_root, python)
def create
return if (@venv_root/"bin/python").exist?

@formula.resource("homebrew-virtualenv").stage do |stage|
old_pythonpath = ENV.delete "PYTHONPATH"
begin
staging = Pathname.new(stage.staging.tmpdir)

ENV.prepend_create_path "PYTHONPATH", staging/"target/vendor"/Language::Python.site_packages(@python)
%w[appdirs distlib filelock six].each do |virtualenv_dependency|
@formula.resource("homebrew-#{virtualenv_dependency}").stage do
@formula.system @python, *Language::Python.setup_install_args(staging/"target/vendor")
end
end

ENV.prepend_create_path "PYTHONPATH", staging/"target"/Language::Python.site_packages(@python)
@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
@formula.system @python, "-m", "venv", @venv_root

# Robustify symlinks to survive python patch upgrades
@venv_root.find do |f|
Expand Down Expand Up @@ -334,7 +287,7 @@ def do_install(targets)
targets = Array(targets)
@formula.system @venv_root/"bin/pip", "install",
"-v", "--no-deps", "--no-binary", ":all:",
"--ignore-installed", *targets
"--no-user", "--ignore-installed", *targets
end
end
end
Expand Down
23 changes: 7 additions & 16 deletions Library/Homebrew/test/language/python_spec.rb
Expand Up @@ -86,34 +86,25 @@
let(:formula) { double("formula", resource: resource, bin: formula_bin) }

describe "#create" do
it "creates a virtual environment" do
expect(formula).to receive(:resource).with("homebrew-virtualenv").and_return(resource)
it "creates a venv" do
expect(formula).to receive(:system).with("python", "-m", "venv", dir)
subject.create
end

specify "virtual environment creation is idempotent" do
expect(formula).to receive(:resource).with("homebrew-virtualenv").and_return(resource)
subject.create
FileUtils.mkdir_p dir/"bin"
FileUtils.touch dir/"bin/python"
subject.create
FileUtils.rm dir/"bin/python"
end
end

describe "#pip_install" do
it "accepts a string" do
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "-v", "--no-deps",
"--no-binary", ":all:", "--ignore-installed", "foo")
"--no-binary", ":all:", "--no-user", "--ignore-installed", "foo")
.and_return(true)
subject.pip_install "foo"
end

it "accepts a multi-line strings" do
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "-v", "--no-deps",
"--no-binary", ":all:", "--ignore-installed", "foo", "bar")
"--no-binary", ":all:", "--no-user", "--ignore-installed", "foo", "bar")
.and_return(true)

subject.pip_install <<~EOS
Expand All @@ -125,12 +116,12 @@
it "accepts an array" do
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "-v", "--no-deps",
"--no-binary", ":all:", "--ignore-installed", "foo")
"--no-binary", ":all:", "--no-user", "--ignore-installed", "foo")
.and_return(true)

expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "-v", "--no-deps",
"--no-binary", ":all:", "--ignore-installed", "bar")
"--no-binary", ":all:", "--no-user", "--ignore-installed", "bar")
.and_return(true)

subject.pip_install ["foo", "bar"]
Expand All @@ -142,7 +133,7 @@
expect(res).to receive(:stage).and_yield
expect(formula).to receive(:system)
.with(dir/"bin/pip", "install", "-v", "--no-deps",
"--no-binary", ":all:", "--ignore-installed", Pathname.pwd)
"--no-binary", ":all:", "--no-user", "--ignore-installed", Pathname.pwd)
.and_return(true)

subject.pip_install res
Expand Down