pathname: store file count and disk usage.

especially for directory instances of `Pathname` class and all
instances of `Keg` class.
This commit is contained in:
Rakesh 2015-11-17 16:49:55 +05:30 committed by Mike McQuaid
parent a96a9004f4
commit e4f2a1e0ef
2 changed files with 49 additions and 11 deletions

View File

@ -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+.

View File

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