Class: DirCat::CatOnYaml

Inherits:
Object
  • Object
show all
Defined in:
lib/dircat/cat_on_yaml/cat_on_yaml.rb

Overview

Catalog of files (contained into directory :-))

Constant Summary collapse

CR =
"\r"
CLEAR =
"\e[K"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CatOnYaml

Returns a new instance of CatOnYaml.

Parameters:

  • options (Hash) (defaults to: {})

    @option options [Number] :verbose list of ignore pattern



27
28
29
30
31
32
33
34
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 27

def initialize(options = {})
  @verbose_level = options.delete(:verbose) || 0
  @show_progress = options.delete(:show_progress)
  @dirname = ""
  @ctime = DateTime.now
  @entries = Array.new
  @md5_to_entries = Hash.new
end

Instance Attribute Details

#ctimeObject

creation date



17
18
19
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 17

def ctime
  @ctime
end

#dirnameObject (readonly)

Directory name



12
13
14
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 12

def dirname
  @dirname
end

#show_progressObject (readonly)

Returns the value of attribute show_progress.



23
24
25
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 23

def show_progress
  @show_progress
end

#verbose_levelObject (readonly)

verbose level used to print message on $stdout



22
23
24
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 22

def verbose_level
  @verbose_level
end

Class Method Details

.from_dir(dirname, options = {}) ⇒ Object

Build catalog from a directory

Parameters:

  • dirname (String)

    directory path

  • options (Hash) (defaults to: {})

    @option options [Number] :verbose list of ignore pattern



41
42
43
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 41

def self.from_dir(dirname, options = {})
  new(options).from_dir(dirname)
end

.from_file(filename, options = {}) ⇒ Object

Load catalog from a serialize file

Parameters:

  • filename (String)
  • options (Hash) (defaults to: {})

    @option options [Number] :verbose list of ignore pattern



50
51
52
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 50

def self.from_file(filename, options = {})
  new(options).from_file(filename)
end

.load(file_or_dir, options = {}) ⇒ Object



54
55
56
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 54

def self.load(file_or_dir, options = {})
  new(options).load(file_or_dir)
end

Instance Method Details

#-(right) ⇒ CatOnYaml

return differences from this catalog and right catalog param [Cat] right

Returns:



208
209
210
211
212
213
214
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 208

def -(right)
  result = CatOnYaml.new
  @entries.each do |e|
    result.add_entry(e) unless right.contains(e)
  end
  result
end

#_load_from_dirObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 113

def _load_from_dir
  start = Time.now
  me = self
  bytes = 0
  TreeRb::DirTreeWalker.new.run @dirname do

    on_leaf do |filename|
      entry = Entry.from_filename(filename)
      me.add_entry(entry)
      bytes += entry.size
      if me.verbose_level > 0
        print "#{CR}#{filename}#{CLEAR}"
      end
      if me.show_progress
        sec = Time.now - start
        print "#{CR}bytes: #{bytes.to_human} time: #{sec} bytes/sec #{bytes/sec} #{CLEAR}"
      end
    end

  end
  self
end

#add_entry(e) ⇒ Object

add entry to this catalog



191
192
193
194
195
196
197
198
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 191

def add_entry(e)
  @entries.push(e)
  if @md5_to_entries.has_key?(e.md5)
    @md5_to_entries[e.md5].push(e)
  else
    @md5_to_entries[e.md5] = [e]
  end
end

#bytesNumber

total size number of file cataloged

Returns:

  • (Number)


170
171
172
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 170

def bytes
  @entries.inject(0) { |sum, entry| sum + entry.size }
end

#contains(e) ⇒ Object



200
201
202
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 200

def contains(e)
  @md5_to_entries.has_key?(e.md5)
end

#duplicatesArray

Returns entries representing duplicate files.

Returns:

  • (Array)

    entries representing duplicate files



244
245
246
247
248
249
250
251
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 244

def duplicates
  list = []
  @md5_to_entries.each_value do |ee|
    next if ee.size < 2
    list.push(ee)
  end
  list
end

#empty?Boolean

number of entries == 0

Returns:

  • (Boolean)


163
164
165
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 163

def empty?
  @entries.size == 0
end

#fmt_report(*columns) ⇒ Object

print a complex report on stdout



228
229
230
231
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 228

def fmt_report(*columns)
  columns = [:md5, :name, :path, :size] if columns.empty?
  OptParseCommand::report(@entries, *columns)
end

#fmt_ruby(dst) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 233

def fmt_ruby(dst)
  puts "require 'fileutils'"
  @entries.each { |entry|
    src = File.join(@dirname, entry.path, entry.name);
    puts "FileUtils.cp( \"#{src}\", \"#{dst}\" )"
  }
end

#fmt_simpleObject Also known as: to_s

list of entries on stdout @return



219
220
221
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 219

def fmt_simple
  @entries.inject('') { |s, e| s << e.to_s << "\n" }
end

#from_dir(dirname) ⇒ Object

Build a catalog from a directory



59
60
61
62
63
64
65
66
67
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 59

def from_dir(dirname)
  unless File.directory?(dirname)
    raise "'#{dirname}' is not a directory or doesn't exists"
  end
  @dirname = File.expand_path dirname
  @ctime = DateTime.now
  _load_from_dir
  self
end

#from_file(filename) ⇒ Object

Load catalog from a file



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 70

def from_file(filename)
  unless File.exist?(filename)
    raise DirCatException.new, "'#{filename}' not exists"
  end
  dircat_ser = File.open(filename) { |f| YAML::load(f) }
  @dirname = dircat_ser.dirname
  @ctime = dircat_ser.ctime
  dircat_ser.entries.each do |entry_ser|
    add_entry(Entry.from_ser(entry_ser))
  end
  self
end

#list_dupObject



253
254
255
256
257
258
259
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 253

def list_dup
  r = ""
  duplicates.flatten.each do |e|
    r += e.to_s + "\n"
  end
  r
end

#load(file_or_dir) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 83

def load(file_or_dir)
  if File.directory?(file_or_dir)
    from_dir(file_or_dir)
  elsif File.exists?(file_or_dir)
    from_file(file_or_dir)
  else
    raise DirCatException.new, "'#{file_or_dir}' not exists"
  end
end

#reportString

simple report with essential information about this catalog

Returns:

  • (String)

    report



177
178
179
180
181
182
183
184
185
186
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 177

def report
  dups = duplicates
  s = "Base dir: #{@dirname}\n"
  s += "Nr. file: #{size}"
  if dups.size > 0
    s+= " (duplicates #{dups.size})"
  end
  s += "\nBytes: #{bytes.with_separator}"
  s
end

#save_to(file) ⇒ Object

Save serialized catalog to file

Parameters:

  • file (String, File)


139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 139

def save_to(file)
  if file.kind_of?(String)
    begin
      File.open(file, "w") do |f|
        f.puts to_ser.to_yaml
      end
    rescue Errno::ENOENT
      raise DirCatException.new, "DirCat: cannot write into '#{file}'", caller
    end
  else
    file.puts to_ser.to_yaml
  end
end

#script_dupString

return ruby script to eliminate duplicated

Returns:

  • (String)

    ruby script



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 265

def script_dup
  r = "require 'fileutils'\n"
  duplicates.each do |entries|
    flg_first = true
    r += "\n"
    entries.each do |entry|
      src = File.join(@dirname, entry.path, entry.name)
      if flg_first
        flg_first = false
        r += "# FileUtils.mv( \"#{src}\", \"#{Dir.tmpdir}\" )\n"
      else
        r += "FileUtils.mv( \"#{src}\", \"#{Dir.tmpdir}\" )\n"
      end
    end
  end
  r
end

#sizeNumber

number of entries (files)

Returns:

  • (Number)


156
157
158
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 156

def size
  @entries.size
end

#to_serDirCatSer

serialize catalog

Returns:



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dircat/cat_on_yaml/cat_on_yaml.rb', line 95

def to_ser
  dircat_ser = DirCatSer.new
  dircat_ser.dircat_version = DirCat::VERSION
  dircat_ser.dirname = @dirname
  dircat_ser.ctime = @ctime
  dircat_ser.entries = []
  @entries.each do |entry|
    dircat_ser.entries << entry.to_ser
  end
  dircat_ser
end