On Wed, 10 May 2006, Daniel B. wrote:
Hi all,
Is there a method in Net::FTP that corresponds to ftp’s mget and mput
functions?
If not, is there any chance of adding this to ftp.rb?
Thanks,
Dan
this does something like mput
#!/dmsp/reference/bin/ruby
require “net/ftp”
require “uri”
require “socket”
require “yaml”
def uriparse uri
unless uri =~ %r/^ftp/
host, path = uri.split %r|:/+|o
URI::parse “ftp://#{ host }/#{ path }”
else
URI::parse uri
end
end
abort “#{ $0 } src [srcs]+ dsturi” if ARGV.empty?
user = ENV[“FTP_USER”] || “anonymous”
password = ENV[“FTP_PASS”] || ENV[“FTP_PASSWORD”] || “[email protected]”
debug = ENV[“FTP_DEBUG”]
srcs, uri = ARGV[0…-2], ARGV[-1]
uri = uriparse uri
host, hostdest = uri.host, uri.path
rpaths = []
hostname, pid = Socket::gethostname, Process::pid
Net::FTP.open(host) do |ftp|
ftp.debug_mode = true if debug
ftp.login user, password
ftp.passive = true
ftp.binary = true
srcs.each do |src|
dest =
if hostdest =~ %r|/+\s*$|o or srcs.size > 1
File::join(hostdest, File::basename(src))
else
hostdest
end
destdir = File::dirname dest
destdirs = []
d = nil
destdirs.unshift(d) while((d = File::dirname(d || dest)) !~
%r|^\s*/+\s*$|o)
dirname, basename = File::dirname(dest), File::basename(dest)
tmp = File::join(dirname, ".#{ basename }.#{ hostname }.#{ pid
}.ftp")
if debug
state = <<-eos
|@src <#{ src }>
|@host <#{ host }>
|@dest <#{ dest }>
|@destdir <#{ destdir }>
|@destdirs <#{ destdirs.inspect }>
|@tmp <#{ tmp.inspect }>
eos
state.gsub!(%r/^\s*\|/o,'')
puts state
end
destdirs.each{|d| begin; ftp.mkdir d; rescue Net::FTPPermError;
end}
ftp.putbinaryfile src, tmp
ftp.rename tmp, dest
rpaths << dest
end
end
y rpaths
-a