From e4f2a1e0ef83d426bb167f003c06369fdce54db2 Mon Sep 17 00:00:00 2001 From: Rakesh Date: Tue, 17 Nov 2015 16:49:55 +0530 Subject: [PATCH] pathname: store file count and disk usage. especially for directory instances of `Pathname` class and all instances of `Keg` class. --- Library/Homebrew/extend/pathname.rb | 52 +++++++++++++++++++++++------ Library/Homebrew/keg.rb | 8 +++++ 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index 8ff1f29a98..555ca7f35b 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -1,10 +1,51 @@ require "pathname" require "resource" require "metafiles" +require "utils" + +module DiskUsageExtension + def disk_usage + return @disk_usage if @disk_usage + compute_disk_usage + @disk_usage + end + + def file_count + return @file_count if @file_count + compute_disk_usage + @file_count + end + + def abv + out = "" + compute_disk_usage + out << "#{number_readable(@file_count)} files, " if @file_count > 1 + out << "#{disk_usage_readable(@disk_usage)}" + end + + private + + def compute_disk_usage + if self.directory? + @file_count, @disk_usage = [0, 0] + self.find.each do |f| + if !File.directory?(f) and !f.to_s.match(/\.DS_Store/) + @file_count += 1 + @disk_usage += File.size(f) + end + end + else + @file_count = 1 + @disk_usage = self.size + end + end +end # Homebrew extends Ruby's `Pathname` to make our code more readable. # @see http://ruby-doc.org/stdlib-1.8.7/libdoc/pathname/rdoc/Pathname.html Ruby's Pathname API class Pathname + include DiskUsageExtension + # @private BOTTLE_EXTNAME_RX = /(\.[a-z0-9_]+\.bottle\.(\d+\.)?tar\.gz)$/ @@ -394,17 +435,6 @@ class Pathname end end - # @private - def abv - out = "" - n = Utils.popen_read("find", expand_path.to_s, "-type", "f", "!", "-name", ".DS_Store").split("\n").size - out << "#{n} files, " if n > 1 - size = Utils.popen_read("/usr/bin/du", "-hs", expand_path.to_s).split("\t")[0] - size ||= "0B" - out << size.strip - out - end - # We redefine these private methods in order to add the /o modifier to # the Regexp literals, which forces string interpolation to happen only # once instead of each time the method is called. This is fixed in 1.9+. diff --git a/Library/Homebrew/keg.rb b/Library/Homebrew/keg.rb index 5245382384..b977ea055c 100644 --- a/Library/Homebrew/keg.rb +++ b/Library/Homebrew/keg.rb @@ -134,6 +134,14 @@ class Keg path.abv end + def disk_usage + path.disk_usage + end + + def file_count + path.file_count + end + def directory? path.directory? end