2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4 # See LICENSE.txt for permissions.
8 require 'rubygems/user_interaction'
9 require 'rubygems/specification'
10 require 'rubygems/spec_fetcher'
13 # The SourceIndex object indexes all the gems available from a
14 # particular source (e.g. a list of gem directories, or a remote
15 # source). A SourceIndex maps a gem full name to a gem
18 # NOTE:: The class used to be named Cache, but that became
19 # confusing when cached source fetchers where introduced. The
20 # constant Gem::Cache is an alias for this class to allow old
21 # YAMLized source index objects to load properly.
23 class Gem::SourceIndex
27 include Gem::UserInteraction
29 attr_reader :gems # :nodoc:
32 # Directories to use to refresh this SourceIndex when calling refresh!
34 attr_accessor :spec_dirs
37 include Gem::UserInteraction
40 # Factory method to construct a source index instance for a given
44 # If supplied, from_installed_gems will act just like
45 # +from_gems_in+. This argument is deprecated and is provided
46 # just for backwards compatibility, and should not generally
50 # SourceIndex instance
52 def from_installed_gems(*deprecated)
54 from_gems_in(*installed_spec_directories)
56 from_gems_in(*deprecated) # HACK warn
61 # Returns a list of directories from Gem.path that contain specifications.
63 def installed_spec_directories
64 Gem.path.collect { |dir| File.join(dir, "specifications") }
68 # Creates a new SourceIndex from the ruby format gem specifications in
71 def from_gems_in(*spec_dirs)
73 source_index.spec_dirs = spec_dirs
78 # Loads a ruby-format specification from +file_name+ and returns the
81 def load_specification(file_name)
83 spec_code = File.read(file_name).untaint
84 gemspec = eval spec_code, binding, file_name
85 if gemspec.is_a?(Gem::Specification)
86 gemspec.loaded_from = file_name
89 alert_warning "File '#{file_name}' does not evaluate to a gem specification"
90 rescue SignalException, SystemExit
92 rescue SyntaxError => e
94 alert_warning spec_code
96 alert_warning "#{e.inspect}\n#{spec_code}"
97 alert_warning "Invalid .gemspec format in '#{file_name}'"
105 # Constructs a source index instance from the provided
109 # [Hash] hash of [Gem name, Gem::Specification] pairs
111 def initialize(specifications={})
112 @gems = specifications
117 # Reconstruct the source index from the specifications in +spec_dirs+.
119 def load_gems_in(*spec_dirs)
122 spec_dirs.reverse_each do |spec_dir|
123 spec_files = Dir.glob File.join(spec_dir, '*.gemspec')
125 spec_files.each do |spec_file|
126 gemspec = self.class.load_specification spec_file.untaint
127 add_spec gemspec if gemspec
135 # Returns an Array specifications for the latest versions of each gem in
139 result = Hash.new { |h,k| h[k] = [] }
142 sort.each do |_, spec|
144 curr_ver = spec.version
145 prev_ver = latest.key?(name) ? latest[name].version : nil
147 next unless prev_ver.nil? or curr_ver >= prev_ver or
148 latest[name].platform != Gem::Platform::RUBY
151 (curr_ver > prev_ver and spec.platform == Gem::Platform::RUBY) then
156 if spec.platform != Gem::Platform::RUBY then
157 result[name].delete_if do |result_spec|
158 result_spec.platform == spec.platform
165 result.values.flatten
169 # Add a gem specification to the source index.
171 def add_spec(gem_spec)
172 @gems[gem_spec.full_name] = gem_spec
176 # Add gem specifications to the source index.
178 def add_specs(*gem_specs)
179 gem_specs.each do |spec|
185 # Remove a gem specification named +full_name+.
187 def remove_spec(full_name)
188 @gems.delete(full_name)
192 # Iterate over the specifications in the source index.
194 def each(&block) # :yields: gem.full_name, gem
199 # The gem specification given a full gem spec name.
201 def specification(full_name)
206 # The signature for the source index. Changes in the signature indicate a
207 # change in the index.
210 require 'rubygems/digest/sha2'
212 Gem::SHA256.new.hexdigest(@gems.keys.sort.join(',')).to_s
216 # The signature for the given gem specification.
218 def gem_signature(gem_full_name)
219 require 'rubygems/digest/sha2'
221 Gem::SHA256.new.hexdigest(@gems[gem_full_name].to_yaml).to_s
230 # Find a gem by an exact match on the short name.
232 def find_name(gem_name, version_requirement = Gem::Requirement.default)
233 search(/^#{gem_name}$/, version_requirement)
237 # Search for a gem by Gem::Dependency +gem_pattern+. If +only_platform+
238 # is true, only gems matching Gem::Platform.local will be returned. An
239 # Array of matching Gem::Specification objects is returned.
241 # For backwards compatibility, a String or Regexp pattern may be passed as
242 # +gem_pattern+, and a Gem::Requirement for +platform_only+. This
243 # behavior is deprecated and will be removed.
245 def search(gem_pattern, platform_only = false)
246 version_requirement = nil
247 only_platform = false
249 case gem_pattern # TODO warn after 2008/03, remove three months after
251 version_requirement = platform_only || Gem::Requirement.default
252 when Gem::Dependency then
253 only_platform = platform_only
254 version_requirement = gem_pattern.version_requirements
255 gem_pattern = if Regexp === gem_pattern.name then
257 elsif gem_pattern.name.empty? then
260 /^#{Regexp.escape gem_pattern.name}$/
263 version_requirement = platform_only || Gem::Requirement.default
264 gem_pattern = /#{gem_pattern}/i
267 unless Gem::Requirement === version_requirement then
268 version_requirement = Gem::Requirement.create version_requirement
271 specs = @gems.values.select do |spec|
272 spec.name =~ gem_pattern and
273 version_requirement.satisfied_by? spec.version
276 if only_platform then
277 specs = specs.select do |spec|
278 Gem::Platform.match spec.platform
282 specs.sort_by { |s| s.sort_obj }
286 # Replaces the gems in the source index from specifications in the
287 # directories this source index was created from. Raises an exception if
288 # this source index wasn't created from a directory (via from_gems_in or
289 # from_installed_gems, or having spec_dirs set).
292 raise 'source index not created from disk' if @spec_dirs.nil?
293 load_gems_in(*@spec_dirs)
297 # Returns an Array of Gem::Specifications that are not up to date.
302 latest_specs.each do |local|
303 dependency = Gem::Dependency.new local.name, ">= #{local.version}"
306 fetcher = Gem::SpecFetcher.fetcher
307 remotes = fetcher.find_matching dependency
308 remotes = remotes.map { |(name, version,_),_| version }
309 rescue Gem::RemoteFetcher::FetchError => e
310 raise unless fetcher.warn_legacy e do
311 require 'rubygems/source_info_cache'
313 specs = Gem::SourceInfoCache.search_with_source dependency, true
315 remotes = specs.map { |spec,| spec.version }
319 latest = remotes.sort.last
321 outdateds << local.name if latest and local.version < latest
328 # Updates this SourceIndex from +source_uri+. If +all+ is false, only the
329 # latest gems are fetched.
331 def update(source_uri, all)
332 source_uri = URI.parse source_uri unless URI::Generic === source_uri
333 source_uri.path += '/' unless source_uri.path =~ /\/$/
335 use_incremental = false
338 gem_names = fetch_quick_index source_uri, all
339 remove_extra gem_names
340 missing_gems = find_missing gem_names
342 return false if missing_gems.size.zero?
344 say "Missing metadata for #{missing_gems.size} gems" if
345 missing_gems.size > 0 and Gem.configuration.really_verbose
347 use_incremental = missing_gems.size <= Gem.configuration.bulk_threshold
348 rescue Gem::OperationNotSupportedError => ex
349 alert_error "Falling back to bulk fetch: #{ex.message}" if
350 Gem.configuration.really_verbose
351 use_incremental = false
354 if use_incremental then
355 update_with_missing(source_uri, missing_gems)
357 new_index = fetch_bulk_index(source_uri)
358 @gems.replace(new_index.gems)
364 def ==(other) # :nodoc:
365 self.class === other and @gems == other.gems
375 require 'rubygems/remote_fetcher'
377 Gem::RemoteFetcher.fetcher
380 def fetch_index_from(source_uri)
384 Marshal.#{Gem.marshal_version}.Z
385 Marshal.#{Gem.marshal_version}
390 indexes.each do |name|
392 index = source_uri + name
394 spec_data = fetcher.fetch_path index
395 spec_data = unzip(spec_data) if name =~ /\.Z$/
397 if name =~ /Marshal/ then
398 return Marshal.load(spec_data)
400 return YAML.load(spec_data)
403 if Gem.configuration.really_verbose then
404 alert_error "Unable to fetch #{name}: #{e.message}"
414 def fetch_bulk_index(source_uri)
415 say "Bulk updating Gem source index for: #{source_uri}" if
416 Gem.configuration.verbose
418 index = fetch_index_from(source_uri)
420 raise Gem::RemoteSourceException,
421 "Error fetching remote gem cache: #{@fetch_error}"
428 # Get the quick index needed for incremental updates.
430 def fetch_quick_index(source_uri, all)
431 index = all ? 'index' : 'latest_index'
433 zipped_index = fetcher.fetch_path source_uri + "quick/#{index}.rz"
435 unzip(zipped_index).split("\n")
436 rescue ::Exception => e
438 say "Latest index not found, using quick index" if
439 Gem.configuration.really_verbose
441 fetch_quick_index source_uri, true
443 raise Gem::OperationNotSupportedError,
444 "No quick index found: #{e.message}"
449 # Make a list of full names for all the missing gemspecs.
451 def find_missing(spec_names)
452 unless defined? @originals then
454 each do |full_name, spec|
455 @originals[spec.original_name] = spec
459 spec_names.find_all { |full_name|
460 @originals[full_name].nil?
464 def remove_extra(spec_names)
465 dictionary = spec_names.inject({}) { |h, k| h[k] = true; h }
467 remove_spec name unless dictionary.include? spec.original_name
472 # Unzip the given string.
480 # Tries to fetch Marshal representation first, then YAML
482 def fetch_single_spec(source_uri, spec_name)
486 marshal_uri = source_uri + "quick/Marshal.#{Gem.marshal_version}/#{spec_name}.gemspec.rz"
487 zipped = fetcher.fetch_path marshal_uri
488 return Marshal.load(unzip(zipped))
492 if Gem.configuration.really_verbose then
493 say "unable to fetch marshal gemspec #{marshal_uri}: #{ex.class} - #{ex}"
498 yaml_uri = source_uri + "quick/#{spec_name}.gemspec.rz"
499 zipped = fetcher.fetch_path yaml_uri
500 return YAML.load(unzip(zipped))
503 if Gem.configuration.really_verbose then
504 say "unable to fetch YAML gemspec #{yaml_uri}: #{ex.class} - #{ex}"
512 # Update the cached source index with the missing names.
514 def update_with_missing(source_uri, missing_names)
515 progress = ui.progress_reporter(missing_names.size,
516 "Updating metadata for #{missing_names.size} gems from #{source_uri}")
517 missing_names.each do |spec_name|
518 gemspec = fetch_single_spec(source_uri, spec_name)
520 ui.say "Failed to download spec #{spec_name} from #{source_uri}:\n" \
521 "\t#{@fetch_error.message}"
524 progress.updated spec_name
538 # Cache is an alias for SourceIndex to allow older YAMLized source index
539 # objects to load properly.