2
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4
# See LICENSE.txt for permissions.
9
require 'rubygems/package'
14
# The format class knows the guts of the RubyGem .gem file format
15
# and provides the capability to read gem files
18
attr_accessor :spec, :file_entries, :gem_path
19
extend Gem::UserInteraction
22
# Constructs an instance of a Format object, representing the gem's
25
# gem:: [String] The file name of the gem
27
def initialize(gem_path)
32
# Reads the named gem file and returns a Format object, representing
33
# the data from the gem file
35
# file_path:: [String] Path to the gem file
37
def self.from_file_by_path(file_path, security_policy = nil)
40
unless File.exist?(file_path)
41
raise Gem::Exception, "Cannot load gem at [#{file_path}] in #{Dir.pwd}"
44
# check for old version gem
45
if File.read(file_path, 20).include?("MD5SUM =")
46
#alert_warning "Gem #{file_path} is in old format."
47
require 'rubygems/old_format'
48
format = OldFormat.from_file_by_path(file_path)
51
f = File.open(file_path, 'rb')
52
format = from_io(f, file_path, security_policy)
54
f.close unless f.closed?
62
# Reads a gem from an io stream and returns a Format object, representing
63
# the data from the gem file
65
# io:: [IO] Stream from which to read the gem
67
def self.from_io(io, gem_path="(io)", security_policy = nil)
68
format = self.new(gem_path)
69
Package.open_from_io(io, 'r', security_policy) do |pkg|
70
format.spec = pkg.metadata
71
format.file_entries = []
73
format.file_entries << [{"size" => entry.size, "mode" => entry.mode,
74
"path" => entry.full_name}, entry.read]