class Autobuild::TarImporter

Constants

Bzip

The tarball is compressed using bzip

Gzip

The tarball is compressed with gzip

Plain

The tarball is not compressed

TAR_OPTION
VALID_URI_SCHEMES

Known URI schemes for url

Zip

Not a tarball but a zip

Attributes

cachefile[R]

The local file (either a downloaded file if url is not local, or url itself)

mode[R]

The unpack mode. One of Zip, Bzip, Gzip or Plain

url[R]

The source URL

Public Class Methods

filename_to_mode(filename) click to toggle source

Returns the unpack mode from the file name

# File lib/autobuild/import/archive.rb, line 35
def self.filename_to_mode(filename)
    case filename
        when /\.zip$/; Zip
        when /\.tar$/; Plain
        when /\.tar\.gz$|\.tgz$/;  Gzip
        when /\.bz2$/; Bzip
        else
            raise "unknown file type '#{filename}'"
    end
end
new(url, options) click to toggle source

Creates a new importer which downloads url in cachedir and unpacks it. The following options are allowed:

:cachedir

the cache directory. Defaults to “#{Autobuild.prefix}/cache”

:archive_dir

the directory contained in the archive file. If set,

the importer will rename that directory to make it match
Package#srcdir
:no_subdirectory

the archive does not have the custom archive

subdirectory.
Calls superclass method
# File lib/autobuild/import/archive.rb, line 171
def initialize(url, options)
    super(options)
    if !@options.has_key?(:update_cached_file)
        @options[:update_cached_file] = false
    end
    @options[:cachedir] ||= "#{Autobuild.prefix}/cache"

    @url = URI.parse(url)
    if !VALID_URI_SCHEMES.include?(@url.scheme)
        raise ConfigException, "invalid URL #{@url} (local files must be prefixed with file://)" 
    end

    filename = options[:filename] || File.basename(url).gsub(/\?.*/, '')
    @mode = options[:mode] || ArchiveImporter.filename_to_mode(filename)
    if @url.scheme == 'file'
        @cachefile = @url.path
    else
        @cachefile = File.join(cachedir, filename)
    end
end

Public Instance Methods

archive_dir() click to toggle source

The directory contained in the archive. If not set, we assume that it is the same than the source dir

# File lib/autobuild/import/archive.rb, line 154
def archive_dir; @options[:archive_dir] || tardir end
cachedir() click to toggle source

The directory in which remote files are cached

# File lib/autobuild/import/archive.rb, line 147
def cachedir; @options[:cachedir] end
extract_tar_on_windows(filename,target) click to toggle source
# File lib/autobuild/import/archive.rb, line 70
def extract_tar_on_windows(filename,target)

        Gem::Package::TarReader.new(Zlib::GzipReader.open(filename)).each do |entry|
                newname = File.join(target,entry.full_name.slice(entry.full_name.index('/'),entry.full_name.size))
                if(entry.directory?)
                        FileUtils.mkdir_p(newname)
                end
                if(entry.file?)
                        dir = newname.slice(0,newname.rindex('/'))
                        if(!File.directory?(dir))
                                FileUtils.mkdir_p(dir)
                        end
                        open(newname, "wb") do |file|
                                file.write(entry.read)
                        end
                end
        end
end
get_url_on_windows(url, filename) click to toggle source
# File lib/autobuild/import/archive.rb, line 49
def get_url_on_windows(url, filename)
        uri = URI(url)                
        STDOUT.puts("Host: #{uri.host} Port: #{uri.port} url: #{url}")
        
        http = Net::HTTP.new(uri.host,uri.port)
        http.use_ssl = true if uri.port == 443
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE  #Unsure, critical?, Review this
        resp = http.get(uri.request_uri)
        
        if resp.code == "301" or resp.code == "302"
                get_url_on_windows(resp.header['location'],filename)
        else
                if(resp.message != 'OK')
                        raise "Could not get File from url \"#{url}\", got response #{resp.message} (#{resp.code})"
                end
                open(filename, "wb") do |file|
                        file.write(resp.body)
                end
        end
end
repository_id() click to toggle source

Returns a string that identifies the remote repository uniquely

This is meant for display purposes

# File lib/autobuild/import/archive.rb, line 159
def repository_id
    url.dup
end
tardir() click to toggle source

The directory contained in the tar file

DEPRECATED use archive_dir instead

# File lib/autobuild/import/archive.rb, line 151
def tardir; @options[:tardir] end
update_cache(package) click to toggle source

Updates the downloaded file in cache only if it is needed

# File lib/autobuild/import/archive.rb, line 90
def update_cache(package)
    do_update = false

    if !File.file?(cachefile)
        do_update = true
    elsif self.update_cached_file?
        cached_size = File.lstat(cachefile).size
        cached_mtime = File.lstat(cachefile).mtime

        size, mtime = nil
        if @url.scheme == "file"
            size  = File.stat(@url.path).size
            mtime = File.stat(@url.path).mtime
        else
            open @url, :content_length_proc => lambda { |size| } do |file| 
                mtime = file.last_modified
            end
        end

        if mtime && size
            do_update = (size != cached_size || mtime > cached_mtime)
        elsif mtime
            package.warn "%s: archive size is not available for #{@url}, relying on modification time"
            do_update = (mtime > cached_mtime)
        elsif size
            package.warn "%s: archive modification time is not available for #{@url}, relying on size"
            do_update = (size != cached_size)
        else
            package.warn "%s: neither the archive size nor its modification time available for #{@url}, will always update"
            do_update = true
        end
    end

    if do_update
        FileUtils.mkdir_p(cachedir)
        begin
                                if(WINDOWS)
                                        get_url_on_windows(@url, "#{cachefile}.partial")
                                else
                                        Subprocess.run(package, :import, Autobuild.tool('wget'), '-q', '-P', cachedir, @url, '-O', "#{cachefile}.partial")
                                end
        rescue Exception
            FileUtils.rm_f "#{cachefile}.partial"
            raise
        end
        FileUtils.mv "#{cachefile}.partial", cachefile
        true
    end
end
update_cached_file?() click to toggle source
# File lib/autobuild/import/archive.rb, line 46
def update_cached_file?; @options[:update_cached_file] end