Class: Distillery::Archiver Abstract

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/distillery/archiver.rb,
lib/distillery/archiver/zip.rb,
lib/distillery/archiver/archive.rb,
lib/distillery/archiver/external.rb,
lib/distillery/archiver/libarchive.rb

Overview

This class is abstract.

Allow processing of archives

Direct Known Subclasses

External, LibArchive, Zip

Defined Under Namespace

Classes: Archive, ArchiverNotFound, Error, ExecError, External, InputStream, LibArchive, OutputStream, ProcessingError, Zip

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArchiver

Returns a new instance of Archiver.



271
272
273
# File 'lib/distillery/archiver.rb', line 271

def initialize
    raise "abstract class"
end

Class Method Details

.add(provider) ⇒ self

Add an archiver provider class

Parameters:

  • provider (Class)

    archiver provider

Returns:

  • (self)


112
113
114
115
# File 'lib/distillery/archiver.rb', line 112

def self.add(provider)
    @@providers << provider
    self
end

.archiversArray<Archiver>

List of registered archivers

Returns:



187
188
189
# File 'lib/distillery/archiver.rb', line 187

def self.archivers
    @@archivers.to_a
end

.for(file) ⇒ Archive .for(file) {|archive| ... } ⇒ self

Return an archive instance of the specified file, or invokes the block with the archive instance passed as parameter.

Overloads:

  • .for(file) ⇒ Archive

    Returns archive instance.

    Parameters:

    • file (String)

      archive file

    Returns:

    Raises:

    • (ArchiverNotFound)

      an archiver able to process this file has not been found

  • .for(file) {|archive| ... } ⇒ self

    Parameters:

    • file (String)

      archive file

    Yield Parameters:

    • archive (Archive)

      archive instance

    Returns:

    • (self)

    Raises:

    • (ArchiverNotFound)

      an archiver able to process this file has not been found



257
258
259
260
261
262
263
264
265
# File 'lib/distillery/archiver.rb', line 257

def self.for(file)
    archive = Archive.new(file)
    if block_given?
        yield(archive)
        self
    else
        archive
    end
end

.for_extension(extension) ⇒ Object

Archiver able to process the selected extension



201
202
203
204
# File 'lib/distillery/archiver.rb', line 201

def self.for_extension(extension)
    extension = extension[1..-1] if extension[0] == '.'
    @@extensions[extension.downcase]
end

.for_file(file) ⇒ Archiver?

Archiver able to process the selected file.

Parameters:

  • file (String)

    archive file tpo consider

Returns:

  • (Archiver)

    archiver to use for processing file

  • (nil)

    no matching archiver found



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/distillery/archiver.rb', line 214

def self.for_file(file)
    # Find by extension
    parts      = File.basename(file).split('.')
    extlist    = 1.upto(parts.size-1).map {|i| parts[i..-1].join('.') }
    archiver   = extlist.lazy.map  {|ext| self.for_extension(ext) }
                             .find {|arc| ! arc.nil?              }

    # Find by mimetype if previously failed
    archiver ||= if defined?(MimeMagic)
                     begin
                         File.open(file) {|io|
                             self.for_mimetype(MimeMagic.by_magic(io))
                         }
                     rescue Errno::ENOENT
                     end
                 end

    # Return found archiver (or nil)
    archiver
end

.for_mimetype(mimetype) ⇒ Object

Archiver able to process the selected mimetype



194
195
196
# File 'lib/distillery/archiver.rb', line 194

def self.for_mimetype(mimetype)
    @@mimetypes[mimetype]
end

.loggerLogger?

Get the regisered logger

Returns:

  • (Logger, nil)


101
102
103
# File 'lib/distillery/archiver.rb', line 101

def self.logger
    @@logger
end

.logger=(logger) ⇒ Object

Register a logger

Parameters:

  • logger (Logger, nil)

    logger

Returns:

  • logger



92
93
94
# File 'lib/distillery/archiver.rb', line 92

def self.logger=(logger)
    @@logger = logger
end

.providersArray<Class>

List of archivers’ providers in loading order.

Returns:

  • (Array<Class>)

    Archiver class



122
123
124
125
126
127
# File 'lib/distillery/archiver.rb', line 122

def self.providers
    # Could be done by looking at constants
    #   constants.lazy.map {|c| const_get(c) }.select {|k| k < self }.to_a
    # But we want to keep loading order
    @@providers
end

.register(archiver, warnings: true) ⇒ Object

Register an archiver.

Parameters:

  • archiver (Archiver)

    Archiver to register

  • warnings (Boolean) (defaults to: true)

    Emit warning when overriding



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/distillery/archiver.rb', line 148

def self.register(archiver, warnings: true)
    # Notifier
    notify = if warnings
                 ->(type, key, old, new) {
                     oname = old.class.name.split('::').last
                     nname = new.class.name.split('::').last
                     Archiver.logger&.warn {
                         "#{self} overriding #{type} for #{key}" \
                         " [#{oname} -> #{nname}]"
                     }
                 }
             end

    # Add archiver
    @@archivers.add(archiver)

    # Register mimetypes
    Array(archiver.mimetypes).each {|mt|
        @@mimetypes.merge!(mt => archiver) {|key, old, new|
            notify&.call("mimetype", key, old, new)
            new
        }
    }
    # Register extensions
    Array(archiver.extensions).each {|ext|
        @@extensions.merge!(ext.downcase => archiver) {|key, old, new|
            notify&.call("extension", key, old, new)
            new
        }
    }
    # Return archiver
    archiver
end

.registeringself

Perform automatic registration of all the archive providers

Returns:

  • (self)


134
135
136
137
138
139
# File 'lib/distillery/archiver.rb', line 134

def self.registering
    self.providers.each do |p|
        p.registering
    end
    self
end

Instance Method Details

#delete!(file, entry) ⇒ Boolean

Delete the entry from the archive

Parameters:

  • file (String)

    archive file

  • entry (String)

    entry name

Returns:

  • (Boolean)

    operation status



368
369
370
# File 'lib/distillery/archiver.rb', line 368

def delete!(file, entry)
    false
end

#each(file) {|entry, io| ... } ⇒ self, Enumerator

Iterate over each archive entry

Parameters:

  • file (String)

    archive file

Yield Parameters:

Returns:

  • (self, Enumerator)


303
304
305
# File 'lib/distillery/archiver.rb', line 303

def each(file, &block)
    raise "abstract method"
end

#empty?(file) ⇒ Boolean

Check if the archive contains no entry

Parameters:

  • file (String)

    archive file

Returns:

  • (Boolean)


314
315
316
# File 'lib/distillery/archiver.rb', line 314

def empty?(file)
    ! each(file).any?
end

#entries(file) ⇒ Array<String>

List archive entries

Parameters:

  • file (String)

    archive file

Returns:

  • (Array<String>)


325
326
327
# File 'lib/distillery/archiver.rb', line 325

def entries(file)
    each(file).map {|a_entry, _| a_entry }
end

#extensionsArray<String>

List of supported extensions

Returns:

  • (Array<String>)


280
281
282
# File 'lib/distillery/archiver.rb', line 280

def extensions
    [ 'zip' ]
end

#mimetypesArray<String>

List of supported mimetypes

Returns:

  • (Array<String>)


289
290
291
# File 'lib/distillery/archiver.rb', line 289

def mimetypes
    [ 'application/zip' ]
end

#reader(file, entry) {|io| ... } ⇒ Object

Allow to perform read operation on an archive entry

Parameters:

  • file (String)

    archive file

  • entry (String)

    entry name

Yield Parameters:

Returns:

  • block value



339
340
341
342
343
344
# File 'lib/distillery/archiver.rb', line 339

def reader(file, entry, &block)
    each(file) {|a_entry, a_io|
        next unless a_entry == entry
        return block.call(a_io)
    }
end

#writer(file, entry) {|io| ... } ⇒ Object

Allow to perform write operation on an archive entry

Parameters:

  • file (String)

    archive file

  • entry (String)

    entry name

Yield Parameters:

Returns:

  • block value



356
357
358
# File 'lib/distillery/archiver.rb', line 356

def writer(file, entry, &block)
    nil
end