<commit_before>require 'rubygems'
require 'fileutils'
require 'digest/sha1'

module Maven
  class LocalRepository
    
    attr_reader :tuples

    def initialize(name = '', repository_id = 'rubygems', source_uri = "rubygems.org", local_repository = nil)
      @repository_id = repository_id
      if local_repository.nil?
        if File.exists?("/var/cache/gem-proxy")
          @local_repo = "/var/cache/gem-proxy"
        elsif File.exists?("./target")
          @local_repo = "./target/gem-proxy"
          FileUtils.makedirs(@local_repo)
        else
          @local_repo = File.join(Gem.user_home, ".m2", "repository", "rubygems-test")
        end
      else
        @local_repo = local_repository
      end
      @source_uri = source_uri
      @fetcher = Gem::SpecFetcher.fetcher
    end

    def find(fetcher, dep, prerelease)
      # all = !req.prerelease? since "all == true" excludes prereleases
      # all == true => prerelease == false
      # all == false => only latest !! unless prerelease == true
      fetcher.find_matching(dep, !prerelease, false, prerelease)
    end

    def update(name = '', update = true)
      dep = Gem::Dependency.new(/^#{name}/, Gem::Requirement.default)
      dep.name = '' if dep.name == //
      fetcher = Gem::SpecFetcher.fetcher
      fetcher.instance_variable_set(:@update_cache, update)
      #TODO not sure if this is threadsafe
      @tuples = find(fetcher, dep, false) + find(fetcher, dep, true)
      @fetcher = fetcher
      @last_update = File.new(@fetcher.cache_dir(URI.parse("http://rubygems.org/gems")) + "/specs.#{Gem.marshal_version}").mtime      
    end
    
    def spec(name, version)
      req = Gem::Requirement.new(version)
      dep = Gem::Dependency.new(/^#{name}$/, req)
      
      tuples = find(@fetcher, dep, req.prerelease?)
      unless tuples.empty?
        tuple = tuples.first
        @fetcher.fetch_spec(tuple[0], URI.parse(tuple[1]))
      end
    end

    def to_pom(name, version)
      pom = pom_file(name, version)
      unless File.exists?(pom)
        spec = spec(name, version)
        return nil unless spec
        _spec_to_pom(spec, pom)
      end
    end
    
    def spec_to_pom(spec, pom)
      #TODO spec file to Gem::Specification<commit_msg>finally the update really does an update :-)<commit_after>require 'rubygems'
require 'fileutils'
require 'digest/sha1'

module Maven
  class LocalRepository
    
    attr_reader :tuples

    def initialize(name = '', repository_id = 'rubygems', source_uri = "rubygems.org", local_repository = nil)
      @repository_id = repository_id
      if local_repository.nil?
        if File.exists?("/var/cache/gem-proxy")
          @local_repo = "/var/cache/gem-proxy"
        elsif File.exists?("./target")
          @local_repo = "./target/gem-proxy"
          FileUtils.makedirs(@local_repo)
        else
          @local_repo = File.join(Gem.user_home, ".m2", "repository", "rubygems-test")
        end
      else
        @local_repo = local_repository
      end
      @source_uri = source_uri
      @fetcher = Gem::SpecFetcher.fetcher
    end

    def find(fetcher, dep, prerelease)
      # all = !req.prerelease? since "all == true" excludes prereleases
      # all == true => prerelease == false
      # all == false => only latest !! unless prerelease == true
      fetcher.find_matching(dep, !prerelease, false, prerelease)
    end

    def update(name = '', update = true)
      dep = Gem::Dependency.new(/^#{name}/, Gem::Requirement.default)
      dep.name = '' if dep.name == //
      fetcher = Gem::SpecFetcher.new
      fetcher.instance_variable_set(:@update_cache, update)
      #TODO not sure if this is threadsafe
      @tuples = find(fetcher, dep, false) + find(fetcher, dep, true)
      @fetcher = fetcher
      @last_update = File.new(@fetcher.cache_dir(URI.parse("http://rubygems.org/gems")) + "/specs.#{Gem.marshal_version}").mtime      
    end
    
    def spec(name, version)
      req = Gem::Requirement.new(version)
      dep = Gem::Dependency.new(/^#{name}$/, req)
      
      tuples = find(@fetcher, dep, req.prerelease?)
      unless tuples.empty?
        tuple = tuples.first
        @fetcher.fetch_spec(tuple[0], URI.parse(tuple[1]))
      end
    end

    def to_pom(name, version)
      pom = pom_file(name, version)
      unless File.exists?(pom)
        spec = spec(name, version)
        return nil unless spec
        _spec_to_pom(spec, pom)
      end
    end
    
    def spec_to_pom(spec, pom)
      #TODO spec file to Gem::Specification
{'fraction_non_alphanumeric': 0.12785286948814537, 'fraction_numerical': 0.0017726567693330379, 'mean_word_length': 2.596812749003984, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4759867', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1445, 'n_words': 320}