2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2021-10-10 11:10:49 -04:00
|
|
|
require_relative "text"
|
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
|
2019-12-13 06:19:08 -05:00
|
|
|
include Gem::Text
|
2013-11-21 18:27:30 -05:00
|
|
|
|
|
|
|
FILES = { # :nodoc:
|
2012-11-29 01:52:18 -05:00
|
|
|
:released => 'specs',
|
|
|
|
:latest => 'latest_specs',
|
|
|
|
:prerelease => 'prerelease_specs',
|
2018-10-21 20:27:02 -04:00
|
|
|
}.freeze
|
2012-11-29 01:52:18 -05:00
|
|
|
|
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
|
2021-01-03 20:09:05 -05:00
|
|
|
@update_cache = nil
|
2012-11-29 01:52:18 -05:00
|
|
|
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
|
|
|
|
|
2018-11-21 05:20:47 -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:
|
2018-10-21 20:27:02 -04:00
|
|
|
return Gem::Resolver::IndexSet.new self if 'file' == uri.scheme
|
2014-09-13 23:30:02 -04:00
|
|
|
|
2020-12-22 18:45:19 -05:00
|
|
|
fetch_uri = if uri.host == "rubygems.org"
|
|
|
|
index_uri = uri.dup
|
|
|
|
index_uri.host = "index.rubygems.org"
|
|
|
|
index_uri
|
|
|
|
else
|
|
|
|
uri
|
|
|
|
end
|
|
|
|
|
|
|
|
bundler_api_uri = enforce_trailing_slash(fetch_uri)
|
2013-11-10 12:51:40 -05:00
|
|
|
|
|
|
|
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
|
2020-12-22 18:45:19 -05:00
|
|
|
Gem::Resolver::APISet.new response.uri + "./info/"
|
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-/')
|
2019-11-11 01:03:57 -05:00
|
|
|
escaped_path.tap(&Gem::UNTAINT)
|
2014-09-13 23:30:02 -04:00
|
|
|
|
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?
|
2021-01-03 20:09:05 -05:00
|
|
|
return @update_cache unless @update_cache.nil?
|
|
|
|
@update_cache =
|
2013-10-18 17:56:18 -04:00
|
|
|
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+.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
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
|
|
|
|
2020-12-08 02:33:39 -05:00
|
|
|
source_uri = enforce_trailing_slash(uri) + "#{Gem::MARSHAL_SPEC_DIR}#{spec_file_name}"
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2018-10-21 20:27:02 -04:00
|
|
|
cache_dir = cache_dir source_uri
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
local_spec = File.join cache_dir, spec_file_name
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if File.exist? local_spec
|
2012-11-29 01:52:18 -05:00
|
|
|
spec = Gem.read_binary local_spec
|
|
|
|
spec = Marshal.load(spec) rescue nil
|
|
|
|
return spec if spec
|
|
|
|
end
|
|
|
|
|
2018-10-21 20:27:02 -04:00
|
|
|
source_uri.path << '.rz'
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2018-10-21 20:27:02 -04:00
|
|
|
spec = fetcher.fetch_path source_uri
|
2018-05-17 21:39:13 -04:00
|
|
|
spec = Gem::Util.inflate spec
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if update_cache?
|
2020-12-14 18:32:54 -05:00
|
|
|
require "fileutils"
|
2012-11-29 01:52:18 -05:00
|
|
|
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}"
|
2020-12-08 02:33:39 -05:00
|
|
|
spec_path = enforce_trailing_slash(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
|
|
|
|
|
2020-08-28 04:16:21 -04:00
|
|
|
if update_cache?
|
|
|
|
require "fileutils"
|
|
|
|
FileUtils.mkdir_p cache_dir
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
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
|
2018-10-21 20:27:02 -04:00
|
|
|
fetcher.download spec, uri.to_s, dir
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def pretty_print(q) # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
q.group 2, '[Remote:', ']' do
|
|
|
|
q.breakable
|
|
|
|
q.text @uri.to_s
|
2013-12-07 20:22:39 -05:00
|
|
|
|
2018-10-21 20:27:02 -04:00
|
|
|
if 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
|
|
|
|
|
2019-12-13 06:19:08 -05:00
|
|
|
def typo_squatting?(host, distance_threshold=4)
|
|
|
|
return if @uri.host.nil?
|
2020-12-08 02:33:39 -05:00
|
|
|
levenshtein_distance(@uri.host, host).between? 1, distance_threshold
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def enforce_trailing_slash(uri)
|
|
|
|
uri.merge(uri.path.gsub(/\/+$/, '') + '/')
|
2019-12-13 06:19:08 -05:00
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2019-04-22 07:56:16 -04:00
|
|
|
require_relative 'source/git'
|
|
|
|
require_relative 'source/installed'
|
|
|
|
require_relative 'source/specific_file'
|
|
|
|
require_relative 'source/local'
|
|
|
|
require_relative 'source/lock'
|
|
|
|
require_relative 'source/vendor'
|