Skip to content

Commit

Permalink
language/python: order args for virtualenv_install_with_resources
Browse files Browse the repository at this point in the history
Add `without`, `start_with`, and `end_with` to allow basic control over
the order that resources are installed so that the situations where we
have to split up `virtualenv_install_with_resources` is reduced.

Co-authored-by: Kevin <[email protected]>
Co-authored-by: Mike McQuaid <[email protected]>
Signed-off-by: Michael Cho <[email protected]>
  • Loading branch information
3 people committed Apr 13, 2024
1 parent 17d5ab3 commit e333cfc
Show file tree
Hide file tree
Showing 2 changed files with 303 additions and 124 deletions.
35 changes: 33 additions & 2 deletions Library/Homebrew/language/python.rb
Expand Up @@ -211,6 +211,21 @@ def needs_python?(python)
(requirements.to_a | deps).any? { |r| r.name.split("/").last == python && r.required? }
end

# @api private
sig {
params(
resources_hash: T::Hash[String, Resource],
resource_names: T::Array[String],
).returns(T::Array[Resource])
}
def slice_resources!(resources_hash, resource_names)
resource_names.map do |resource_name|
resources_hash.delete(resource_name) do
raise ArgumentError, "Resource \"#{resource_name}\" is not defined in formula or is already used"
end
end
end

# Helper method for the common case of installing a Python application.
# Creates a virtualenv in `libexec`, installs all `resource`s defined
# on the formula, and then installs the formula. An options hash may be
Expand All @@ -224,10 +239,13 @@ def needs_python?(python)
system_site_packages: T::Boolean,
without_pip: T::Boolean,
link_manpages: T::Boolean,
without: T.nilable(T.any(String, T::Array[String])),
start_with: T.nilable(T.any(String, T::Array[String])),
end_with: T.nilable(T.any(String, T::Array[String])),
).returns(Virtualenv)
}
def virtualenv_install_with_resources(using: nil, system_site_packages: true, without_pip: true,
link_manpages: false)
link_manpages: false, without: nil, start_with: nil, end_with: nil)
python = using
if python.nil?
wanted = python_names.select { |py| needs_python?(py) }
Expand All @@ -237,9 +255,22 @@ def virtualenv_install_with_resources(using: nil, system_site_packages: true, wi
python = T.must(wanted.first)
python = "python3" if python == "python"
end

venv_resources = if without.nil? && start_with.nil? && end_with.nil?
resources
else
remaining_resources = resources.to_h { |resource| [resource.name, resource] }

slice_resources!(remaining_resources, Array(without))
start_with_resources = slice_resources!(remaining_resources, Array(start_with))
end_with_resources = slice_resources!(remaining_resources, Array(end_with))

start_with_resources + remaining_resources.values + end_with_resources
end

venv = virtualenv_create(libexec, python.delete("@"), system_site_packages:,
without_pip:)
venv.pip_install resources
venv.pip_install venv_resources
venv.pip_install_and_link(T.must(buildpath), link_manpages:)
venv
end
Expand Down

0 comments on commit e333cfc

Please sign in to comment.