Sha256: fe7c953d3f4915565943eb7fbf11d86242e16464c0628920eda0a85e697bb91f

Contents?: true

Size: 1.96 KB

Versions: 21

Compression:

Stored size: 1.96 KB

Contents

#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
# See LICENSE.txt for permissions.
#++

require 'rubygems/package'

##
# Gem::Format knows the guts of the RubyGem .gem file format and provides the
# capability to read gem files

class Gem::Format

  attr_accessor :spec
  attr_accessor :file_entries
  attr_accessor :gem_path

  ##
  # Constructs a Format representing the gem's data which came from +gem_path+

  def initialize(gem_path)
    @gem_path = gem_path
  end

  ##
  # Reads the gem +file_path+ using +security_policy+ and returns a Format
  # representing the data in the gem

  def self.from_file_by_path(file_path, security_policy = nil)
    unless File.exist?(file_path)
      raise Gem::Exception, "Cannot load gem at [#{file_path}] in #{Dir.pwd}"
    end

    start = File.read file_path, 20

    if start.nil? or start.length < 20 then
      nil
    elsif start.include?("MD5SUM =") # old version gems
      require 'rubygems/old_format'

      Gem::OldFormat.from_file_by_path file_path
    else
      begin
        open file_path, Gem.binary_mode do |io|
          from_io io, file_path, security_policy
        end
      rescue Gem::Package::TarInvalidError => e
        message = "corrupt gem (#{e.class}: #{e.message})"
        raise Gem::Package::FormatError.new(message, file_path)
      end
    end
  end

  ##
  # Reads a gem from +io+ at +gem_path+ using +security_policy+ and returns a
  # Format representing the data from the gem

  def self.from_io(io, gem_path="(io)", security_policy = nil)
    format = new gem_path

    Gem::Package.open io, 'r', security_policy do |pkg|
      format.spec = pkg.metadata
      format.file_entries = []

      pkg.each do |entry|
        size = entry.header.size
        mode = entry.header.mode

        format.file_entries << [{
            "size" => size, "mode" => mode, "path" => entry.full_name,
          },
          entry.read
        ]
      end
    end

    format
  end

end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
rubygems-update-1.8.11 lib/rubygems/format.rb
rubygems-update-1.8.10 lib/rubygems/format.rb
rubygems-update-1.8.9 lib/rubygems/format.rb
rubygems-update-1.8.8 lib/rubygems/format.rb
rubygems-update-1.8.7 lib/rubygems/format.rb
rubygems-update-1.8.6 lib/rubygems/format.rb
rubygems-update-1.8.5 lib/rubygems/format.rb
rubygems-update-1.8.4 lib/rubygems/format.rb
rubygems-update-1.8.3 lib/rubygems/format.rb
rubygems-update-1.8.2 lib/rubygems/format.rb
rubygems-update-1.8.1 lib/rubygems/format.rb
rubygems-update-1.8.0 lib/rubygems/format.rb
rubygems-update-1.7.2 lib/rubygems/format.rb
rubygems-update-1.7.1 lib/rubygems/format.rb
rubygems-update-1.7.0 lib/rubygems/format.rb
rubygems-update-1.6.2 lib/rubygems/format.rb
rubygems-update-1.6.1 lib/rubygems/format.rb
rubygems-update-1.6.0 lib/rubygems/format.rb
rubygems-update-1.5.3 lib/rubygems/format.rb
rubygems-update-1.5.2 lib/rubygems/format.rb