<commit_before>require "uri"
require "rubygems/installer"
require "rubygems/spec_fetcher"
require "rubygems/format"
require "digest/sha1"
require "open3"

module Bundler
  module Source
    # TODO: Refactor this class
    class Rubygems
      attr_reader :remotes

      def initialize(options = {})
        @options = options
        @remotes = (options["remotes"] || []).map { |r| normalize_uri(r) }
        @allow_remote = false
        # Hardcode the paths for now
        @installed = {}
        @caches = [ Bundler.app_cache ] + Gem.path.map { |p| File.expand_path("#{p}/cache") }
        @spec_fetch_map = {}
      end

      def remote!
        @allow_remote = true
      end

      def [](spec)
        installed_specs[spec].first ||
        @allow_remote && (
          cached_specs[spec].first  ||
          remote_specs[spec].first)
      end

      def hash
        Rubygems.hash
      end

      def eql?(o)
        Rubygems === o
      end

      alias == eql?

      # Not really needed, but it seems good to implement this method for interface
      # consistency. Source name is mostly used to identify Path & Git sources
      def name
        ":gems"
      end

      def options
        { "remotes" => @remotes.map { |r| r.to_s } }
      end

      def self.from_lock(options)
        s = new(options)
        Array(options["remote"]).each { |r| s.add_remote(r) }
        s
      end

      def to_lock
        out = "GEM\n"
        out << remotes.map {|r| "  remote: #{r}\n" }.join
        out << "  specs:\n"
      end

      def to_s
        remotes = self.remotes.map { |r| r.to_s }.join(', ')
        "rubygems repository #{remotes}"
      end

      def specs
        @specs ||= @allow_remote ? fetch_specs : installed_specs
      end

      def fetch(spec)
        action = @spec_fetch_map[spec.full_name]
        action.call if action
      end

      def install(spec)
        Bundler.ui.info "Installing #{spec.name} (#{spec.version}) "

        path = cached_gem(spec)

        return if @installed[spec.full_name]

        install_path = Bundler.requires_sudo? ? Bundler.tmp : Gem.dir

        installer = Gem::Installer.new path,
          :install_dir         => install_path,
          :ignore_dependencies => true,
          :wrappers            => true,
          :env_shebang         => true,
          :bin_dir             => "#{install_path}/bin"

        installer.install

        # SUDO HAX
        if Bundler.requires_sudo?
          `sudo mkdir -p #{Gem.dir}/gems #{Gem.dir}/specifications`
          `sudo mv #{Bundler.tmp}/gems/#{spec.full_name} #{Gem.dir}/gems/`
          `sudo mv #{Bundler.tmp}/specifications/#{spec.full_name}.gemspec #{Gem.dir}/specifications/`
        end

        spec.loaded_from = "#{Gem.dir}/specifications/#{spec.full_name}.gemspec"<commit_msg>Say "Using gemname (0.1)" if gem is already installed<commit_after>require "uri"
require "rubygems/installer"
require "rubygems/spec_fetcher"
require "rubygems/format"
require "digest/sha1"
require "open3"

module Bundler
  module Source
    # TODO: Refactor this class
    class Rubygems
      attr_reader :remotes

      def initialize(options = {})
        @options = options
        @remotes = (options["remotes"] || []).map { |r| normalize_uri(r) }
        @allow_remote = false
        # Hardcode the paths for now
        @installed = {}
        @caches = [ Bundler.app_cache ] + Gem.path.map { |p| File.expand_path("#{p}/cache") }
        @spec_fetch_map = {}
      end

      def remote!
        @allow_remote = true
      end

      def [](spec)
        installed_specs[spec].first ||
        @allow_remote && (
          cached_specs[spec].first  ||
          remote_specs[spec].first)
      end

      def hash
        Rubygems.hash
      end

      def eql?(o)
        Rubygems === o
      end

      alias == eql?

      # Not really needed, but it seems good to implement this method for interface
      # consistency. Source name is mostly used to identify Path & Git sources
      def name
        ":gems"
      end

      def options
        { "remotes" => @remotes.map { |r| r.to_s } }
      end

      def self.from_lock(options)
        s = new(options)
        Array(options["remote"]).each { |r| s.add_remote(r) }
        s
      end

      def to_lock
        out = "GEM\n"
        out << remotes.map {|r| "  remote: #{r}\n" }.join
        out << "  specs:\n"
      end

      def to_s
        remotes = self.remotes.map { |r| r.to_s }.join(', ')
        "rubygems repository #{remotes}"
      end

      def specs
        @specs ||= @allow_remote ? fetch_specs : installed_specs
      end

      def fetch(spec)
        action = @spec_fetch_map[spec.full_name]
        action.call if action
      end

      def install(spec)
        path = cached_gem(spec)

        if @installed[spec.full_name]
          Bundler.ui.info "Using #{spec.name} (#{spec.version}) "
          return
        else
          Bundler.ui.info "Installing #{spec.name} (#{spec.version}) "
        end

        install_path = Bundler.requires_sudo? ? Bundler.tmp : Gem.dir
        installer = Gem::Installer.new path,
          :install_dir         => install_path,
          :ignore_dependencies => true,
          :wrappers            => true,
          :env_shebang         => true,
          :bin_dir             => "#{install_path}/bin"
        installer.install

        # SUDO HAX
        if Bundler.requires_sudo?
          `sudo mkdir -p #{Gem.dir}/gems #{Gem.dir}/specifications`
          `sudo mv #{Bundler.tmp}/gems/#{spec.full_name} #{Gem.dir}/gems/`
          `sudo mv #{Bundler.tmp}/specifications/#{spec.full_name}.gemspec #{Gem.dir}/specifications/`
        end

        spec.loaded_from = "#{Gem.dir}/specifications/#{spec.full_name}.gemspec"
{'fraction_non_alphanumeric': 0.12231833910034602, 'fraction_numerical': 0.0010380622837370243, 'mean_word_length': 1.9196969696969697, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1739278', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1825, 'n_words': 419}