diff options
author | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-11-10 07:48:56 +0000 |
---|---|---|
committer | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-11-10 07:48:56 +0000 |
commit | fbf59bdbea63efd34ccc144e648467d2f52e7345 (patch) | |
tree | 244f0e7ae112cc7dd135e5d1ac24e6c70ba71b4a /lib/rubygems/format.rb | |
parent | 7a4aad75356496559460041a6c063bdb736c7236 (diff) | |
download | ruby-fbf59bdbea63efd34ccc144e648467d2f52e7345.tar.gz |
Import RubyGems trunk revision 1493.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13862 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rubygems/format.rb')
-rw-r--r-- | lib/rubygems/format.rb | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/rubygems/format.rb b/lib/rubygems/format.rb new file mode 100644 index 0000000000..378a93018c --- /dev/null +++ b/lib/rubygems/format.rb @@ -0,0 +1,81 @@ +#-- +# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others. +# All rights reserved. +# See LICENSE.txt for permissions. +#++ + +require 'fileutils' + +require 'rubygems/package' + +module Gem + + ## + # The format class knows the guts of the RubyGem .gem file format + # and provides the capability to read gem files + # + class Format + attr_accessor :spec, :file_entries, :gem_path + extend Gem::UserInteraction + + ## + # Constructs an instance of a Format object, representing the gem's + # data structure. + # + # gem:: [String] The file name of the gem + # + def initialize(gem_path) + @gem_path = gem_path + end + + ## + # Reads the named gem file and returns a Format object, representing + # 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}" + end + + # check for old version gem + if File.read(file_path, 20).include?("MD5SUM =") + #alert_warning "Gem #{file_path} is in old format." + require 'rubygems/old_format' + format = OldFormat.from_file_by_path(file_path) + else + begin + f = File.open(file_path, 'rb') + format = from_io(f, file_path, security_policy) + ensure + f.close unless f.closed? + end + end + + return format + end + + ## + # 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 + # + def self.from_io(io, gem_path="(io)", security_policy = nil) + format = self.new(gem_path) + Package.open_from_io(io, 'r', security_policy) do |pkg| + format.spec = pkg.metadata + format.file_entries = [] + pkg.each do |entry| + format.file_entries << [{"size" => entry.size, "mode" => entry.mode, + "path" => entry.full_name}, entry.read] + end + end + format + end + + end +end |