2011-01-18 19:08:49 -05:00
|
|
|
######################################################################
|
|
|
|
# This file is imported from the rubygems project.
|
|
|
|
# DO NOT make modifications in this repo. They _will_ be reverted!
|
|
|
|
# File a patch instead and assign it to Ryan Davis or Eric Hodel.
|
|
|
|
######################################################################
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
require 'rubygems/remote_fetcher'
|
|
|
|
require 'rubygems/user_interaction'
|
2010-04-22 04:24:42 -04:00
|
|
|
require 'rubygems/errors'
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'rubygems/text'
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# SpecFetcher handles metadata updates from remote gem repositories.
|
|
|
|
|
|
|
|
class Gem::SpecFetcher
|
|
|
|
|
|
|
|
include Gem::UserInteraction
|
2011-01-18 19:08:49 -05:00
|
|
|
include Gem::Text
|
|
|
|
|
|
|
|
FILES = {
|
|
|
|
:all => 'specs',
|
|
|
|
:latest => 'latest_specs',
|
|
|
|
:prerelease => 'prerelease_specs',
|
|
|
|
}
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# The SpecFetcher cache dir.
|
|
|
|
|
|
|
|
attr_reader :dir # :nodoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Cache of latest specs
|
|
|
|
|
|
|
|
attr_reader :latest_specs # :nodoc:
|
|
|
|
|
|
|
|
##
|
2009-12-08 02:19:09 -05:00
|
|
|
# Cache of all released specs
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
attr_reader :specs # :nodoc:
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Cache of prerelease specs
|
|
|
|
|
|
|
|
attr_reader :prerelease_specs # :nodoc:
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
@fetcher = nil
|
|
|
|
|
|
|
|
def self.fetcher
|
|
|
|
@fetcher ||= new
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.fetcher=(fetcher) # :nodoc:
|
|
|
|
@fetcher = fetcher
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'fileutils'
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
@dir = File.join Gem.user_home, '.gem', 'specs'
|
|
|
|
@update_cache = File.stat(Gem.user_home).uid == Process.uid
|
|
|
|
|
|
|
|
@specs = {}
|
|
|
|
@latest_specs = {}
|
2009-06-09 17:38:59 -04:00
|
|
|
@prerelease_specs = {}
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
@caches = {
|
|
|
|
:latest => @latest_specs,
|
|
|
|
:prerelease => @prerelease_specs,
|
|
|
|
:all => @specs
|
|
|
|
}
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
@fetcher = Gem::RemoteFetcher.fetcher
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-03-19 23:30:59 -04:00
|
|
|
# Returns the local directory to write +uri+ to.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
def cache_dir(uri)
|
2011-03-09 17:32:29 -05:00
|
|
|
# Correct for windows paths
|
|
|
|
escaped_path = uri.path.sub(/^\/([a-z]):\//i, '/\\1-/')
|
2011-03-01 04:41:32 -05:00
|
|
|
File.join @dir, "#{uri.host}%#{uri.port}", File.dirname(escaped_path)
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Fetch specs matching +dependency+. If +all+ is true, all matching
|
2009-12-08 02:19:09 -05:00
|
|
|
# (released) versions are returned. If +matching_platform+ is
|
|
|
|
# false, all platforms are returned. If +prerelease+ is true,
|
|
|
|
# prerelease versions are included.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-03-09 17:32:29 -05:00
|
|
|
def fetch_with_errors(dependency,
|
|
|
|
all = false,
|
|
|
|
matching_platform = true,
|
|
|
|
prerelease = false)
|
|
|
|
specs_and_sources, errors = find_matching_with_errors(dependency,
|
|
|
|
all,
|
|
|
|
matching_platform,
|
|
|
|
prerelease)
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
ss = specs_and_sources.map do |spec_tuple, source_uri|
|
2008-06-17 18:04:18 -04:00
|
|
|
[fetch_spec(spec_tuple, URI.parse(source_uri)), source_uri]
|
|
|
|
end
|
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
return [ss, errors]
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
def fetch(*args)
|
|
|
|
fetch_with_errors(*args).first
|
|
|
|
end
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
def fetch_spec(spec, source_uri)
|
2011-03-01 04:41:32 -05:00
|
|
|
source_uri = URI.parse source_uri if String === source_uri
|
2008-06-17 18:04:18 -04:00
|
|
|
spec = spec - [nil, 'ruby', '']
|
|
|
|
spec_file_name = "#{spec.join '-'}.gemspec"
|
|
|
|
|
|
|
|
uri = source_uri + "#{Gem::MARSHAL_SPEC_DIR}#{spec_file_name}"
|
|
|
|
|
|
|
|
cache_dir = cache_dir uri
|
|
|
|
|
|
|
|
local_spec = File.join cache_dir, spec_file_name
|
|
|
|
|
|
|
|
if File.exist? local_spec then
|
|
|
|
spec = Gem.read_binary local_spec
|
|
|
|
else
|
|
|
|
uri.path << '.rz'
|
|
|
|
|
|
|
|
spec = @fetcher.fetch_path uri
|
|
|
|
spec = Gem.inflate spec
|
|
|
|
|
|
|
|
if @update_cache then
|
|
|
|
FileUtils.mkdir_p cache_dir
|
|
|
|
|
|
|
|
open local_spec, 'wb' do |io|
|
|
|
|
io.write spec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Investigate setting Gem::Specification#loaded_from to a URI
|
|
|
|
Marshal.load spec
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2009-12-08 02:19:09 -05:00
|
|
|
# Find spec names that match +dependency+. If +all+ is true, all
|
|
|
|
# matching released versions are returned. If +matching_platform+
|
|
|
|
# is false, gems for all platforms are returned.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
def find_matching_with_errors(dependency, all = false, matching_platform = true, prerelease = false)
|
2008-06-17 18:04:18 -04:00
|
|
|
found = {}
|
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
rejected_specs = {}
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
list(all, prerelease).each do |source_uri, specs|
|
2008-06-17 18:04:18 -04:00
|
|
|
found[source_uri] = specs.select do |spec_name, version, spec_platform|
|
2010-04-22 04:24:42 -04:00
|
|
|
if dependency.match?(spec_name, version)
|
|
|
|
if matching_platform and !Gem::Platform.match(spec_platform)
|
|
|
|
pm = (rejected_specs[dependency] ||= Gem::PlatformMismatch.new(spec_name, version))
|
|
|
|
pm.add_platform spec_platform
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
errors = rejected_specs.values
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
specs_and_sources = []
|
|
|
|
|
|
|
|
found.each do |source_uri, specs|
|
|
|
|
uri_str = source_uri.to_s
|
|
|
|
specs_and_sources.push(*specs.map { |spec| [spec, uri_str] })
|
|
|
|
end
|
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
[specs_and_sources, errors]
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_matching(*args)
|
|
|
|
find_matching_with_errors(*args).first
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2011-01-18 19:08:49 -05:00
|
|
|
# Suggests a gem based on the supplied +gem_name+. Returns a string
|
|
|
|
# of the gem name if an approximate match can be found or nil
|
|
|
|
# otherwise. NOTE: for performance reasons only gems which exactly
|
|
|
|
# match the first character of +gem_name+ are considered.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def suggest_gems_from_name gem_name
|
|
|
|
gem_name = gem_name.downcase
|
|
|
|
max = gem_name.size / 2
|
2011-03-01 04:41:32 -05:00
|
|
|
specs = list.values.flatten 1
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
matches = specs.map { |name, version, platform|
|
|
|
|
next unless Gem::Platform.match platform
|
|
|
|
|
|
|
|
distance = levenshtein_distance gem_name, name.downcase
|
|
|
|
|
|
|
|
next if distance >= max
|
|
|
|
|
|
|
|
return [name] if distance == 0
|
|
|
|
|
|
|
|
[name, distance]
|
|
|
|
}.compact
|
|
|
|
|
|
|
|
matches = matches.uniq.sort_by { |name, dist| dist }
|
|
|
|
|
|
|
|
matches.first(5).map { |name, dist| name }
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns a list of gems available for each source in Gem::sources. If
|
2009-12-08 02:19:09 -05:00
|
|
|
# +all+ is true, all released versions are returned instead of only latest
|
2009-06-09 17:38:59 -04:00
|
|
|
# versions. If +prerelease+ is true, include prerelease versions.
|
|
|
|
|
|
|
|
def list(all = false, prerelease = false)
|
|
|
|
# TODO: make type the only argument
|
|
|
|
type = if all
|
|
|
|
:all
|
|
|
|
elsif prerelease
|
|
|
|
:prerelease
|
|
|
|
else
|
|
|
|
:latest
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
list = {}
|
|
|
|
file = FILES[type]
|
|
|
|
cache = @caches[type]
|
2009-12-08 02:19:09 -05:00
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
Gem.sources.each do |source_uri|
|
|
|
|
source_uri = URI.parse source_uri
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
unless cache.include? source_uri
|
|
|
|
cache[source_uri] = load_specs source_uri, file
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
|
|
|
list[source_uri] = cache[source_uri]
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
|
2009-12-08 02:19:09 -05:00
|
|
|
if type == :all
|
|
|
|
list.values.map do |gems|
|
2010-02-21 21:52:35 -05:00
|
|
|
gems.reject! { |g| !g[1] || g[1].prerelease? }
|
2009-12-08 02:19:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
list
|
|
|
|
end
|
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
##
|
|
|
|
# Loads specs in +file+, fetching from +source_uri+ if the on-disk cache is
|
|
|
|
# out of date.
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
def load_specs(source_uri, file)
|
2008-06-25 22:06:00 -04:00
|
|
|
file_name = "#{file}.#{Gem.marshal_version}"
|
|
|
|
spec_path = source_uri + "#{file_name}.gz"
|
|
|
|
cache_dir = cache_dir spec_path
|
|
|
|
local_file = File.join(cache_dir, file_name)
|
|
|
|
loaded = false
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
if File.exist? local_file then
|
2008-06-25 22:06:00 -04:00
|
|
|
spec_dump = @fetcher.fetch_path spec_path, File.mtime(local_file)
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2008-09-25 06:13:50 -04:00
|
|
|
if spec_dump.nil? then
|
2008-06-25 22:06:00 -04:00
|
|
|
spec_dump = Gem.read_binary local_file
|
|
|
|
else
|
|
|
|
loaded = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
spec_dump = @fetcher.fetch_path spec_path
|
2008-06-17 18:04:18 -04:00
|
|
|
loaded = true
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
specs = begin
|
|
|
|
Marshal.load spec_dump
|
|
|
|
rescue ArgumentError
|
|
|
|
spec_dump = @fetcher.fetch_path spec_path
|
|
|
|
loaded = true
|
|
|
|
|
|
|
|
Marshal.load spec_dump
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
if loaded and @update_cache then
|
|
|
|
begin
|
|
|
|
FileUtils.mkdir_p cache_dir
|
|
|
|
|
|
|
|
open local_file, 'wb' do |io|
|
2010-02-21 21:52:35 -05:00
|
|
|
io << spec_dump
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
specs
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|