Module: Weigh::Util

Defined in:
lib/weigh/util.rb

Class Method Summary collapse

Class Method Details

.neat_size(bytes) ⇒ String

Convert a byte count into something a bit more human friendly

Parameters:

  • a (Int)

    byte count

Returns:

  • (String)

    human readable byte count



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/weigh/util.rb', line 9

def self.neat_size(bytes)
  # return a human readble size from the bytes supplied
  bytes = bytes.to_f
  if bytes > 2 ** 50       # PiB: 1024 TiB
    neat = sprintf("%.2f PiB", bytes / 2**50)
  elsif bytes > 2 ** 40    # TiB: 1024 GiB
    neat = sprintf("%.2f TiB", bytes / 2**40)
  elsif bytes > 2 ** 30    # GiB: 1024 MiB
    neat = sprintf("%.2f GiB", bytes / 2**30)
  elsif  bytes > 2 ** 20   # MiB: 1024 KiB
    neat = sprintf("%.2f MiB", bytes / 2**20)
  elsif bytes > 2 ** 10    # KiB: 1024 B
    neat = sprintf("%.2f KiB", bytes / 2**10)
  else                    # bytes
    neat = sprintf("%.0f bytes", bytes)
  end
  neat
end

.report(data) ⇒ Object

Print a summary of the received data

Parameters:

  • (Hash)


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/weigh/util.rb', line 80

def self.report(data)
  data[:summary].sort{|a,b| a[1]<=>b[1]}.each { |elem|
    size     = elem[1]
    filename = elem[0]
    puts sprintf("%15s   %s\n", neat_size(size), filename)
  }

  puts sprintf("%16s %s\n", "---", "---")
  puts sprintf("%15s   %s\n", self.neat_size(data[:total_size]), ":total size")
  puts sprintf("%16s %s\n", "---", "---")

  data
end

.sum_dir(dir, verbose = false) ⇒ Object

Sumarize the size of the given directory

Parameters:

  • full (String)

    path to directory

  • increase (Boolean)

    verbosity



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/weigh/util.rb', line 34

def self.sum_dir(dir,verbose=false)
  # return the size of a given directory
  #"Entering: #{dir}"
  count    = 0
  dir_size = 0
  data     = {}

  Find.find(dir) do |path|
    begin
      puts path if verbose
      count += 1
      if FileTest.symlink?(path)
        puts "skipping symlink " + path if verbose
        next
      end
      if dir == path and File.directory?(path)
        puts "skipping current directory " + path if verbose
        next
      end
      if FileTest.directory?(path)
        ret = sum_dir(path,verbose)
        size = ret[:dir_size]
        count += ret[:count]
        dir_size += size
        Find.prune
      else
        size = FileTest.size(path)
        puts "Found zero size file: #{path}" if verbose
        dir_size += size
      end
    rescue IOError => e
      puts "File vanished #{path}:#{e.message}"
    rescue Errno::ENOENT => e
      puts "Could not open file #{path}:#{e.message}"
    end
  end
  data[:dir_size] = dir_size
  data[:count] = count
  data
end