Blame SOURCES/operating_system.rb

a54e24
module Gem
a54e24
  class << self
a54e24
a54e24
    ##
a54e24
    # Returns full path of previous but one directory of dir in path
a54e24
    # E.g. for '/usr/share/ruby', 'ruby', it returns '/usr'
a54e24
a54e24
    def previous_but_one_dir_to(path, dir)
a54e24
      return unless path
a54e24
a54e24
      split_path = path.split(File::SEPARATOR)
a54e24
      File.join(split_path.take_while { |one_dir| one_dir !~ /^#{dir}$/ }[0..-2])
a54e24
    end
a54e24
    private :previous_but_one_dir_to
a54e24
a54e24
    ##
a54e24
    # Detects --install-dir option specified on command line.
a54e24
a54e24
    def opt_install_dir?
a54e24
      @opt_install_dir ||= ARGV.include?('--install-dir') || ARGV.include?('-i')
a54e24
    end
a54e24
    private :opt_install_dir?
a54e24
a54e24
    ##
a54e24
    # Detects --build-root option specified on command line.
a54e24
a54e24
    def opt_build_root?
a54e24
      @opt_build_root ||= ARGV.include?('--build-root')
a54e24
    end
a54e24
    private :opt_build_root?
a54e24
a54e24
    ##
a54e24
    # Tries to detect, if arguments and environment variables suggest that
a54e24
    # 'gem install' is executed from rpmbuild.
a54e24
a54e24
    def rpmbuild?
a54e24
      @rpmbuild ||= ENV['RPM_PACKAGE_NAME'] && (opt_install_dir? || opt_build_root?)
a54e24
    end
a54e24
    private :rpmbuild?
a54e24
a54e24
    ##
a54e24
    # Get enabled SCLs in order of (most) dependent SCL to base SCL
a54e24
a54e24
    def x_scls
a54e24
      @x_scls ||= if ENV['X_SCLS'].kind_of?(String)
a54e24
        ENV['X_SCLS'].split(' ').reverse!
a54e24
      else
a54e24
        []
a54e24
      end
a54e24
    end
a54e24
    private :x_scls
a54e24
a54e24
    ##
a54e24
    # Additional default locations for enabled software collections
a54e24
    # Dependent scls needs to add themselves on $GEM_PATH
a54e24
a54e24
    def default_locations_added_for_scls
a54e24
      if ENV['GEM_PATH']
a54e24
        gem_paths = ENV['GEM_PATH'].split(':')
a54e24
a54e24
        x_scls.each do |scl|
a54e24
          next if scl == '@SCL@'
a54e24
a54e24
          regexp = /#{scl}\/root\/usr\/share\/gems/
a54e24
          scl_gem_path = gem_paths.grep(regexp)[0]
a54e24
          if scl_gem_path
a54e24
            prefix = scl_gem_path.gsub(/\A(.*)#{regexp}\z/, "\\1")
a54e24
            @default_locations["#{scl}_system".to_sym] = "#{prefix}#{scl}/root/usr"
a54e24
            @default_locations["#{scl}_local".to_sym] = "#{prefix}#{scl}/root/usr/local"
a54e24
          end
a54e24
        end
a54e24
      end
a54e24
a54e24
      @default_locations
a54e24
    end
a54e24
    private :default_locations_added_for_scls
a54e24
a54e24
    ##
a54e24
    # SCL prefix genereatd from first detected SCL except own SCL.
a54e24
a54e24
    def scl_prefix
a54e24
      prefix = x_scls.detect {|c| c != '@SCL@'}
a54e24
      prefix = prefix ? prefix + '_': nil
a54e24
      prefix
a54e24
    end
a54e24
    private :scl_prefix
a54e24
a54e24
    ##
a54e24
    # Default gems locations allowed on FHS system (/usr, /usr/share).
a54e24
    # The locations are derived from directories specified during build
a54e24
    # configuration.
a54e24
a54e24
    def default_locations
a54e24
      @default_locations ||= {
a54e24
        :system => previous_but_one_dir_to(RbConfig::CONFIG['vendordir'], RbConfig::CONFIG['RUBY_INSTALL_NAME']),
a54e24
        :local => previous_but_one_dir_to(RbConfig::CONFIG['sitedir'], RbConfig::CONFIG['RUBY_INSTALL_NAME'])
a54e24
      }
a54e24
a54e24
      default_locations_added_for_scls
a54e24
    end
a54e24
a54e24
    ##
a54e24
    # For each location provides set of directories for binaries (:bin_dir)
a54e24
    # platform independent (:gem_dir) and dependent (:ext_dir) files.
a54e24
a54e24
    def default_dirs
a54e24
      @libdir ||= case RUBY_PLATFORM
a54e24
      when 'java'
a54e24
        RbConfig::CONFIG['datadir']
a54e24
      else
a54e24
        RbConfig::CONFIG['libdir']
a54e24
      end
a54e24
a54e24
      @default_dirs ||= default_locations.inject(Hash.new) do |hash, location|
a54e24
        destination, path = location
a54e24
a54e24
        hash[destination] = if path
a54e24
          {
a54e24
            :bin_dir => File.join(path, RbConfig::CONFIG['bindir'].split(File::SEPARATOR).last),
a54e24
            :gem_dir => File.join(path, RbConfig::CONFIG['datadir'].split(File::SEPARATOR).last, 'gems'),
a54e24
            :ext_dir => File.join(path, @libdir.split(File::SEPARATOR).last, 'gems')
a54e24
          }
a54e24
        else
a54e24
          {
a54e24
            :bin_dir => '',
a54e24
            :gem_dir => '',
a54e24
            :ext_dir => ''
a54e24
          }
a54e24
        end
a54e24
a54e24
        hash
a54e24
      end
a54e24
    end
a54e24
a54e24
    ##
a54e24
    # Remove methods we are going to override. This avoids "method redefined;"
a54e24
    # warnings otherwise issued by Ruby.
a54e24
a54e24
    remove_method :operating_system_defaults if method_defined? :operating_system_defaults
a54e24
    remove_method :default_dir if method_defined? :default_dir
a54e24
    remove_method :default_path if method_defined? :default_path
a54e24
    remove_method :default_ext_dir_for if method_defined? :default_ext_dir_for
a54e24
a54e24
    ##
a54e24
    # Regular user installs into user directory, root manages /usr/local.
a54e24
a54e24
    def operating_system_defaults
a54e24
      unless opt_build_root?
a54e24
        options = if Process.uid == 0
a54e24
          "--install-dir=#{Gem.default_dirs[:local][:gem_dir]} --bindir #{Gem.default_dirs[:local][:bin_dir]}"
a54e24
        else
a54e24
          "--user-install --bindir #{File.join [Dir.home, 'bin']}"
a54e24
        end
a54e24
a54e24
        {"gem" => options}
a54e24
      else
a54e24
        {}
a54e24
      end
a54e24
    end
a54e24
a54e24
    ##
a54e24
    # RubyGems default overrides.
a54e24
a54e24
    def default_dir
a54e24
      prefix = scl_prefix
a54e24
      if Gem.default_dirs.key?(:"#{prefix}system")
a54e24
        Gem.default_dirs[:"#{prefix}system"][:gem_dir]
a54e24
      else
a54e24
        Gem.default_dirs[:"system"][:gem_dir]
a54e24
      end
a54e24
    end
a54e24
a54e24
    def default_path
a54e24
      path = default_dirs.collect {|location, paths| paths[:gem_dir]}
a54e24
      path.unshift Gem.user_dir if File.exist? Gem.user_home
a54e24
      path
a54e24
    end
a54e24
a54e24
    def default_ext_dir_for base_dir
a54e24
      dir = if rpmbuild?
a54e24
        prefix = scl_prefix
a54e24
        build_dir = base_dir.chomp Gem.default_dirs[:"#{prefix}system"][:gem_dir]
a54e24
        if build_dir != base_dir
a54e24
          File.join build_dir, Gem.default_dirs[:"#{prefix}system"][:ext_dir]
a54e24
        end
a54e24
      else
a54e24
        dirs = Gem.default_dirs.detect {|location, paths| paths[:gem_dir] == base_dir}
a54e24
        dirs && dirs.last[:ext_dir]
a54e24
      end
a54e24
      dir && File.join(dir, RbConfig::CONFIG['RUBY_INSTALL_NAME'])
a54e24
    end
a54e24
a54e24
    # This method should be available since RubyGems 2.2 until RubyGems 3.0.
a54e24
    # https://github.com/rubygems/rubygems/issues/749
a54e24
    if method_defined? :install_extension_in_lib
a54e24
      remove_method :install_extension_in_lib
a54e24
a54e24
      def install_extension_in_lib
a54e24
        false
a54e24
      end
a54e24
    end
a54e24
  end
a54e24
end