From 0d76f683413675d476805cf5ada70bcf6fff7527 Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Mon, 26 Aug 2024 16:59:37 -0400 Subject: [PATCH] cmd/info: show size information --- Library/Homebrew/cmd/info.rb | 13 ++++++ Library/Homebrew/downloadable.rb | 11 ++++- Library/Homebrew/resource.rb | 40 +++++++++++++------ Library/Homebrew/software_spec.rb | 18 ++++++++- .../sorbet/rbi/dsl/homebrew/cmd/info.rbi | 3 ++ 5 files changed, 68 insertions(+), 17 deletions(-) diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index d213dd9c8d4efa..b1bd60145550b4 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -45,6 +45,8 @@ class Info < AbstractCommand switch "--github", description: "Open the GitHub source page for and in a browser. " \ "To view the history locally: `brew log -p` or " + switch "--github-manifest", + description: "Fetch Github package manifest for additional information when not installed." flag "--json", description: "Print a JSON representation. Currently the default value for is `v1` for " \ ". For and use `v2`. See the docs for examples of using the " \ @@ -303,6 +305,17 @@ def info_formula(formula) ] if kegs.empty? puts "Not installed" + if args.github_manifest? && !(bottle = formula.bottle).nil? + begin + bottle.fetch_tab(quiet: !args.debug?) + bottle_size = bottle.bottle_size + puts "Bottle size: #{disk_usage_readable(bottle_size)}" if bottle_size.positive? + installed_size = bottle.installed_size + puts "Installed size: #{disk_usage_readable(installed_size)}" if installed_size.positive? + rescue RuntimeError => e + odebug e + end + end else puts "Installed" kegs.each do |keg| diff --git a/Library/Homebrew/downloadable.rb b/Library/Homebrew/downloadable.rb index 9a82d99f4ee5d3..1fa7dff91a81f2 100644 --- a/Library/Homebrew/downloadable.rb +++ b/Library/Homebrew/downloadable.rb @@ -79,11 +79,18 @@ def downloader end end - sig { params(verify_download_integrity: T::Boolean, timeout: T.nilable(T.any(Integer, Float))).returns(Pathname) } - def fetch(verify_download_integrity: true, timeout: nil) + sig { + params( + verify_download_integrity: T::Boolean, + timeout: T.nilable(T.any(Integer, Float)), + quiet: T::Boolean, + ).returns(Pathname) + } + def fetch(verify_download_integrity: true, timeout: nil, quiet: false) cache.mkpath begin + downloader.quiet! if quiet downloader.fetch(timeout:) rescue ErrorDuringExecution, CurlDownloadStrategyError => e raise DownloadError.new(self, e) diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 61ce2eb408af1b..eb41d06975b29e 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -140,7 +140,7 @@ def files(*files) Partial.new(self, files) end - def fetch(verify_download_integrity: true) + def fetch(verify_download_integrity: true, quiet: false) fetch_patches super @@ -286,6 +286,27 @@ def verify_download_integrity(_filename) end def tab + tab = manifest_annotations["sh.brew.tab"] + raise Error, "Couldn't find tab from manifest." if tab.blank? + + begin + JSON.parse(tab) + rescue JSON::ParserError + raise Error, "Couldn't parse tab JSON." + end + end + + def bottle_size + manifest_annotations["sh.brew.bottle.size"].to_i + end + + def installed_size + manifest_annotations["sh.brew.bottle.installed_size"].to_i + end + + private + + def manifest_annotations json = begin JSON.parse(cached_download.read) rescue JSON::ParserError @@ -296,26 +317,19 @@ def tab manifests = json["manifests"] raise Error, "Missing 'manifests' section." if manifests.blank? - manifests_annotations = manifests.filter_map { |m| m["annotations"] } - raise Error, "Missing 'annotations' section." if manifests_annotations.blank? + annotations = manifests.filter_map { |m| m["annotations"] } + raise Error, "Missing 'annotations' section." if annotations.blank? bottle_digest = bottle.resource.checksum.hexdigest image_ref = GitHubPackages.version_rebuild(bottle.resource.version, bottle.rebuild, bottle.tag.to_s) - manifest_annotations = manifests_annotations.find do |m| + annotations = annotations.find do |m| next if m["sh.brew.bottle.digest"] != bottle_digest m["org.opencontainers.image.ref.name"] == image_ref end - raise Error, "Couldn't find manifest matching bottle checksum." if manifest_annotations.blank? - - tab = manifest_annotations["sh.brew.tab"] - raise Error, "Couldn't find tab from manifest." if tab.blank? + raise Error, "Couldn't find manifest matching bottle checksum." if annotations.blank? - begin - JSON.parse(tab) - rescue JSON::ParserError - raise Error, "Couldn't parse tab JSON." - end + annotations end end diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index ebf7821dad9bb7..252de0eed72fa8 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -388,10 +388,10 @@ def stage resource.downloader.stage end - def fetch_tab + def fetch_tab(quiet: false) return if github_packages_manifest_resource.blank? - github_packages_manifest_resource.fetch + github_packages_manifest_resource.fetch(quiet:) rescue DownloadError raise unless fallback_on_error @@ -410,6 +410,20 @@ def tab_attributes github_packages_manifest_resource.tab end + sig { returns(Integer) } + def bottle_size + return 0 unless github_packages_manifest_resource&.downloaded? + + github_packages_manifest_resource.bottle_size + end + + sig { returns(Integer) } + def installed_size + return 0 unless github_packages_manifest_resource&.downloaded? + + github_packages_manifest_resource.installed_size + end + sig { returns(Filename) } def filename Filename.create(resource.owner, @tag, @spec.rebuild) diff --git a/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/info.rbi b/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/info.rbi index 21c5bdeeded031..031d5fb831b5d2 100644 --- a/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/info.rbi +++ b/Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/info.rbi @@ -38,6 +38,9 @@ class Homebrew::Cmd::Info::Args < Homebrew::CLI::Args sig { returns(T::Boolean) } def github?; end + sig { returns(T::Boolean) } + def github_manifest?; end + sig { returns(T::Boolean) } def github_packages_downloads?; end