diff options
| author | Rakesh | 2015-11-17 16:49:55 +0530 |
|---|---|---|
| committer | Mike McQuaid | 2015-12-30 08:11:23 +0000 |
| commit | e4f2a1e0ef83d426bb167f003c06369fdce54db2 (patch) | |
| tree | 535085b68537df690585a0f418a0f29e116fa2e8 | |
| parent | a96a9004f4600ed71b0d9f6a22fbc9b2a2fe41f8 (diff) | |
| download | brew-e4f2a1e0ef83d426bb167f003c06369fdce54db2.tar.bz2 | |
pathname: store file count and disk usage.
especially for directory instances of `Pathname` class and all
instances of `Keg` class.
| -rw-r--r-- | Library/Homebrew/extend/pathname.rb | 52 | ||||
| -rw-r--r-- | 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 8ff1f29a9..555ca7f35 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 524538238..b977ea055 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 |
