~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to lib/rubygems/format.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#--
 
2
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
 
3
# All rights reserved.
 
4
# See LICENSE.txt for permissions.
 
5
#++
 
6
 
 
7
require 'fileutils'
 
8
 
 
9
require 'rubygems/package'
 
10
 
 
11
module Gem
 
12
 
 
13
  ##
 
14
  # The format class knows the guts of the RubyGem .gem file format
 
15
  # and provides the capability to read gem files
 
16
  #
 
17
  class Format
 
18
    attr_accessor :spec, :file_entries, :gem_path
 
19
    extend Gem::UserInteraction
 
20
  
 
21
    ##
 
22
    # Constructs an instance of a Format object, representing the gem's
 
23
    # data structure.
 
24
    #
 
25
    # gem:: [String] The file name of the gem
 
26
    #
 
27
    def initialize(gem_path)
 
28
      @gem_path = gem_path
 
29
    end
 
30
    
 
31
    ##
 
32
    # Reads the named gem file and returns a Format object, representing 
 
33
    # the data from the gem file
 
34
    #
 
35
    # file_path:: [String] Path to the gem file
 
36
    #
 
37
    def self.from_file_by_path(file_path, security_policy = nil)
 
38
      format = nil
 
39
 
 
40
      unless File.exist?(file_path)
 
41
        raise Gem::Exception, "Cannot load gem at [#{file_path}] in #{Dir.pwd}"
 
42
      end
 
43
 
 
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)
 
49
      else
 
50
        begin
 
51
          f = File.open(file_path, 'rb')
 
52
          format = from_io(f, file_path, security_policy)
 
53
        ensure
 
54
          f.close unless f.closed?
 
55
        end
 
56
      end
 
57
 
 
58
      return format
 
59
    end
 
60
 
 
61
    ##
 
62
    # Reads a gem from an io stream and returns a Format object, representing
 
63
    # the data from the gem file
 
64
    #
 
65
    # io:: [IO] Stream from which to read the gem
 
66
    #
 
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 = []
 
72
        pkg.each do |entry|
 
73
          format.file_entries << [{"size" => entry.size, "mode" => entry.mode,
 
74
              "path" => entry.full_name}, entry.read]
 
75
        end
 
76
      end
 
77
      format
 
78
    end
 
79
 
 
80
  end
 
81
end