2009-10-03 11:40:20 -04:00
|
|
|
# = Tools for FTP uploading.
|
|
|
|
#
|
|
|
|
# This file is still under development and is not released for general
|
|
|
|
# use.
|
|
|
|
|
|
|
|
require 'date'
|
|
|
|
require 'net/ftp'
|
2012-11-15 17:32:34 -05:00
|
|
|
require 'rake/file_list'
|
2009-10-03 11:40:20 -04:00
|
|
|
|
|
|
|
module Rake # :nodoc:
|
|
|
|
|
2014-07-14 23:07:37 -04:00
|
|
|
class FtpFile # :nodoc: all
|
2009-10-03 11:40:20 -04:00
|
|
|
attr_reader :name, :size, :owner, :group, :time
|
|
|
|
|
|
|
|
def self.date
|
|
|
|
@date_class ||= Date
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.time
|
|
|
|
@time_class ||= Time
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(path, entry)
|
|
|
|
@path = path
|
2010-11-08 15:58:12 -05:00
|
|
|
@mode, _, @owner, @group, size, d1, d2, d3, @name = entry.split(' ')
|
2009-10-03 11:40:20 -04:00
|
|
|
@size = size.to_i
|
|
|
|
@time = determine_time(d1, d2, d3)
|
|
|
|
end
|
|
|
|
|
|
|
|
def path
|
|
|
|
File.join(@path, @name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def directory?
|
|
|
|
@mode[0] == ?d
|
|
|
|
end
|
|
|
|
|
|
|
|
def mode
|
|
|
|
parse_mode(@mode)
|
|
|
|
end
|
|
|
|
|
|
|
|
def symlink?
|
|
|
|
@mode[0] == ?l
|
|
|
|
end
|
|
|
|
|
|
|
|
private # --------------------------------------------------------
|
|
|
|
|
|
|
|
def parse_mode(m)
|
|
|
|
result = 0
|
|
|
|
(1..9).each do |i|
|
2013-10-11 17:35:01 -04:00
|
|
|
result = 2 * result + ((m[i] == ?-) ? 0 : 1)
|
2009-10-03 11:40:20 -04:00
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def determine_time(d1, d2, d3)
|
|
|
|
now = self.class.time.now
|
2013-10-11 17:35:01 -04:00
|
|
|
if /:/ !~ d3
|
2009-10-03 11:40:20 -04:00
|
|
|
result = Time.parse("#{d1} #{d2} #{d3}")
|
2013-10-11 17:35:01 -04:00
|
|
|
else
|
|
|
|
result = Time.parse("#{d1} #{d2} #{now.year} #{d3}")
|
|
|
|
result = Time.parse("#{d1} #{d2} #{now.year - 1} #{d3}") if
|
|
|
|
result > now
|
2009-10-03 11:40:20 -04:00
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-14 23:07:37 -04:00
|
|
|
##
|
2009-10-03 11:40:20 -04:00
|
|
|
# Manage the uploading of files to an FTP account.
|
2014-07-14 23:07:37 -04:00
|
|
|
class FtpUploader # :nodoc:
|
2009-10-03 11:40:20 -04:00
|
|
|
|
|
|
|
# Log uploads to standard output when true.
|
|
|
|
attr_accessor :verbose
|
|
|
|
|
|
|
|
class << FtpUploader
|
|
|
|
# Create an uploader and pass it to the given block as +up+.
|
|
|
|
# When the block is complete, close the uploader.
|
|
|
|
def connect(path, host, account, password)
|
|
|
|
up = self.new(path, host, account, password)
|
|
|
|
begin
|
|
|
|
yield(up)
|
|
|
|
ensure
|
|
|
|
up.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-23 18:11:55 -04:00
|
|
|
# Create an FTP uploader targeting the directory +path+ on +host+
|
2009-10-03 11:40:20 -04:00
|
|
|
# using the given account and password. +path+ will be the root
|
|
|
|
# path of the uploader.
|
|
|
|
def initialize(path, host, account, password)
|
|
|
|
@created = Hash.new
|
|
|
|
@path = path
|
|
|
|
@ftp = Net::FTP.new(host, account, password)
|
|
|
|
makedirs(@path)
|
|
|
|
@ftp.chdir(@path)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create the directory +path+ in the uploader root path.
|
|
|
|
def makedirs(path)
|
|
|
|
route = []
|
|
|
|
File.split(path).each do |dir|
|
|
|
|
route << dir
|
|
|
|
current_dir = File.join(route)
|
|
|
|
if @created[current_dir].nil?
|
|
|
|
@created[current_dir] = true
|
2011-06-23 18:11:55 -04:00
|
|
|
$stderr.puts "Creating Directory #{current_dir}" if @verbose
|
2009-10-03 11:40:20 -04:00
|
|
|
@ftp.mkdir(current_dir) rescue nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Upload all files matching +wildcard+ to the uploader's root
|
|
|
|
# path.
|
|
|
|
def upload_files(wildcard)
|
2012-11-15 17:32:34 -05:00
|
|
|
FileList.glob(wildcard).each do |fn|
|
2009-10-03 11:40:20 -04:00
|
|
|
upload(fn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Close the uploader.
|
|
|
|
def close
|
|
|
|
@ftp.close
|
|
|
|
end
|
|
|
|
|
|
|
|
private # --------------------------------------------------------
|
|
|
|
|
|
|
|
# Upload a single file to the uploader's root path.
|
|
|
|
def upload(file)
|
2011-06-23 18:11:55 -04:00
|
|
|
$stderr.puts "Uploading #{file}" if @verbose
|
2009-10-03 11:40:20 -04:00
|
|
|
dir = File.dirname(file)
|
|
|
|
makedirs(dir)
|
|
|
|
@ftp.putbinaryfile(file, file) unless File.directory?(file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|