[前][次][番号順一覧][スレッド一覧]

ruby-changes:35609

From: duerst <ko1@a...>
Date: Tue, 23 Sep 2014 16:08:17 +0900 (JST)
Subject: [ruby-changes:35609] duerst:r47691 (trunk): tool/downloader.rb: added Downloader.download_if_modified_since

duerst	2014-09-23 16:08:03 +0900 (Tue, 23 Sep 2014)

  New Revision: 47691

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47691

  Log:
    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]

  Modified files:
    trunk/ChangeLog
    trunk/tool/downloader.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47690)
+++ ChangeLog	(revision 47691)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Sep 23 16:07:07 2014  Martin Duerst <duerst@i...>
+
+	* 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]
+
 Tue Sep 23 11:55:09 2014  SHIBATA Hiroshi  <shibata.hiroshi@g...>
 
 	* .travis.yml: added rubyspec into travis tasks and eliminate to stdout.
Index: tool/downloader.rb
===================================================================
--- tool/downloader.rb	(revision 47690)
+++ tool/downloader.rb	(revision 47691)
@@ -8,4 +8,26 @@ class Downloader https://github.com/ruby/ruby/blob/trunk/tool/downloader.rb#L8
   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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]