2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2017-10-07 21:32:18 -04:00
|
|
|
autoload :FileUtils, 'fileutils'
|
|
|
|
autoload :URI, 'uri'
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
##
|
|
|
|
# A Source knows how to list and fetch gems from a RubyGems marshal index.
|
|
|
|
#
|
|
|
|
# There are other Source subclasses for installed gems, local gems, the
|
|
|
|
# bundler dependency API and so-forth.
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
class Gem::Source
|
2013-11-21 18:27:30 -05:00
|
|
|
|
|
|
|
include Comparable
|
|
|
|
|
|
|
|
FILES = { # :nodoc:
|
2012-11-29 01:52:18 -05:00
|
|
|
:released => 'specs',
|
|
|
|
:latest => 'latest_specs',
|
|
|
|
:prerelease => 'prerelease_specs',
|
|
|
|
}
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
##
|
|
|
|
# The URI this source will fetch gems from.
|
|
|
|
|
|
|
|
attr_reader :uri
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new Source which will use the index located at +uri+.
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def initialize(uri)
|
2014-12-06 19:53:01 -05:00
|
|
|
begin
|
|
|
|
unless uri.kind_of? URI
|
|
|
|
uri = URI.parse(uri.to_s)
|
|
|
|
end
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
raise if Gem::Source == self.class
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@uri = uri
|
|
|
|
@api_uri = nil
|
|
|
|
end
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
##
|
|
|
|
# Use an SRV record on the host to look up the true endpoint for the index.
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
def api_uri # :nodoc:
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/remote_fetcher'
|
|
|
|
@api_uri ||= Gem::RemoteFetcher.fetcher.api_endpoint uri
|
|
|
|
end
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
##
|
|
|
|
# Sources are ordered by installation preference.
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def <=>(other)
|
2013-09-14 04:59:02 -04:00
|
|
|
case other
|
|
|
|
when Gem::Source::Installed,
|
|
|
|
Gem::Source::Local,
|
2013-11-30 18:27:52 -05:00
|
|
|
Gem::Source::Lock,
|
2013-11-18 19:34:13 -05:00
|
|
|
Gem::Source::SpecificFile,
|
|
|
|
Gem::Source::Git,
|
|
|
|
Gem::Source::Vendor then
|
2013-09-14 04:59:02 -04:00
|
|
|
-1
|
|
|
|
when Gem::Source then
|
|
|
|
if !@uri
|
|
|
|
return 0 unless other.uri
|
|
|
|
return 1
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
return -1 if !other.uri
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
# Returning 1 here ensures that when sorting a list of sources, the
|
|
|
|
# original ordering of sources supplied by the user is preserved.
|
|
|
|
return 1 unless @uri.to_s == other.uri.to_s
|
|
|
|
|
|
|
|
0
|
2013-09-14 04:59:02 -04:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
def == other # :nodoc:
|
2013-10-18 17:56:18 -04:00
|
|
|
self.class === other and @uri == other.uri
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
alias_method :eql?, :== # :nodoc:
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
##
|
|
|
|
# Returns a Set that can fetch specifications from this source.
|
|
|
|
|
|
|
|
def dependency_resolver_set # :nodoc:
|
2014-09-13 23:30:02 -04:00
|
|
|
return Gem::Resolver::IndexSet.new self if 'file' == api_uri.scheme
|
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
bundler_api_uri = api_uri + './api/v1/dependencies'
|
|
|
|
|
|
|
|
begin
|
|
|
|
fetcher = Gem::RemoteFetcher.fetcher
|
2014-09-13 23:30:02 -04:00
|
|
|
response = fetcher.fetch_path bundler_api_uri, nil, true
|
2013-11-10 12:51:40 -05:00
|
|
|
rescue Gem::RemoteFetcher::FetchError
|
2013-11-18 19:34:13 -05:00
|
|
|
Gem::Resolver::IndexSet.new self
|
2013-11-10 12:51:40 -05:00
|
|
|
else
|
2014-09-13 23:30:02 -04:00
|
|
|
if response.respond_to? :uri then
|
|
|
|
Gem::Resolver::APISet.new response.uri
|
|
|
|
else
|
|
|
|
Gem::Resolver::APISet.new bundler_api_uri
|
|
|
|
end
|
2013-11-10 12:51:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
def hash # :nodoc:
|
2012-11-29 01:52:18 -05:00
|
|
|
@uri.hash
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns the local directory to write +uri+ to.
|
|
|
|
|
|
|
|
def cache_dir(uri)
|
|
|
|
# Correct for windows paths
|
|
|
|
escaped_path = uri.path.sub(/^\/([a-z]):\//i, '/\\1-/')
|
2014-09-13 23:30:02 -04:00
|
|
|
escaped_path.untaint
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
File.join Gem.spec_cache_dir, "#{uri.host}%#{uri.port}", File.dirname(escaped_path)
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
##
|
|
|
|
# Returns true when it is possible and safe to update the cache directory.
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def update_cache?
|
2013-10-18 17:56:18 -04:00
|
|
|
@update_cache ||=
|
|
|
|
begin
|
|
|
|
File.stat(Gem.user_home).uid == Process.uid
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
false
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
##
|
|
|
|
# Fetches a specification for the given +name_tuple+.
|
|
|
|
|
|
|
|
def fetch_spec name_tuple
|
2012-11-29 01:52:18 -05:00
|
|
|
fetcher = Gem::RemoteFetcher.fetcher
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
spec_file_name = name_tuple.spec_name
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
uri = api_uri + "#{Gem::MARSHAL_SPEC_DIR}#{spec_file_name}"
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
spec = Marshal.load(spec) rescue nil
|
|
|
|
return spec if spec
|
|
|
|
end
|
|
|
|
|
|
|
|
uri.path << '.rz'
|
|
|
|
|
|
|
|
spec = fetcher.fetch_path uri
|
2018-05-17 21:39:13 -04:00
|
|
|
spec = Gem::Util.inflate spec
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
if update_cache? then
|
|
|
|
FileUtils.mkdir_p cache_dir
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open local_spec, 'wb' do |io|
|
2012-11-29 01:52:18 -05:00
|
|
|
io.write spec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Investigate setting Gem::Specification#loaded_from to a URI
|
|
|
|
Marshal.load spec
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Loads +type+ kind of specs fetching from +@uri+ if the on-disk cache is
|
|
|
|
# out of date.
|
|
|
|
#
|
|
|
|
# +type+ is one of the following:
|
|
|
|
#
|
|
|
|
# :released => Return the list of all released specs
|
|
|
|
# :latest => Return the list of only the highest version of each gem
|
|
|
|
# :prerelease => Return the list of all prerelease only specs
|
|
|
|
#
|
|
|
|
|
|
|
|
def load_specs(type)
|
|
|
|
file = FILES[type]
|
|
|
|
fetcher = Gem::RemoteFetcher.fetcher
|
|
|
|
file_name = "#{file}.#{Gem.marshal_version}"
|
2013-11-18 19:34:13 -05:00
|
|
|
spec_path = api_uri + "#{file_name}.gz"
|
2012-11-29 01:52:18 -05:00
|
|
|
cache_dir = cache_dir spec_path
|
|
|
|
local_file = File.join(cache_dir, file_name)
|
|
|
|
retried = false
|
|
|
|
|
|
|
|
FileUtils.mkdir_p cache_dir if update_cache?
|
|
|
|
|
2012-12-06 00:13:08 -05:00
|
|
|
spec_dump = fetcher.cache_update_path spec_path, local_file, update_cache?
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
Gem::NameTuple.from_list Marshal.load(spec_dump)
|
|
|
|
rescue ArgumentError
|
|
|
|
if update_cache? && !retried
|
|
|
|
FileUtils.rm local_file
|
|
|
|
retried = true
|
|
|
|
retry
|
|
|
|
else
|
|
|
|
raise Gem::Exception.new("Invalid spec cache file in #{local_file}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
##
|
|
|
|
# Downloads +spec+ and writes it to +dir+. See also
|
|
|
|
# Gem::RemoteFetcher#download.
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def download(spec, dir=Dir.pwd)
|
|
|
|
fetcher = Gem::RemoteFetcher.fetcher
|
2013-11-18 19:34:13 -05:00
|
|
|
fetcher.download spec, api_uri.to_s, dir
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
def pretty_print q # :nodoc:
|
|
|
|
q.group 2, '[Remote:', ']' do
|
|
|
|
q.breakable
|
|
|
|
q.text @uri.to_s
|
2013-12-07 20:22:39 -05:00
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
if api = api_uri
|
2013-12-07 20:22:39 -05:00
|
|
|
q.breakable
|
|
|
|
q.text 'API URI: '
|
2013-11-21 18:27:30 -05:00
|
|
|
q.text api.to_s
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
require 'rubygems/source/git'
|
2013-09-14 04:59:02 -04:00
|
|
|
require 'rubygems/source/installed'
|
|
|
|
require 'rubygems/source/specific_file'
|
|
|
|
require 'rubygems/source/local'
|
2013-11-30 18:27:52 -05:00
|
|
|
require 'rubygems/source/lock'
|
2013-10-17 20:41:36 -04:00
|
|
|
require 'rubygems/source/vendor'
|