tree.rb 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/tree_rb.rb CHANGED
@@ -37,12 +37,20 @@ require 'tree_rb/directory_walker'
37
37
  #
38
38
  # visitors
39
39
  #
40
- require 'tree_rb/visitors/block_tree_node_visitor'
41
- require 'tree_rb/visitors/build_dir_tree_visitor'
42
- require 'tree_rb/visitors/callback_tree_node_visitor2'
43
- require 'tree_rb/visitors/clone_tree_node_visitor'
44
- require 'tree_rb/visitors/depth_tree_node_visitor'
45
- require 'tree_rb/visitors/print_dir_tree_visitor'
46
- require 'tree_rb/visitors/directory_to_hash_visitor'
40
+ #require 'tree_rb/visitors/block_tree_node_visitor'
41
+ #require 'tree_rb/visitors/build_dir_tree_visitor'
42
+ #require 'tree_rb/visitors/callback_tree_node_visitor2'
43
+ #require 'tree_rb/visitors/clone_tree_node_visitor'
44
+ #require 'tree_rb/visitors/depth_tree_node_visitor'
45
+ #require 'tree_rb/visitors/print_dir_tree_visitor'
46
+ #require 'tree_rb/visitors/directory_to_hash_visitor'
47
+ #require 'tree_rb/visitors/sqlite_dir_tree_visitor'
48
+
49
+ visitors_dir = File.join(File.dirname(__FILE__), "tree_rb", "visitors")
50
+ unless Dir.exist? visitors_dir
51
+ raise "cannot found directory '#{visitors_dir}'"
52
+ end
53
+ Dir[ File.join(visitors_dir, "*.rb") ].each { |f|require f }
54
+
47
55
 
48
56
  require 'tree_rb/util/dir_processor'
@@ -116,13 +116,15 @@ module TreeRb
116
116
  options[:only_files] = true
117
117
  options[:show_md5sum] = true
118
118
  options[:show_indentation] = false
119
+ options[:show_report] = false
119
120
  end
120
121
 
121
122
  options[:show_sha1sum] = false
122
- parser.on("--md5sum", "show ake md5sum implies -i and --only-files") do
123
+ parser.on("--sha1sum", "show ake sha1sum output implies -i and --only-files") do
123
124
  options[:only_files] = true
124
125
  options[:show_sha1sum] = true
125
126
  options[:show_indentation] = false
127
+ options[:show_report] = false
126
128
  end
127
129
 
128
130
  parser.on("-o [FILE]", "--output [FILE]", String) do |v|
@@ -138,6 +140,11 @@ module TreeRb
138
140
  options[:force_overwrite_output] = true
139
141
  end
140
142
 
143
+ options[:show_report] = true
144
+ parser.on("--noreport", "Omits printing of the file and directory report at the end of the tree listing.") do
145
+ options[:show_report] = false
146
+ end
147
+
141
148
  parser
142
149
  end
143
150
 
@@ -210,8 +217,11 @@ module TreeRb
210
217
  dtw.run(visitor)
211
218
 
212
219
  output.puts visitor.root.to_str('', options)
213
- output.puts
214
- output.puts "#{visitor.nr_directories} directories, #{visitor.nr_files} files"
220
+
221
+ if options[:show_report]
222
+ output.puts
223
+ output.puts "#{visitor.nr_directories} directories, #{visitor.nr_files} files"
224
+ end
215
225
 
216
226
  when 'print-dir'
217
227
  visitor = PrintDirTreeVisitor.new
@@ -235,45 +245,11 @@ module TreeRb
235
245
  else
236
246
  output.close
237
247
  filename = options[:output]
238
-
239
- db = SQLite3::Database.new(filename)
240
- db.execute("create table files(digest varchar(40),path varchar(1024))")
241
-
242
- start = Time.now
243
- me = self
244
- bytes = 0
245
- dtw.run do
246
- on_leaf do |filename|
247
-
248
- puts filename
249
- digest = SHA1.file(filename).hexdigest
250
- db.execute("insert into files values(\"#{digest}\",\"#{filename}\")")
251
-
252
- # entry = Entry.from_filename(filename)
253
- # me.add_entry(entry)
254
- # bytes += entry.size
255
- # if me.verbose_level > 0
256
- # print "#{CR}#{filename}#{CLEAR}"
257
- # end
258
- # if me.show_progress
259
- # sec = Time.now - start
260
- # print "#{CR}bytes: #{bytes.to_human} time: #{sec} bytes/sec #{bytes/sec} #{CLEAR}"
261
- # end
262
- end
263
- end
264
-
265
- # Loop through digests.
266
- db.execute("select digest,count(1) as count from files group by digest order by count desc").each do |row|
267
- if row[1] > 1 # Skip unique files.
268
- puts "Duplicates found:"
269
- digest = row[0]
270
- # List the duplicate files.
271
- db.execute("select digest,path from files where digest='#{digest}'").each do |dup_row|
272
- puts "[#{digest}] #{dup_row[1]}"
273
- end
274
- end
275
- end
276
-
248
+ visitor = SqliteDirTreeVisitor.new(filename)
249
+ #start = Time.now
250
+ #me = self
251
+ #bytes = 0
252
+ dtw.run(visitor)
277
253
  end
278
254
 
279
255
  rescue LoadError
@@ -1,4 +1,4 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module TreeRb
3
- VERSION="0.3.5"
3
+ VERSION="0.3.6"
4
4
  end
@@ -35,7 +35,11 @@ module TreeRb
35
35
  end
36
36
 
37
37
  if options[:show_md5sum]
38
- @str = "#{MD5.file(pathname).hexdigest} #{file_name}"
38
+ @str = "#{MD5.file(pathname).hexdigest} #{file_name}"
39
+ return
40
+ end
41
+ if options[:show_sha1sum]
42
+ @str = "#{SHA1.file(pathname).hexdigest} #{file_name}"
39
43
  return
40
44
  end
41
45
 
@@ -0,0 +1,48 @@
1
+ # -*- coding: utf-8 -*-
2
+ module TreeRb
3
+
4
+ class SqliteDirTreeVisitor < BasicTreeNodeVisitor
5
+
6
+ def initialize(filename)
7
+ @db = SQLite3::Database.new(filename)
8
+ @db.execute("create table files(path varchar(1024), size integer, digest varchar(40))")
9
+ end
10
+
11
+ def visit_leaf(pathname)
12
+ puts pathname
13
+ stat = File.lstat(pathname)
14
+ digest = SHA1.file(pathname).hexdigest
15
+ @db.execute("insert into files (path, size, digest) values ( \"#{pathname}\", \"#{stat.size}\", \"#{digest}\")")
16
+
17
+ # entry = Entry.from_filename(filename)
18
+ # me.add_entry(entry)
19
+ # bytes += entry.size
20
+ # if me.verbose_level > 0
21
+ # print "#{CR}#{filename}#{CLEAR}"
22
+ # end
23
+ # if me.show_progress
24
+ # sec = Time.now - start
25
+ # print "#{CR}bytes: #{bytes.to_human} time: #{sec} bytes/sec #{bytes/sec} #{CLEAR}"
26
+ # end
27
+ #content = ContentFile.new(pathname, @options)
28
+ # connect the leaf_node created to the last tree_node on the stack
29
+ #@nr_files += 1
30
+ #LeafNode.new(content, @stack.last)
31
+ end
32
+
33
+ def find_duplicates
34
+ # Loop through digests.
35
+ @db.execute("select digest,count(1) as count from files group by digest order by count desc").each do |row|
36
+ if row[1] > 1 # Skip unique files.
37
+ puts "Duplicates found:"
38
+ digest = row[0]
39
+ # List the duplicate files.
40
+ db.execute("select digest,path from files where digest='#{digest}'").each do |dup_row|
41
+ puts "[#{digest}] #{dup_row[1]}"
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ end
48
+ end # end module TreeVisitor
@@ -0,0 +1 @@
1
+ dummy.txt
@@ -0,0 +1 @@
1
+ file.1.2.1
@@ -0,0 +1 @@
1
+ file.1.1
@@ -0,0 +1 @@
1
+ file.2.1
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,11 @@
3
3
  # std lib
4
4
  #
5
5
  require "stringio"
6
+ require 'ostruct'
6
7
 
8
+ #
9
+ # gem
10
+ #
7
11
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
12
  require 'tree_rb'
9
13
  require 'tree_rb_cli'
@@ -11,7 +15,7 @@ include TreeRb
11
15
 
12
16
  FIXTURES = File.expand_path( File.join( File.dirname(__FILE__), "fixtures" ) )
13
17
 
14
- def with_output_captured
18
+ def capture_output
15
19
  old_stdout, old_stderr = $stdout, $stderr
16
20
  out = StringIO.new
17
21
  err = StringIO.new
@@ -23,5 +27,5 @@ def with_output_captured
23
27
  $stdout = old_stdout
24
28
  $stderr = old_stderr
25
29
  end
26
- { :stdout => out.string, :stderr => err.string }
30
+ OpenStruct.new(:out => out.string, :err => err.string)
27
31
  end
@@ -0,0 +1,36 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
+
4
+ describe CliTree do
5
+
6
+ it "should accepts --md5sum switch" do
7
+ captured = capture_output do
8
+ args = %w{--md5sum}
9
+ args << File.join(FIXTURES, "test_dir_1")
10
+ CliTree.new.parse_args(args)
11
+ end
12
+
13
+ certified_out=<<-EOS
14
+ a4788ba25d42f21003db11ec53112f1e file.1.1
15
+ afb96f2652f4ba3cd84f7be0ae6fffe3 file.1.2.1
16
+ c4c0c530b842efe4038ac4a659bfbe77 file.2.1
17
+ EOS
18
+ captured.out.should == certified_out
19
+ end
20
+
21
+ it "should accepts --sha1sum switch" do
22
+ captured = capture_output do
23
+ args = %w{--sha1sum}
24
+ args << File.join(FIXTURES, "test_dir_1")
25
+ CliTree.new.parse_args(args)
26
+ end
27
+
28
+ certified_out=<<-EOS
29
+ 9140d4031d95f459eb0bee8160509e5d83271fe8 file.1.1
30
+ 4b7f45a8c0be069e7eac5f7d64b997c92c740656 file.1.2.1
31
+ 3c58aeb1552b0318a86279b52c918f2fb953b4b9 file.2.1
32
+ EOS
33
+ captured.out.should == certified_out
34
+ end
35
+
36
+ end
@@ -0,0 +1,73 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
+
4
+ describe CliTree do
5
+
6
+ it "should accept --help switch" do
7
+ captured = capture_output do
8
+ args = %w{--help}
9
+ CliTree.new.parse_args(args)
10
+ end
11
+ captured.out.should match /Usage:/
12
+ end
13
+
14
+ it "should accept --version switch" do
15
+ captured = capture_output do
16
+ args = %w{--version}
17
+ CliTree.new.parse_args(args)
18
+ end
19
+ version = TreeRb::VERSION
20
+ captured.out.should match version
21
+ end
22
+
23
+ it "should accepts -d switch (directories only)" do
24
+ captured = capture_output do
25
+ args = %w{-d}
26
+ args << File.join(FIXTURES, "test_dir_1")
27
+ CliTree.new.parse_args(args)
28
+ end
29
+ # puts captured
30
+ captured.out.split("\n").length.should == 6
31
+ end
32
+
33
+ it "should accepts -da switch (directories only)" do
34
+ captured = capture_output do
35
+ args = %w{-da}
36
+ args << File.join(FIXTURES, "test_dir_1")
37
+ CliTree.new.parse_args(args)
38
+ end
39
+ # puts captured
40
+ captured.out.split("\n").length.should == 7
41
+ end
42
+
43
+ it "should accepts -a switch (all files)" do
44
+ captured = capture_output do
45
+ args = %w{-a}
46
+ args << File.join(FIXTURES, "test_dir_1")
47
+ CliTree.new.parse_args(args)
48
+ end
49
+ # pp captured
50
+ captured.out.split("\n").length.should == 11
51
+ end
52
+
53
+ it "should accepts -a switch (all files)" do
54
+ captured = capture_output do
55
+ args = []
56
+ args << File.join(FIXTURES, "test_dir_1")
57
+ CliTree.new.parse_args(args)
58
+ end
59
+ # puts captured
60
+ captured.out.split("\n").length.should == 9
61
+ end
62
+
63
+ it "should show tree with inaccessible directories" do
64
+ captured = capture_output do
65
+ args = []
66
+ args << File.join(FIXTURES, "test_dir_3_with_error")
67
+ CliTree.new.parse_args(args)
68
+ end
69
+ #puts captured
70
+ captured.err.should_not be_empty
71
+ captured.out.split("\n").length.should == 4
72
+ end
73
+ end
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
+
4
+ describe CliTree do
5
+
6
+ it "should accepts '--format sqlite -o test.db' switches" do
7
+ db_filename = File.join(FIXTURES, 'tmp', 'test.db')
8
+
9
+ File.unlink(db_filename) if File.exist?(db_filename)
10
+
11
+ captured = capture_output do
12
+ args = %w{--format sqlite -o } << db_filename
13
+ args << File.join(FIXTURES, "test_dir_1")
14
+ CliTree.new.parse_args(args)
15
+ end
16
+
17
+ File.exist?(db_filename).should be_true
18
+
19
+ File.unlink(db_filename) if File.exist?(db_filename)
20
+ end
21
+
22
+ end
@@ -3,10 +3,12 @@ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
3
 
4
4
  describe DirProcessor do
5
5
 
6
- it do
6
+ it "should capture all files" do
7
7
  files = []
8
8
  dp = DirProcessor.new { |f| files << f }
9
9
  dp.process(FIXTURES)
10
+
11
+ # puts files
10
12
  files.length.should == 3
11
13
  end
12
14
 
data/tree.rb.gemspec CHANGED
@@ -48,7 +48,7 @@ An example of DSL to build tree:
48
48
 
49
49
  gem.add_runtime_dependency(%q<json>, [">= 0"])
50
50
  gem.add_runtime_dependency(%q<ansi>, [">= 0"])
51
- gem.add_runtime_dependency(%q<sqlite3-ruby>, [">= 0"])
51
+ gem.add_runtime_dependency(%q<sqlite3>, [">= 0"])
52
52
 
53
53
  gem.add_development_dependency(%q<rake>, [">= 0"])
54
54
  gem.add_development_dependency(%q<yard>, [">= 0"])
@@ -58,7 +58,6 @@ An example of DSL to build tree:
58
58
  #
59
59
  # files
60
60
  #
61
- # s.files = `git ls-files`.split("\n")
62
61
  # gem.files = `git ls-files`.split($\)
63
62
  gem.files = %w{LICENSE.txt README.md Rakefile tree.rb.gemspec .gemtest}
64
63
  gem.files.concat Dir['ext/**/*.rb']
@@ -69,10 +68,9 @@ An example of DSL to build tree:
69
68
  #
70
69
  # bin
71
70
  #
72
- # s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
71
+ # gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
73
72
  # gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
74
- # Dir["bin/*"].map(&File.method(:basename))
75
- gem.executables = %w{ tree.rb rtree tree_rb }
73
+ gem.executables = Dir["bin/*"].map(&File.method(:basename))
76
74
 
77
75
 
78
76
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tree.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -44,7 +44,7 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  - !ruby/object:Gem::Dependency
47
- name: sqlite3-ruby
47
+ name: sqlite3
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
@@ -131,8 +131,8 @@ description: ! "(This gem was named as treevisitor)\ntree.rb is a 'clone' of tre
131
131
  \"l3\"\n end\n node \"wo leaves\"\n end\n</pre>\n"
132
132
  email: tokiro.oyama@gmail.com
133
133
  executables:
134
- - tree.rb
135
134
  - rtree
135
+ - tree.rb
136
136
  - tree_rb
137
137
  extensions: []
138
138
  extra_rdoc_files: []
@@ -153,6 +153,7 @@ files:
153
153
  - lib/tree_rb/visitors/depth_tree_node_visitor.rb
154
154
  - lib/tree_rb/visitors/print_tree_node_visitor.rb
155
155
  - lib/tree_rb/visitors/callback_tree_node_visitor2.rb
156
+ - lib/tree_rb/visitors/sqlite_dir_tree_visitor.rb
156
157
  - lib/tree_rb/visitors/directory_to_hash_visitor.rb
157
158
  - lib/tree_rb/visitors/flat_print_tree_node_visitors.rb
158
159
  - lib/tree_rb/visitors/block_tree_node_visitor.rb
@@ -179,7 +180,9 @@ files:
179
180
  - examples/protovis/treevisitor.js
180
181
  - examples/protovis/directory_to_json_visitor.rb
181
182
  - spec/spec_helper.rb
182
- - spec/tree_rb/cli/cli_tree_spec.rb
183
+ - spec/tree_rb/cli/cli_tree_sqlite_spec.rb
184
+ - spec/tree_rb/cli/cli_tree_digest_spec.rb
185
+ - spec/tree_rb/cli/cli_tree_generic_spec.rb
183
186
  - spec/tree_rb/util/dir_processor_spec.rb
184
187
  - spec/tree_rb/visitors/depth_tree_node_visitor_spec.rb
185
188
  - spec/tree_rb/visitors/callback_tree_node_visitor2_spec.rb
@@ -200,8 +203,8 @@ files:
200
203
  - spec/fixtures/test_dir_1/dir.2/file.2.1
201
204
  - spec/fixtures/test_dir_2/[Dsube]/sub/.gitkeep
202
205
  - spec/fixtures/test_dir_1/.dir_with_dot/dummy.txt
203
- - bin/tree.rb
204
206
  - bin/rtree
207
+ - bin/tree.rb
205
208
  - bin/tree_rb
206
209
  homepage: http://github.com/tokiro/tree.rb
207
210
  licenses: []
@@ -230,7 +233,9 @@ summary: tree.rb is a 'clone' of tree unix command. The gem implements a library
230
233
  mange tree structures.
231
234
  test_files:
232
235
  - spec/spec_helper.rb
233
- - spec/tree_rb/cli/cli_tree_spec.rb
236
+ - spec/tree_rb/cli/cli_tree_sqlite_spec.rb
237
+ - spec/tree_rb/cli/cli_tree_digest_spec.rb
238
+ - spec/tree_rb/cli/cli_tree_generic_spec.rb
234
239
  - spec/tree_rb/util/dir_processor_spec.rb
235
240
  - spec/tree_rb/visitors/depth_tree_node_visitor_spec.rb
236
241
  - spec/tree_rb/visitors/callback_tree_node_visitor2_spec.rb
@@ -1,69 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
-
4
- describe CliTree do
5
-
6
- it "help message" do
7
- out = with_output_captured do
8
- args = %w{--help}
9
- CliTree.new.parse_args(args)
10
- end
11
- out[:stdout].should match /Usage:/
12
- end
13
-
14
- it "version" do
15
- out = with_output_captured do
16
- args = %w{--version}
17
- CliTree.new.parse_args(args)
18
- end
19
- version = TreeRb::VERSION
20
- out[:stdout].should match version
21
- end
22
-
23
- it "should accepts switch -d (directories only)" do
24
- out = with_output_captured do
25
- args = %w{-d}
26
- args << File.join(FIXTURES, "test_dir_1")
27
- CliTree.new.parse_args(args)
28
- end
29
- # puts out
30
- out[:stdout].split("\n").length.should == 6
31
-
32
- out = with_output_captured do
33
- args = %w{-da}
34
- args << File.join(FIXTURES, "test_dir_1")
35
- CliTree.new.parse_args(args)
36
- end
37
- # puts out
38
- out[:stdout].split("\n").length.should == 7
39
- end
40
-
41
- it "should accepts switch -a (all files)" do
42
- out = with_output_captured do
43
- args = %w{-a}
44
- args << File.join(FIXTURES, "test_dir_1")
45
- CliTree.new.parse_args(args)
46
- end
47
- # pp out
48
- out[:stdout].split("\n").length.should == 11
49
-
50
- out = with_output_captured do
51
- args = []
52
- args << File.join(FIXTURES, "test_dir_1")
53
- CliTree.new.parse_args(args)
54
- end
55
- # puts out
56
- out[:stdout].split("\n").length.should == 9
57
- end
58
-
59
- it "should show tree with inaccessible directories" do
60
- out = with_output_captured do
61
- args = []
62
- args << File.join(FIXTURES, "test_dir_3_with_error")
63
- CliTree.new.parse_args(args)
64
- end
65
- puts out
66
- out[:stderr].should_not be_empty
67
- out[:stdout].split("\n").length.should == 4
68
- end
69
- end