2007-11-10 02:48:56 -05:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
|
|
|
require 'rubygems/package'
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Gem::Format knows the guts of the RubyGem .gem file format and provides the
|
|
|
|
# capability to read gem files
|
|
|
|
|
|
|
|
class Gem::Format
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
attr_accessor :spec
|
|
|
|
attr_accessor :file_entries
|
|
|
|
attr_accessor :gem_path
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
##
|
2010-02-21 21:52:35 -05:00
|
|
|
# Constructs a Format representing the gem's data which came from +gem_path+
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def initialize(gem_path)
|
|
|
|
@gem_path = gem_path
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-02-21 21:52:35 -05:00
|
|
|
# Reads the gem +file_path+ using +security_policy+ and returns a Format
|
|
|
|
# representing the data in the gem
|
2009-06-09 17:38:59 -04:00
|
|
|
|
|
|
|
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}"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
start = File.read file_path, 20
|
|
|
|
|
|
|
|
if start.nil? or start.length < 20 then
|
|
|
|
nil
|
|
|
|
elsif start.include?("MD5SUM =") # old version gems
|
2009-06-09 17:38:59 -04:00
|
|
|
require 'rubygems/old_format'
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
Gem::OldFormat.from_file_by_path file_path
|
2009-06-09 17:38:59 -04:00
|
|
|
else
|
2011-01-31 22:11:34 -05:00
|
|
|
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)
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2010-02-21 21:52:35 -05:00
|
|
|
# Reads a gem from +io+ at +gem_path+ using +security_policy+ and returns a
|
|
|
|
# Format representing the data from the gem
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def self.from_io(io, gem_path="(io)", security_policy = nil)
|
|
|
|
format = new gem_path
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
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
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
format.file_entries << [{
|
|
|
|
"size" => size, "mode" => mode, "path" => entry.full_name,
|
|
|
|
},
|
|
|
|
entry.read
|
|
|
|
]
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
format
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|