Class: Downloader
- Inherits:
-
Object
- Object
- Downloader
- Defined in:
- lib/uniprop/downloader.rb
Class Method Summary collapse
- .cache_file(url, name, cache_dir = nil) ⇒ Object
- .download(url, name, dir = nil, since = true, options = {}) ⇒ Object
- .http_options(file, since) ⇒ Object
- .httpdate(date) ⇒ Object
- .https ⇒ Object
- .https=(https) ⇒ Object
- .https? ⇒ Boolean
- .link_cache(cache, file, name, verbose = false) ⇒ Object
- .mode_for(data) ⇒ Object
- .save_cache(cache, file, name) ⇒ Object
- .under(dir, name) ⇒ Object
Class Method Details
.cache_file(url, name, cache_dir = nil) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/uniprop/downloader.rb', line 186 def self.cache_file(url, name, cache_dir = nil) case cache_dir when false return nil when nil cache_dir = ENV['CACHE_DIR'] if !cache_dir or cache_dir.empty? cache_dir = ".downloaded-cache" end end Pathname.new(cache_dir) + (name || File.basename(URI(url).path)) end |
.download(url, name, dir = nil, since = true, options = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/uniprop/downloader.rb', line 84 def self.download(url, name, dir = nil, since = true, = {}) = .dup url = URI(url) dryrun = .delete(:dryrun) .delete(:unicode_beta) # just to be on the safe side for gems and gcc if name file = Pathname.new(under(dir, name)) else name = File.basename(url.path) end cache_save = .delete(:cache_save) { ENV["CACHE_SAVE"] != "no" } cache = cache_file(url, name, .delete(:cache_dir)) file ||= cache if since.nil? and file.exist? if $VERBOSE $stdout.puts "#{file} already exists" $stdout.flush end if cache_save save_cache(cache, file, name) end return file.to_path end if dryrun puts "Download #{url} into #{file}" return end if link_cache(cache, file, name, $VERBOSE) return file.to_path end if !https? and URI::HTTPS === url warn "*** using http instead of https ***" url.scheme = 'http' url = URI(url.to_s) end if $VERBOSE $stdout.print "downloading #{name} ... " $stdout.flush end mtime = nil = .merge((file, since.nil? ? true : since)) begin data = with_retry(10) do data = url.read() if mtime = data.["last-modified"] mtime = Time.httpdate(mtime) end data end rescue OpenURI::HTTPError => http_error if http_error. =~ /^304 / # 304 Not Modified if $VERBOSE $stdout.puts "not modified" $stdout.flush end return file.to_path end raise rescue Timeout::Error if since.nil? and file.exist? puts "Request for #{url} timed out, using old version." return file.to_path end raise rescue SocketError if since.nil? and file.exist? puts "No network connection, unable to download #{url}, using old version." return file.to_path end raise end dest = (cache_save && cache && !cache.exist? ? cache : file) dest.parent.mkpath dest.open("wb", 0600) do |f| data.scrub!('?') # invalid byte sequence in UTF-8 対策 f.write(data) f.chmod(mode_for(data)) end if mtime dest.utime(mtime, mtime) end if $VERBOSE $stdout.puts "done" $stdout.flush end if dest.eql?(cache) link_cache(cache, file, name) elsif cache_save save_cache(cache, file, name) end return file.to_path rescue => e raise "failed to download #{name}\n#{e.class}: #{e.}: #{url}" end |
.http_options(file, since) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/uniprop/downloader.rb', line 52 def self.(file, since) = {} if since case since when true since = (File.mtime(file).httpdate rescue nil) when Time since = since.httpdate end if since ['If-Modified-Since'] = since end end ['Accept-Encoding'] = 'identity' # to disable Net::HTTP::GenericRequest#decode_content end |
.httpdate(date) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/uniprop/downloader.rb', line 69 def self.httpdate(date) Time.httpdate(date) rescue ArgumentError => e # Some hosts (e.g., zlib.net) return similar to RFC 850 but 4 # digit year, sometimes. /\A\s* (?:Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day,\x20 (\d\d)-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{4})\x20 (\d\d):(\d\d):(\d\d)\x20 GMT \s*\z/ix =~ date or raise warn e. Time.utc($3, $2, $1, $4, $5, $6) end |
.https ⇒ Object
44 45 46 |
# File 'lib/uniprop/downloader.rb', line 44 def self.https @@https end |
.https=(https) ⇒ Object
36 37 38 |
# File 'lib/uniprop/downloader.rb', line 36 def self.https=(https) @@https = https end |
.https? ⇒ Boolean
40 41 42 |
# File 'lib/uniprop/downloader.rb', line 40 def self.https? @@https == 'https' end |
.link_cache(cache, file, name, verbose = false) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/uniprop/downloader.rb', line 199 def self.link_cache(cache, file, name, verbose = false) return false unless cache and cache.exist? return true if cache.eql?(file) if /cygwin/ !~ RUBY_PLATFORM or /winsymlink:nativestrict/ =~ ENV['CYGWIN'] begin file.make_symlink(cache.relative_path_from(file.parent)) rescue SystemCallError else if verbose $stdout.puts "made symlink #{name} to #{cache}" $stdout.flush end return true end end begin file.make_link(cache) rescue SystemCallError else if verbose $stdout.puts "made link #{name} to #{cache}" $stdout.flush end return true end end |
.mode_for(data) ⇒ Object
48 49 50 |
# File 'lib/uniprop/downloader.rb', line 48 def self.mode_for(data) /\A#!/ =~ data ? 0755 : 0644 end |
.save_cache(cache, file, name) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/uniprop/downloader.rb', line 226 def self.save_cache(cache, file, name) return unless cache or cache.eql?(file) begin st = cache.stat rescue begin file.rename(cache) rescue return end else return unless st.mtime > file.lstat.mtime file.unlink end link_cache(cache, file, name) end |
.under(dir, name) ⇒ Object
182 183 184 |
# File 'lib/uniprop/downloader.rb', line 182 def self.under(dir, name) dir ? File.join(dir, File.basename(name)) : name end |