Ruby Snapshot Notifier

R

Randy Lawrence

Has anyone already implemented a ruby script to check if a new ruby
snapshot is available? Would be neat if it could do this without
downloading the snapshot.

It seems the Ruby 1.8.1 snapshot is updated fairly frequently and the
download page doesn't indicate the date/time stamp (so we periodically
download, untar, ungzip and examine the timestamps manually).
 
R

Ryan Phillips

You could write a script that FTP's to the server and uses the list
command to get the filestamp of the file and compare it to a filestamp
locally.

-ryan
 
R

Randy Lawrence

Ryan said:
You could write a script that FTP's to the server and uses the list
command to get the filestamp of the file and compare it to a filestamp
locally.

-ryan

Yea, we just poked a hole in our firewall to allow ftp now. We were
downloading to our public server, and then downloading to our lan. Now
we can just connect directly. :)
 
D

daz

Ryan said:
You could write a script that FTP's to the server and uses the list
command to get the filestamp of the file and compare it to a filestamp
locally.

If it would save some of Matz' valuable bandwith, it's worth doing.
All you need to do is run this, then find out why it didn't do
what I expected on your machine. :)

File.utime isn't universally available (according to PickAxe I).
That often means "Windows has no chance", but it works for me.


daz



#---------------------------------------------------------------
# <ftp_snap.rb> -DfB- 2004/07/10
# Download stable-snapshot only if updated

ftp_site = 'ftp.iij.ad.jp' # ftp.ruby-lang.org mirror
fn_snap = 'stable-snapshot.tar.gz' # 1.8.x

################################################
# Download to site-ruby/stable-snapshot.tar.gz #
################################################
require 'rbconfig'
fn_snap_save = File.join(Config::CONFIG['sitedir'], fn_snap)
# fn_snap_save = 'some.other'

#---------------------------------------------------------------
$stdout.sync = true

# [Y,M,D,h,m] to Time
def pdt_to_time(pdt, now = Time.now)
unless pdt[1,4].compact.size==4
raise "#{pdt[1,4].inspect} ... expected [M, D, h, m]"
end
pdt[0] ||= now.year # assume year
pdt[0] -= 1 if pdt[1] > now.month # last year ?
Time.utc(*pdt[0,5])
end

require 'ParseDate'
require 'net/ftp'

ftp = Net::FTP.open(ftp_site)
END { ftp.close unless ftp.closed? }
ftp.login
##
ftp.chdir('/pub/lang/ruby')
fline = ftp.list(fn_snap)
if fline.empty?
ftp.chdir('/pub/ruby')
fline = ftp.list(fn_snap)
end
unless fline.size==1
puts '+++', fline, '+++'
raise "Multiple, or no files match \"#{fn_snap}\""
end
# <---- Jan..Dec ---->
fline[0] =~ /(\s[AFJMNOS][aceopu][b-y]\s.*)#{Regexp.quote(fn_snap)}\Z/
ft = pdt_to_time(ParseDate::parsedate($1))
ft_prev = File.exist?(fn_snap_save) ? File.mtime(fn_snap_save).utc : 0
if ft > ft_prev
if ft_prev > 0
puts "Replacing: #{ft_prev}"
else
puts "No previous snapshot found (first run)"
end
puts "Getting latest (#{ft}) ..."
ftp.getbinaryfile(fn_snap, fn_snap_save, 1024)
File.utime(ft, ft, fn_snap_save)
else
puts "Existing snapshot is current"
end
 
R

Randy Lawrence

daz wrote:

[snip]
If it would save some of Matz' valuable bandwith, it's worth doing.
All you need to do is run this, then find out why it didn't do
what I expected on your machine. :)

File.utime isn't universally available (according to PickAxe I).
That often means "Windows has no chance", but it works for me.


daz



#---------------------------------------------------------------
# <ftp_snap.rb> -DfB- 2004/07/10
# Download stable-snapshot only if updated

ftp_site = 'ftp.iij.ad.jp' # ftp.ruby-lang.org mirror
fn_snap = 'stable-snapshot.tar.gz' # 1.8.x

################################################
# Download to site-ruby/stable-snapshot.tar.gz #
################################################
require 'rbconfig'
fn_snap_save = File.join(Config::CONFIG['sitedir'], fn_snap)
# fn_snap_save = 'some.other'

#---------------------------------------------------------------
$stdout.sync = true

# [Y,M,D,h,m] to Time
def pdt_to_time(pdt, now = Time.now)
unless pdt[1,4].compact.size==4
raise "#{pdt[1,4].inspect} ... expected [M, D, h, m]"
end
pdt[0] ||= now.year # assume year
pdt[0] -= 1 if pdt[1] > now.month # last year ?
Time.utc(*pdt[0,5])
end

require 'ParseDate'
require 'net/ftp'

ftp = Net::FTP.open(ftp_site)
END { ftp.close unless ftp.closed? }
ftp.login
##
ftp.chdir('/pub/lang/ruby')
fline = ftp.list(fn_snap)
if fline.empty?
ftp.chdir('/pub/ruby')
fline = ftp.list(fn_snap)
end
unless fline.size==1
puts '+++', fline, '+++'
raise "Multiple, or no files match \"#{fn_snap}\""
end
# <---- Jan..Dec ---->
fline[0] =~ /(\s[AFJMNOS][aceopu][b-y]\s.*)#{Regexp.quote(fn_snap)}\Z/
ft = pdt_to_time(ParseDate::parsedate($1))
ft_prev = File.exist?(fn_snap_save) ? File.mtime(fn_snap_save).utc : 0
if ft > ft_prev
if ft_prev > 0
puts "Replacing: #{ft_prev}"
else
puts "No previous snapshot found (first run)"
end
puts "Getting latest (#{ft}) ..."
ftp.getbinaryfile(fn_snap, fn_snap_save, 1024)
File.utime(ft, ft, fn_snap_save)
else
puts "Existing snapshot is current"
end

You are much to kind. Thank you very much!

Aside from the functionality, it is good for ruby beginners like me to
see other people's ruby coding style.
 
D

daz

Randy said:
[...] Thank you very much!

Quite a pleasure. Valuable question.
I couldn't concentrate on what I was supposed to be doing :)

Aside from the functionality, it is good for ruby beginners like me to
see other people's ruby coding style.

Normally, yes; but not from my example.
I looked at it and saw I'd written it in Perl.

I'll do coding style another day, perhaps ;-)


cheers,

daz
 
S

Shashank Date

Daz,


if ft > ft_prev
^^^^^^^^^
This condition gives me an error (ruby 1.8.2-rc14) but it can
easily be fixed like so:

if ft.to_i > ft_prev.to_i
if ft_prev > 0
puts "Replacing: #{ft_prev}"
else
puts "No previous snapshot found (first run)"
end
puts "Getting latest (#{ft}) ..."
ftp.getbinaryfile(fn_snap, fn_snap_save, 1024)
File.utime(ft, ft, fn_snap_save)
else
puts "Existing snapshot is current"
end

Thanks,
-- shanko
 
D

daz

Shashank said:
^^^^^^^^^
This condition gives me an error (ruby 1.8.2-rc14) ...


Hi Shanko,

Oh, true. The line immediately below that should be modified
in the same way, I guess.

I must have run with:
ruby 1.8.0 (2003-05-15) [i386-mswin32]

Most later versions of Ruby require to_i before comparison of
Time and Fixnum objects.


... but it can easily be fixed like so:
^^^^^^^^^
if ft.to_i > ft_prev.to_i <----------* new

^^^^^^^^^
if ft_prev.to_i > 0 <----------* new


Thanks for the 'patch'.

I said that it looked like Perl ...
.... I think it looks more like a Makefile :-#

ruby ftp_snap.rb # doesn't look as bad, tho'.

-- shanko


daz
 
N

nobu.nokada

Hi,

At Sat, 10 Jul 2004 22:02:29 +0900,
daz wrote in [ruby-talk:105895]:
require 'ParseDate'

$ ruby -rParseDate -ep
ruby: No such file to load -- ParseDate (LoadError)

No such library is bundled.
 
D

daz

Hi,

At Sat, 10 Jul 2004 22:02:29 +0900,
daz wrote in [ruby-talk:105895]:
require 'ParseDate'

$ ruby -rParseDate -ep
ruby: No such file to load -- ParseDate (LoadError)

No such library is bundled.


Thanks Nobu, is this OK ? (of course ;)

$ ruby -rparsedate -ep

(Windows doesn't care too much about case, but I should.)


daz


# parsedate.rb: Written by Tadayoshi Funaba 2001, 2002
# $Id: parsedate.rb,v 2.6 2002-05-14 07:43:18+09 tadf Exp $

require 'date/format'

module ParseDate

def parsedate(str, comp=false)
Date._parse(str, comp).
values_at:)year, :mon, :mday, :hour, :min, :sec, :zone, :wday)
end

module_function :parsedate

end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,837
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top