~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to lib/rubygems.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2010-07-31 17:08:39 UTC
  • mfrom: (1.1.4 upstream) (8.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100731170839-j034dmpdqt1cc4p6
Tags: 1.9.2~svn28788-1
* New release based on upstream snapshot from the 1.9.2 branch,
  after 1.9.2 RC2. That branch is (supposed to be) binary-compatible
  with the 1.9.1 branch.
  + Builds fine on i386. Closes: #580852.
* Upgrade to Standards-Version: 3.9.1. No changes needed.
* Updated generated incs.
* Patches that still need work:
  + Unclear status, need more investigation:
   090729_fix_Makefile_deps.dpatch
   090803_exclude_rdoc.dpatch
   203_adjust_base_of_search_path.dpatch
   902_define_YAML_in_yaml_stringio.rb.dpatch
   919_common.mk_tweaks.dpatch
   931_libruby_suffix.dpatch
   940_test_thread_mutex_sync_shorter.dpatch
  + Maybe not needed anymore, keeping but not applying.
   102_skip_test_copy_stream.dpatch (test doesn't block anymore?)
   104_skip_btest_io.dpatch (test doesn't block anymore?)
   201_gem_prelude.dpatch (we don't use that rubygems anyway?)
   202_gem_default_dir.dpatch (we don't use that rubygems anyway?)
   940_test_file_exhaustive_fails_as_root.dpatch
   940_test_priority_fails.dpatch
   100518_load_libc_libm.dpatch
* Add disable-tests.diff: disable some tests that cause failures on FreeBSD.
  Closes: #590002, #543805, #542927.
* However, many new failures on FreeBSD. Since that version is still an
  improvement, add the check that makes test suite failures non-fatal on
  FreeBSD again. That still needs to be investigated.
* Re-add 903_skip_base_ruby_check.dpatch
* Add build-dependency on ruby1.8 and drop all pre-generated files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
# See LICENSE.txt for permissions.
6
6
#++
7
7
 
8
 
require 'rubygems/rubygems_version'
 
8
gem_disabled = !defined? Gem
 
9
 
 
10
unless gem_disabled
 
11
  # Nuke the Quickloader stuff
 
12
  Gem::QuickLoader.remove
 
13
end
 
14
 
9
15
require 'rubygems/defaults'
10
16
require 'thread'
 
17
require 'etc'
 
18
 
 
19
##
 
20
# RubyGems is the Ruby standard for publishing and managing third party
 
21
# libraries.
 
22
#
 
23
# For user documentation, see:
 
24
#
 
25
# * <tt>gem help</tt> and <tt>gem help [command]</tt>
 
26
# * {RubyGems User Guide}[http://docs.rubygems.org/read/book/1]
 
27
# * {Frequently Asked Questions}[http://docs.rubygems.org/read/book/3]
 
28
#
 
29
# For gem developer documentation see:
 
30
#
 
31
# * {Creating Gems}[http://docs.rubygems.org/read/chapter/5]
 
32
# * Gem::Specification
 
33
# * Gem::Version for version dependency notes
 
34
#
 
35
# Further RubyGems documentation can be found at:
 
36
#
 
37
# * {RubyGems API}[http://rubygems.rubyforge.org/rdoc] (also available from
 
38
#   <tt>gem server</tt>)
 
39
# * {RubyGems Bookshelf}[http://rubygem.org]
 
40
#
 
41
# == RubyGems Plugins
 
42
#
 
43
# As of RubyGems 1.3.2, RubyGems will load plugins installed in gems or
 
44
# $LOAD_PATH.  Plugins must be named 'rubygems_plugin' (.rb, .so, etc) and
 
45
# placed at the root of your gem's #require_path.  Plugins are discovered via
 
46
# Gem::find_files then loaded.  Take care when implementing a plugin as your
 
47
# plugin file may be loaded multiple times if multiple versions of your gem
 
48
# are installed.
 
49
#
 
50
# For an example plugin, see the graph gem which adds a `gem graph` command.
 
51
#
 
52
# == RubyGems Defaults, Packaging
 
53
#
 
54
# RubyGems defaults are stored in rubygems/defaults.rb.  If you're packaging
 
55
# RubyGems or implementing Ruby you can change RubyGems' defaults.
 
56
#
 
57
# For RubyGems packagers, provide lib/rubygems/operating_system.rb and
 
58
# override any defaults from lib/rubygems/defaults.rb.
 
59
#
 
60
# For Ruby implementers, provide lib/rubygems/#{RUBY_ENGINE}.rb and override
 
61
# any defaults from lib/rubygems/defaults.rb.
 
62
#
 
63
# If you need RubyGems to perform extra work on install or uninstall, your
 
64
# defaults override file can set pre and post install and uninstall hooks.
 
65
# See Gem::pre_install, Gem::pre_uninstall, Gem::post_install,
 
66
# Gem::post_uninstall.
 
67
#
 
68
# == Bugs
 
69
#
 
70
# You can submit bugs to the
 
71
# {RubyGems bug tracker}[http://rubyforge.org/tracker/?atid=575&group_id=126]
 
72
# on RubyForge
 
73
#
 
74
# == Credits
 
75
#
 
76
# RubyGems is currently maintained by Eric Hodel.
 
77
#
 
78
# RubyGems was originally developed at RubyConf 2003 by:
 
79
#
 
80
# * Rich Kilmer  -- rich(at)infoether.com
 
81
# * Chad Fowler  -- chad(at)chadfowler.com
 
82
# * David Black  -- dblack(at)wobblini.net
 
83
# * Paul Brannan -- paul(at)atdesk.com
 
84
# * Jim Weirch   -- jim(at)weirichhouse.org
 
85
#
 
86
# Contributors:
 
87
#
 
88
# * Gavin Sinclair     -- gsinclair(at)soyabean.com.au
 
89
# * George Marrows     -- george.marrows(at)ntlworld.com
 
90
# * Dick Davies        -- rasputnik(at)hellooperator.net
 
91
# * Mauricio Fernandez -- batsman.geo(at)yahoo.com
 
92
# * Simon Strandgaard  -- neoneye(at)adslhome.dk
 
93
# * Dave Glasser       -- glasser(at)mit.edu
 
94
# * Paul Duncan        -- pabs(at)pablotron.org
 
95
# * Ville Aine         -- vaine(at)cs.helsinki.fi
 
96
# * Eric Hodel         -- drbrain(at)segment7.net
 
97
# * Daniel Berger      -- djberg96(at)gmail.com
 
98
# * Phil Hagelberg     -- technomancy(at)gmail.com
 
99
# * Ryan Davis         -- ryand-ruby(at)zenspider.com
 
100
#
 
101
# (If your name is missing, PLEASE let us know!)
 
102
#
 
103
# Thanks!
 
104
#
 
105
# -The RubyGems Team
11
106
 
12
107
module Gem
 
108
  RubyGemsVersion = VERSION = '1.3.7'
 
109
 
 
110
  ##
 
111
  # Raised when RubyGems is unable to load or activate a gem.  Contains the
 
112
  # name and version requirements of the gem that either conflicts with
 
113
  # already activated gems or that RubyGems is otherwise unable to activate.
 
114
 
13
115
  class LoadError < ::LoadError
14
 
    attr_accessor :name, :version_requirement
 
116
    # Name of gem
 
117
    attr_accessor :name
 
118
 
 
119
    # Version requirement of gem
 
120
    attr_accessor :version_requirement
15
121
  end
16
 
end
17
 
 
18
 
module Kernel
19
122
 
20
123
  ##
21
 
  # Use Kernel#gem to activate a specific version of +gem_name+.
22
 
  #
23
 
  # +version_requirements+ is a list of version requirements that the
24
 
  # specified gem must match, most commonly "= example.version.number".  See
25
 
  # Gem::Requirement for how to specify a version requirement.
26
 
  #
27
 
  # If you will be activating the latest version of a gem, there is no need to
28
 
  # call Kernel#gem, Kernel#require will do the right thing for you.
29
 
  #
30
 
  # Kernel#gem returns true if the gem was activated, otherwise false.  If the
31
 
  # gem could not be found, didn't match the version requirements, or a
32
 
  # different version was already activated, an exception will be raised.
33
 
  #
34
 
  # Kernel#gem should be called *before* any require statements (otherwise
35
 
  # RubyGems may load a conflicting library version).
36
 
  #
37
 
  # In older RubyGems versions, the environment variable GEM_SKIP could be
38
 
  # used to skip activation of specified gems, for example to test out changes
39
 
  # that haven't been installed yet.  Now RubyGems defers to -I and the
40
 
  # RUBYLIB environment variable to skip activation of a gem.
41
 
  #
42
 
  # Example:
43
 
  #
44
 
  #   GEM_SKIP=libA:libB ruby -I../libA -I../libB ./mycode.rb
45
 
 
46
 
  def gem(gem_name, *version_requirements) # :doc:
47
 
    skip_list = (ENV['GEM_SKIP'] || "").split(/:/)
48
 
    raise Gem::LoadError, "skipping #{gem_name}" if skip_list.include? gem_name
49
 
    Gem.activate(gem_name, *version_requirements)
50
 
  end
51
 
 
52
 
  private :gem
53
 
 
54
 
end
55
 
 
56
 
##
57
 
# Main module to hold all RubyGem classes/modules.
58
 
 
59
 
module Gem
 
124
  # Configuration settings from ::RbConfig
60
125
 
61
126
  ConfigMap = {} unless defined?(ConfigMap)
 
127
 
62
128
  require 'rbconfig'
63
 
  RbConfig = Config unless defined? ::RbConfig
64
129
 
65
130
  ConfigMap.merge!(
66
 
    :BASERUBY => RbConfig::CONFIG["BASERUBY"],
67
 
    :EXEEXT => RbConfig::CONFIG["EXEEXT"],
68
 
    :RUBY_INSTALL_NAME => RbConfig::CONFIG["RUBY_INSTALL_NAME"],
69
 
    :RUBY_SO_NAME => RbConfig::CONFIG["RUBY_SO_NAME"],
70
 
    :arch => RbConfig::CONFIG["arch"],
71
 
    :bindir => RbConfig::CONFIG["bindir"],
72
 
    :datadir => RbConfig::CONFIG["datadir"],
73
 
    :libdir => RbConfig::CONFIG["libdir"],
 
131
    :EXEEXT            => RbConfig::CONFIG["EXEEXT"],
 
132
    :RUBY_SO_NAME      => RbConfig::CONFIG["RUBY_SO_NAME"],
 
133
    :arch              => RbConfig::CONFIG["arch"],
 
134
    :bindir            => RbConfig::CONFIG["bindir"],
 
135
    :datadir           => RbConfig::CONFIG["datadir"],
 
136
    :libdir            => RbConfig::CONFIG["libdir"],
74
137
    :ruby_install_name => RbConfig::CONFIG["ruby_install_name"],
75
 
    :ruby_version => RbConfig::CONFIG["ruby_version"],
76
 
    :sitedir => RbConfig::CONFIG["sitedir"],
77
 
    :sitelibdir => RbConfig::CONFIG["sitelibdir"],
78
 
    :vendordir => RbConfig::CONFIG["vendordir"] ,
79
 
    :vendorlibdir => RbConfig::CONFIG["vendorlibdir"]
 
138
    :ruby_version      => RbConfig::CONFIG["ruby_version"],
 
139
    :rubylibprefix     => RbConfig::CONFIG["rubylibprefix"],
 
140
    :sitedir           => RbConfig::CONFIG["sitedir"],
 
141
    :sitelibdir        => RbConfig::CONFIG["sitelibdir"],
 
142
    :vendordir         => RbConfig::CONFIG["vendordir"] ,
 
143
    :vendorlibdir      => RbConfig::CONFIG["vendorlibdir"]
80
144
  )
81
145
 
 
146
  ##
 
147
  # Default directories in a gem repository
 
148
 
82
149
  DIRECTORIES = %w[cache doc gems specifications] unless defined?(DIRECTORIES)
83
150
 
 
151
  # :stopdoc:
84
152
  MUTEX = Mutex.new
85
153
 
86
 
  RubyGemsPackageVersion = RubyGemsVersion
 
154
  RubyGemsPackageVersion = VERSION
 
155
  # :startdoc:
87
156
 
88
157
  ##
89
158
  # An Array of Regexps that match windows ruby platforms.
102
171
 
103
172
  @configuration = nil
104
173
  @loaded_specs = {}
 
174
  @loaded_stacks = {}
105
175
  @platforms = []
106
176
  @ruby = nil
107
177
  @sources = []
128
198
  # Gem::Requirement and Gem::Version documentation.
129
199
 
130
200
  def self.activate(gem, *version_requirements)
 
201
    if version_requirements.last.is_a?(Hash)
 
202
      options = version_requirements.pop
 
203
    else
 
204
      options = {}
 
205
    end
 
206
 
 
207
    sources = options[:sources] || []
 
208
 
131
209
    if version_requirements.empty? then
132
210
      version_requirements = Gem::Requirement.default
133
211
    end
134
212
 
135
213
    unless gem.respond_to?(:name) and
136
 
           gem.respond_to?(:version_requirements) then
 
214
           gem.respond_to?(:requirement) then
137
215
      gem = Gem::Dependency.new(gem, version_requirements)
138
216
    end
139
217
 
140
 
    matches = Gem.source_index.find_name(gem.name, gem.version_requirements)
 
218
    matches = Gem.source_index.find_name(gem.name, gem.requirement)
141
219
    report_activate_error(gem) if matches.empty?
142
220
 
143
221
    if @loaded_specs[gem.name] then
146
224
      existing_spec = @loaded_specs[gem.name]
147
225
 
148
226
      unless matches.any? { |spec| spec.version == existing_spec.version } then
149
 
        raise Gem::Exception,
150
 
              "can't activate #{gem}, already activated #{existing_spec.full_name}"
 
227
         sources_message = sources.map { |spec| spec.full_name }
 
228
         stack_message = @loaded_stacks[gem.name].map { |spec| spec.full_name }
 
229
 
 
230
         msg = "can't activate #{gem} for #{sources_message.inspect}, "
 
231
         msg << "already activated #{existing_spec.full_name} for "
 
232
         msg << "#{stack_message.inspect}"
 
233
 
 
234
         e = Gem::LoadError.new msg
 
235
         e.name = gem.name
 
236
         e.version_requirement = gem.requirement
 
237
 
 
238
         raise e
151
239
      end
152
240
 
153
241
      return false
159
247
 
160
248
    spec.loaded = true
161
249
    @loaded_specs[spec.name] = spec
 
250
    @loaded_stacks[spec.name] = sources.dup
162
251
 
163
252
    # Load dependent gems first
164
253
    spec.runtime_dependencies.each do |dep_gem|
165
 
      activate dep_gem
 
254
      activate dep_gem, :sources => [spec, *sources]
166
255
    end
167
256
 
168
257
    # bin directory must come before library directories
172
261
      File.join spec.full_gem_path, path
173
262
    end
174
263
 
175
 
    sitelibdir = ConfigMap[:sitelibdir]
176
 
 
177
264
    # gem directories must come after -I and ENV['RUBYLIB']
178
265
    insert_index = load_path_insert_index
179
266
 
220
307
    requirements = Gem::Requirement.default if requirements.empty?
221
308
 
222
309
    unless gem.respond_to?(:name) and
223
 
           gem.respond_to?(:version_requirements) then
 
310
           gem.respond_to?(:requirement) then
224
311
      gem = Gem::Dependency.new gem, requirements
225
312
    end
226
313
 
228
315
  end
229
316
 
230
317
  ##
 
318
  # Find the full path to the executable for gem +name+.  If the +exec_name+
 
319
  # is not given, the gem's default_executable is chosen, otherwise the
 
320
  # specified executable's path is returned.  +version_requirements+ allows
 
321
  # you to specify specific gem versions.
 
322
 
 
323
  def self.bin_path(name, exec_name = nil, *version_requirements)
 
324
    version_requirements = Gem::Requirement.default if
 
325
      version_requirements.empty?
 
326
    spec = Gem.source_index.find_name(name, version_requirements).last
 
327
 
 
328
    raise Gem::GemNotFoundException,
 
329
          "can't find gem #{name} (#{version_requirements})" unless spec
 
330
 
 
331
    exec_name ||= spec.default_executable
 
332
 
 
333
    unless exec_name
 
334
      msg = "no default executable for #{spec.full_name}"
 
335
      raise Gem::Exception, msg
 
336
    end
 
337
 
 
338
    unless spec.executables.include? exec_name
 
339
      msg = "can't find executable #{exec_name} for #{spec.full_name}"
 
340
      raise Gem::Exception, msg
 
341
    end
 
342
 
 
343
    File.join(spec.full_gem_path, spec.bindir, exec_name)
 
344
  end
 
345
 
 
346
  ##
231
347
  # The mode needed to read a file as straight binary.
232
348
 
233
349
  def self.binary_mode
234
 
    @binary_mode ||= RUBY_VERSION > '1.9' ? 'rb:ascii-8bit' : 'rb'
 
350
    'rb'
235
351
  end
236
352
 
237
353
  ##
351
467
  #
352
468
  #   Gem.find_files('rdoc/discover').each do |path| load path end
353
469
  #
354
 
  # find_files does not search $LOAD_PATH for files, only gems.
 
470
  # find_files search $LOAD_PATH for files as well as gems.
 
471
  #
 
472
  # Note that find_files will return all files even if they are from different
 
473
  # versions of the same gem.
355
474
 
356
475
  def self.find_files(path)
 
476
    load_path_files = $LOAD_PATH.map do |load_path|
 
477
      files = Dir["#{File.expand_path path, load_path}#{Gem.suffix_pattern}"]
 
478
 
 
479
      files.select do |load_path_file|
 
480
        File.file? load_path_file.untaint
 
481
      end
 
482
    end.flatten
 
483
 
357
484
    specs = searcher.find_all path
358
485
 
359
 
    specs.map do |spec|
 
486
    specs_files = specs.map do |spec|
360
487
      searcher.matching_files spec, path
361
488
    end.flatten
 
489
 
 
490
    (load_path_files + specs_files).flatten.uniq
362
491
  end
363
492
 
364
493
  ##
365
494
  # Finds the user's home directory.
366
 
  #--
367
 
  # Some comments from the ruby-talk list regarding finding the home
368
 
  # directory:
369
 
  #
370
 
  #   I have HOME, USERPROFILE and HOMEDRIVE + HOMEPATH. Ruby seems
371
 
  #   to be depending on HOME in those code samples. I propose that
372
 
  #   it should fallback to USERPROFILE and HOMEDRIVE + HOMEPATH (at
373
 
  #   least on Win32).
374
495
 
375
496
  def self.find_home
376
 
    ['HOME', 'USERPROFILE'].each do |homekey|
377
 
      return ENV[homekey] if ENV[homekey]
378
 
    end
379
 
 
380
 
    if ENV['HOMEDRIVE'] && ENV['HOMEPATH'] then
381
 
      return "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}"
382
 
    end
383
 
 
384
 
    begin
385
 
      File.expand_path("~")
386
 
    rescue
387
 
      if File::ALT_SEPARATOR then
388
 
          "C:/"
389
 
      else
390
 
          "/"
391
 
      end
 
497
    File.expand_path "~"
 
498
  rescue
 
499
    if File::ALT_SEPARATOR then
 
500
      "C:/"
 
501
    else
 
502
      "/"
392
503
    end
393
504
  end
394
505
 
470
581
  # so you can override the gem_prelude.rb default $LOAD_PATH paths.
471
582
 
472
583
  def self.load_path_insert_index
473
 
    index = $LOAD_PATH.index ConfigMap[:sitelibdir]
 
584
    $LOAD_PATH.index { |p| p.instance_variable_defined? :@gem_prelude_index }
 
585
  end
474
586
 
475
 
    $LOAD_PATH.each_with_index do |path, i|
476
 
      if path.instance_variables.include?(:@gem_prelude_index) or
477
 
        path.instance_variables.include?('@gem_prelude_index') then
478
 
        index = i
479
 
        break
480
 
      end
 
587
  def self.remove_prelude_paths
 
588
    Gem::QuickLoader::GemLoadPaths.each do |path|
 
589
      $LOAD_PATH.delete(path)
481
590
    end
482
 
 
483
 
    index
484
591
  end
485
592
 
486
593
  ##
487
594
  # The file name and line number of the caller of the caller of this method.
488
595
 
489
596
  def self.location_of_caller
490
 
    caller[1] =~ /(.*?):(\d+)$/i
 
597
    caller[1] =~ /(.*?):(\d+).*?$/i
491
598
    file = $1
492
599
    lineno = $2.to_i
493
600
 
495
602
  end
496
603
 
497
604
  ##
498
 
  # manage_gems is useless and deprecated.  Don't call it anymore.
499
 
 
500
 
  def self.manage_gems # :nodoc:
501
 
    file, lineno = location_of_caller
502
 
 
503
 
    warn "#{file}:#{lineno}:Warning: Gem::manage_gems is deprecated and will be removed on or after March 2009."
504
 
  end
505
 
 
506
 
  ##
507
605
  # The version of the Marshal format for your Ruby.
508
606
 
509
607
  def self.marshal_version
585
683
  # The directory prefix this RubyGems was installed at.
586
684
 
587
685
  def self.prefix
588
 
    prefix = File.dirname File.expand_path(__FILE__)
 
686
    dir = File.dirname File.expand_path(__FILE__)
 
687
    prefix = File.dirname dir
589
688
 
590
 
    if File.dirname(prefix) == File.expand_path(ConfigMap[:sitelibdir]) or
591
 
       File.dirname(prefix) == File.expand_path(ConfigMap[:libdir]) or
592
 
       'lib' != File.basename(prefix) then
 
689
    if prefix == File.expand_path(ConfigMap[:sitelibdir]) or
 
690
       prefix == File.expand_path(ConfigMap[:libdir]) or
 
691
       'lib' != File.basename(dir) then
593
692
      nil
594
693
    else
595
 
      File.dirname prefix
596
 
    end
 
694
      prefix
 
695
    end
 
696
  end
 
697
 
 
698
  ##
 
699
  # Promotes the load paths of the +gem_name+ over the load paths of
 
700
  # +over_name+.  Useful for allowing one gem to override features in another
 
701
  # using #find_files.
 
702
 
 
703
  def self.promote_load_path(gem_name, over_name)
 
704
    gem = Gem.loaded_specs[gem_name]
 
705
    over = Gem.loaded_specs[over_name]
 
706
 
 
707
    raise ArgumentError, "gem #{gem_name} is not activated" if gem.nil?
 
708
    raise ArgumentError, "gem #{over_name} is not activated" if over.nil?
 
709
 
 
710
    last_gem_path = File.join gem.full_gem_path, gem.require_paths.last
 
711
 
 
712
    over_paths = over.require_paths.map do |path|
 
713
      File.join over.full_gem_path, path
 
714
    end
 
715
 
 
716
    over_paths.each do |path|
 
717
      $LOAD_PATH.delete path
 
718
    end
 
719
 
 
720
    gem = $LOAD_PATH.index(last_gem_path) + 1
 
721
 
 
722
    $LOAD_PATH.insert(gem, *over_paths)
597
723
  end
598
724
 
599
725
  ##
624
750
 
625
751
    if matches.empty? then
626
752
      error = Gem::LoadError.new(
627
 
          "Could not find RubyGem #{gem.name} (#{gem.version_requirements})\n")
 
753
          "Could not find RubyGem #{gem.name} (#{gem.requirement})\n")
628
754
    else
629
755
      error = Gem::LoadError.new(
630
756
          "RubyGem version error: " +
631
 
          "#{gem.name}(#{matches.first.version} not #{gem.version_requirements})\n")
 
757
          "#{gem.name}(#{matches.first.version} not #{gem.requirement})\n")
632
758
    end
633
759
 
634
760
    error.name = gem.name
635
 
    error.version_requirement = gem.version_requirements
 
761
    error.version_requirement = gem.requirement
636
762
    raise error
637
763
  end
638
764
 
639
765
  private_class_method :report_activate_error
640
766
 
641
 
  def self.required_location(gemname, libfile, *version_constraints)
642
 
    version_constraints = Gem::Requirement.default if version_constraints.empty?
643
 
    matches = Gem.source_index.find_name(gemname, version_constraints)
 
767
  ##
 
768
  # Full path to +libfile+ in +gemname+.  Searches for the latest gem unless
 
769
  # +requirements+ is given.
 
770
 
 
771
  def self.required_location(gemname, libfile, *requirements)
 
772
    requirements = Gem::Requirement.default if requirements.empty?
 
773
 
 
774
    matches = Gem.source_index.find_name gemname, requirements
 
775
 
644
776
    return nil if matches.empty?
 
777
 
645
778
    spec = matches.last
646
779
    spec.require_paths.each do |path|
647
 
      result = File.join(spec.full_gem_path, path, libfile)
648
 
      return result if File.exist?(result)
 
780
      result = File.join spec.full_gem_path, path, libfile
 
781
      return result if File.exist? result
649
782
    end
 
783
 
650
784
    nil
651
785
  end
652
786
 
672
806
  def self.ruby_version
673
807
    return @ruby_version if defined? @ruby_version
674
808
    version = RUBY_VERSION.dup
675
 
    version << ".#{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
 
809
 
 
810
    if defined?(RUBY_PATCHLEVEL) && RUBY_PATCHLEVEL != -1 then
 
811
      version << ".#{RUBY_PATCHLEVEL}"
 
812
    elsif defined?(RUBY_REVISION) then
 
813
      version << ".dev.#{RUBY_REVISION}"
 
814
    end
 
815
 
676
816
    @ruby_version = Gem::Version.new version
677
817
  end
678
818
 
689
829
  # Set the Gem home directory (as reported by Gem.dir).
690
830
 
691
831
  def self.set_home(home)
692
 
    home = home.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
 
832
    home = home.gsub File::ALT_SEPARATOR, File::SEPARATOR if File::ALT_SEPARATOR
693
833
    @gem_home = home
694
 
    ensure_gem_subdirectories(@gem_home)
695
834
  end
696
835
 
697
836
  private_class_method :set_home
716
855
    end
717
856
 
718
857
    @gem_path.uniq!
719
 
    @gem_path.each do |path|
720
 
      if 0 == File.expand_path(path).index(Gem.user_home)
721
 
        next unless File.directory? Gem.user_home
722
 
        unless win_platform? then
723
 
          # only create by matching user
724
 
          next if Etc.getpwuid.uid != File::Stat.new(Gem.user_home).uid
725
 
        end
726
 
      end
727
 
      ensure_gem_subdirectories path
728
 
    end
729
858
  end
730
859
 
731
860
  private_class_method :set_paths
778
907
  end
779
908
 
780
909
  ##
 
910
  # Prints the amount of time the supplied block takes to run using the debug
 
911
  # UI output.
 
912
 
 
913
  def self.time(msg, width = 0, display = Gem.configuration.verbose)
 
914
    now = Time.now
 
915
 
 
916
    value = yield
 
917
 
 
918
    elapsed = Time.now - now
 
919
 
 
920
    ui.say "%2$*1$s: %3$3.3fs" % [-width, msg, elapsed] if display
 
921
 
 
922
    value
 
923
  end
 
924
 
 
925
  ##
 
926
  # Lazily loads DefaultUserInteraction and returns the default UI.
 
927
 
 
928
  def self.ui
 
929
    require 'rubygems/user_interaction'
 
930
 
 
931
    Gem::DefaultUserInteraction.ui
 
932
  end
 
933
 
 
934
  ##
781
935
  # Use the +home+ and +paths+ values for Gem.dir and Gem.path.  Used mainly
782
936
  # by the unit tests to provide environment isolation.
783
937
 
805
959
    @@win_platform
806
960
  end
807
961
 
 
962
  ##
 
963
  # Find all 'rubygems_plugin' files and load them
 
964
 
 
965
  def self.load_plugins
 
966
    plugins = Gem.find_files 'rubygems_plugin'
 
967
 
 
968
    plugins.each do |plugin|
 
969
 
 
970
      # Skip older versions of the GemCutter plugin: Its commands are in
 
971
      # RubyGems proper now.
 
972
 
 
973
      next if plugin =~ /gemcutter-0\.[0-3]/
 
974
 
 
975
      begin
 
976
        load plugin
 
977
      rescue ::Exception => e
 
978
        details = "#{plugin.inspect}: #{e.message} (#{e.class})"
 
979
        warn "Error loading RubyGems plugin #{details}"
 
980
      end
 
981
    end
 
982
  end
 
983
 
808
984
  class << self
809
985
 
 
986
    ##
 
987
    # Hash of loaded Gem::Specification keyed by name
 
988
 
810
989
    attr_reader :loaded_specs
811
990
 
812
991
    ##
838
1017
 
839
1018
  end
840
1019
 
 
1020
  ##
 
1021
  # Location of Marshal quick gemspecs on remote repositories
 
1022
 
841
1023
  MARSHAL_SPEC_DIR = "quick/Marshal.#{Gem.marshal_version}/"
842
1024
 
 
1025
  ##
 
1026
  # Location of legacy YAML quick gemspecs on remote repositories
 
1027
 
843
1028
  YAML_SPEC_DIR = 'quick/'
844
1029
 
845
1030
end
846
1031
 
847
 
module Config
848
 
  # :stopdoc:
849
 
  class << self
850
 
    # Return the path to the data directory associated with the named
851
 
    # package.  If the package is loaded as a gem, return the gem
852
 
    # specific data directory.  Otherwise return a path to the share
853
 
    # area as define by "#{ConfigMap[:datadir]}/#{package_name}".
854
 
    def datadir(package_name)
855
 
      Gem.datadir(package_name) ||
856
 
        File.join(Gem::ConfigMap[:datadir], package_name)
857
 
    end
 
1032
module Kernel
 
1033
 
 
1034
  remove_method :gem if respond_to?(:gem, true) # defined in gem_prelude.rb on 1.9
 
1035
 
 
1036
  ##
 
1037
  # Use Kernel#gem to activate a specific version of +gem_name+.
 
1038
  #
 
1039
  # +version_requirements+ is a list of version requirements that the
 
1040
  # specified gem must match, most commonly "= example.version.number".  See
 
1041
  # Gem::Requirement for how to specify a version requirement.
 
1042
  #
 
1043
  # If you will be activating the latest version of a gem, there is no need to
 
1044
  # call Kernel#gem, Kernel#require will do the right thing for you.
 
1045
  #
 
1046
  # Kernel#gem returns true if the gem was activated, otherwise false.  If the
 
1047
  # gem could not be found, didn't match the version requirements, or a
 
1048
  # different version was already activated, an exception will be raised.
 
1049
  #
 
1050
  # Kernel#gem should be called *before* any require statements (otherwise
 
1051
  # RubyGems may load a conflicting library version).
 
1052
  #
 
1053
  # In older RubyGems versions, the environment variable GEM_SKIP could be
 
1054
  # used to skip activation of specified gems, for example to test out changes
 
1055
  # that haven't been installed yet.  Now RubyGems defers to -I and the
 
1056
  # RUBYLIB environment variable to skip activation of a gem.
 
1057
  #
 
1058
  # Example:
 
1059
  #
 
1060
  #   GEM_SKIP=libA:libB ruby -I../libA -I../libB ./mycode.rb
 
1061
 
 
1062
  def gem(gem_name, *version_requirements) # :doc:
 
1063
    skip_list = (ENV['GEM_SKIP'] || "").split(/:/)
 
1064
    raise Gem::LoadError, "skipping #{gem_name}" if skip_list.include? gem_name
 
1065
    Gem.activate(gem_name, *version_requirements)
858
1066
  end
859
 
  # :startdoc:
 
1067
 
 
1068
  private :gem
 
1069
 
 
1070
end
 
1071
 
 
1072
##
 
1073
# Return the path to the data directory associated with the named package.  If
 
1074
# the package is loaded as a gem, return the gem specific data directory.
 
1075
# Otherwise return a path to the share area as define by
 
1076
# "#{ConfigMap[:datadir]}/#{package_name}".
 
1077
 
 
1078
def RbConfig.datadir(package_name)
 
1079
  Gem.datadir(package_name) ||
 
1080
    File.join(Gem::ConfigMap[:datadir], package_name)
860
1081
end
861
1082
 
862
1083
require 'rubygems/exceptions'
869
1090
require 'rubygems/builder'              # HACK: Needed for rake's package task.
870
1091
 
871
1092
begin
 
1093
  ##
 
1094
  # Defaults the operating system (or packager) wants to provide for RubyGems.
 
1095
 
872
1096
  require 'rubygems/defaults/operating_system'
873
1097
rescue LoadError
874
1098
end
875
1099
 
876
1100
if defined?(RUBY_ENGINE) then
877
1101
  begin
 
1102
    ##
 
1103
    # Defaults the ruby implementation wants to provide for RubyGems
 
1104
 
878
1105
    require "rubygems/defaults/#{RUBY_ENGINE}"
879
1106
  rescue LoadError
880
1107
  end
882
1109
 
883
1110
require 'rubygems/config_file'
884
1111
 
885
 
if RUBY_VERSION < '1.9' then
 
1112
class << Gem
 
1113
  remove_method :try_activate if Gem.respond_to?(:try_activate, true)
 
1114
 
 
1115
  def try_activate(path)
 
1116
    spec = Gem.searcher.find(path)
 
1117
    return false unless spec
 
1118
 
 
1119
    Gem.activate(spec.name, "= #{spec.version}")
 
1120
    return true
 
1121
  end
 
1122
end
 
1123
 
 
1124
##
 
1125
# Enables the require hook for RubyGems.
 
1126
#
 
1127
# if --disable-rubygems was used, then the prelude wasn't loaded, so
 
1128
# we need to load the custom_require now.
 
1129
 
 
1130
if gem_disabled
886
1131
  require 'rubygems/custom_require'
887
1132
end
888
1133
 
889
1134
Gem.clear_paths
 
1135
 
 
1136
Gem.load_plugins
 
1137