/tools/Ruby/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/contrib/ftptools.rb

http://github.com/agross/netopenspace · Ruby · 150 lines · 98 code · 22 blank · 30 comment · 5 complexity · b3165b0933200ea65ae09ede18e2a936 MD5 · raw file

  1. # = Tools for FTP uploading.
  2. #
  3. # This file is still under development and is not released for general
  4. # use.
  5. require 'date'
  6. require 'net/ftp'
  7. module Rake # :nodoc:
  8. ####################################################################
  9. # <b>Note:</b> <em> Not released for general use.</em>
  10. class FtpFile
  11. attr_reader :name, :size, :owner, :group, :time
  12. def self.date
  13. @date_class ||= Date
  14. end
  15. def self.time
  16. @time_class ||= Time
  17. end
  18. def initialize(path, entry)
  19. @path = path
  20. @mode, _, @owner, @group, size, d1, d2, d3, @name = entry.split(' ')
  21. @size = size.to_i
  22. @time = determine_time(d1, d2, d3)
  23. end
  24. def path
  25. File.join(@path, @name)
  26. end
  27. def directory?
  28. @mode[0] == ?d
  29. end
  30. def mode
  31. parse_mode(@mode)
  32. end
  33. def symlink?
  34. @mode[0] == ?l
  35. end
  36. private # --------------------------------------------------------
  37. def parse_mode(m)
  38. result = 0
  39. (1..9).each do |i|
  40. result = 2*result + ((m[i]==?-) ? 0 : 1)
  41. end
  42. result
  43. end
  44. def determine_time(d1, d2, d3)
  45. now = self.class.time.now
  46. if /:/ =~ d3
  47. result = Time.parse("#{d1} #{d2} #{now.year} #{d3}")
  48. if result > now
  49. result = Time.parse("#{d1} #{d2} #{now.year-1} #{d3}")
  50. end
  51. else
  52. result = Time.parse("#{d1} #{d2} #{d3}")
  53. end
  54. result
  55. # elements = ParseDate.parsedate("#{d1} #{d2} #{d3}")
  56. # if elements[0].nil?
  57. # today = self.class.date.today
  58. # if elements[1] > today.month
  59. # elements[0] = today.year - 1
  60. # else
  61. # elements[0] = today.year
  62. # end
  63. # end
  64. # elements = elements.collect { |el| el.nil? ? 0 : el }
  65. # Time.mktime(*elements[0,7])
  66. end
  67. end
  68. ####################################################################
  69. # Manage the uploading of files to an FTP account.
  70. class FtpUploader
  71. # Log uploads to standard output when true.
  72. attr_accessor :verbose
  73. class << FtpUploader
  74. # Create an uploader and pass it to the given block as +up+.
  75. # When the block is complete, close the uploader.
  76. def connect(path, host, account, password)
  77. up = self.new(path, host, account, password)
  78. begin
  79. yield(up)
  80. ensure
  81. up.close
  82. end
  83. end
  84. end
  85. # Create an FTP uploader targeting the directory +path+ on +host+
  86. # using the given account and password. +path+ will be the root
  87. # path of the uploader.
  88. def initialize(path, host, account, password)
  89. @created = Hash.new
  90. @path = path
  91. @ftp = Net::FTP.new(host, account, password)
  92. makedirs(@path)
  93. @ftp.chdir(@path)
  94. end
  95. # Create the directory +path+ in the uploader root path.
  96. def makedirs(path)
  97. route = []
  98. File.split(path).each do |dir|
  99. route << dir
  100. current_dir = File.join(route)
  101. if @created[current_dir].nil?
  102. @created[current_dir] = true
  103. $stderr.puts "Creating Directory #{current_dir}" if @verbose
  104. @ftp.mkdir(current_dir) rescue nil
  105. end
  106. end
  107. end
  108. # Upload all files matching +wildcard+ to the uploader's root
  109. # path.
  110. def upload_files(wildcard)
  111. Dir[wildcard].each do |fn|
  112. upload(fn)
  113. end
  114. end
  115. # Close the uploader.
  116. def close
  117. @ftp.close
  118. end
  119. private # --------------------------------------------------------
  120. # Upload a single file to the uploader's root path.
  121. def upload(file)
  122. $stderr.puts "Uploading #{file}" if @verbose
  123. dir = File.dirname(file)
  124. makedirs(dir)
  125. @ftp.putbinaryfile(file, file) unless File.directory?(file)
  126. end
  127. end
  128. end