dircat 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,6 +25,11 @@ query the contents of catalog
25
25
  Ex.: ruby bin/dircat query cat_dir1.yaml
26
26
  Ex.: ruby bin/dircat query cat_dir1.yaml duplicates
27
27
 
28
+ ### Rubies
29
+
30
+ see [gem-testers][http://gem-testers.org/gems/dircat/]
31
+ and contribute to the test :-)
32
+
28
33
  ### INSTALL:
29
34
 
30
35
  sudo gem install dircat
data/dircat.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |gem|
21
21
  #
22
22
  gem.add_runtime_dependency(%q<treevisitor>, ["= 0.2.2"])
23
23
  gem.add_runtime_dependency(%q<optparse-command>, ["= 0.1.6"])
24
+
24
25
  gem.add_development_dependency(%q<rake>, [">= 0"])
25
26
  gem.add_development_dependency(%q<yard>, [">= 0"])
26
27
  gem.add_development_dependency(%q<bundler>, [">= 0"])
data/lib/dircat/cat.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module DirCat
3
3
 
4
+ #
5
+ # Catalog of files (contained into directory :-))
6
+ #
4
7
  class Cat
5
8
 
6
9
  #
@@ -13,21 +16,35 @@ module DirCat
13
16
  #
14
17
  attr_accessor :ctime
15
18
 
16
-
19
+ #
20
+ # verbose level used to print message on $stdout
21
+ #
17
22
  attr_reader :verbose_level
18
23
 
24
+ # @param [Hash] options
25
+ # @option options [Number] :verbose list of ignore pattern
19
26
  def initialize(options = {})
20
- @verbose_level = options.delete(:verbose) || 0
21
- @dirname = ""
22
- @ctime = DateTime.now
23
- @entries = Array.new
27
+ @verbose_level = options.delete(:verbose) || 0
28
+ @dirname = ""
29
+ @ctime = DateTime.now
30
+ @entries = Array.new
24
31
  @md5_to_entries = Hash.new
25
32
  end
26
33
 
34
+ #
35
+ # Build catalog from a directory
36
+ # @param [String] dirname directory path
37
+ # @param options (see #initialize)
38
+ #
27
39
  def self.from_dir(dirname, options = {})
28
40
  new(options).from_dir(dirname)
29
41
  end
30
42
 
43
+ #
44
+ # Load catalog from a serialize file
45
+ # @param [String] filename
46
+ # @param options (see #initialize)
47
+ #
31
48
  def self.from_file(filename, options = {})
32
49
  new(options).from_file(filename)
33
50
  end
@@ -36,26 +53,28 @@ module DirCat
36
53
  new(options).load(file_or_dir)
37
54
  end
38
55
 
56
+ # Build a catalog from a directory
39
57
  def from_dir(dirname)
40
58
  if not File.directory?(dirname)
41
59
  raise "'#{dirname}' is not a directory or doesn't exists"
42
60
  end
43
61
  @dirname = File.expand_path dirname
44
- @ctime = DateTime.now
62
+ @ctime = DateTime.now
45
63
  _load_from_dir
46
64
  self
47
65
  end
48
66
 
67
+ # Load catalog from a file
49
68
  def from_file(filename)
50
69
  if not File.exist?(filename)
51
70
  raise DirCatException.new, "'#{filename}' not exists"
52
71
  end
53
- dircat_ser = YAML::load(File.open(filename))
54
- @dirname = dircat_ser.dirname
55
- @ctime = dircat_ser.ctime
56
- dircat_ser.entries.each { |entry_ser|
72
+ dircat_ser = File.open(filename) { |f| YAML::load(f) }
73
+ @dirname = dircat_ser.dirname
74
+ @ctime = dircat_ser.ctime
75
+ dircat_ser.entries.each do |entry_ser|
57
76
  add_entry(Entry.new.from_ser(entry_ser))
58
- }
77
+ end
59
78
  self
60
79
  end
61
80
 
@@ -69,45 +88,29 @@ module DirCat
69
88
  end
70
89
  end
71
90
 
91
+ # serialize catalog
92
+ # @return [DirCatSer] serialized catalog
72
93
  def to_ser
73
- dircat_ser = DirCatSer.new
94
+ dircat_ser = DirCatSer.new
74
95
  dircat_ser.version = 0.1
75
96
  dircat_ser.dirname = @dirname
76
- dircat_ser.ctime = @ctime
97
+ dircat_ser.ctime = @ctime
77
98
  dircat_ser.entries = []
78
- @entries.each { |entry|
99
+ @entries.each do |entry|
79
100
  dircat_ser.entries << entry.to_ser
80
- }
101
+ end
81
102
  dircat_ser
82
103
  end
83
104
 
84
- # def _load_from_dir
85
- # old_dirname = Dir.pwd
86
- # Dir.chdir(@dirname)
87
- # Dir["**/*"].each { |f|
88
- # next if File.directory?(f)
89
- #
90
- # if @verbose_level > 0
91
- # cr = "\r"
92
- # clear = "\e[K"
93
- # print "#{cr}#{filename}#{clear}"
94
- # end
95
- #
96
- # add_entry(Entry.new.from_filename(f))
97
- # }
98
- # if @verbose_level > 0
99
- # print "\n"
100
- # end
101
- # Dir.chdir(old_dirname)
102
- # self
103
- # end
104
-
105
- CR = "\r"
105
+ CR = "\r"
106
106
  CLEAR = "\e[K"
107
107
 
108
+ #
109
+ # @private
110
+ #
108
111
  def _load_from_dir
109
112
  me = self
110
- TreeVisitor::DirTreeWalker.new.run @dirname do
113
+ TreeVisitor::DirTreeWalker.new.run @dirname do
111
114
  on_leaf do |filename|
112
115
  me.add_entry(Entry.new.from_filename(filename))
113
116
  if me.verbose_level > 0
@@ -118,6 +121,9 @@ module DirCat
118
121
  self
119
122
  end
120
123
 
124
+ #
125
+ # Save serialized catalog to file
126
+ # @param [String,File] file
121
127
  def save_to(file)
122
128
  if file.kind_of?(String)
123
129
  begin
@@ -132,29 +138,44 @@ module DirCat
132
138
  end
133
139
  end
134
140
 
141
+ #
142
+ # number of entries (files)
143
+ # @return [Number]
135
144
  def size
136
145
  @entries.size
137
146
  end
138
147
 
148
+ #
149
+ # number of entries == 0
150
+ #
139
151
  def empty?
140
152
  @entries.size == 0
141
153
  end
142
154
 
155
+ #
156
+ # total size number of file cataloged
157
+ # @return [Number]
143
158
  def bytes
144
159
  @entries.inject(0) { |sum, entry| sum + entry.size }
145
160
  end
146
161
 
162
+ #
163
+ # simple report with essential information about this catalog
164
+ # @return [String] report
147
165
  def report
148
166
  dups = duplicates
149
- s = "Base dir: #{@dirname}\n"
150
- s += "Nr. file: #{size}"
151
- if duplicates.size > 0
167
+ s = "Base dir: #{@dirname}\n"
168
+ s += "Nr. file: #{size}"
169
+ if dups.size > 0
152
170
  s+= " (duplicates #{dups.size})"
153
171
  end
154
172
  s += "\nBytes: #{bytes.with_separator}"
155
173
  s
156
174
  end
157
175
 
176
+ #
177
+ # add entry to this catalog
178
+ # @private
158
179
  def add_entry(e)
159
180
  @entries.push(e)
160
181
  if @md5_to_entries.has_key?(e.md5)
@@ -168,31 +189,33 @@ module DirCat
168
189
  @md5_to_entries.has_key?(e.md5)
169
190
  end
170
191
 
171
- def -(s)
192
+ #
193
+ # return differences from this catalog and right catalog
194
+ # param [Cat] right
195
+ # @return [Cat]
196
+ def -(right)
172
197
  result = Cat.new
173
- @entries.each { |e|
174
- result.add_entry(e) unless s.contains(e)
175
- }
176
- result
177
- end
178
-
179
- def duplicates
180
- list = []
181
- @md5_to_entries.each_value do |ee|
182
- next if ee.size < 2
183
- list.push(ee)
198
+ @entries.each do |e|
199
+ result.add_entry(e) unless right.contains(e)
184
200
  end
185
- list
201
+ result
186
202
  end
187
203
 
204
+ #
205
+ # list of entries on stdout
206
+ # @return[String]
188
207
  def fmt_simple
189
- @entries.each { |e|
190
- print e.to_s
191
- }
208
+ @entries.inject('') { |s, e| s << e.to_s << "\n" }
192
209
  end
193
210
 
194
- def fmt_report
195
- DirCat::report(@entries, :md5, :name, :path)
211
+ alias :to_s :fmt_simple
212
+
213
+ #
214
+ # print a complex report on stdout
215
+ #
216
+ def fmt_report(*columns)
217
+ columns = [:md5, :name, :path, :size] unless columns
218
+ OptParseCommand::report(@entries, *columns)
196
219
  end
197
220
 
198
221
  def fmt_ruby(dst)
@@ -203,29 +226,45 @@ module DirCat
203
226
  }
204
227
  end
205
228
 
229
+ #
230
+ # @return [Array] entries representing duplicate files
231
+ #
232
+ def duplicates
233
+ list = []
234
+ @md5_to_entries.each_value do |ee|
235
+ next if ee.size < 2
236
+ list.push(ee)
237
+ end
238
+ list
239
+ end
240
+
206
241
  def list_dup
207
242
  r = ""
208
- duplicates.flatten.each { |e|
243
+ duplicates.flatten.each do |e|
209
244
  r += e.to_s + "\n"
210
- }
245
+ end
211
246
  r
212
247
  end
213
248
 
249
+ #
250
+ # return ruby script to eliminate duplicated
251
+ # @return [String] ruby script
252
+ #
214
253
  def script_dup
215
254
  r = "require 'fileutils'\n"
216
- duplicates.each { |entries|
255
+ duplicates.each do |entries|
217
256
  flg_first = true
218
- r += "\n"
219
- entries.each { |entry|
220
- src = File.join(@dirname, entry.path, entry.name);
257
+ r += "\n"
258
+ entries.each do |entry|
259
+ src = File.join(@dirname, entry.path, entry.name)
221
260
  if flg_first
222
261
  flg_first = false
223
- r += "# FileUtils.mv( \"#{src}\", \"#{Dir.tmpdir}\" )\n"
262
+ r += "# FileUtils.mv( \"#{src}\", \"#{Dir.tmpdir}\" )\n"
224
263
  else
225
264
  r += "FileUtils.mv( \"#{src}\", \"#{Dir.tmpdir}\" )\n"
226
265
  end
227
- }
228
- }
266
+ end
267
+ end
229
268
  r
230
269
  end
231
270
 
@@ -1,4 +1,4 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module DirCat
3
- VERSION="0.1.11"
3
+ VERSION="0.1.12"
4
4
  end
@@ -18,7 +18,7 @@ describe Cat do
18
18
  it "should build catalog from dir1" do
19
19
  cat1 = Cat.from_dir(File.join(@data_dir, "dir1"))
20
20
  cat1.size.should == 2
21
- cat1.bytes.size.should == 8
21
+ cat1.bytes.should == 4
22
22
  end
23
23
 
24
24
  it "should load catalog from dir2" do
@@ -44,7 +44,6 @@ describe Cat do
44
44
  cat_diff.size.should == 1
45
45
  end
46
46
 
47
-
48
47
  it "should detect duplicates" do
49
48
  cat1 = Cat.from_dir(File.join(@data_dir, "dir3"))
50
49
  cat1.duplicates.should have(1).files
@@ -56,7 +55,7 @@ describe Cat do
56
55
  end
57
56
 
58
57
  after do
59
- FileUtils.rm(@tmp_file) if File.exist? @tmp_file
58
+ FileUtils.rm(@tmp_file) if File.exist? @tmp_file
60
59
  end
61
60
 
62
61
  it "saving to a file" do
@@ -74,4 +73,23 @@ describe Cat do
74
73
  lambda { cat1.save_to(not_existent_file) }.should raise_exception(DirCatException)
75
74
  end
76
75
  end
76
+
77
+ it "should print a report" do
78
+ cat1 = Cat.from_dir(File.join(@data_dir, "dir1"))
79
+ cat1.size.should == 2
80
+ cat1.bytes.should == 4
81
+
82
+ out = capture_out { cat1.fmt_report(:md5, :name, :size) }.out
83
+
84
+ str =<<-EOS
85
+ +----------------------------------+-----------+------+
86
+ | md5 | name | size |
87
+ +----------------------------------+-----------+------+
88
+ | 60b725f10c9c85c70d97880dfe8191b3 | file1.txt | 2 |
89
+ | 4124bc0a9335c27f086f24ba207a4912 | file3.txt | 2 |
90
+ +----------------------------------+-----------+------+
91
+ 2 rows in set
92
+ EOS
93
+ out.should == str
94
+ end
77
95
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: dircat
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.11
5
+ version: 0.1.12
6
6
  platform: ruby
7
7
  authors:
8
8
  - Tokiro
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-23 00:00:00 +01:00
13
+ date: 2011-02-25 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency