123456789_123456789_123456789_123456789_123456789_

Class: Gem::Validator

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/rubygems/validator.rb

Overview

Validator performs various gem file and gem database validation

Class Method Summary

Instance Attribute Summary

Instance Method Summary

UserInteraction - Included

#alert

Displays an alert statement.

#alert_error

Displays an error statement to the error output location.

#alert_warning

Displays a warning statement to the warning output location.

#ask

Asks a question and returns the answer.

#ask_for_password

Asks for a password with a prompt

#ask_yes_no

Asks a yes or no question.

#choose_from_list

Asks the user to answer question with an answer from the given list.

#say

Displays the given statement on the standard output (or equivalent).

#terminate_interaction

Terminates the RubyGems process with the given exit_code

#verbose

Calls say with msg or the results of the block if really_verbose is true.

DefaultUserInteraction - Included

Text - Included

#clean_text

Remove any non-printable characters and make the text suitable for printing.

#format_text

Wraps text to wrap characters and optionally indents by indent characters.

#levenshtein_distance

This code is based directly on the Text gem implementation Returns a value representing the “cost” of transforming str1 into str2.

#truncate_text, #min3

Constructor Details

.newValidator

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/validator.rb', line 18

def initialize # :nodoc:
  require 'find'
end

Instance Method Details

#alien(gems = [])

Checks the gem directory for the following potential inconsistencies/problems:

  • Checksum gem itself

  • For each file in each gem, check consistency of installed versions

  • Check for files that aren't part of the gem but are in the gems directory

  • 1 cache - 1 spec - 1 directory.

returns a hash of Validator::ErrorData objects, keyed on the problem gem's name.

[ GitHub ]

  
# File 'lib/rubygems/validator.rb', line 85

def alien(gems=[])
  errors = Hash.new { |h,k| h[k] = {} }

  Gem::Specification.each do |spec|
    next unless gems.include? spec.name unless gems.empty?
    next if spec.default_gem?

    gem_name      = spec.file_name
    gem_path      = spec.cache_file
    spec_path     = spec.spec_file
    gem_directory = spec.full_gem_path

    unless File.directory? gem_directory then
      errors[gem_name][spec.full_name] =
        "Gem registered but doesn't exist at #{gem_directory}"
      next
    end

    unless File.exist? spec_path then
      errors[gem_name][spec_path] = "Spec file missing for installed gem"
    end

    begin
      verify_gem_file(gem_path)

      good, gone, unreadable = nil, nil, nil, nil

      File.open gem_path, Gem.binary_mode do |file|
        package = Gem::Package.new gem_path

        good, gone = package.contents.partition { |file_name|
          File.exist? File.join(gem_directory, file_name)
        }

        gone.sort.each do |path|
          errors[gem_name][path] = "Missing file"
        end

        good, unreadable = good.partition { |file_name|
          File.readable? File.join(gem_directory, file_name)
        }

        unreadable.sort.each do |path|
          errors[gem_name][path] = "Unreadable file"
        end

        good.each do |entry, data|
          begin
            next unless data # HACK `gem check -a mkrf`

            source = File.join gem_directory, entry['path']

            File.open source, Gem.binary_mode do |f|
              unless f.read == data then
                errors[gem_name][entry['path']] = "Modified from original"
              end
            end
          end
        end
      end

      installed_files = find_files_for_gem(gem_directory)
      extras = installed_files - good - unreadable

      extras.each do |extra|
        errors[gem_name][extra] = "Extra file"
      end
    rescue Gem::VerificationError => e
      errors[gem_name][gem_path] = e.message
    end
  end

  errors.each do |name, subhash|
    errors[name] = subhash.map do |path, msg|
      ErrorData.new path, msg
    end.sort
  end

  errors
end

#find_files_for_gem(gem_directory) (private)

[ GitHub ]

  
# File 'lib/rubygems/validator.rb', line 47

def find_files_for_gem(gem_directory)
  installed_files = []

  Find.find gem_directory do |file_name|
    fn = file_name[gem_directory.size..file_name.size-1].sub(/^\//, "")
    installed_files << fn unless
      fn =~ /CVS/ || fn.empty? || File.directory?(file_name)
  end

  installed_files
end

#verify_gem(gem_data)

Given a gem file's contents, validates against its own MD5 checksum

gem_data
String

Contents of the gem file

[ GitHub ]

  
# File 'lib/rubygems/validator.rb', line 26

def verify_gem(gem_data)
  # TODO remove me? The code here only validate an MD5SUM that was
  # in some old formatted gems, but hasn't been for a long time.
end

#verify_gem_file(gem_path)

Given the path to a gem file, validates against its own MD5 checksum

gem_path
String

Path to gem file

[ GitHub ]

  
# File 'lib/rubygems/validator.rb', line 36

def verify_gem_file(gem_path)
  File.open gem_path, Gem.binary_mode do |file|
    gem_data = file.read
    verify_gem gem_data
  end
rescue Errno::ENOENT, Errno::EINVAL
  raise Gem::VerificationError, "missing gem file #{gem_path}"
end