Skip to content

Commit

Permalink
cellars: write tag specific cellars
Browse files Browse the repository at this point in the history
With Homebrew#10186 now merged, the tag specific cellar information is being read by brew.
This PR (once merged) will start adding the cellar information for each tag instead
of having a single cellar line on the top of the bottle block.

Each new CI build in homebrew-core will slowly start migrating the cellar lines to
the right place. If keep-old is used, the old "all tag" cellar line is removed and
added to each tag.
  • Loading branch information
iMichka authored and MikeMcQuaid committed Jan 28, 2021
1 parent fc1c142 commit de2e309
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 67 deletions.
71 changes: 40 additions & 31 deletions Library/Homebrew/dev-cmd/bottle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,11 @@
HOMEBREW_LINUX_DEFAULT_PREFIX].include?(prefix) %>
prefix "<%= prefix %>"
<% end %>
<% if cellar.is_a? Symbol %>
cellar :<%= cellar %>
<% elsif ![Homebrew::DEFAULT_MACOS_CELLAR,
Homebrew::DEFAULT_MACOS_ARM_CELLAR,
Homebrew::DEFAULT_LINUX_CELLAR].include?(cellar) %>
cellar "<%= cellar %>"
<% end %>
<% if rebuild.positive? %>
rebuild <%= rebuild %>
<% end %>
<% checksums.each do |checksum_value| %>
<% checksum, macos = checksum_value.shift %>
sha256 "<%= checksum %>" => :<%= macos %>
<% sha256_lines.each do |line| %>
<%= line %>
<% end %>
end
EOS
Expand Down Expand Up @@ -202,9 +194,29 @@ def keg_contain_absolute_symlink_starting_with?(string, keg, args:)
!absolute_symlinks_start_with_string.empty?
end

def generate_sha256_line(tag, digest, cellar)
line = %Q(sha256 "#{digest}" => :#{tag})
return line if cellar.blank?
return "#{line}, :cellar => :#{cellar}" if cellar.is_a? Symbol

default_cellars = [
Homebrew::DEFAULT_MACOS_CELLAR,
Homebrew::DEFAULT_MACOS_ARM_CELLAR,
Homebrew::DEFAULT_LINUX_CELLAR,
]
return %Q(#{line}, :cellar => "#{cellar}") if default_cellars.exclude?(cellar)

line
end

def bottle_output(bottle)
sha256_lines = bottle.checksums.map do |checksum|
generate_sha256_line(checksum["tag"], checksum["digest"], checksum["cellar"])
end
erb_binding = bottle.instance_eval { binding }
erb_binding.local_variable_set(:sha256_lines, sha256_lines)
erb = ERB.new BOTTLE_ERB
erb.result(bottle.instance_eval { binding }).gsub(/^\s*$\n/, "")
erb.result(erb_binding).gsub(/^\s*$\n/, "")
end

def sudo_purge
Expand Down Expand Up @@ -451,19 +463,14 @@ def parse_json_files(filenames)

def merge_json_files(json_files)
json_files.reduce({}) do |hash, json_file|
hash.deep_merge(json_file) do |key, first, second|
if key == "cellar"
# Prioritize HOMEBREW_CELLAR over :any over :any_skip_relocation
cellars = [first, second]
next HOMEBREW_CELLAR if cellars.include?(HOMEBREW_CELLAR)
next first if first.start_with?("/")
next second if second.start_with?("/")
next "any" if cellars.include?("any")
next "any_skip_relocation" if cellars.include?("any_skip_relocation")
json_file.each_value do |json_hash|
json_bottle = json_hash["bottle"]
cellar = json_bottle.delete("cellar")
json_bottle["tags"].each_value do |json_platform|
json_platform["cellar"] ||= cellar
end

second
end
hash.deep_merge(json_file)
end
end

Expand All @@ -476,13 +483,13 @@ def merge(args:)

bottle = BottleSpecification.new
bottle.root_url bottle_hash["bottle"]["root_url"]
cellar = bottle_hash["bottle"]["cellar"]
cellar = cellar.to_sym if any_cellars.include?(cellar)
bottle.cellar cellar
bottle.prefix bottle_hash["bottle"]["prefix"]
bottle.rebuild bottle_hash["bottle"]["rebuild"]
bottle_hash["bottle"]["tags"].each do |tag, tag_hash|
bottle.sha256 tag_hash["sha256"] => tag.to_sym
cellar = tag_hash["cellar"]
cellar = cellar.to_sym if any_cellars.include?(cellar)
sha256_hash = { tag_hash["sha256"] => tag.to_sym, :cellar => cellar }
bottle.sha256 sha256_hash
end

if args.write?
Expand Down Expand Up @@ -533,16 +540,16 @@ def merge_bottle_spec(old_keys, old_bottle_spec, new_bottle_hash)
new_values = {
root_url: new_bottle_hash["root_url"],
prefix: new_bottle_hash["prefix"],
cellar: new_bottle_hash["cellar"],
rebuild: new_bottle_hash["rebuild"],
}

skip_keys = [:sha256, :cellar]
old_keys.each do |key|
next if key == :sha256
next if skip_keys.include?(key)

old_value = old_bottle_spec.send(key).to_s
new_value = new_values[key].to_s
next if key == :cellar && old_value == "any" && new_value == "any_skip_relocation"

next if old_value.present? && new_value == old_value

mismatches << "#{key}: old: #{old_value.inspect}, new: #{new_value.inspect}"
Expand All @@ -551,12 +558,14 @@ def merge_bottle_spec(old_keys, old_bottle_spec, new_bottle_hash)
return [mismatches, checksums] if old_keys.exclude? :sha256

old_bottle_spec.collector.each_key do |tag|
old_value = old_bottle_spec.collector[tag][:checksum].hexdigest
old_checksum_hash = old_bottle_spec.collector[tag]
old_hexdigest = old_checksum_hash[:checksum].hexdigest
old_cellar = old_checksum_hash[:cellar]
new_value = new_bottle_hash.dig("tags", tag.to_s)
if new_value.present?
mismatches << "sha256 => #{tag}"
else
checksums << { old_value => tag }
checksums << { old_hexdigest => tag, :cellar => old_cellar }
end
end

Expand Down
5 changes: 3 additions & 2 deletions Library/Homebrew/software_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,9 @@ def checksums
end
tags.reverse.map do |tag|
{
collector[tag][:checksum] => tag,
cellar: collector[tag][:cellar],
"tag" => tag,
"digest" => collector[tag][:checksum],
"cellar" => collector[tag][:cellar],
}
end
end
Expand Down
90 changes: 56 additions & 34 deletions Library/Homebrew/test/dev-cmd/bottle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,26 @@ def stub_hash(parameters)
)

hello_hash = bottles_hash["hello"]
expect(hello_hash["bottle"]["cellar"]).to eq("any_skip_relocation")
expect(hello_hash["bottle"]["tags"]["big_sur"]["cellar"]).to eq("any_skip_relocation")
expect(hello_hash["bottle"]["tags"]["big_sur"]["filename"]).to eq("hello-1.0.big_sur.bottle.tar.gz")
expect(hello_hash["bottle"]["tags"]["big_sur"]["local_filename"]).to eq("hello--1.0.big_sur.bottle.tar.gz")
expect(hello_hash["bottle"]["tags"]["big_sur"]["sha256"]).to eq(
"a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f",
)
expect(hello_hash["bottle"]["tags"]["catalina"]["cellar"]).to eq("any_skip_relocation")
expect(hello_hash["bottle"]["tags"]["catalina"]["filename"]).to eq("hello-1.0.catalina.bottle.tar.gz")
expect(hello_hash["bottle"]["tags"]["catalina"]["local_filename"]).to eq("hello--1.0.catalina.bottle.tar.gz")
expect(hello_hash["bottle"]["tags"]["catalina"]["sha256"]).to eq(
"5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac",
)
unzip_hash = bottles_hash["unzip"]
expect(unzip_hash["bottle"]["cellar"]).to eq("any")
expect(unzip_hash["bottle"]["tags"]["big_sur"]["cellar"]).to eq("any_skip_relocation")
expect(unzip_hash["bottle"]["tags"]["big_sur"]["filename"]).to eq("unzip-2.0.big_sur.bottle.tar.gz")
expect(unzip_hash["bottle"]["tags"]["big_sur"]["local_filename"]).to eq("unzip--2.0.big_sur.bottle.tar.gz")
expect(unzip_hash["bottle"]["tags"]["big_sur"]["sha256"]).to eq(
"16cf230afdfcb6306c208d169549cf8773c831c8653d2c852315a048960d7e72",
)
expect(unzip_hash["bottle"]["tags"]["catalina"]["cellar"]).to eq("any")
expect(unzip_hash["bottle"]["tags"]["catalina"]["filename"]).to eq("unzip-2.0.catalina.bottle.tar.gz")
expect(unzip_hash["bottle"]["tags"]["catalina"]["local_filename"]).to eq("unzip--2.0.catalina.bottle.tar.gz")
expect(unzip_hash["bottle"]["tags"]["catalina"]["sha256"]).to eq(
Expand Down Expand Up @@ -202,16 +204,6 @@ def stub_hash(parameters)
]
end

it "checks for conflicting cellar" do
old_spec = BottleSpecification.new
old_spec.cellar("/opt/failbrew/Cellar")
new_hash = { "cellar" => "/opt/testbrew/Cellar" }
expect(homebrew.merge_bottle_spec([:cellar], old_spec, new_hash)).to eq [
['cellar: old: "/opt/failbrew/Cellar", new: "/opt/testbrew/Cellar"'],
[],
]
end

it "checks for conflicting rebuild number" do
old_spec = BottleSpecification.new
old_spec.rebuild(1)
Expand All @@ -227,12 +219,48 @@ def stub_hash(parameters)
old_spec.sha256("109c0cb581a7b5d84da36d84b221fb9dd0f8a927b3044d82611791c9907e202e" => :catalina)
old_spec.sha256("7571772bf7a0c9fe193e70e521318b53993bee6f351976c9b6e01e00d13d6c3f" => :mojave)
new_hash = { "tags" => { "catalina" => "ec6d7f08412468f28dee2be17ad8cd8b883b16b34329efcecce019b8c9736428" } }
expected_checksum_hash = { "7571772bf7a0c9fe193e70e521318b53993bee6f351976c9b6e01e00d13d6c3f" => :mojave }
expected_checksum_hash[:cellar] = Homebrew::DEFAULT_CELLAR
expect(homebrew.merge_bottle_spec([:sha256], old_spec, new_hash)).to eq [
["sha256 => catalina"],
[{ "7571772bf7a0c9fe193e70e521318b53993bee6f351976c9b6e01e00d13d6c3f" => :mojave }],
[expected_checksum_hash],
]
end
end

describe "::generate_sha256_line", :needs_linux do
it "generates a string without cellar" do
expect(homebrew.generate_sha256_line(:catalina, "deadbeef", nil)).to eq(
<<~RUBY.chomp,
sha256 "deadbeef" => :catalina
RUBY
)
end

it "generates a string with cellar symbol" do
expect(homebrew.generate_sha256_line(:catalina, "deadbeef", :any)).to eq(
<<~RUBY.chomp,
sha256 "deadbeef" => :catalina, :cellar => :any
RUBY
)
end

it "generates a string with default cellar path" do
expect(homebrew.generate_sha256_line(:catalina, "deadbeef", Homebrew::DEFAULT_LINUX_CELLAR)).to eq(
<<~RUBY.chomp,
sha256 "deadbeef" => :catalina
RUBY
)
end

it "generates a string with non-default cellar path" do
expect(homebrew.generate_sha256_line(:catalina, "deadbeef", "/home/test")).to eq(
<<~RUBY.chomp,
sha256 "deadbeef" => :catalina, :cellar => "/home/test"
RUBY
)
end
end
end

describe "brew bottle --merge", :integration_test, :needs_linux do
Expand Down Expand Up @@ -292,9 +320,8 @@ def stub_hash(parameters)
==> testball
bottle do
root_url "#{HOMEBREW_BOTTLE_DEFAULT_DOMAIN}"
cellar :any_skip_relocation
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur, :cellar => :any_skip_relocation
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina, :cellar => :any_skip_relocation
end
EOS

Expand All @@ -307,9 +334,8 @@ class Testball < Formula
bottle do
root_url "#{HOMEBREW_BOTTLE_DEFAULT_DOMAIN}"
cellar :any_skip_relocation
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur, :cellar => :any_skip_relocation
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina, :cellar => :any_skip_relocation
end
option "with-foo", "Build with foo"
Expand Down Expand Up @@ -357,9 +383,8 @@ def install
==> testball
bottle do
root_url "#{HOMEBREW_BOTTLE_DEFAULT_DOMAIN}"
cellar :any_skip_relocation
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur, :cellar => :any_skip_relocation
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina, :cellar => :any_skip_relocation
end
EOS

Expand All @@ -374,9 +399,8 @@ class Testball < Formula
bottle do
root_url "#{HOMEBREW_BOTTLE_DEFAULT_DOMAIN}"
cellar :any_skip_relocation
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur, :cellar => :any_skip_relocation
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina, :cellar => :any_skip_relocation
end
def install
Expand Down Expand Up @@ -421,7 +445,7 @@ def install
bottle do
root_url "#{HOMEBREW_BOTTLE_DEFAULT_DOMAIN}"
cellar :any_skip_relocation
cellar :any
sha256 "6971b6eebf4c00eaaed72a1104a49be63861eabc95d679a0c84040398e320059" => :high_sierra
end
EOS
Expand All @@ -440,10 +464,9 @@ def install
==> testball
bottle do
root_url "#{HOMEBREW_BOTTLE_DEFAULT_DOMAIN}"
cellar :any_skip_relocation
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina
sha256 "6971b6eebf4c00eaaed72a1104a49be63861eabc95d679a0c84040398e320059" => :high_sierra
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur, :cellar => :any_skip_relocation
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina, :cellar => :any_skip_relocation
sha256 "6971b6eebf4c00eaaed72a1104a49be63861eabc95d679a0c84040398e320059" => :high_sierra, :cellar => :any
end
EOS

Expand All @@ -458,10 +481,9 @@ class Testball < Formula
bottle do
root_url "#{HOMEBREW_BOTTLE_DEFAULT_DOMAIN}"
cellar :any_skip_relocation
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina
sha256 "6971b6eebf4c00eaaed72a1104a49be63861eabc95d679a0c84040398e320059" => :high_sierra
sha256 "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f" => :big_sur, :cellar => :any_skip_relocation
sha256 "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac" => :catalina, :cellar => :any_skip_relocation
sha256 "6971b6eebf4c00eaaed72a1104a49be63861eabc95d679a0c84040398e320059" => :high_sierra, :cellar => :any
end
def install
Expand Down

0 comments on commit de2e309

Please sign in to comment.