diff options
author | duerst <duerst@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-09-23 07:08:03 +0000 |
---|---|---|
committer | duerst <duerst@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-09-23 07:08:03 +0000 |
commit | 96245d6dd37557acd9a066491db6c80b125f0676 (patch) | |
tree | 329a7991e8f042e35ecd702ecfc99a89eb6b3a20 /tool | |
parent | ab5517a06c407721520000060fce95d905b8d219 (diff) |
tool/downloader.rb: added Downloader.download_if_modified_since
to reduce downloads of large files that change only rarely.
[ruby-core:65164] [CommonRuby - Feature #10084]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'tool')
-rw-r--r-- | tool/downloader.rb | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/tool/downloader.rb b/tool/downloader.rb index 03d26eaa5f..7c29132d7b 100644 --- a/tool/downloader.rb +++ b/tool/downloader.rb @@ -8,4 +8,26 @@ class Downloader rescue => e raise "failed to download #{name}\n#{e.message}: #{url}" end + + # Update a file from url if newer version is available. + # Creates the file if the file doesn't yet exist; however, the + # directory where the file is being created has to exist already. + # Example usage: + # download_if_modified_since 'http://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt', + # 'enc/unicode/data/UnicodeData.txt' + def self.download_if_modified_since(url, name, dir=nil, since=nil) + file = dir ? File.join(dir, name) : name + since = Date.new(1980,1,1) unless File.exist? file # use very old date to assure file creation + since = File.mtime file unless since # get last modification time for file + since = since.to_datetime if since.respond_to? :to_datetime # convert Time/Date to DateTime + since = since.httpdate if since.respond_to? :httpdate # convert DateTime to String + open(url, 'If-Modified-Since' => since) do |r| + body = r.read + open(file, 'wb', 0755) { |f| f.write(body) } + end + rescue OpenURI::HTTPError => http_error + unless http_error.message =~ /^304 / # 304 Not Modified + raise "Failed to (check for) downloading #{url}: #{http_error.message}" + end + end end |