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 'fileutils'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
attr_accessor :spec, :file_entries, :gem_path
|
|
|
|
|
|
|
|
extend Gem::UserInteraction
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
##
|
2009-06-09 17:38:59 -04:00
|
|
|
# Constructs an instance of a Format object, representing the gem's
|
|
|
|
# data structure.
|
2007-11-10 02:48:56 -05:00
|
|
|
#
|
2009-06-09 17:38:59 -04:00
|
|
|
# gem:: [String] The file name of the gem
|
|
|
|
#
|
|
|
|
def initialize(gem_path)
|
|
|
|
@gem_path = gem_path
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2009-12-08 02:19:09 -05:00
|
|
|
# Reads the named gem file and returns a Format object, representing
|
2009-06-09 17:38:59 -04:00
|
|
|
# the data from the gem file
|
|
|
|
#
|
|
|
|
# file_path:: [String] Path to the gem file
|
|
|
|
|
|
|
|
def self.from_file_by_path(file_path, security_policy = nil)
|
|
|
|
format = 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
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
# check for old version gem
|
|
|
|
if File.read(file_path, 20).include?("MD5SUM =")
|
|
|
|
require 'rubygems/old_format'
|
|
|
|
|
|
|
|
format = Gem::OldFormat.from_file_by_path(file_path)
|
|
|
|
else
|
|
|
|
open file_path, Gem.binary_mode do |io|
|
|
|
|
format = from_io io, file_path, security_policy
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
return format
|
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Reads a gem from an io stream and returns a Format object, representing
|
|
|
|
# the data from the gem file
|
|
|
|
#
|
|
|
|
# io:: [IO] Stream from which to read 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
|
|
|
|