Class: Webgen::Source::TarArchive

Inherits:
Object
  • Object
show all
Defined in:
lib/webgen/source/tar_archive.rb

Overview

This class is used to read source paths from a (gzipped) tar archive. The archive can be remote (http(s) or ftp) or local.

For example, the following are all valid URIs:

http://example.com/directory/file.tgz
/home/test/my.tar.gz
ftp://ftp.example.com/archives/archive.tar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(website, uri, glob = '**/*') ⇒ TarArchive

Create a new tar archive source for the URI string uri.



32
33
34
35
# File 'lib/webgen/source/tar_archive.rb', line 32

def initialize(website, uri, glob = '**/*')
  @uri = uri
  @glob = glob
end

Instance Attribute Details

#globObject (readonly)

The glob (see File.fnmatch for details) that is used to specify which paths in the archive should be returned by #paths.



29
30
31
# File 'lib/webgen/source/tar_archive.rb', line 29

def glob
  @glob
end

#uriObject (readonly)

The URI of the tar archive.



25
26
27
# File 'lib/webgen/source/tar_archive.rb', line 25

def uri
  @uri
end

Instance Method Details

#pathsObject

Return all paths in the tar archive available at #uri.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/webgen/source/tar_archive.rb', line 38

def paths
  if !defined?(@paths)
    stream = open(@uri)
    stream = Zlib::GzipReader.new(stream) if @uri =~ /(\.tar\.gz|\.tgz)$/
    Archive::Tar::Minitar::Input.open(stream) do |input|
      @paths = input.collect do |entry|
        path = entry.full_name
        next unless File.fnmatch(@glob, path, File::FNM_DOTMATCH|File::FNM_CASEFOLD|File::FNM_PATHNAME)
        path += '/' if entry.directory? && path[-1] != ?/
        path = '/' + path unless path[0] == ?/
        data = entry.read.to_s
        Path.new(path, 'modified_at' => Time.at(entry.mtime)) {|mode| StringIO.new(data, mode) }
      end.compact.to_set
    end
  end
  @paths
end