Skip to content

Commit

Permalink
Merge pull request #10449 from iMichka/bottle-write
Browse files Browse the repository at this point in the history
cellars: write tag specific cellars
  • Loading branch information
iMichka authored Jan 28, 2021
2 parents ca4a444 + 4afebd2 commit 61e3865
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 72 deletions.
1 change: 1 addition & 0 deletions Library/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ Layout/LineLength:
' name "',
' pkg "',
' pkgutil: "',
' sha256 "',
"#{language}",
"#{version.",
' "/Library/Application Support/',
Expand Down
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)
default_cellars = [
Homebrew::DEFAULT_MACOS_CELLAR,
Homebrew::DEFAULT_MACOS_ARM_CELLAR,
Homebrew::DEFAULT_LINUX_CELLAR,
]
if cellar.is_a?(Symbol)
%Q(sha256 cellar: :#{cellar}, #{tag}: "#{digest}")
elsif cellar.present? && default_cellars.exclude?(cellar)
%Q(sha256 cellar: "#{cellar}", #{tag}: "#{digest}")
else
%Q(sha256 #{tag}: "#{digest}")
end
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 = { cellar: cellar, tag.to_sym => tag_hash["sha256"] }
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 << { cellar: old_cellar, tag => old_hexdigest }
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
100 changes: 61 additions & 39 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 @@ -224,18 +216,54 @@ def stub_hash(parameters)

it "checks for conflicting checksums" do
old_spec = BottleSpecification.new
old_spec.sha256("109c0cb581a7b5d84da36d84b221fb9dd0f8a927b3044d82611791c9907e202e" => :catalina)
old_spec.sha256("7571772bf7a0c9fe193e70e521318b53993bee6f351976c9b6e01e00d13d6c3f" => :mojave)
old_spec.sha256(catalina: "109c0cb581a7b5d84da36d84b221fb9dd0f8a927b3044d82611791c9907e202e")
old_spec.sha256(mojave: "7571772bf7a0c9fe193e70e521318b53993bee6f351976c9b6e01e00d13d6c3f")
new_hash = { "tags" => { "catalina" => "ec6d7f08412468f28dee2be17ad8cd8b883b16b34329efcecce019b8c9736428" } }
expected_checksum_hash = { mojave: "7571772bf7a0c9fe193e70e521318b53993bee6f351976c9b6e01e00d13d6c3f" }
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" do
it "generates a string without cellar" do
expect(homebrew.generate_sha256_line(:catalina, "deadbeef", nil)).to eq(
<<~RUBY.chomp,
sha256 catalina: "deadbeef"
RUBY
)
end

it "generates a string with cellar symbol" do
expect(homebrew.generate_sha256_line(:catalina, "deadbeef", :any)).to eq(
<<~RUBY.chomp,
sha256 cellar: :any, catalina: "deadbeef"
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 catalina: "deadbeef"
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 cellar: "/home/test", catalina: "deadbeef"
RUBY
)
end
end
end

describe "brew bottle --merge", :integration_test, :needs_linux do
describe "brew bottle --merge", :integration_test do
let(:core_tap) { CoreTap.new }
let(:tarball) do
if OS.linux?
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 cellar: :any_skip_relocation, big_sur: "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f"
sha256 cellar: :any_skip_relocation, catalina: "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac"
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 cellar: :any_skip_relocation, big_sur: "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f"
sha256 cellar: :any_skip_relocation, catalina: "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac"
end
option "with-foo", "Build with foo"
Expand Down Expand Up @@ -339,8 +365,8 @@ def install
bottle do
root_url "#{HOMEBREW_BOTTLE_DEFAULT_DOMAIN}"
cellar :any_skip_relocation
sha256 "6b276491297d4052538bd2fd22d5129389f27d90a98f831987236a5b90511b98" => :big_sur
sha256 "16cf230afdfcb6306c208d169549cf8773c831c8653d2c852315a048960d7e72" => :catalina
sha256 big_sur: "6b276491297d4052538bd2fd22d5129389f27d90a98f831987236a5b90511b98"
sha256 catalina: "16cf230afdfcb6306c208d169549cf8773c831c8653d2c852315a048960d7e72"
end
EOS
system "git", "add", "--all"
Expand All @@ -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 cellar: :any_skip_relocation, big_sur: "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f"
sha256 cellar: :any_skip_relocation, catalina: "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac"
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 cellar: :any_skip_relocation, big_sur: "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f"
sha256 cellar: :any_skip_relocation, catalina: "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac"
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 cellar: :any_skip_relocation, big_sur: "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f"
sha256 cellar: :any_skip_relocation, catalina: "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac"
sha256 cellar: :any, high_sierra: "6971b6eebf4c00eaaed72a1104a49be63861eabc95d679a0c84040398e320059"
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 cellar: :any_skip_relocation, big_sur: "a0af7dcbb5c83f6f3f7ecd507c2d352c1a018f894d51ad241ce8492fa598010f"
sha256 cellar: :any_skip_relocation, catalina: "5334dd344986e46b2aa4f0471cac7b0914bd7de7cb890a34415771788d03f2ac"
sha256 cellar: :any, high_sierra: "6971b6eebf4c00eaaed72a1104a49be63861eabc95d679a0c84040398e320059"
end
def install
Expand Down

0 comments on commit 61e3865

Please sign in to comment.