diff options
Diffstat (limited to 'lib/rubygems/config_file.rb')
-rw-r--r-- | lib/rubygems/config_file.rb | 136 |
1 files changed, 82 insertions, 54 deletions
diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb index 136e8b4610..61ee3b73eb 100644 --- a/lib/rubygems/config_file.rb +++ b/lib/rubygems/config_file.rb @@ -5,9 +5,9 @@ #++ ## -# Gem::ConfigFile RubyGems options and gem command options from ~/.gemrc. +# Gem::ConfigFile RubyGems options and gem command options from gemrc. # -# ~/.gemrc is a YAML file that uses strings to match gem command arguments and +# gemrc is a YAML file that uses strings to match gem command arguments and # symbols to match RubyGems options. # # Gem command arguments use a String key that matches the command name and @@ -21,16 +21,19 @@ # RubyGems options use symbol keys. Valid options are: # # +:backtrace+:: See #backtrace -# +:benchmark+:: See #benchmark # +:sources+:: Sets Gem::sources # +:verbose+:: See #verbose - -require 'rbconfig' +# +# gemrc files may exist in various locations and are read and merged in +# the following order: +# +# - system wide (/etc/gemrc) +# - per user (~/.gemrc) +# - per environment (gemrc files listed in the GEMRC environment variable) class Gem::ConfigFile DEFAULT_BACKTRACE = false - DEFAULT_BENCHMARK = false DEFAULT_BULK_THRESHOLD = 1000 DEFAULT_VERBOSITY = true DEFAULT_UPDATE_SOURCES = true @@ -97,11 +100,6 @@ class Gem::ConfigFile attr_writer :backtrace ## - # True if we are benchmarking this run. - - attr_accessor :benchmark - - ## # Bulk threshold value. If the number of missing gems are above this # threshold value, then a bulk download technique is used. (deprecated) @@ -131,6 +129,10 @@ class Gem::ConfigFile attr_reader :api_keys ## + # True if we want to force specification of gem server when pushing a gem + + attr_accessor :disable_default_gem_server + # openssl verify mode value, used for remote https connection attr_reader :ssl_verify_mode @@ -158,29 +160,29 @@ class Gem::ConfigFile # <tt>--debug</tt>:: # Enable Ruby level debug messages. Handled early for the same reason as # --backtrace. + #-- + # TODO: parse options upstream, pass in options directly - def initialize(arg_list) + def initialize(args) @config_file_name = nil need_config_file_name = false - arg_list = arg_list.map do |arg| + arg_list = [] + + args.each do |arg| if need_config_file_name then @config_file_name = arg need_config_file_name = false - nil elsif arg =~ /^--config-file=(.*)/ then @config_file_name = $1 - nil elsif arg =~ /^--config-file$/ then need_config_file_name = true - nil else - arg + arg_list << arg end - end.compact + end @backtrace = DEFAULT_BACKTRACE - @benchmark = DEFAULT_BENCHMARK @bulk_threshold = DEFAULT_BULK_THRESHOLD @verbose = DEFAULT_VERBOSITY @update_sources = DEFAULT_UPDATE_SOURCES @@ -189,19 +191,25 @@ class Gem::ConfigFile platform_config = Marshal.load Marshal.dump(PLATFORM_DEFAULTS) system_config = load_file SYSTEM_WIDE_CONFIG_FILE user_config = load_file config_file_name.dup.untaint + environment_config = (ENV['GEMRC'] || '').split(/[:;]/).inject({}) do |result, file| + result.merge load_file file + end + @hash = operating_system_config.merge platform_config @hash = @hash.merge system_config @hash = @hash.merge user_config + @hash = @hash.merge environment_config # HACK these override command-line args, which is bad - @backtrace = @hash[:backtrace] if @hash.key? :backtrace - @benchmark = @hash[:benchmark] if @hash.key? :benchmark - @bulk_threshold = @hash[:bulk_threshold] if @hash.key? :bulk_threshold - @home = @hash[:gemhome] if @hash.key? :gemhome - @path = @hash[:gempath] if @hash.key? :gempath - @update_sources = @hash[:update_sources] if @hash.key? :update_sources - @verbose = @hash[:verbose] if @hash.key? :verbose + @backtrace = @hash[:backtrace] if @hash.key? :backtrace + @bulk_threshold = @hash[:bulk_threshold] if @hash.key? :bulk_threshold + @home = @hash[:gemhome] if @hash.key? :gemhome + @path = @hash[:gempath] if @hash.key? :gempath + @update_sources = @hash[:update_sources] if @hash.key? :update_sources + @verbose = @hash[:verbose] if @hash.key? :verbose + @disable_default_gem_server = @hash[:disable_default_gem_server] if @hash.key? :disable_default_gem_server + @ssl_verify_mode = @hash[:ssl_verify_mode] if @hash.key? :ssl_verify_mode @ssl_ca_cert = @hash[:ssl_ca_cert] if @hash.key? :ssl_ca_cert @@ -224,6 +232,7 @@ class Gem::ConfigFile else @hash end + if @api_keys.key? :rubygems_api_key then @rubygems_api_key = @api_keys[:rubygems_api_key] @api_keys[:rubygems] = @api_keys.delete :rubygems_api_key unless @api_keys.key? :rubygems @@ -238,7 +247,8 @@ class Gem::ConfigFile Gem.load_yaml - File.open(credentials_path, 'w') do |f| + permissions = 0600 & (~File.umask) + File.open(credentials_path, 'w', permissions) do |f| f.write config.to_yaml end @@ -249,13 +259,21 @@ class Gem::ConfigFile Gem.load_yaml return {} unless filename and File.exist? filename + begin - YAML.load(File.read(filename)) + content = YAML.load(File.read(filename)) + unless content.kind_of? Hash + warn "Failed to load #{config_file_name} because it doesn't contain valid YAML hash" + return {} + end + return content rescue ArgumentError warn "Failed to load #{config_file_name}" rescue Errno::EACCES warn "Failed to load #{config_file_name} due to permissions problem." - end or {} + end + + {} end # True if the backtrace option has been specified, or debug is on. @@ -273,13 +291,11 @@ class Gem::ConfigFile hash = @hash.dup hash.delete :update_sources hash.delete :verbose - hash.delete :benchmark hash.delete :backtrace hash.delete :bulk_threshold yield :update_sources, @update_sources yield :verbose, @verbose - yield :benchmark, @benchmark yield :backtrace, @backtrace yield :bulk_threshold, @bulk_threshold @@ -296,8 +312,6 @@ class Gem::ConfigFile case arg when /^--(backtrace|traceback)$/ then @backtrace = true - when /^--bench(mark)?$/ then - @benchmark = true when /^--debug$/ then $DEBUG = true else @@ -309,25 +323,41 @@ class Gem::ConfigFile # Really verbose mode gives you extra output. def really_verbose case verbose - when true, false, nil then false - else true + when true, false, nil then + false + else + true end end # to_yaml only overwrites things you can't override on the command line. def to_yaml # :nodoc: yaml_hash = {} - yaml_hash[:backtrace] = @hash.key?(:backtrace) ? @hash[:backtrace] : - DEFAULT_BACKTRACE - yaml_hash[:benchmark] = @hash.key?(:benchmark) ? @hash[:benchmark] : - DEFAULT_BENCHMARK - yaml_hash[:bulk_threshold] = @hash.key?(:bulk_threshold) ? - @hash[:bulk_threshold] : DEFAULT_BULK_THRESHOLD - yaml_hash[:sources] = Gem.sources - yaml_hash[:update_sources] = @hash.key?(:update_sources) ? - @hash[:update_sources] : DEFAULT_UPDATE_SOURCES - yaml_hash[:verbose] = @hash.key?(:verbose) ? @hash[:verbose] : - DEFAULT_VERBOSITY + yaml_hash[:backtrace] = if @hash.key?(:backtrace) + @hash[:backtrace] + else + DEFAULT_BACKTRACE + end + + yaml_hash[:bulk_threshold] = if @hash.key?(:bulk_threshold) + @hash[:bulk_threshold] + else + DEFAULT_BULK_THRESHOLD + end + + yaml_hash[:sources] = Gem.sources.to_a + + yaml_hash[:update_sources] = if @hash.key?(:update_sources) + @hash[:update_sources] + else + DEFAULT_UPDATE_SOURCES + end + + yaml_hash[:verbose] = if @hash.key?(:verbose) + @hash[:verbose] + else + DEFAULT_VERBOSITY + end keys = yaml_hash.keys.map { |key| key.to_s } keys << 'debug' @@ -361,15 +391,13 @@ class Gem::ConfigFile def ==(other) # :nodoc: self.class === other and - @backtrace == other.backtrace and - @benchmark == other.benchmark and - @bulk_threshold == other.bulk_threshold and - @verbose == other.verbose and - @update_sources == other.update_sources and - @hash == other.hash + @backtrace == other.backtrace and + @bulk_threshold == other.bulk_threshold and + @verbose == other.verbose and + @update_sources == other.update_sources and + @hash == other.hash end - protected - attr_reader :hash + protected :hash end |