class Gem::Validator

Validator 执行各种 gem 文件和 gem 数据库验证

公共实例方法

alien(gems=[]) 点击以切换源代码

检查 gem 目录中是否存在以下潜在的不一致/问题

  • 校验 gem 本身

  • 对于每个 gem 中的每个文件,检查已安装版本的一致性

  • 检查那些不在 gem 中但却在 gems 目录中的文件

  • 1 个缓存 - 1 个 spec - 1 个目录。

返回一个以问题 gem 的名称为键的 ErrorData 对象哈希。

# File rubygems/validator.rb, line 62
def alien(gems=[])
  errors = Hash.new {|h,k| h[k] = {} }

  Gem::Specification.each do |spec|
    unless gems.empty?
      next unless gems.include? spec.name
    end
    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
      errors[gem_name][spec.full_name] =
        "Gem registered but doesn't exist at #{gem_directory}"
      next
    end

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

    begin
      unless File.readable?(gem_path)
        raise Gem::VerificationError, "missing gem file #{gem_path}"
      end

      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 do |file_name|
          File.exist? File.join(gem_directory, file_name)
        end

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

        good, unreadable = good.partition do |file_name|
          File.readable? File.join(gem_directory, file_name)
        end

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

        good.each do |entry, data|
          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
              errors[gem_name][entry["path"]] = "Modified from original"
            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) 点击以切换源代码
# File rubygems/validator.rb, line 24
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(%r{^/}, "")
    installed_files << fn unless
      fn.empty? || fn.include?("CVS") || File.directory?(file_name)
  end

  installed_files
end