Class: Drupid::DownloadStrategy::Curl

Inherits:
Base
  • Object
show all
Defined in:
lib/drupid/download_strategy.rb

Direct Known Subclasses

CurlApacheMirror

Instance Attribute Summary collapse

Attributes inherited from Base

#dest, #name, #staged_path, #url

Instance Method Summary collapse

Methods included from Utils

#blah, #bzr, #compare_paths, #curl, #cvs, #debug, #dont_debug, #git, #hg, #ignore_interrupts, #interactive_shell, #odie, #ofail, #ohai, #owarn, #runBabyRun, #svn, #tempdir, #uncompress, #which, #writeFile

Constructor Details

#initialize(url, dest, name = nil, download_specs = {}) ⇒ Curl

Returns a new instance of Curl.



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/drupid/download_strategy.rb', line 127

def initialize url, dest, name = nil, download_specs = {}
  super
  if @name.to_s.empty?
    @tarball_path = @dest + File.basename(@url)
  else
    # Do not add an extension if the provided name has an extension
    n = @name.match(/\.\w+$/) ? @name : @name+ext
    @tarball_path = @dest + n
  end
  if @specs.has_key?('file_type')
    @tarball_path = @tarball_path.sub_ext('.' + @specs['file_type'])
  end
end

Instance Attribute Details

#tarball_pathObject (readonly)

Returns the value of attribute tarball_path.



125
126
127
# File 'lib/drupid/download_strategy.rb', line 125

def tarball_path
  @tarball_path
end

Instance Method Details

#fetchObject

Retrieves a file from this object’s URL.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/drupid/download_strategy.rb', line 155

def fetch
  dont_debug { @tarball_path.rmtree if @tarball_path.exist? }
  begin
    dont_debug { @dest.mkpath }
    _fetch
  rescue Exception => e
    ignore_interrupts { @tarball_path.unlink if @tarball_path.exist? }
    if e.kind_of? ErrorDuringExecution
      raise CurlError, "Download failed: #{@url}"
    else
      raise
    end
  end
  return @tarball_path
end

#stage(wd = @dest) ⇒ Object

Stages this download into the specified directory. Invokes #fetch to retrieve the file if needed.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/drupid/download_strategy.rb', line 173

def stage wd = @dest
  fetch unless @tarball_path.exist?
  dont_debug { wd.mkpath }
  debug "Staging into #{wd}"
  target = wd + @tarball_path.basename
  type = @tarball_path.compression_type
  if type
    tempdir do # uncompress inside a temporary directory
      uncompress @tarball_path, :type => type
      # Move extracted archive into the destination
      content = Pathname.pwd.children
      if 1 == content.size and content.first.directory?
        src = content.first
        target = wd + src.basename
        debug "Moving #{src} into #{wd}"
        dont_debug { FileUtils.mv src.to_s, wd.to_s, :force => true }
      else # the archive did not have a root folder or it expanded to a file instead of a folder
        # We cannot move the temporary directory we are in, so we copy its content
        src = Pathname.pwd
        target = wd + src.basename
        dont_debug { target.rmtree if target.exist? } # Overwrite
        dont_debug { target.mkpath }
        src.ditto target
      end
      debug "Temporary staging target: #{target}"
    end
  elsif wd != @dest
    debug "Moving #{@tarball_path} into #{wd}"
    dont_debug { FileUtils.mv @tarball_path.to_s, wd.to_s, :force => true }
 end
  if @name and @name != target.basename.to_s
    new_path = target.dirname + @name
    dont_debug { new_path.rmtree if new_path.exist? }
    debug "Renaming from #{target} to #{new_path}"
    File.rename target.to_s, new_path.to_s
    target = target.dirname+@name
  end
  @staged_path = target
  debug "Staging completed"
end