/tools/Ruby/lib/ruby/site_ruby/1.8/rubygems/package.rb

http://github.com/agross/netopenspace · Ruby · 107 lines · 80 code · 17 blank · 10 comment · 3 complexity · 70e881b8b453e435fa7fb48086341b23 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. #--
  3. # Copyright (C) 2004 Mauricio Julio Fernández Pradier
  4. # See LICENSE.txt for additional licensing information.
  5. #++
  6. require 'rubygems/specification'
  7. ##
  8. # Wrapper for FileUtils meant to provide logging and additional operations if
  9. # needed.
  10. class Gem::FileOperations
  11. def initialize(logger = nil)
  12. require 'fileutils'
  13. @logger = logger
  14. end
  15. def method_missing(meth, *args, &block)
  16. case
  17. when FileUtils.respond_to?(meth)
  18. @logger.log "#{meth}: #{args}" if @logger
  19. FileUtils.send meth, *args, &block
  20. when Gem::FileOperations.respond_to?(meth)
  21. @logger.log "#{meth}: #{args}" if @logger
  22. Gem::FileOperations.send meth, *args, &block
  23. else
  24. super
  25. end
  26. end
  27. end
  28. module Gem::Package
  29. class Error < StandardError; end
  30. class NonSeekableIO < Error; end
  31. class ClosedIO < Error; end
  32. class BadCheckSum < Error; end
  33. class TooLongFileName < Error; end
  34. class FormatError < Error
  35. attr_reader :path
  36. def initialize message, path = nil
  37. @path = path
  38. message << " in #{path}" if path
  39. super message
  40. end
  41. end
  42. ##
  43. # Raised when a tar file is corrupt
  44. class TarInvalidError < Error; end
  45. def self.open(io, mode = "r", signer = nil, &block)
  46. tar_type = case mode
  47. when 'r' then TarInput
  48. when 'w' then TarOutput
  49. else
  50. raise "Unknown Package open mode"
  51. end
  52. tar_type.open(io, signer, &block)
  53. end
  54. def self.pack(src, destname, signer = nil)
  55. TarOutput.open(destname, signer) do |outp|
  56. dir_class.chdir(src) do
  57. outp.metadata = (file_class.read("RPA/metadata") rescue nil)
  58. find_class.find('.') do |entry|
  59. case
  60. when file_class.file?(entry)
  61. entry.sub!(%r{\./}, "")
  62. next if entry =~ /\ARPA\//
  63. stat = File.stat(entry)
  64. outp.add_file_simple(entry, stat.mode, stat.size) do |os|
  65. file_class.open(entry, "rb") do |f|
  66. os.write(f.read(4096)) until f.eof?
  67. end
  68. end
  69. when file_class.dir?(entry)
  70. entry.sub!(%r{\./}, "")
  71. next if entry == "RPA"
  72. outp.mkdir(entry, file_class.stat(entry).mode)
  73. else
  74. raise "Don't know how to pack this yet!"
  75. end
  76. end
  77. end
  78. end
  79. end
  80. end
  81. require 'rubygems/package/f_sync_dir'
  82. require 'rubygems/package/tar_header'
  83. require 'rubygems/package/tar_input'
  84. require 'rubygems/package/tar_output'
  85. require 'rubygems/package/tar_reader'
  86. require 'rubygems/package/tar_reader/entry'
  87. require 'rubygems/package/tar_writer'