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