class Gem::Mirror

Constants

DEFAULT_TO
DEFAULT_URI
RUBY
SPECS_FILES
VERSION

Public Class Methods

new(from = DEFAULT_URI, to = DEFAULT_TO, parallelism = nil, retries = nil, skiperror = nil) click to toggle source
# File lib/rubygems/mirror.rb, line 17
def initialize(from = DEFAULT_URI, to = DEFAULT_TO, parallelism = nil, retries = nil, skiperror = nil)
  @from, @to = from, to
  @fetcher = Fetcher.new :retries => retries, :skiperror => skiperror
  @pool = Pool.new(parallelism || 10)
end

Public Instance Methods

delete_gems() { || ... } click to toggle source
# File lib/rubygems/mirror.rb, line 112
def delete_gems
  gems_to_delete.each do |g|
    @pool.job do
      File.delete(to('gems', g))
      yield if block_given?
    end
  end

  @pool.run_til_done
end
existing_gems() click to toggle source
# File lib/rubygems/mirror.rb, line 72
def existing_gems
  Dir.glob(to('gems', '*.gem'), File::FNM_DOTMATCH).entries.map { |f| File.basename(f) }
end
existing_gemspecs() click to toggle source
# File lib/rubygems/mirror.rb, line 76
def existing_gemspecs
  Dir[to("quick/Marshal.#{Gem.marshal_version}", '*.rz')].entries.map { |f| File.basename(f) }
end
from(*args) click to toggle source
# File lib/rubygems/mirror.rb, line 23
def from(*args)
  File.join(@from, *args)
end
gems() click to toggle source
# File lib/rubygems/mirror.rb, line 41
def gems
  gems = []

  SPECS_FILES.each do |sf|
    update_specs unless File.exist?(to(sf))

    gems += Marshal.load(File.read(to(sf)))
  end

  if ENV["RUBYGEMS_MIRROR_ONLY_LATEST"].to_s.upcase != "TRUE"
    gems.map! do |name, ver, plat|
      # If the platform is ruby, it is not in the gem name
      "#{name}-#{ver}#{"-#{plat}" unless plat == RUBY}.gem"
    end
  else
    latest_gems = {}

    gems.each do |name, ver, plat|
      next if ver.prerelease?
      next unless plat == RUBY
      latest_gems[name] = ver
    end

    gems = latest_gems.map do |name, ver|
      "#{name}-#{ver}.gem"
    end
  end

  gems
end
gems_to_delete() click to toggle source
# File lib/rubygems/mirror.rb, line 88
def gems_to_delete
  existing_gems - gems
end
gems_to_fetch() click to toggle source
# File lib/rubygems/mirror.rb, line 80
def gems_to_fetch
  gems - existing_gems
end
gemspecs_to_fetch() click to toggle source
# File lib/rubygems/mirror.rb, line 84
def gemspecs_to_fetch
  gems.map { |g| "#{g}spec.rz" } - existing_gemspecs
end
to(*args) click to toggle source
# File lib/rubygems/mirror.rb, line 27
def to(*args)
  File.join(@to, *args)
end
update() click to toggle source
# File lib/rubygems/mirror.rb, line 123
def update
  update_specs
  update_gems
  cleanup_gems
end
update_gems() { || ... } click to toggle source
# File lib/rubygems/mirror.rb, line 92
def update_gems
  gems_to_fetch.each do |g|
    @pool.job do
      @fetcher.fetch(from('gems', g), to('gems', g))
      yield if block_given?
    end
  end

  if ENV["RUBYGEMS_MIRROR_ONLY_LATEST"].to_s.upcase != "TRUE"
    gemspecs_to_fetch.each do |g_spec|
      @pool.job do
        @fetcher.fetch(from("quick/Marshal.#{Gem.marshal_version}", g_spec), to("quick/Marshal.#{Gem.marshal_version}", g_spec))
        yield if block_given?
      end
    end
  end

  @pool.run_til_done
end
update_specs() click to toggle source
# File lib/rubygems/mirror.rb, line 31
def update_specs
  SPECS_FILES.each do |sf|
    sfz = "#{sf}.gz"

    specz = to(sfz)
    @fetcher.fetch(from(sfz), specz)
    open(to(sf), 'wb') { |f| f << Gem.gunzip(File.read(specz)) }
  end
end