Skip to content

Commit

Permalink
cmd/info: show size information
Browse files Browse the repository at this point in the history
  • Loading branch information
cho-m committed Aug 26, 2024
1 parent abc0584 commit 0d76f68
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 17 deletions.
13 changes: 13 additions & 0 deletions Library/Homebrew/cmd/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Info < AbstractCommand
switch "--github",
description: "Open the GitHub source page for <formula> and <cask> in a browser. " \
"To view the history locally: `brew log -p` <formula> or <cask>"
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 <version> is `v1` for " \
"<formula>. For <formula> and <cask> use `v2`. See the docs for examples of using the " \
Expand Down Expand Up @@ -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

Check warning on line 311 in Library/Homebrew/cmd/info.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/info.rb#L311

Added line #L311 was not covered by tests
puts "Bottle size: #{disk_usage_readable(bottle_size)}" if bottle_size.positive?
installed_size = bottle.installed_size

Check warning on line 313 in Library/Homebrew/cmd/info.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/info.rb#L313

Added line #L313 was not covered by tests
puts "Installed size: #{disk_usage_readable(installed_size)}" if installed_size.positive?
rescue RuntimeError => e
odebug e

Check warning on line 316 in Library/Homebrew/cmd/info.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/info.rb#L316

Added line #L316 was not covered by tests
end
end
else
puts "Installed"
kegs.each do |keg|
Expand Down
11 changes: 9 additions & 2 deletions Library/Homebrew/downloadable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 27 additions & 13 deletions Library/Homebrew/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -286,6 +286,27 @@ def verify_download_integrity(_filename)
end

def tab
tab = manifest_annotations["sh.brew.tab"]

Check warning on line 289 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L289

Added line #L289 was not covered by tests
raise Error, "Couldn't find tab from manifest." if tab.blank?

begin
JSON.parse(tab)

Check warning on line 293 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L293

Added line #L293 was not covered by tests
rescue JSON::ParserError
raise Error, "Couldn't parse tab JSON."

Check warning on line 295 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L295

Added line #L295 was not covered by tests
end
end

def bottle_size
manifest_annotations["sh.brew.bottle.size"].to_i

Check warning on line 300 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L300

Added line #L300 was not covered by tests
end

def installed_size
manifest_annotations["sh.brew.bottle.installed_size"].to_i

Check warning on line 304 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L304

Added line #L304 was not covered by tests
end

private

def manifest_annotations
json = begin
JSON.parse(cached_download.read)
rescue JSON::ParserError
Expand All @@ -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"] }

Check warning on line 320 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L320

Added line #L320 was not covered by tests
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|

Check warning on line 325 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L325

Added line #L325 was not covered by tests
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

Check warning on line 332 in Library/Homebrew/resource.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/resource.rb#L332

Added line #L332 was not covered by tests
end
end

Expand Down
18 changes: 16 additions & 2 deletions Library/Homebrew/software_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:)

Check warning on line 394 in Library/Homebrew/software_spec.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/software_spec.rb#L394

Added line #L394 was not covered by tests
rescue DownloadError
raise unless fallback_on_error

Expand All @@ -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

Check warning on line 417 in Library/Homebrew/software_spec.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/software_spec.rb#L417

Added line #L417 was not covered by tests
end

sig { returns(Integer) }
def installed_size
return 0 unless github_packages_manifest_resource&.downloaded?

github_packages_manifest_resource.installed_size

Check warning on line 424 in Library/Homebrew/software_spec.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/software_spec.rb#L424

Added line #L424 was not covered by tests
end

sig { returns(Filename) }
def filename
Filename.create(resource.owner, @tag, @spec.rebuild)
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/sorbet/rbi/dsl/homebrew/cmd/info.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0d76f68

Please sign in to comment.