weigh 0.0.3 → 0.1.0
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 +2 -0
- data/bin/weigh +9 -2
- data/lib/weigh/cli.rb +32 -32
- data/lib/weigh/run.rb +86 -0
- data/lib/weigh/util.rb +48 -48
- data/lib/weigh/version.rb +1 -5
- metadata +4 -4
- data/lib/weigh/flags.rb +0 -48
- data/lib/weigh/runner.rb +0 -47
data/README.md
CHANGED
data/bin/weigh
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
#! /usr/bin/env ruby
|
2
|
-
|
2
|
+
|
3
|
+
# Summarize the size of directories and rank the totals.
|
3
4
|
|
4
5
|
require 'weigh/cli'
|
6
|
+
require 'colored'
|
5
7
|
|
6
|
-
|
8
|
+
begin
|
9
|
+
Weigh::CLI.command.run ARGV
|
10
|
+
rescue Interrupt
|
11
|
+
$stderr.puts "Terminating!".red
|
12
|
+
exit 1
|
13
|
+
end
|
data/lib/weigh/cli.rb
CHANGED
@@ -1,44 +1,44 @@
|
|
1
|
-
require 'weigh/
|
2
|
-
require 'weigh/
|
1
|
+
require 'weigh/run'
|
2
|
+
require 'weigh/util'
|
3
|
+
require 'cri'
|
3
4
|
|
4
|
-
module Weigh
|
5
|
-
class CLI
|
5
|
+
module Weigh::CLI
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
def self.command
|
8
|
+
@cmd ||= Cri::Command.define do
|
9
|
+
name 'weigh'
|
10
|
+
usage 'weigh [options] <path1> <path2>'
|
11
|
+
summary 'Summarize the size of directories and files'
|
9
12
|
|
10
|
-
|
11
|
-
@flags = flags
|
12
|
-
@runner = Weigh::Runner.new(@flags)
|
13
|
-
end
|
13
|
+
w = Weigh::Run.new
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
exit
|
15
|
+
flag :h, :help, 'show help for this command' do |value, cmd|
|
16
|
+
puts cmd.help
|
17
|
+
exit 0
|
19
18
|
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
exit 0
|
27
|
-
end
|
28
|
-
|
29
|
-
Signal.trap("TERM") do
|
30
|
-
shutdown()
|
31
|
-
end
|
20
|
+
flag :V, :version, 'show help for this command' do |value, cmd|
|
21
|
+
require 'weigh/version'
|
22
|
+
puts "Weigh.rb version " + Weigh::VERSION
|
23
|
+
exit 0
|
24
|
+
end
|
32
25
|
|
33
|
-
|
34
|
-
|
35
|
-
|
26
|
+
flag :v, :verbose, 'speak up' do |value, cmd|
|
27
|
+
w.verbose = true
|
28
|
+
end
|
36
29
|
|
37
|
-
|
38
|
-
|
30
|
+
flag :d, :depth, 'Sumarize deeper than depth' do |value, cmd|
|
31
|
+
#w.depth = value
|
32
|
+
end
|
39
33
|
|
40
|
-
|
34
|
+
run do |opts, args, cmd|
|
35
|
+
if args.size > 0
|
36
|
+
w.pathlist = args
|
37
|
+
end
|
38
|
+
w.run
|
39
|
+
w.report
|
40
|
+
exit 0
|
41
|
+
end
|
41
42
|
end
|
42
|
-
|
43
43
|
end
|
44
44
|
end
|
data/lib/weigh/run.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'weigh/util'
|
2
|
+
require 'find'
|
3
|
+
|
4
|
+
module Weigh
|
5
|
+
class Run
|
6
|
+
|
7
|
+
attr_accessor :verbose, :pathlist
|
8
|
+
|
9
|
+
def initialize(pathlist=['.'])
|
10
|
+
@pathlist = pathlist
|
11
|
+
|
12
|
+
@data = {
|
13
|
+
:total_size => 0,
|
14
|
+
:summary => {},
|
15
|
+
:count => 0
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def verbose=(value)
|
20
|
+
@verbose = value
|
21
|
+
end
|
22
|
+
|
23
|
+
def verbose
|
24
|
+
@verbose
|
25
|
+
end
|
26
|
+
|
27
|
+
def pathlist=(value)
|
28
|
+
@pathlist = value
|
29
|
+
end
|
30
|
+
|
31
|
+
def pathlist
|
32
|
+
@pathlist
|
33
|
+
end
|
34
|
+
|
35
|
+
def run
|
36
|
+
# Dig into each path given
|
37
|
+
@pathlist.each do |p|
|
38
|
+
Find.find(p) do |path|
|
39
|
+
@data[:count] += 1
|
40
|
+
|
41
|
+
# Skip symlinks
|
42
|
+
next if FileTest.symlink?(path)
|
43
|
+
|
44
|
+
if FileTest.directory?(path)
|
45
|
+
|
46
|
+
# Skip the path that we are already on
|
47
|
+
next if p == path
|
48
|
+
|
49
|
+
# Summarize the directory data
|
50
|
+
ret = Weigh::Util.sum_dir(path,@verbose)
|
51
|
+
dir_size = ret[:dir_size]
|
52
|
+
|
53
|
+
# Skip empty directories
|
54
|
+
next if dir_size == 0
|
55
|
+
|
56
|
+
# Record the data from the directory
|
57
|
+
@data[:count] += ret[:count]
|
58
|
+
@data[:total_size] += dir_size
|
59
|
+
|
60
|
+
# Add a trailing slash to the key for directories
|
61
|
+
pathname = path + "/"
|
62
|
+
|
63
|
+
@data[:summary][pathname] = dir_size
|
64
|
+
|
65
|
+
Find.prune
|
66
|
+
else
|
67
|
+
# Store the size of the current file
|
68
|
+
size = FileTest.size(path)
|
69
|
+
|
70
|
+
# Don't count zero sized files
|
71
|
+
next if size == 0
|
72
|
+
|
73
|
+
@data[:total_size] += size
|
74
|
+
@data[:summary][path] = size
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
@data
|
80
|
+
end
|
81
|
+
|
82
|
+
def report
|
83
|
+
Weigh::Util.report @data
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/lib/weigh/util.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
module Weigh
|
2
2
|
module Util
|
3
3
|
|
4
|
+
# Convert a byte count into something a bit more human friendly
|
5
|
+
#
|
6
|
+
# @param [Int] a byte count
|
7
|
+
#
|
8
|
+
# @return [String] human readable byte count
|
4
9
|
def self.neat_size(bytes)
|
5
10
|
# return a human readble size from the bytes supplied
|
6
11
|
bytes = bytes.to_f
|
@@ -20,73 +25,68 @@ module Weigh
|
|
20
25
|
neat
|
21
26
|
end
|
22
27
|
|
28
|
+
# Sumarize the size of the given directory
|
29
|
+
#
|
30
|
+
# @param [String] full path to directory
|
31
|
+
# @param [Boolean] increase verbosity
|
32
|
+
#
|
33
|
+
# @return Hash {:dir_size => Hash, :count => Int}
|
23
34
|
def self.sum_dir(dir,verbose=false)
|
24
35
|
# return the size of a given directory
|
25
36
|
#"Entering: #{dir}"
|
26
|
-
count=0
|
27
|
-
dir_size=0
|
28
|
-
data={}
|
37
|
+
count = 0
|
38
|
+
dir_size = 0
|
39
|
+
data = {}
|
40
|
+
|
29
41
|
Find.find(dir) do |path|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
42
|
+
begin
|
43
|
+
puts path if verbose
|
44
|
+
count += 1
|
45
|
+
if FileTest.symlink?(path)
|
46
|
+
puts "skipping symlink " + path if verbose
|
47
|
+
next
|
48
|
+
end
|
49
|
+
if dir == path and File.directory?(path)
|
50
|
+
puts "skipping current directory " + path if verbose
|
51
|
+
next
|
52
|
+
end
|
53
|
+
if FileTest.directory?(path)
|
54
|
+
ret = sum_dir(path,verbose)
|
55
|
+
size = ret[:dir_size]
|
56
|
+
count += ret[:count]
|
57
|
+
dir_size += size
|
58
|
+
Find.prune
|
59
|
+
else
|
60
|
+
size = FileTest.size(path)
|
61
|
+
#puts "File: #{path} is #{size}"
|
62
|
+
puts "Found zero size file: #{path}" if verbose
|
63
|
+
dir_size += size
|
64
|
+
end
|
65
|
+
rescue IOError
|
66
|
+
puts "file vanished: " + path
|
44
67
|
end
|
45
68
|
end
|
46
|
-
#puts "Exiting: #{dir} with #{dir_size}"
|
47
69
|
data[:dir_size] = dir_size
|
48
70
|
data[:count] = count
|
49
71
|
data
|
50
72
|
end
|
51
73
|
|
52
|
-
|
53
|
-
|
74
|
+
# Print a summary of the received data
|
75
|
+
#
|
76
|
+
# @param [Hash]
|
77
|
+
#
|
78
|
+
# @returns [Hash] the unmodified data
|
79
|
+
def self.report(data)
|
80
|
+
data[:summary].sort{|a,b| a[1]<=>b[1]}.each { |elem|
|
54
81
|
size = elem[1]
|
55
82
|
filename = elem[0]
|
56
83
|
puts sprintf("%15s %s\n", neat_size(size), filename)
|
57
84
|
}
|
58
85
|
|
59
86
|
puts sprintf("%16s %s\n", "---", "---")
|
60
|
-
puts sprintf("%15s %s\n", self.neat_size(total_size), ":total size")
|
87
|
+
puts sprintf("%15s %s\n", self.neat_size(data[:total_size]), ":total size")
|
61
88
|
puts sprintf("%16s %s\n", "---", "---")
|
62
|
-
end
|
63
89
|
|
64
|
-
def sum_dir(dir,verbose=false)
|
65
|
-
# return the size of a given directory
|
66
|
-
#"Entering: #{dir}"
|
67
|
-
count=0
|
68
|
-
dir_size=0
|
69
|
-
data={}
|
70
|
-
Find.find(dir) do |path|
|
71
|
-
count += 1
|
72
|
-
next if FileTest.symlink?(path)
|
73
|
-
next if dir == path
|
74
|
-
if FileTest.directory?(path)
|
75
|
-
ret = sum_dir(path,verbose)
|
76
|
-
size = ret[:dir_size]
|
77
|
-
count += ret[:count]
|
78
|
-
dir_size += size
|
79
|
-
Find.prune
|
80
|
-
else
|
81
|
-
size = FileTest.size(path)
|
82
|
-
#puts "File: #{path} is #{size}"
|
83
|
-
puts "Found zero size file: #{path}" if verbose
|
84
|
-
dir_size += size
|
85
|
-
end
|
86
|
-
end
|
87
|
-
#puts "Exiting: #{dir} with #{dir_size}"
|
88
|
-
data[:dir_size] = dir_size
|
89
|
-
data[:count] = count
|
90
90
|
data
|
91
91
|
end
|
92
92
|
end
|
data/lib/weigh/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weigh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-05-12 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Useful utility to weigh the directory contents and sort the output by
|
15
15
|
size in a neatly formatted list.
|
@@ -21,8 +21,7 @@ extra_rdoc_files: []
|
|
21
21
|
files:
|
22
22
|
- bin/weigh
|
23
23
|
- lib/weigh/cli.rb
|
24
|
-
- lib/weigh/
|
25
|
-
- lib/weigh/runner.rb
|
24
|
+
- lib/weigh/run.rb
|
26
25
|
- lib/weigh/util.rb
|
27
26
|
- lib/weigh/version.rb
|
28
27
|
- README.md
|
@@ -51,3 +50,4 @@ signing_key:
|
|
51
50
|
specification_version: 3
|
52
51
|
summary: Weigh directory contents
|
53
52
|
test_files: []
|
53
|
+
has_rdoc:
|
data/lib/weigh/flags.rb
DELETED
@@ -1,48 +0,0 @@
|
|
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
|
data/lib/weigh/runner.rb
DELETED
@@ -1,47 +0,0 @@
|
|
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
|