Skip to content

Instantly share code, notes, and snippets.

Created June 2, 2011 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/1004681 to your computer and use it in GitHub Desktop.
Save anonymous/1004681 to your computer and use it in GitHub Desktop.
diff --git a/lib/rubygems/source_index.rb b/lib/rubygems/source_index.rb
index f9b8ea0..0cc7762 100644
--- a/lib/rubygems/source_index.rb
+++ b/lib/rubygems/source_index.rb
@@ -4,14 +4,8 @@
# See LICENSE.txt for permissions.
#++
-require 'rubygems/user_interaction'
require 'rubygems/specification'
-
-# :stopdoc:
-module Gem
- autoload :SpecFetcher, 'rubygems/spec_fetcher'
-end
-# :startdoc:
+require 'rubygems/deprecate'
##
# The SourceIndex object indexes all the gems available from a
@@ -28,8 +22,6 @@ class Gem::SourceIndex
include Enumerable
- include Gem::UserInteraction
-
attr_reader :gems # :nodoc:
##
@@ -37,107 +29,86 @@ class Gem::SourceIndex
attr_accessor :spec_dirs
- class << self
- include Gem::UserInteraction
-
- ##
- # Factory method to construct a source index instance for a given
- # path.
- #
- # deprecated::
- # If supplied, from_installed_gems will act just like
- # +from_gems_in+. This argument is deprecated and is provided
- # just for backwards compatibility, and should not generally
- # be used.
- #
- # return::
- # SourceIndex instance
-
- def from_installed_gems(*deprecated)
- if deprecated.empty?
- from_gems_in(*installed_spec_directories)
- else
- from_gems_in(*deprecated) # HACK warn
- end
- end
-
- ##
- # Returns a list of directories from Gem.path that contain specifications.
-
- def installed_spec_directories
- Gem.path.collect { |dir| File.join(dir, "specifications") }
- end
-
- ##
- # Creates a new SourceIndex from the ruby format gem specifications in
- # +spec_dirs+.
+ ##
+ # Factory method to construct a source index instance for a given
+ # path.
+ #
+ # deprecated::
+ # If supplied, from_installed_gems will act just like
+ # +from_gems_in+. This argument is deprecated and is provided
+ # just for backwards compatibility, and should not generally
+ # be used.
+ #
+ # return::
+ # SourceIndex instance
- def from_gems_in(*spec_dirs)
- source_index = new
- source_index.spec_dirs = spec_dirs
- source_index.refresh!
+ def self.from_installed_gems(*deprecated)
+ if deprecated.empty?
+ from_gems_in(*installed_spec_directories)
+ else
+ warn "NOTE: from_installed_gems(arg) is deprecated. From #{caller.first}"
+ from_gems_in(*deprecated) # HACK warn
end
+ end
- ##
- # Loads a ruby-format specification from +file_name+ and returns the
- # loaded spec.
+ ##
+ # Returns a list of directories from Gem.path that contain specifications.
- def load_specification(file_name)
- return nil unless file_name and File.exist? file_name
+ def self.installed_spec_directories
+ # TODO: move to Gem::Utils
+ Gem.path.collect { |dir| File.join(dir, "specifications") }
+ end
- spec_code = if defined? Encoding then
- File.read file_name, :encoding => 'UTF-8'
- else
- File.read file_name
- end.untaint
+ ##
+ # Creates a new SourceIndex from the ruby format gem specifications in
+ # +spec_dirs+.
- begin
- gemspec = eval spec_code, binding, file_name
+ def self.from_gems_in(*spec_dirs)
+ new spec_dirs
+ end
- if gemspec.is_a?(Gem::Specification)
- gemspec.loaded_from = file_name
- return gemspec
- end
- alert_warning "File '#{file_name}' does not evaluate to a gem specification"
- rescue SignalException, SystemExit
- raise
- rescue SyntaxError => e
- alert_warning e
- alert_warning spec_code
- rescue Exception => e
- alert_warning "#{e.inspect}\n#{spec_code}"
- alert_warning "Invalid .gemspec format in '#{file_name}'"
- end
+ ##
+ # Loads a ruby-format specification from +file_name+ and returns the
+ # loaded spec.
- return nil
+ def self.load_specification(file_name)
+ Deprecate.skip_during do
+ Gem::Specification.load Gem::Path.new(file_name)
end
-
end
##
# Constructs a source index instance from the provided specifications, which
# is a Hash of gem full names and Gem::Specifications.
- #--
- # TODO merge @gems and @prerelease_gems and provide a separate method
- # #prerelease_gems
- def initialize(specifications={})
+ def initialize specs_or_dirs = []
@gems = {}
- specifications.each{ |full_name, spec| add_spec spec }
@spec_dirs = nil
+
+ case specs_or_dirs
+ when Hash then
+ specs_or_dirs.each do |full_name, spec|
+ add_spec spec
+ end
+ when Array, String then
+ self.spec_dirs = Array(specs_or_dirs)
+ refresh!
+ else
+ arg = specs_or_dirs.inspect
+ warn "NOTE: SourceIndex.new(#{arg}) is deprecated; From #{caller.first}."
+ end
end
- # TODO: remove method
def all_gems
- @gems
+ gems
end
def prerelease_gems
- @gems.reject{ |name, gem| !gem.version.prerelease? }
+ @gems.reject { |name, gem| !gem.version.prerelease? }
end
def released_gems
- @gems.reject{ |name, gem| gem.version.prerelease? }
+ @gems.reject { |name, gem| gem.version.prerelease? }
end
##
@@ -147,10 +118,12 @@ class Gem::SourceIndex
@gems.clear
spec_dirs.reverse_each do |spec_dir|
- spec_files = Dir.glob File.join(spec_dir, '*.gemspec')
+ spec_files = Dir[File.join(spec_dir, "*.gemspec")]
spec_files.each do |spec_file|
- gemspec = self.class.load_specification spec_file.untaint
+ gemspec = Deprecate.skip_during do
+ Gem::Specification.load spec_file
+ end
add_spec gemspec if gemspec
end
end
@@ -162,7 +135,7 @@ class Gem::SourceIndex
# Returns an Array specifications for the latest released versions
# of each gem in this index.
- def latest_specs
+ def latest_specs(include_prerelease=false)
result = Hash.new { |h,k| h[k] = [] }
latest = {}
@@ -171,7 +144,7 @@ class Gem::SourceIndex
curr_ver = spec.version
prev_ver = latest.key?(name) ? latest[name].version : nil
- next if curr_ver.prerelease?
+ next if !include_prerelease && curr_ver.prerelease?
next unless prev_ver.nil? or curr_ver >= prev_ver or
latest[name].platform != Gem::Platform::RUBY
@@ -190,8 +163,6 @@ class Gem::SourceIndex
result[name] << spec
end
- # TODO: why is this a hash while @gems is an array? Seems like
- # structural similarity would be good.
result.values.flatten
end
@@ -275,9 +246,12 @@ class Gem::SourceIndex
##
# Find a gem by an exact match on the short name.
- def find_name(gem_name, version_requirement = Gem::Requirement.default)
- dep = Gem::Dependency.new gem_name, version_requirement
- search dep
+ def find_name(gem_name, requirement = Gem::Requirement.default)
+ dep = Gem::Dependency.new gem_name, requirement
+
+ Deprecate.skip_during do
+ search dep
+ end
end
##
@@ -289,9 +263,9 @@ class Gem::SourceIndex
# +gem_pattern+, and a Gem::Requirement for +platform_only+. This
# behavior is deprecated and will be removed.
- def search(gem_pattern, platform_only = false)
- version_requirement = nil
- only_platform = false
+ def search(gem_pattern, platform_or_requirement = false)
+ requirement = nil
+ only_platform = false # FIX: WTF is this?!?
# TODO - Remove support and warning for legacy arguments after 2008/11
unless Gem::Dependency === gem_pattern
@@ -300,10 +274,11 @@ class Gem::SourceIndex
case gem_pattern
when Regexp then
- version_requirement = platform_only || Gem::Requirement.default
+ requirement = platform_or_requirement || Gem::Requirement.default
when Gem::Dependency then
- only_platform = platform_only
- version_requirement = gem_pattern.requirement
+ only_platform = platform_or_requirement
+ requirement = gem_pattern.requirement
+
gem_pattern = if Regexp === gem_pattern.name then
gem_pattern.name
elsif gem_pattern.name.empty? then
@@ -312,17 +287,17 @@ class Gem::SourceIndex
/^#{Regexp.escape gem_pattern.name}$/
end
else
- version_requirement = platform_only || Gem::Requirement.default
+ requirement = platform_or_requirement || Gem::Requirement.default
gem_pattern = /#{gem_pattern}/i
end
- unless Gem::Requirement === version_requirement then
- version_requirement = Gem::Requirement.create version_requirement
+ unless Gem::Requirement === requirement then
+ requirement = Gem::Requirement.create requirement
end
- specs = all_gems.values.select do |spec|
+ specs = @gems.values.select do |spec|
spec.name =~ gem_pattern and
- version_requirement.satisfied_by? spec.version
+ requirement.satisfied_by? spec.version
end
if only_platform then
@@ -354,19 +329,9 @@ class Gem::SourceIndex
latest_specs.each do |local|
dependency = Gem::Dependency.new local.name, ">= #{local.version}"
- begin
- fetcher = Gem::SpecFetcher.fetcher
- remotes = fetcher.find_matching dependency
- remotes = remotes.map { |(name, version,_),_| version }
- rescue Gem::RemoteFetcher::FetchError => e
- raise unless fetcher.warn_legacy e do
- require 'rubygems/source_info_cache'
-
- specs = Gem::SourceInfoCache.search_with_source dependency, true
-
- remotes = specs.map { |spec,| spec.version }
- end
- end
+ fetcher = Gem::SpecFetcher.fetcher
+ remotes = fetcher.find_matching dependency
+ remotes = remotes.map { |(_, version, _), _| version }
latest = remotes.sort.last
@@ -376,43 +341,6 @@ class Gem::SourceIndex
outdateds
end
- ##
- # Updates this SourceIndex from +source_uri+. If +all+ is false, only the
- # latest gems are fetched.
-
- def update(source_uri, all)
- source_uri = URI.parse source_uri unless URI::Generic === source_uri
- source_uri.path += '/' unless source_uri.path =~ /\/$/
-
- use_incremental = false
-
- begin
- gem_names = fetch_quick_index source_uri, all
- remove_extra gem_names
- missing_gems = find_missing gem_names
-
- return false if missing_gems.size.zero?
-
- say "Missing metadata for #{missing_gems.size} gems" if
- missing_gems.size > 0 and Gem.configuration.really_verbose
-
- use_incremental = missing_gems.size <= Gem.configuration.bulk_threshold
- rescue Gem::OperationNotSupportedError => ex
- alert_error "Falling back to bulk fetch: #{ex.message}" if
- Gem.configuration.really_verbose
- use_incremental = false
- end
-
- if use_incremental then
- update_with_missing(source_uri, missing_gems)
- else
- new_index = fetch_bulk_index(source_uri)
- @gems.replace(new_index.gems)
- end
-
- true
- end
-
def ==(other) # :nodoc:
self.class === other and @gems == other.gems
end
@@ -420,167 +348,6 @@ class Gem::SourceIndex
def dump
Marshal.dump(self)
end
-
- private
-
- def fetcher
- require 'rubygems/remote_fetcher'
-
- Gem::RemoteFetcher.fetcher
- end
-
- def fetch_index_from(source_uri)
- @fetch_error = nil
-
- indexes = %W[
- Marshal.#{Gem.marshal_version}.Z
- Marshal.#{Gem.marshal_version}
- yaml.Z
- yaml
- ]
-
- indexes.each do |name|
- spec_data = nil
- index = source_uri + name
- begin
- spec_data = fetcher.fetch_path index
- spec_data = unzip(spec_data) if name =~ /\.Z$/
-
- if name =~ /Marshal/ then
- return Marshal.load(spec_data)
- else
- return YAML.load(spec_data)
- end
- rescue => e
- if Gem.configuration.really_verbose then
- alert_error "Unable to fetch #{name}: #{e.message}"
- end
-
- @fetch_error = e
- end
- end
-
- nil
- end
-
- def fetch_bulk_index(source_uri)
- say "Bulk updating Gem source index for: #{source_uri}" if
- Gem.configuration.verbose
-
- index = fetch_index_from(source_uri)
- if index.nil? then
- raise Gem::RemoteSourceException,
- "Error fetching remote gem cache: #{@fetch_error}"
- end
- @fetch_error = nil
- index
- end
-
- ##
- # Get the quick index needed for incremental updates.
-
- def fetch_quick_index(source_uri, all)
- index = all ? 'index' : 'latest_index'
-
- zipped_index = fetcher.fetch_path source_uri + "quick/#{index}.rz"
-
- unzip(zipped_index).split("\n")
- rescue ::Exception => e
- unless all then
- say "Latest index not found, using quick index" if
- Gem.configuration.really_verbose
-
- fetch_quick_index source_uri, true
- else
- raise Gem::OperationNotSupportedError,
- "No quick index found: #{e.message}"
- end
- end
-
- ##
- # Make a list of full names for all the missing gemspecs.
-
- def find_missing(spec_names)
- unless defined? @originals then
- @originals = {}
- each do |full_name, spec|
- @originals[spec.original_name] = spec
- end
- end
-
- spec_names.find_all { |full_name|
- @originals[full_name].nil?
- }
- end
-
- def remove_extra(spec_names)
- dictionary = spec_names.inject({}) { |h, k| h[k] = true; h }
- each do |name, spec|
- remove_spec name unless dictionary.include? spec.original_name
- end
- end
-
- ##
- # Unzip the given string.
-
- def unzip(string)
- require 'zlib'
- Gem.inflate string
- end
-
- ##
- # Tries to fetch Marshal representation first, then YAML
-
- def fetch_single_spec(source_uri, spec_name)
- @fetch_error = nil
-
- begin
- marshal_uri = source_uri + "quick/Marshal.#{Gem.marshal_version}/#{spec_name}.gemspec.rz"
- zipped = fetcher.fetch_path marshal_uri
- return Marshal.load(unzip(zipped))
- rescue => ex
- @fetch_error = ex
-
- if Gem.configuration.really_verbose then
- say "unable to fetch marshal gemspec #{marshal_uri}: #{ex.class} - #{ex}"
- end
- end
-
- begin
- yaml_uri = source_uri + "quick/#{spec_name}.gemspec.rz"
- zipped = fetcher.fetch_path yaml_uri
- return YAML.load(unzip(zipped))
- rescue => ex
- @fetch_error = ex
- if Gem.configuration.really_verbose then
- say "unable to fetch YAML gemspec #{yaml_uri}: #{ex.class} - #{ex}"
- end
- end
-
- nil
- end
-
- ##
- # Update the cached source index with the missing names.
-
- def update_with_missing(source_uri, missing_names)
- progress = ui.progress_reporter(missing_names.size,
- "Updating metadata for #{missing_names.size} gems from #{source_uri}")
- missing_names.each do |spec_name|
- gemspec = fetch_single_spec(source_uri, spec_name)
- if gemspec.nil? then
- ui.say "Failed to download spec #{spec_name} from #{source_uri}:\n" \
- "\t#{@fetch_error.message}"
- else
- add_spec gemspec
- progress.updated spec_name
- end
- @fetch_error = nil
- end
- progress.done
- progress.count
- end
-
end
# :stopdoc:
@@ -593,5 +360,45 @@ module Gem
Cache = SourceIndex
end
-# :startdoc:
+class Gem::SourceIndex
+ extend Deprecate
+
+ deprecate :all_gems, :none, 2011, 10
+
+ deprecate :==, :none, 2011, 11 # noisy
+ deprecate :add_specs, :none, 2011, 11 # noisy
+ deprecate :each, :none, 2011, 11
+ deprecate :gems, :none, 2011, 11
+ deprecate :load_gems_in, :none, 2011, 11
+ deprecate :refresh!, :none, 2011, 11
+ deprecate :spec_dirs=, "Specification.dirs=", 2011, 11 # noisy
+ deprecate :add_spec, "Specification.add_spec", 2011, 11
+ deprecate :find_name, "Specification.find_by_name", 2011, 11
+ deprecate :gem_signature, :none, 2011, 11
+ deprecate :index_signature, :none, 2011, 11
+ deprecate :initialize, :none, 2011, 11
+ deprecate :latest_specs, "Specification.latest_specs", 2011, 11
+ deprecate :length, "Specification.all.length", 2011, 11
+ deprecate :outdated, :none, 2011, 11
+ deprecate :prerelease_gems, :none, 2011, 11
+ deprecate :prerelease_specs, :none, 2011, 11
+ deprecate :released_gems, :none, 2011, 11
+ deprecate :released_specs, :none, 2011, 11
+ deprecate :remove_spec, "Specification.remove_spec", 2011, 11
+ deprecate :search, :none, 2011, 11
+ deprecate :size, "Specification.all.size", 2011, 11
+ deprecate :spec_dirs, "Specification.dirs", 2011, 11
+ deprecate :specification, "Specification.find", 2011, 11
+
+ class << self
+ extend Deprecate
+
+ deprecate :from_gems_in, :none, 2011, 10
+ deprecate :from_installed_gems, :none, 2011, 10
+ deprecate :installed_spec_directories, "Specification.dirs", 2011, 11
+ deprecate :load_specification, :none, 2011, 10
+ end
+end
+
+# :startdoc:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment