weigh 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # Weigh
2
+
3
+ A small ruby utility to wieigh directory contents. Heavy stuff to the bottom.
4
+
data/bin/weigh ADDED
@@ -0,0 +1,6 @@
1
+ #! /usr/bin/env ruby
2
+ # Weigh directory contents
3
+
4
+ require 'weigh/cli'
5
+
6
+ Weigh::CLI.run ARGV
data/lib/weigh/cli.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'weigh/flags'
2
+ require 'weigh/runner'
3
+
4
+ module Weigh
5
+ class CLI
6
+
7
+ attr_reader :flags
8
+ attr_reader :runner
9
+
10
+ def initialize(flags)
11
+ @flags = flags
12
+ @runner = Weigh::Runner.new(@flags)
13
+ end
14
+
15
+ def run
16
+ if flags.help?
17
+ puts flags
18
+ exit
19
+ end
20
+
21
+ runner.run
22
+ end
23
+
24
+ def self.run(*args)
25
+ flags = Weigh::Flags.new args
26
+
27
+ return Weigh::CLI.new(flags).run
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,48 @@
1
+ require 'optparse'
2
+
3
+ module Weigh
4
+ class Flags
5
+
6
+ attr_reader :args
7
+ attr_reader :verbose
8
+ attr_reader :depth
9
+ attr_reader :pathlist
10
+
11
+ def initialize(*args)
12
+ @args = []
13
+ @verbose = false
14
+ @depth = 1
15
+ @pathlist = []
16
+
17
+ @options = OptionParser.new do|o|
18
+ o.banner = "Usage: #{File.basename $0} [options] [file|directory...]\n\n"
19
+
20
+ o.on( '--verbose', '-v', 'Speak up' ) do
21
+ @verbose = true
22
+ end
23
+
24
+ o.on( '--depth DEPTH', '-d', 'Sumarize deeper than DEPTH' ) do|d|
25
+ @depth = d
26
+ end
27
+
28
+ o.on( '-h', '--help', 'Display this screen' ) do
29
+ @help = true
30
+ end
31
+ end
32
+
33
+ @args = @options.parse!
34
+
35
+ if ARGV.size > 0
36
+ ARGV.each do |f|
37
+ @pathlist << f
38
+ end
39
+ else
40
+ @pathlist << "."
41
+ end
42
+
43
+ def help?
44
+ @help
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,47 @@
1
+ require 'weigh/util'
2
+ require 'pp'
3
+ require 'find'
4
+
5
+ module Weigh
6
+ class Runner
7
+
8
+ attr_reader :flags
9
+
10
+ def initialize(flags)
11
+ @flags = flags
12
+ end
13
+
14
+ def run
15
+ total_size = 0
16
+ summary = {}
17
+ count = 0
18
+ sumdep = @flags.depth
19
+
20
+ @flags.pathlist.each do |p|
21
+ curdep = 0
22
+ Find.find(p) do |path|
23
+ count += 1
24
+ next if FileTest.symlink?(path)
25
+ if FileTest.directory?(path)
26
+ next if p == path
27
+ ret = Weigh::Util.sum_dir(path,@flags.verbose)
28
+ dir_size = ret[:dir_size]
29
+ next if dir_size == 0
30
+ count += ret[:count]
31
+ total_size += dir_size
32
+ summary["#{path}/"] = dir_size
33
+ Find.prune
34
+ else
35
+ size = FileTest.size(path)
36
+ next if size == 0
37
+ total_size += size
38
+ summary["#{path}"] = size
39
+ end
40
+ end
41
+ end
42
+
43
+ Weigh::Util.report(summary,total_size)
44
+ end
45
+
46
+ end
47
+ end
data/lib/weigh/util.rb ADDED
@@ -0,0 +1,91 @@
1
+ module Weigh
2
+ module Util
3
+
4
+ def self.neat_size(bytes)
5
+ # return a human readble size from the bytes supplied
6
+ bytes = bytes.to_f
7
+ if bytes > 2 ** 40 # TiB: 1024 GiB
8
+ neat = sprintf("%.2f TiB", bytes / 2**40)
9
+ elsif bytes > 2 ** 30 # GiB: 1024 MiB
10
+ neat = sprintf("%.2f GiB", bytes / 2**30)
11
+ elsif bytes > 2 ** 20 # MiB: 1024 KiB
12
+ neat = sprintf("%.2f MiB", bytes / 2**20)
13
+ elsif bytes > 2 ** 10 # KiB: 1024 B
14
+ neat = sprintf("%.2f KiB", bytes / 2**10)
15
+ else # bytes
16
+ neat = sprintf("%.0f bytes", bytes)
17
+ end
18
+ neat
19
+ end
20
+
21
+ def self.sum_dir(dir,verbose=false)
22
+ # return the size of a given directory
23
+ #"Entering: #{dir}"
24
+ count=0
25
+ dir_size=0
26
+ data={}
27
+ Find.find(dir) do |path|
28
+ count += 1
29
+ next if FileTest.symlink?(path)
30
+ next if dir == path
31
+ if FileTest.directory?(path)
32
+ ret = sum_dir(path,verbose)
33
+ size = ret[:dir_size]
34
+ count += ret[:count]
35
+ dir_size += size
36
+ Find.prune
37
+ else
38
+ size = FileTest.size(path)
39
+ #puts "File: #{path} is #{size}"
40
+ puts "Found zero size file: #{path}" if verbose
41
+ dir_size += size
42
+ end
43
+ end
44
+ #puts "Exiting: #{dir} with #{dir_size}"
45
+ data[:dir_size] = dir_size
46
+ data[:count] = count
47
+ data
48
+ end
49
+
50
+ def self.report(summary,total_size)
51
+ summary.sort{|a,b| a[1]<=>b[1]}.each { |elem|
52
+ size = elem[1]
53
+ filename = elem[0]
54
+ puts sprintf("%15s %s\n", neat_size(size), filename)
55
+ }
56
+
57
+ puts sprintf("%16s %s\n", "---", "---")
58
+ puts sprintf("%15s %s\n", self.neat_size(total_size), ":total size")
59
+ puts sprintf("%16s %s\n", "---", "---")
60
+ end
61
+
62
+ def sum_dir(dir,verbose=false)
63
+ # return the size of a given directory
64
+ #"Entering: #{dir}"
65
+ count=0
66
+ dir_size=0
67
+ data={}
68
+ Find.find(dir) do |path|
69
+ count += 1
70
+ next if FileTest.symlink?(path)
71
+ next if dir == path
72
+ if FileTest.directory?(path)
73
+ ret = sum_dir(path,verbose)
74
+ size = ret[:dir_size]
75
+ count += ret[:count]
76
+ dir_size += size
77
+ Find.prune
78
+ else
79
+ size = FileTest.size(path)
80
+ #puts "File: #{path} is #{size}"
81
+ puts "Found zero size file: #{path}" if verbose
82
+ dir_size += size
83
+ end
84
+ end
85
+ #puts "Exiting: #{dir} with #{dir_size}"
86
+ data[:dir_size] = dir_size
87
+ data[:count] = count
88
+ data
89
+ end
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weigh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zach Leslie
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Useful utility to weigh the directory contents and sort the output by
15
+ size in a neatly formatted list.
16
+ email: xaque208@gmail.com
17
+ executables:
18
+ - weigh
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - bin/weigh
23
+ - lib/weigh/cli.rb
24
+ - lib/weigh/flags.rb
25
+ - lib/weigh/runner.rb
26
+ - lib/weigh/util.rb
27
+ - README.md
28
+ homepage: https://github.com/xaque208/weigh
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.23
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Weigh directory contents
52
+ test_files: []
53
+ has_rdoc: