801 lines
27 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
require "formula"
2016-04-25 17:57:51 +01:00
require "utils/bottles"
require "tab"
require "keg"
require "formula_versions"
2019-04-17 18:25:08 +09:00
require "cli/parser"
require "utils/inreplace"
require "erb"
require "utils/gzip"
2021-08-06 11:42:55 -04:00
require "api"
BOTTLE_ERB = <<-EOS
2013-09-21 21:21:42 +01:00
bottle do
<% if [HOMEBREW_BOTTLE_DEFAULT_DOMAIN.to_s,
"#{HOMEBREW_BOTTLE_DEFAULT_DOMAIN}/bottles"].exclude?(root_url) %>
root_url "<%= root_url %>"<% if root_url_using.present? %>,
using: <%= root_url_using %>
<% end %>
<% end %>
2017-09-24 20:12:58 +01:00
<% if rebuild.positive? %>
2016-08-18 17:32:35 +01:00
rebuild <%= rebuild %>
2013-09-21 21:21:42 +01:00
<% end %>
<% sha256_lines.each do |line| %>
<%= line %>
2013-09-23 17:30:47 +01:00
<% end %>
2013-09-21 21:21:42 +01:00
end
EOS
MAXIMUM_STRING_MATCHES = 100
ALLOWABLE_HOMEBREW_REPOSITORY_LINKS = [
%r{#{Regexp.escape(HOMEBREW_LIBRARY)}/Homebrew/os/(mac|linux)/pkgconfig},
].freeze
module Homebrew
2020-10-20 12:03:48 +02:00
extend T::Sig
2016-09-26 01:44:51 +02:00
module_function
2020-10-20 12:03:48 +02:00
sig { returns(CLI::Parser) }
def bottle_args
Homebrew::CLI::Parser.new do
description <<~EOS
Generate a bottle (binary package) from a formula that was installed with
2018-09-28 21:39:52 +05:30
`--build-bottle`.
If the formula specifies a rebuild version, it will be incremented in the
generated DSL. Passing `--keep-old` will attempt to keep it at its original
value, while `--no-rebuild` will remove it.
2018-09-28 21:39:52 +05:30
EOS
switch "--skip-relocation",
2019-04-30 08:44:35 +01:00
description: "Do not check if the bottle can be marked as relocatable."
2018-09-28 21:39:52 +05:30
switch "--force-core-tap",
2019-04-30 08:44:35 +01:00
description: "Build a bottle even if <formula> is not in `homebrew/core` or any installed taps."
2018-09-28 21:39:52 +05:30
switch "--no-rebuild",
2019-04-30 08:44:35 +01:00
description: "If the formula specifies a rebuild version, remove it from the generated DSL."
2018-09-28 21:39:52 +05:30
switch "--keep-old",
2022-06-28 10:09:59 +01:00
description: "If the formula specifies a rebuild version, attempt to preserve its value in the " \
2019-04-30 08:44:35 +01:00
"generated DSL."
switch "--json",
2022-06-28 10:09:59 +01:00
description: "Write bottle information to a JSON file, which can be used as the value for " \
2019-04-30 08:44:35 +01:00
"`--merge`."
2018-09-28 21:39:52 +05:30
switch "--merge",
2022-06-28 10:09:59 +01:00
description: "Generate an updated bottle block for a formula and optionally merge it into the " \
2022-06-28 10:17:14 +01:00
"formula file. Instead of a formula name, requires the path to a JSON file generated " \
"with `brew bottle --json` <formula>."
2018-09-28 21:39:52 +05:30
switch "--write",
2019-04-30 08:44:35 +01:00
depends_on: "--merge",
2022-06-28 10:09:59 +01:00
description: "Write changes to the formula file. A new commit will be generated unless " \
2019-04-30 08:44:35 +01:00
"`--no-commit` is passed."
2018-09-28 21:39:52 +05:30
switch "--no-commit",
2019-04-30 08:44:35 +01:00
depends_on: "--write",
2022-06-28 10:09:59 +01:00
description: "When passed with `--write`, a new commit will not generated after writing changes " \
2019-04-30 08:44:35 +01:00
"to the formula file."
switch "--only-json-tab",
depends_on: "--json",
description: "When passed with `--json`, the tab will be written to the JSON file but not the bottle."
2022-11-04 18:46:43 +00:00
switch "--no-all-checks",
depends_on: "--merge",
description: "Don't try to create an `all` bottle or stop a no-change upload."
flag "--committer=",
description: "Specify a committer name and email in `git`'s standard author format."
flag "--root-url=",
2019-04-30 08:44:35 +01:00
description: "Use the specified <URL> as the root of the bottle's URL instead of Homebrew's default."
flag "--root-url-using=",
2022-06-28 10:09:59 +01:00
description: "Use the specified download strategy class for downloading the bottle's URL instead of " \
2021-04-29 12:51:15 -04:00
"Homebrew's default."
2020-07-30 18:40:10 +02:00
conflicts "--no-rebuild", "--keep-old"
2021-01-10 14:26:40 -05:00
named_args [:installed_formula, :file], min: 1
2018-03-25 12:22:29 +05:30
end
end
def bottle
2020-07-30 18:40:10 +02:00
args = bottle_args.parse
2018-03-25 12:22:29 +05:30
if args.merge?
Homebrew.install_bundler_gems!
return merge(args: args)
end
2018-09-17 02:45:00 +02:00
args.named.to_resolved_formulae(uniq: false).each do |f|
bottle_formula f, args: args
2018-03-25 12:22:29 +05:30
end
end
def keg_contain?(string, keg, ignores, formula_and_runtime_deps_names = nil, args:)
@put_string_exists_header, @put_filenames = nil
2015-02-26 19:13:10 +00:00
print_filename = lambda do |str, filename|
2015-02-26 19:13:10 +00:00
unless @put_string_exists_header
opoo "String '#{str}' still exists in these files:"
2015-02-26 19:13:10 +00:00
@put_string_exists_header = true
end
2015-02-26 19:13:10 +00:00
@put_filenames ||= []
2016-09-22 20:12:28 +02:00
return if @put_filenames.include?(filename)
2016-09-22 20:12:28 +02:00
2016-08-30 21:38:13 +02:00
puts Formatter.error(filename.to_s)
2016-09-22 20:12:28 +02:00
@put_filenames << filename
end
result = false
keg.each_unique_file_matching(string) do |file|
next if Metafiles::EXTENSIONS.include?(file.extname) # Skip document files.
linked_libraries = Keg.file_linked_libraries(file, string)
result ||= !linked_libraries.empty?
if args.verbose?
print_filename.call(string, file) unless linked_libraries.empty?
linked_libraries.each do |lib|
2016-10-02 08:40:38 +02:00
puts " #{Tty.bold}-->#{Tty.reset} links to #{lib}"
end
end
text_matches = Keg.text_matches_in_file(file, string, ignores, linked_libraries, formula_and_runtime_deps_names)
result = true if text_matches.any?
next if !args.verbose? || text_matches.empty?
2018-09-17 02:45:00 +02:00
print_filename.call(string, file)
2016-09-11 17:41:51 +01:00
text_matches.first(MAXIMUM_STRING_MATCHES).each do |match, offset|
2016-10-02 08:40:38 +02:00
puts " #{Tty.bold}-->#{Tty.reset} match '#{match}' at offset #{Tty.bold}0x#{offset}#{Tty.reset}"
2016-09-11 17:41:51 +01:00
end
2016-09-11 17:41:51 +01:00
if text_matches.size > MAXIMUM_STRING_MATCHES
2019-11-29 14:53:01 -05:00
puts "Only the first #{MAXIMUM_STRING_MATCHES} matches were output."
end
end
2020-08-01 14:56:11 -04:00
keg_contain_absolute_symlink_starting_with?(string, keg, args: args) || result
2016-07-26 21:50:00 -07:00
end
2020-08-01 14:56:11 -04:00
def keg_contain_absolute_symlink_starting_with?(string, keg, args:)
absolute_symlinks_start_with_string = []
keg.find do |pn|
next if !pn.symlink? || !(link = pn.readlink).absolute?
2018-09-17 02:45:00 +02:00
2016-09-23 11:01:40 +02:00
absolute_symlinks_start_with_string << pn if link.to_s.start_with?(string)
end
2020-09-01 14:05:52 +01:00
if args.verbose? && absolute_symlinks_start_with_string.present?
opoo "Absolute symlink starting with #{string}:"
absolute_symlinks_start_with_string.each do |pn|
puts " #{pn} -> #{pn.resolved_path}"
end
end
!absolute_symlinks_start_with_string.empty?
end
2021-02-02 15:09:39 -05:00
def cellar_parameter_needed?(cellar)
default_cellars = [
Homebrew::DEFAULT_MACOS_CELLAR,
Homebrew::DEFAULT_MACOS_ARM_CELLAR,
Homebrew::DEFAULT_LINUX_CELLAR,
]
2021-02-02 15:09:39 -05:00
cellar.present? && default_cellars.exclude?(cellar)
end
def generate_sha256_line(tag, digest, cellar, tag_column, digest_column)
line = "sha256 "
tag_column += line.length
digest_column += line.length
if cellar.is_a?(Symbol)
2021-02-02 15:09:39 -05:00
line += "cellar: :#{cellar},"
elsif cellar_parameter_needed?(cellar)
line += %Q(cellar: "#{cellar}",)
end
2021-02-02 15:09:39 -05:00
line += " " * (tag_column - line.length)
line += "#{tag}:"
line += " " * (digest_column - line.length)
%Q(#{line}"#{digest}")
end
def bottle_output(bottle, root_url_using)
2021-02-02 15:09:39 -05:00
cellars = bottle.checksums.map do |checksum|
cellar = checksum["cellar"]
next unless cellar_parameter_needed? cellar
case cellar
when String
%Q("#{cellar}")
when Symbol
":#{cellar}"
end
end.compact
tag_column = cellars.empty? ? 0 : "cellar: #{cellars.max_by(&:length)}, ".length
tags = bottle.checksums.map { |checksum| checksum["tag"] }
# Start where the tag ends, add the max length of the tag, add two for the `: `
digest_column = tag_column + tags.max_by(&:length).length + 2
sha256_lines = bottle.checksums.map do |checksum|
2021-02-02 15:09:39 -05:00
generate_sha256_line(checksum["tag"], checksum["digest"], checksum["cellar"], tag_column, digest_column)
end
erb_binding = bottle.instance_eval { binding }
erb_binding.local_variable_set(:sha256_lines, sha256_lines)
erb_binding.local_variable_set(:root_url_using, root_url_using)
2013-09-21 21:21:42 +01:00
erb = ERB.new BOTTLE_ERB
erb.result(erb_binding).gsub(/^\s*$\n/, "")
end
def sudo_purge
return unless ENV["HOMEBREW_BOTTLE_SUDO_PURGE"]
system "/usr/bin/sudo", "--non-interactive", "/usr/sbin/purge"
end
def setup_tar_and_args!(args, mtime)
# Without --only-json-tab bottles are never reproducible
default_tar_args = ["tar", [].freeze].freeze
return default_tar_args unless args.only_json_tab?
2023-02-06 21:15:38 +09:00
2023-02-07 19:17:00 +09:00
# Ensure tar is set up for reproducibility.
# https://reproducible-builds.org/docs/archives/
gnutar_args = [
2023-02-07 19:17:00 +09:00
"--format", "pax", "--owner", "0", "--group", "0", "--sort", "name", "--mtime=#{mtime}",
# Set exthdr names to exclude PID (for GNU tar <1.33). Also don't store atime and ctime.
"--pax-option", "globexthdr.name=/GlobalHead.%n,exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime"
].freeze
# TODO: Refactor and move to extend/os
return ["tar", gnutar_args].freeze if OS.linux? # rubocop:disable Homebrew/MoveToExtendOS
# Use gnu-tar on macOS as it can be set up for reproducibility better than libarchive.
begin
gnu_tar = Formula["gnu-tar"]
rescue FormulaUnavailableError
return default_tar_args
end
ensure_formula_installed!(gnu_tar, reason: "bottling")
["#{gnu_tar.opt_bin}/gtar", gnutar_args].freeze
2023-02-07 19:17:00 +09:00
end
def formula_ignores(f)
ignores = []
cellar_regex = Regexp.escape(HOMEBREW_CELLAR)
prefix_regex = Regexp.escape(HOMEBREW_PREFIX)
# Ignore matches to go keg, because all go binaries are statically linked.
any_go_deps = f.deps.any? do |dep|
dep.name =~ Version.formula_optionally_versioned_regex(:go)
end
if any_go_deps
go_regex = Version.formula_optionally_versioned_regex(:go, full: false)
2021-10-25 23:21:01 -07:00
ignores << %r{#{cellar_regex}/#{go_regex}/[\d.]+/libexec}
end
# TODO: Refactor and move to extend/os
# rubocop:disable Homebrew/MoveToExtendOS
ignores << case f.name
# On Linux, GCC installation can be moved so long as the whole directory tree is moved together:
# https://gcc-help.gcc.gnu.narkive.com/GnwuCA7l/moving-gcc-from-the-installation-path-is-it-allowed.
when Version.formula_optionally_versioned_regex(:gcc)
Regexp.union(%r{#{cellar_regex}/gcc}, %r{#{prefix_regex}/opt/gcc}) if OS.linux?
# binutils is relocatable for the same reason: https://github.com/Homebrew/brew/pull/11899#issuecomment-906804451.
when Version.formula_optionally_versioned_regex(:binutils)
%r{#{cellar_regex}/binutils} if OS.linux?
end
# rubocop:enable Homebrew/MoveToExtendOS
ignores.compact
end
def bottle_formula(f, args:)
local_bottle_json = args.json? && f.local_bottle_path.present?
unless local_bottle_json
return ofail "Formula not installed or up-to-date: #{f.full_name}" unless f.latest_version_installed?
return ofail "Formula was not installed with --build-bottle: #{f.full_name}" unless Utils::Bottles.built_as? f
end
tap = f.tap
if tap.nil?
return ofail "Formula not from core or any installed taps: #{f.full_name}" unless args.force_core_tap?
2016-09-22 20:12:28 +02:00
tap = CoreTap.instance
end
2016-09-23 11:01:40 +02:00
return ofail "Formula has no stable version: #{f.full_name}" unless f.stable
bottle_tag, rebuild = if local_bottle_json
_, tag_string, rebuild_string = Utils::Bottles.extname_tag_rebuild(f.local_bottle_path.to_s)
[tag_string.to_sym, rebuild_string.to_i]
end
bottle_tag = if bottle_tag
Utils::Bottles::Tag.from_symbol(bottle_tag)
else
Utils::Bottles.tag
end
rebuild ||= if args.no_rebuild? || !tap
0
elsif args.keep_old?
f.bottle_specification.rebuild
else
2016-08-18 17:32:35 +01:00
ohai "Determining #{f.full_name} bottle rebuild..."
2021-05-04 09:41:53 -04:00
FormulaVersions.new(f).formula_at_revision("origin/HEAD") do |upstream_f|
if f.pkg_version == upstream_f.pkg_version
2021-05-04 09:41:53 -04:00
upstream_f.bottle_specification.rebuild + 1
else
0
end
end || 0
end
filename = Bottle::Filename.create(f, bottle_tag.to_sym, rebuild)
local_filename = filename.to_s
bottle_path = Pathname.pwd/filename
tab = nil
keg = nil
tap_path = tap.path
tap_git_revision = tap.git_head
tap_git_remote = tap.remote
root_url = args.root_url
relocatable = T.let(false, T::Boolean)
skip_relocation = T.let(false, T::Boolean)
prefix = HOMEBREW_PREFIX.to_s
cellar = HOMEBREW_CELLAR.to_s
if local_bottle_json
bottle_path = f.local_bottle_path
local_filename = bottle_path.basename.to_s
tab_path = Utils::Bottles.receipt_path(bottle_path)
raise "This bottle does not contain the file INSTALL_RECEIPT.json: #{bottle_path}" unless tab_path
tab_json = Utils::Bottles.file_from_bottle(bottle_path, tab_path)
tab = Tab.from_file_content(tab_json, tab_path)
tag_spec = Formula[f.name].bottle_specification.tag_specification_for(bottle_tag, no_older_versions: true)
relocatable = [:any, :any_skip_relocation].include?(tag_spec.cellar)
skip_relocation = tag_spec.cellar == :any_skip_relocation
prefix = bottle_tag.default_prefix
cellar = bottle_tag.default_cellar
else
tar_filename = filename.to_s.sub(/.gz$/, "")
tar_path = Pathname.pwd/tar_filename
keg = Keg.new(f.prefix)
end
ohai "Bottling #{local_filename}..."
2013-09-21 21:21:42 +01:00
formula_and_runtime_deps_names = [f.name] + f.runtime_dependencies.map(&:name)
# this will be nil when using a local bottle
keg&.lock do
original_tab = nil
changed_files = nil
begin
keg.delete_pyc_files!
changed_files = keg.replace_locations_with_placeholders unless args.skip_relocation?
Formula.clear_cache
2019-11-05 20:33:32 +00:00
Keg.clear_cache
Tab.clear_cache
Dependency.clear_cache
Requirement.clear_cache
tab = Tab.for_keg(keg)
original_tab = tab.dup
tab.poured_from_bottle = false
tab.HEAD = nil
tab.time = nil
tab.changed_files = changed_files.dup
if args.only_json_tab?
tab.changed_files.delete(Pathname.new(Tab::FILENAME))
tab.tabfile.unlink
else
tab.write
end
keg.consistent_reproducible_symlink_permissions!
cd cellar do
sudo_purge
# Tar then gzip for reproducible bottles.
tar_mtime = tab.source_modified_time.strftime("%Y-%m-%d %H:%M:%S")
tar, tar_args = setup_tar_and_args!(args, tar_mtime)
safe_system tar, "--create", "--numeric-owner",
*tar_args,
"--file", tar_path, "#{f.name}/#{f.pkg_version}"
sudo_purge
# Set filename as it affects the tarball checksum.
relocatable_tar_path = "#{f}-bottle.tar"
mv tar_path, relocatable_tar_path
# Use gzip, faster to compress than bzip2, faster to uncompress than bzip2
# or an uncompressed tarball (and more bandwidth friendly).
Utils::Gzip.compress_with_options(relocatable_tar_path,
mtime: tab.source_modified_time,
orig_name: relocatable_tar_path,
output: bottle_path)
sudo_purge
end
ohai "Detecting if #{local_filename} is relocatable..." if bottle_path.size > 1 * 1024 * 1024
2020-03-13 21:15:06 +00:00
prefix_check = if Homebrew.default_prefix?(prefix)
File.join(prefix, "opt")
else
2020-03-13 21:15:06 +00:00
prefix
end
# Ignore matches to source code, which is not required at run time.
# These matches may be caused by debugging symbols.
ignores = [%r{/include/|\.(c|cc|cpp|h|hpp)$}]
# Add additional workarounds to ignore
ignores += formula_ignores(f)
repository_reference = if HOMEBREW_PREFIX == HOMEBREW_REPOSITORY
HOMEBREW_LIBRARY
else
HOMEBREW_REPOSITORY
end.to_s
if keg_contain?(repository_reference, keg, ignores + ALLOWABLE_HOMEBREW_REPOSITORY_LINKS, args: args)
odie "Bottle contains non-relocatable reference to #{repository_reference}!"
end
relocatable = true
if args.skip_relocation?
skip_relocation = true
else
relocatable = false if keg_contain?(prefix_check, keg, ignores, formula_and_runtime_deps_names, args: args)
relocatable = false if keg_contain?(cellar, keg, ignores, formula_and_runtime_deps_names, args: args)
if keg_contain?(HOMEBREW_LIBRARY.to_s, keg, ignores, formula_and_runtime_deps_names, args: args)
relocatable = false
end
if prefix != prefix_check
2020-08-01 14:56:11 -04:00
relocatable = false if keg_contain_absolute_symlink_starting_with?(prefix, keg, args: args)
relocatable = false if keg_contain?("#{prefix}/etc", keg, ignores, args: args)
relocatable = false if keg_contain?("#{prefix}/var", keg, ignores, args: args)
relocatable = false if keg_contain?("#{prefix}/share/vim", keg, ignores, args: args)
end
skip_relocation = relocatable && !keg.require_relocation?
end
puts if !relocatable && args.verbose?
rescue Interrupt
ignore_interrupts { bottle_path.unlink if bottle_path.exist? }
raise
ensure
ignore_interrupts do
2017-09-24 19:24:46 +01:00
original_tab&.write
keg.replace_placeholders_with_locations changed_files unless args.skip_relocation?
end
end
end
bottle = BottleSpecification.new
bottle.tap = tap
bottle.root_url(root_url) if root_url
bottle_cellar = if relocatable
if skip_relocation
:any_skip_relocation
else
:any
end
else
cellar
end
2016-08-18 17:32:35 +01:00
bottle.rebuild rebuild
sha256 = bottle_path.sha256
bottle.sha256 cellar: bottle_cellar, bottle_tag.to_sym => sha256
old_spec = f.bottle_specification
if args.keep_old? && !old_spec.checksums.empty?
mismatches = [:root_url, :rebuild].reject do |key|
2017-05-29 18:24:52 +01:00
old_spec.send(key) == bottle.send(key)
end
unless mismatches.empty?
bottle_path.unlink if bottle_path.exist?
mismatches.map! do |key|
old_value = old_spec.send(key).inspect
value = bottle.send(key).inspect
"#{key}: old: #{old_value}, new: #{value}"
end
odie <<~EOS
`--keep-old` was passed but there are changes in:
#{mismatches.join("\n")}
EOS
end
end
output = bottle_output(bottle, args.root_url_using)
puts "./#{local_filename}"
puts output
return unless args.json?
2018-09-17 02:45:00 +02:00
2016-09-22 20:12:28 +02:00
json = {
f.full_name => {
"formula" => {
"name" => f.name,
"pkg_version" => f.pkg_version.to_s,
"path" => f.path.to_s.delete_prefix("#{HOMEBREW_REPOSITORY}/"),
"tap_git_path" => f.path.to_s.delete_prefix("#{tap_path}/"),
"tap_git_revision" => tap_git_revision,
"tap_git_remote" => tap_git_remote,
# descriptions can contain emoji. sigh.
"desc" => f.desc.to_s.encode(
Encoding.find("ASCII"),
invalid: :replace, undef: :replace, replace: "",
).strip,
"license" => SPDX.license_expression_to_string(f.license),
"homepage" => f.homepage,
2016-09-22 20:12:28 +02:00
},
2018-11-02 17:18:07 +00:00
"bottle" => {
2016-09-22 20:12:28 +02:00
"root_url" => bottle.root_url,
"cellar" => bottle_cellar.to_s,
2018-11-02 17:18:07 +00:00
"rebuild" => bottle.rebuild,
"date" => Pathname(filename.to_s).mtime.strftime("%F"),
2018-11-02 17:18:07 +00:00
"tags" => {
bottle_tag.to_s => {
"filename" => filename.url_encode,
"local_filename" => filename.to_s,
"sha256" => sha256,
"tab" => tab.to_bottle_hash,
2016-09-11 17:41:51 +01:00
},
},
},
2016-09-22 20:12:28 +02:00
},
}
puts "Writing #{filename.json}" if args.verbose?
json_path = Pathname(filename.json)
json_path.unlink if json_path.exist?
json_path.write(JSON.pretty_generate(json))
end
def parse_json_files(filenames)
filenames.map do |filename|
2021-04-14 12:42:26 +01:00
JSON.parse(File.read(filename))
end
end
def merge_json_files(json_files)
json_files.reduce({}) do |hash, json_file|
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
end
hash.deep_merge(json_file)
end
end
def merge(args:)
bottles_hash = merge_json_files(parse_json_files(args.named))
2020-09-11 10:29:21 +01:00
any_cellars = ["any", "any_skip_relocation"]
bottles_hash.each do |formula_name, bottle_hash|
ohai formula_name
bottle = BottleSpecification.new
bottle.root_url bottle_hash["bottle"]["root_url"]
2016-08-18 17:32:35 +01:00
bottle.rebuild bottle_hash["bottle"]["rebuild"]
path = HOMEBREW_REPOSITORY/bottle_hash["formula"]["path"]
formula = Formulary.factory(path)
old_bottle_spec = formula.bottle_specification
old_pkg_version = formula.pkg_version
FormulaVersions.new(formula).formula_at_revision("origin/HEAD") do |upstream_f|
old_pkg_version = upstream_f.pkg_version
end
old_bottle_spec_matches = old_bottle_spec &&
bottle_hash["formula"]["pkg_version"] == old_pkg_version.to_s &&
bottle.root_url == old_bottle_spec.root_url &&
old_bottle_spec.collector.tags.present?
# if all the cellars and checksums are the same: we can create an
# `all: $SHA256` bottle.
tag_hashes = bottle_hash["bottle"]["tags"].values
2022-11-04 18:46:43 +00:00
all_bottle = !args.no_all_checks? &&
(!old_bottle_spec_matches || bottle.rebuild != old_bottle_spec.rebuild) &&
tag_hashes.count > 1 &&
tag_hashes.uniq { |tag_hash| "#{tag_hash["cellar"]}-#{tag_hash["sha256"]}" }.count == 1
bottle_hash["bottle"]["tags"].each do |tag, tag_hash|
cellar = tag_hash["cellar"]
cellar = cellar.to_sym if any_cellars.include?(cellar)
tag_sym = if all_bottle
:all
else
tag.to_sym
end
sha256_hash = { cellar: cellar, tag_sym => tag_hash["sha256"] }
bottle.sha256 sha256_hash
break if all_bottle
end
unless args.write?
puts bottle_output(bottle, args.root_url_using)
next
end
2022-11-04 18:46:43 +00:00
no_bottle_changes = if !args.no_all_checks? && old_bottle_spec_matches &&
bottle.rebuild != old_bottle_spec.rebuild
bottle.collector.tags.all? do |tag|
tag_spec = bottle.collector.specification_for(tag)
next false if tag_spec.blank?
old_tag_spec = old_bottle_spec.collector.specification_for(tag)
next false if old_tag_spec.blank?
next false if tag_spec.cellar != old_tag_spec.cellar
tag_spec.checksum.hexdigest == old_tag_spec.checksum.hexdigest
end
end
all_bottle_hash = nil
bottle_hash["bottle"]["tags"].each do |tag, tag_hash|
filename = Bottle::Filename.new(
formula_name,
bottle_hash["formula"]["pkg_version"],
tag,
bottle_hash["bottle"]["rebuild"],
)
if all_bottle && all_bottle_hash.nil?
all_bottle_tag_hash = tag_hash.dup
all_filename = Bottle::Filename.new(
formula_name,
bottle_hash["formula"]["pkg_version"],
"all",
bottle_hash["bottle"]["rebuild"],
)
all_bottle_tag_hash["filename"] = all_filename.url_encode
all_bottle_tag_hash["local_filename"] = all_filename.to_s
cellar = all_bottle_tag_hash.delete("cellar")
all_bottle_formula_hash = bottle_hash.dup
all_bottle_formula_hash["bottle"]["cellar"] = cellar
all_bottle_formula_hash["bottle"]["tags"] = { all: all_bottle_tag_hash }
all_bottle_hash = { formula_name => all_bottle_formula_hash }
puts "Copying #{filename} to #{all_filename}" if args.verbose?
FileUtils.cp filename.to_s, all_filename.to_s
puts "Writing #{all_filename.json}" if args.verbose?
all_local_json_path = Pathname(all_filename.json)
all_local_json_path.unlink if all_local_json_path.exist?
all_local_json_path.write(JSON.pretty_generate(all_bottle_hash))
end
2013-12-27 16:43:34 -06:00
if all_bottle || no_bottle_changes
puts "Removing #{filename} and #{filename.json}" if args.verbose?
FileUtils.rm_f [filename.to_s, filename.json]
end
end
next if no_bottle_changes
require "utils/ast"
formula_ast = Utils::AST::FormulaAST.new(path.read)
checksums = old_checksums(formula, formula_ast, bottle_hash, args: args)
update_or_add = checksums.nil? ? "add" : "update"
checksums&.each(&bottle.method(:sha256))
output = bottle_output(bottle, args.root_url_using)
puts output
case update_or_add
when "update"
formula_ast.replace_bottle_block(output)
when "add"
formula_ast.add_bottle_block(output)
end
path.atomic_write(formula_ast.process)
next if args.no_commit?
Utils::Git.set_name_email!(committer: args.committer.blank?)
Utils::Git.setup_gpg!
if (committer = args.committer)
committer = Utils.parse_author!(committer)
ENV["GIT_COMMITTER_NAME"] = committer[:name]
ENV["GIT_COMMITTER_EMAIL"] = committer[:email]
end
short_name = formula_name.split("/", -1).last
pkg_version = bottle_hash["formula"]["pkg_version"]
path.parent.cd do
safe_system "git", "commit", "--no-edit", "--verbose",
"--message=#{short_name}: #{update_or_add} #{pkg_version} bottle.",
"--", path
end
end
end
def merge_bottle_spec(old_keys, old_bottle_spec, new_bottle_hash)
mismatches = []
checksums = []
new_values = {
root_url: new_bottle_hash["root_url"],
rebuild: new_bottle_hash["rebuild"],
}
skip_keys = [:sha256, :cellar]
old_keys.each do |key|
next if skip_keys.include?(key)
old_value = old_bottle_spec.send(key).to_s
new_value = new_values[key].to_s
next if old_value.present? && new_value == old_value
mismatches << "#{key}: old: #{old_value.inspect}, new: #{new_value.inspect}"
end
return [mismatches, checksums] if old_keys.exclude? :sha256
old_bottle_spec.collector.each_tag do |tag|
old_tag_spec = old_bottle_spec.collector.specification_for(tag)
old_hexdigest = old_tag_spec.checksum.hexdigest
old_cellar = old_tag_spec.cellar
new_value = new_bottle_hash.dig("tags", tag.to_s)
if new_value.present? && new_value["sha256"] != old_hexdigest
mismatches << "sha256 #{tag}: old: #{old_hexdigest.inspect}, new: #{new_value["sha256"].inspect}"
elsif new_value.present? && new_value["cellar"] != old_cellar.to_s
mismatches << "cellar #{tag}: old: #{old_cellar.to_s.inspect}, new: #{new_value["cellar"].inspect}"
else
checksums << { cellar: old_cellar, tag.to_sym => old_hexdigest }
end
end
[mismatches, checksums]
end
2021-01-10 09:14:16 -08:00
def old_checksums(formula, formula_ast, bottle_hash, args:)
bottle_node = formula_ast.bottle_block
return if bottle_node.nil?
return [] unless args.keep_old?
old_keys = Utils::AST.body_children(bottle_node.body).map(&:method_name)
2021-01-10 09:14:16 -08:00
old_bottle_spec = formula.bottle_specification
mismatches, checksums = merge_bottle_spec(old_keys, old_bottle_spec, bottle_hash["bottle"])
if mismatches.present?
odie <<~EOS
`--keep-old` was passed but there are changes in:
#{mismatches.join("\n")}
EOS
end
checksums
end
end