class Drupid::DownloadStrategy::Curl
Attributes
tarball_path[R]
Public Class Methods
new(url, dest, name = nil, download_specs = {})
click to toggle source
Calls superclass method
Drupid::DownloadStrategy::Base::new
# File lib/drupid/download_strategy.rb 126 def initialize url, dest, name = nil, download_specs = {} 127 super 128 if @name.to_s.empty? 129 @tarball_path = @dest + File.basename(@url) 130 else 131 # Do not add an extension if the provided name has an extension 132 n = @name.match(/\.\w+$/) ? @name : @name+ext 133 @tarball_path = @dest + n 134 end 135 if @specs.has_key?('file_type') 136 @tarball_path = @tarball_path.sub_ext('.' + @specs['file_type']) 137 end 138 end
Public Instance Methods
fetch()
click to toggle source
Retrieves a file from this object’s URL.
# File lib/drupid/download_strategy.rb 154 def fetch 155 dont_debug { @tarball_path.rmtree if @tarball_path.exist? } 156 begin 157 dont_debug { @dest.mkpath } 158 _fetch 159 rescue Exception => e 160 ignore_interrupts { @tarball_path.unlink if @tarball_path.exist? } 161 if e.kind_of? ErrorDuringExecution 162 raise CurlError, "Download failed: #{@url}" 163 else 164 raise 165 end 166 end 167 return @tarball_path 168 end
stage(wd = @dest)
click to toggle source
Stages this download into the specified directory. Invokes fetch
to retrieve the file if needed.
# File lib/drupid/download_strategy.rb 172 def stage wd = @dest 173 fetch unless @tarball_path.exist? 174 dont_debug { wd.mkpath } 175 debug "Staging into #{wd}" 176 target = wd + @tarball_path.basename 177 type = @tarball_path.compression_type 178 if type 179 tempdir do # uncompress inside a temporary directory 180 uncompress @tarball_path, :type => type 181 # Move extracted archive into the destination 182 content = Pathname.pwd.children 183 if 1 == content.size and content.first.directory? 184 src = content.first 185 target = wd + src.basename 186 debug "Moving #{src} into #{wd}" 187 dont_debug { FileUtils.mv src.to_s, wd.to_s, :force => true } 188 else # the archive did not have a root folder or it expanded to a file instead of a folder 189 # We cannot move the temporary directory we are in, so we copy its content 190 src = Pathname.pwd 191 target = wd + src.basename 192 dont_debug { target.rmtree if target.exist? } # Overwrite 193 dont_debug { target.mkpath } 194 src.ditto target 195 end 196 debug "Temporary staging target: #{target}" 197 end 198 elsif wd != @dest 199 debug "Moving #{@tarball_path} into #{wd}" 200 dont_debug { FileUtils.mv @tarball_path.to_s, wd.to_s, :force => true } 201 end 202 if @name and @name != target.basename.to_s 203 new_path = target.dirname + @name 204 dont_debug { new_path.rmtree if new_path.exist? } 205 debug "Renaming from #{target} to #{new_path}" 206 File.rename target.to_s, new_path.to_s 207 target = target.dirname+@name 208 end 209 @staged_path = target 210 debug "Staging completed" 211 end
Protected Instance Methods
_fetch()
click to toggle source
Private method, can be overridden if needed.
# File lib/drupid/download_strategy.rb 143 def _fetch 144 if @specs.has_key?('post_data') 145 curl @url, '-d', @specs['post_data'], '-o', @tarball_path 146 else 147 curl @url, '-o', @tarball_path 148 end 149 end
Private Instance Methods
ext()
click to toggle source
# File lib/drupid/download_strategy.rb 215 def ext 216 # GitHub uses odd URLs for zip files, so check for those 217 rx=%r[https?://(www\.)?github\.com/.*/(zip|tar)ball/] 218 if rx.match @url 219 if $2 == 'zip' 220 '.zip' 221 else 222 '.tgz' 223 end 224 else 225 Pathname.new(@url).extname # uses extended extname that supports double extensions 226 end 227 end