1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Importing rubygems @ c2d4131: Deal with platforms that have DLEXT2 == nil. Fixes RF#28867

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30608 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ryan 2011-01-19 21:23:04 +00:00
parent df2762fb1a
commit 64847a9cfe
10 changed files with 274 additions and 91 deletions

View file

@ -193,6 +193,7 @@ module Gem
@ruby = nil @ruby = nil
@sources = [] @sources = []
@post_build_hooks ||= []
@post_install_hooks ||= [] @post_install_hooks ||= []
@post_uninstall_hooks ||= [] @post_uninstall_hooks ||= []
@pre_uninstall_hooks ||= [] @pre_uninstall_hooks ||= []
@ -728,6 +729,17 @@ module Gem
@platforms @platforms
end end
##
# Adds a post-build hook that will be passed an Gem::Installer instance
# when Gem::Installer#install is called. The hook is called after the gem
# has been extracted and extensions have been built but before the
# executables or gemspec has been written. If the hook returns +false+ then
# the gem's files will be removed and the install will be aborted.
def self.post_build(&hook)
@post_build_hooks << hook
end
## ##
# Adds a post-install hook that will be passed an Gem::Installer instance # Adds a post-install hook that will be passed an Gem::Installer instance
# when Gem::Installer#install is called # when Gem::Installer#install is called
@ -747,7 +759,8 @@ module Gem
## ##
# Adds a pre-install hook that will be passed an Gem::Installer instance # Adds a pre-install hook that will be passed an Gem::Installer instance
# when Gem::Installer#install is called # when Gem::Installer#install is called. If the hook returns +false+ then
# the install will be aborted.
def self.pre_install(&hook) def self.pre_install(&hook)
@pre_install_hooks << hook @pre_install_hooks << hook
@ -986,7 +999,8 @@ module Gem
'.rb', '.rb',
*%w(DLEXT DLEXT2).map { |key| *%w(DLEXT DLEXT2).map { |key|
val = RbConfig::CONFIG[key] val = RbConfig::CONFIG[key]
".#{val}" unless val.empty? next unless val and not val.empty?
".#{val}"
} }
].compact.uniq ].compact.uniq
end end
@ -1096,6 +1110,12 @@ module Gem
attr_reader :loaded_specs attr_reader :loaded_specs
##
# The list of hooks to be run before Gem::Install#install finishes
# installation
attr_reader :post_build_hooks
## ##
# The list of hooks to be run before Gem::Install#install does any work # The list of hooks to be run before Gem::Install#install does any work
@ -1219,9 +1239,6 @@ end
## ##
# Enables the require hook for RubyGems. # Enables the require hook for RubyGems.
#
# Ruby 1.9 allows --disable-gems, so we require it when we didn't detect a Gem
# constant at rubygems.rb load time.
require 'rubygems/custom_require' require 'rubygems/custom_require'

View file

@ -20,7 +20,6 @@ end
Gem.load_yaml Gem.load_yaml
require 'rubygems/package' require 'rubygems/package'
require 'rubygems/security'
## ##
# The Builder class processes RubyGem specification files # The Builder class processes RubyGem specification files
@ -73,6 +72,8 @@ EOM
signer = nil signer = nil
if @spec.respond_to?(:signing_key) and @spec.signing_key then if @spec.respond_to?(:signing_key) and @spec.signing_key then
require 'rubygems/security'
signer = Gem::Security::Signer.new @spec.signing_key, @spec.cert_chain signer = Gem::Security::Signer.new @spec.signing_key, @spec.cert_chain
@spec.signing_key = nil @spec.signing_key = nil
@spec.cert_chain = signer.cert_chain.map { |cert| cert.to_s } @spec.cert_chain = signer.cert_chain.map { |cert| cert.to_s }

View file

@ -57,13 +57,17 @@ class Gem::Commands::SourcesCommand < Gem::Command
path = Gem::SpecFetcher.fetcher.dir path = Gem::SpecFetcher.fetcher.dir
FileUtils.rm_rf path FileUtils.rm_rf path
if not File.exist?(path) then unless File.exist? path then
say "*** Removed specs cache ***" say "*** Removed specs cache ***"
elsif not File.writable?(path) then else
unless File.writable? path then
say "*** Unable to remove source cache (write protected) ***" say "*** Unable to remove source cache (write protected) ***"
else else
say "*** Unable to remove source cache ***" say "*** Unable to remove source cache ***"
end end
terminate_interaction 1
end
end end
if options[:add] then if options[:add] then
@ -78,8 +82,10 @@ class Gem::Commands::SourcesCommand < Gem::Command
say "#{source_uri} added to sources" say "#{source_uri} added to sources"
rescue URI::Error, ArgumentError rescue URI::Error, ArgumentError
say "#{source_uri} is not a URI" say "#{source_uri} is not a URI"
terminate_interaction 1
rescue Gem::RemoteFetcher::FetchError => e rescue Gem::RemoteFetcher::FetchError => e
say "Error fetching #{source_uri}:\n\t#{e.message}" say "Error fetching #{source_uri}:\n\t#{e.message}"
terminate_interaction 1
end end
end end

View file

@ -11,7 +11,13 @@
#++ #++
require 'rubygems' require 'rubygems'
require 'rubygems/security'
# forward-declare
module Gem::Security # :nodoc:
class Policy # :nodoc:
end
end
## ##
# Mixin methods for install and update options for Gem::Commands # Mixin methods for install and update options for Gem::Commands
@ -23,8 +29,12 @@ module Gem::InstallUpdateOptions
def add_install_update_options def add_install_update_options
OptionParser.accept Gem::Security::Policy do |value| OptionParser.accept Gem::Security::Policy do |value|
require 'rubygems/security'
value = Gem::Security::Policies[value] value = Gem::Security::Policies[value]
raise OptionParser::InvalidArgument, value if value.nil? valid = Gem::Security::Policies.keys.sort
message = "#{value} (#{valid.join ', '} are valid)"
raise OptionParser::InvalidArgument, message if value.nil?
value value
end end

View file

@ -24,7 +24,7 @@ require 'rubygems/require_paths_builder'
# gemspec in the specifications dir, storing the cached gem in the cache dir, # gemspec in the specifications dir, storing the cached gem in the cache dir,
# and installing either wrappers or symlinks for executables. # and installing either wrappers or symlinks for executables.
# #
# The installer fires pre and post install hooks. Hooks can be added either # The installer invokes pre and post install hooks. Hooks can be added either
# through a rubygems_plugin.rb file in an installed gem or via a # through a rubygems_plugin.rb file in an installed gem or via a
# rubygems/defaults/#{RUBY_ENGINE}.rb or rubygems/defaults/operating_system.rb # rubygems/defaults/#{RUBY_ENGINE}.rb or rubygems/defaults/operating_system.rb
# file. See Gem.pre_install and Gem.post_install for details. # file. See Gem.pre_install and Gem.post_install for details.
@ -61,6 +61,11 @@ class Gem::Installer
attr_reader :spec attr_reader :spec
##
# The options passed when the Gem::Installer was instantiated.
attr_reader :options
@path_warning = false @path_warning = false
class << self class << self
@ -98,49 +103,16 @@ class Gem::Installer
require 'fileutils' require 'fileutils'
@gem = gem @gem = gem
@options = options
options = { process_options
:bin_dir => nil, load_gem_file
:env_shebang => false,
:exec_format => false,
:force => false,
:install_dir => Gem.dir,
:source_index => Gem.source_index,
}.merge options
@env_shebang = options[:env_shebang]
@force = options[:force]
gem_home = options[:install_dir]
@gem_home = File.expand_path(gem_home)
@ignore_dependencies = options[:ignore_dependencies]
@format_executable = options[:format_executable]
@security_policy = options[:security_policy]
@wrappers = options[:wrappers]
@bin_dir = options[:bin_dir]
@development = options[:development]
@source_index = options[:source_index]
begin
@format = Gem::Format.from_file_by_path @gem, @security_policy
rescue Gem::Package::FormatError
raise Gem::InstallError, "invalid gem format for #{@gem}"
end
if options[:user_install] and not options[:unpack] then if options[:user_install] and not options[:unpack] then
@gem_home = Gem.user_dir @gem_home = Gem.user_dir
check_that_user_bin_dir_is_in_path
user_bin_dir = File.join(@gem_home, 'bin')
unless ENV['PATH'].split(File::PATH_SEPARATOR).include? user_bin_dir then
unless self.class.path_warning then
alert_warning "You don't have #{user_bin_dir} in your PATH,\n\t gem executables will not run."
self.class.path_warning = true
end
end
end end
FileUtils.mkdir_p @gem_home verify_gem_home(options[:unpack])
raise Gem::FilePermissionError, @gem_home unless
options[:unpack] or File.writable? @gem_home
@spec = @format.spec @spec = @format.spec
@ -160,48 +132,48 @@ class Gem::Installer
def install def install
# If we're forcing the install then disable security unless the security # If we're forcing the install then disable security unless the security
# policy says that we only install singed gems. # policy says that we only install signed gems.
@security_policy = nil if @force and @security_policy and @security_policy = nil if @force and @security_policy and
not @security_policy.only_signed not @security_policy.only_signed
unless @force then unless @force
if rrv = @spec.required_ruby_version then ensure_required_ruby_version_met
unless rrv.satisfied_by? Gem.ruby_version then ensure_required_rubygems_version_met
raise Gem::InstallError, "#{@spec.name} requires Ruby version #{rrv}." ensure_dependencies_met unless @ignore_dependencies
end
end
if rrgv = @spec.required_rubygems_version then
unless rrgv.satisfied_by? Gem::Version.new(Gem::VERSION) then
raise Gem::InstallError,
"#{@spec.name} requires RubyGems version #{rrgv}. " +
"Try 'gem update --system' to update RubyGems itself."
end
end
unless @ignore_dependencies then
deps = @spec.runtime_dependencies
deps |= @spec.development_dependencies if @development
deps.each do |dep_gem|
ensure_dependency @spec, dep_gem
end
end
end end
Gem.pre_install_hooks.each do |hook| Gem.pre_install_hooks.each do |hook|
hook.call self result = hook.call self
end
FileUtils.mkdir_p @gem_home unless File.directory? @gem_home if result == false then
location = " at #{$1}" if hook.inspect =~ /@(.*:\d+)/
message = "pre-install hook#{location} failed for #{@spec.full_name}"
raise Gem::InstallError, message
end
end
Gem.ensure_gem_subdirectories @gem_home Gem.ensure_gem_subdirectories @gem_home
FileUtils.mkdir_p @gem_dir FileUtils.mkdir_p @gem_dir
extract_files extract_files
generate_bin
build_extensions build_extensions
Gem.post_build_hooks.each do |hook|
result = hook.call self
if result == false then
FileUtils.rm_rf @gem_dir
location = " at #{$1}" if hook.inspect =~ /@(.*:\d+)/
message = "post-build hook#{location} failed for #{@spec.full_name}"
raise Gem::InstallError, message
end
end
generate_bin
write_spec write_spec
write_require_paths_file_if_needed if QUICKLOADER_SUCKAGE write_require_paths_file_if_needed if QUICKLOADER_SUCKAGE
@ -238,7 +210,6 @@ class Gem::Installer
unless installation_satisfies_dependency? dependency then unless installation_satisfies_dependency? dependency then
raise Gem::InstallError, "#{spec.name} requires #{dependency}" raise Gem::InstallError, "#{spec.name} requires #{dependency}"
end end
true true
end end
@ -388,6 +359,80 @@ class Gem::Installer
end end
end end
def ensure_required_ruby_version_met
if rrv = @spec.required_ruby_version then
unless rrv.satisfied_by? Gem.ruby_version then
raise Gem::InstallError, "#{@spec.name} requires Ruby version #{rrv}."
end
end
end
def ensure_required_rubygems_version_met
if rrgv = @spec.required_rubygems_version then
unless rrgv.satisfied_by? Gem::Version.new(Gem::VERSION) then
raise Gem::InstallError,
"#{@spec.name} requires RubyGems version #{rrgv}. " +
"Try 'gem update --system' to update RubyGems itself."
end
end
end
def ensure_dependencies_met
deps = @spec.runtime_dependencies
deps |= @spec.development_dependencies if @development
deps.each do |dep_gem|
ensure_dependency @spec, dep_gem
end
end
def process_options
@options = {
:bin_dir => nil,
:env_shebang => false,
:exec_format => false,
:force => false,
:install_dir => Gem.dir,
:source_index => Gem.source_index,
}.merge @options
@env_shebang = @options[:env_shebang]
@force = @options[:force]
gem_home = @options[:install_dir]
@gem_home = File.expand_path(gem_home)
@ignore_dependencies = @options[:ignore_dependencies]
@format_executable = @options[:format_executable]
@security_policy = @options[:security_policy]
@wrappers = @options[:wrappers]
@bin_dir = @options[:bin_dir]
@development = @options[:development]
@source_index = @options[:source_index]
end
def load_gem_file
begin
@format = Gem::Format.from_file_by_path @gem, @security_policy
rescue Gem::Package::FormatError
raise Gem::InstallError, "invalid gem format for #{@gem}"
end
end
def check_that_user_bin_dir_is_in_path
user_bin_dir = File.join(@gem_home, 'bin')
unless ENV['PATH'].split(File::PATH_SEPARATOR).include? user_bin_dir then
unless self.class.path_warning then
alert_warning "You don't have #{user_bin_dir} in your PATH,\n\t gem executables will not run."
self.class.path_warning = true
end
end
end
def verify_gem_home(unpack = false)
FileUtils.mkdir_p @gem_home
raise Gem::FilePermissionError, @gem_home unless
unpack or File.writable? @gem_home
end
## ##
# Return the text for an application file. # Return the text for an application file.

View file

@ -10,7 +10,6 @@
# See LICENSE.txt for additional licensing information. # See LICENSE.txt for additional licensing information.
#++ #++
require 'rubygems/security'
require 'rubygems/specification' require 'rubygems/specification'
## ##

View file

@ -85,6 +85,7 @@ class Gem::Package::TarOutput
# if we have a signing key, then sign the data # if we have a signing key, then sign the data
# digest and return the signature # digest and return the signature
if @signer then if @signer then
require 'rubygems/security'
digest = Gem::Security::OPT[:dgst_algo].digest sio.string digest = Gem::Security::OPT[:dgst_algo].digest sio.string
@data_signature = @signer.sign digest @data_signature = @signer.sign digest
inner.write sio.string inner.write sio.string
@ -113,6 +114,7 @@ class Gem::Package::TarOutput
# if we have a signing key, then sign the metadata digest and return # if we have a signing key, then sign the metadata digest and return
# the signature # the signature
if @signer then if @signer then
require 'rubygems/security'
digest = Gem::Security::OPT[:dgst_algo].digest sio.string digest = Gem::Security::OPT[:dgst_algo].digest sio.string
@meta_signature = @signer.sign digest @meta_signature = @signer.sign digest
io.write sio.string io.write sio.string

View file

@ -130,11 +130,17 @@ class RubyGemTestCase < MiniTest::Unit::TestCase
@public_cert = File.expand_path File.join(File.dirname(__FILE__), @public_cert = File.expand_path File.join(File.dirname(__FILE__),
'public_cert.pem') 'public_cert.pem')
Gem.post_build_hooks.clear
Gem.post_install_hooks.clear Gem.post_install_hooks.clear
Gem.post_uninstall_hooks.clear Gem.post_uninstall_hooks.clear
Gem.pre_install_hooks.clear Gem.pre_install_hooks.clear
Gem.pre_uninstall_hooks.clear Gem.pre_uninstall_hooks.clear
Gem.post_build do |installer|
@post_build_hook_arg = installer
true
end
Gem.post_install do |installer| Gem.post_install do |installer|
@post_install_hook_arg = installer @post_install_hook_arg = installer
end end
@ -145,6 +151,7 @@ class RubyGemTestCase < MiniTest::Unit::TestCase
Gem.pre_install do |installer| Gem.pre_install do |installer|
@pre_install_hook_arg = installer @pre_install_hook_arg = installer
true
end end
Gem.pre_uninstall do |uninstaller| Gem.pre_uninstall do |uninstaller|

View file

@ -90,8 +90,10 @@ class TestGemCommandsSourcesCommand < RubyGemTestCase
util_setup_spec_fetcher util_setup_spec_fetcher
use_ui @ui do use_ui @ui do
assert_raises MockGemUi::TermError do
@cmd.execute @cmd.execute
end end
end
expected = <<-EOF expected = <<-EOF
Error fetching http://beta-gems.example.com: Error fetching http://beta-gems.example.com:
@ -108,8 +110,10 @@ Error fetching http://beta-gems.example.com:
util_setup_spec_fetcher util_setup_spec_fetcher
use_ui @ui do use_ui @ui do
assert_raises MockGemUi::TermError do
@cmd.execute @cmd.execute
end end
end
assert_equal [@gem_repo], Gem.sources assert_equal [@gem_repo], Gem.sources

View file

@ -521,15 +521,27 @@ load Gem.bin_path('a', 'my_exec', version)
def test_install def test_install
Dir.mkdir util_inst_bindir Dir.mkdir util_inst_bindir
util_setup_gem util_setup_gem
util_clear_gems
gemdir = File.join @gemhome, 'gems', @spec.full_name
cache_file = File.join @gemhome, 'cache', @spec.file_name cache_file = File.join @gemhome, 'cache', @spec.file_name
stub_exe = File.join @gemhome, 'bin', 'executable'
rakefile = File.join gemdir, 'ext', 'a', 'Rakefile'
Gem.pre_install do |installer| Gem.pre_install do |installer|
refute File.exist?(cache_file), 'cache file should not exist yet' refute File.exist?(cache_file), 'cache file must not exist yet'
true
end
Gem.post_build do |installer|
assert File.exist?(gemdir), 'gem install dir must exist'
assert File.exist?(rakefile), 'gem executable must exist'
refute File.exist?(stub_exe), 'gem executable must not exist'
true
end end
Gem.post_install do |installer| Gem.post_install do |installer|
assert File.exist?(cache_file), 'cache file should exist' assert File.exist?(cache_file), 'cache file must exist'
end end
build_rake_in do build_rake_in do
@ -538,25 +550,27 @@ load Gem.bin_path('a', 'my_exec', version)
end end
end end
gemdir = File.join @gemhome, 'gems', @spec.full_name assert File.exist? gemdir
assert File.exist?(gemdir) assert File.exist?(stub_exe), 'gem executable must exist'
exe = File.join gemdir, 'bin', 'executable'
assert File.exist? exe
exe = File.join(gemdir, 'bin', 'executable')
assert File.exist?(exe)
exe_mode = File.stat(exe).mode & 0111 exe_mode = File.stat(exe).mode & 0111
assert_equal 0111, exe_mode, "0%o" % exe_mode unless win_platform? assert_equal 0111, exe_mode, "0%o" % exe_mode unless win_platform?
assert File.exist?(File.join(gemdir, 'lib', 'code.rb')) assert File.exist?(File.join(gemdir, 'lib', 'code.rb'))
assert File.exist?(File.join(gemdir, 'ext', 'a', 'Rakefile')) assert File.exist? rakefile
spec_file = File.join(@gemhome, 'specifications', @spec.spec_name) spec_file = File.join(@gemhome, 'specifications', @spec.spec_name)
assert_equal spec_file, @spec.loaded_from assert_equal spec_file, @spec.loaded_from
assert File.exist?(spec_file) assert File.exist?(spec_file)
assert_same @installer, @pre_install_hook_arg assert_same @installer, @post_build_hook_arg
assert_same @installer, @post_install_hook_arg assert_same @installer, @post_install_hook_arg
assert_same @installer, @pre_install_hook_arg
end end
def test_install_bad_gem def test_install_bad_gem
@ -669,6 +683,84 @@ load Gem.bin_path('a', 'my_exec', version)
assert File.exist?(File.join(@gemhome, 'specifications', @spec.spec_name)) assert File.exist?(File.join(@gemhome, 'specifications', @spec.spec_name))
end end
def test_install_post_build_false
util_clear_gems
Gem.post_build do
false
end
use_ui @ui do
e = assert_raises Gem::InstallError do
@installer.install
end
location = "#{__FILE__}:#{__LINE__ - 9}"
assert_equal "post-build hook at #{location} failed for a-2", e.message
end
spec_file = File.join @gemhome, 'specifications', @spec.spec_name
refute File.exist? spec_file
gem_dir = File.join @gemhome, 'gems', @spec.full_name
refute File.exist? gem_dir
end
def test_install_post_build_nil
util_clear_gems
Gem.post_build do
nil
end
use_ui @ui do
@installer.install
end
spec_file = File.join @gemhome, 'specifications', @spec.spec_name
assert File.exist? spec_file
gem_dir = File.join @gemhome, 'gems', @spec.full_name
assert File.exist? gem_dir
end
def test_install_pre_install_false
util_clear_gems
Gem.pre_install do
false
end
use_ui @ui do
e = assert_raises Gem::InstallError do
@installer.install
end
location = "#{__FILE__}:#{__LINE__ - 9}"
assert_equal "pre-install hook at #{location} failed for a-2", e.message
end
spec_file = File.join @gemhome, 'specifications', @spec.spec_name
refute File.exist? spec_file
end
def test_install_pre_install_nil
util_clear_gems
Gem.pre_install do
nil
end
use_ui @ui do
@installer.install
end
spec_file = File.join @gemhome, 'specifications', @spec.spec_name
assert File.exist? spec_file
end
def test_install_with_message def test_install_with_message
@spec.post_install_message = 'I am a shiny gem!' @spec.post_install_message = 'I am a shiny gem!'