Class: AsProject::AbstractRemoteFileTask

Inherits:
Object
  • Object
show all
Includes:
Archive::Tar
Defined in:
lib/tasks/remote_file_task.rb

Overview

Concrete instances returned from User objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, user) {|_self| ... } ⇒ AbstractRemoteFileTask

Returns a new instance of AbstractRemoteFileTask.

Yields:

  • (_self)

Yield Parameters:



81
82
83
84
85
86
# File 'lib/tasks/remote_file_task.rb', line 81

def initialize(name, user)
  @name = name
  @user = user
  yield self if block_given?
  define
end

Instance Attribute Details

#extracted_fileObject

Returns the value of attribute extracted_file.



76
77
78
# File 'lib/tasks/remote_file_task.rb', line 76

def extracted_file
  @extracted_file
end

#mounted_pathObject

Returns the value of attribute mounted_path.



76
77
78
# File 'lib/tasks/remote_file_task.rb', line 76

def mounted_path
  @mounted_path
end

#nameObject

Returns the value of attribute name.



76
77
78
# File 'lib/tasks/remote_file_task.rb', line 76

def name
  @name
end

#urlObject

Returns the value of attribute url.



76
77
78
# File 'lib/tasks/remote_file_task.rb', line 76

def url
  @url
end

Instance Method Details

#clean_path(path) ⇒ Object



279
280
281
282
283
284
# File 'lib/tasks/remote_file_task.rb', line 279

def clean_path(path)
  if(path.index(' '))
    return %{'#{path}'}
  end
  return path
end

#defineObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tasks/remote_file_task.rb', line 88

def define
  @uri = URI.parse(url)
  downloaded_file = downloaded_file_path
  file downloaded_file do |f|
    get_remote_file(@uri, downloaded_file)
    if(extracted_file_path == downloaded_file)
      File.chmod(0755, extracted_file_path)
    end
  end
  
  if(extracted_file_path != downloaded_file)
    if(!Rake::Task.task_defined?(extracted_file_path))
      file extracted_file_path => downloaded_file do |f|
        if(!File.exists?(extracted_file_path))
          unpack_downloaded_file(downloaded_file, extracted_file_path)
          File.chmod(0755, extracted_file_path)
        end
      end
    end
 end

  task @name => [extracted_file_path]
end

#downloaded_file_pathObject



116
117
118
119
120
# File 'lib/tasks/remote_file_task.rb', line 116

def downloaded_file_path
  parts = @uri.path.split('/')
  file = parts.pop
  return File.join(@user.downloads, @name.to_s, file)
end

#execute(params) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/tasks/remote_file_task.rb', line 286

def execute(params)
 # Execute the system call with params
 # Sometimes DOS wants a 'run' prefix
 # Sometimes OSX wants a 'open' prefix
 # Sometimes Cygwin wants a './' prefix
 # This method should be overridden in
 # subclasses
 target = File.basename(extracted_file_path)
 if(!Logger.debug)
   puts("#{target} #{params}")
 end
 system("#{clean_path(extracted_file_path)} #{params}")
end

#extracted_file_pathObject



112
113
114
# File 'lib/tasks/remote_file_task.rb', line 112

def extracted_file_path
  return File.join(@user.downloads, @name.to_s, extracted_file)
end

#fetch(location, limit = 10) ⇒ Object

Raises:



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/tasks/remote_file_task.rb', line 225

def fetch(location, limit = 10)
  uri = URI.parse(location)

  # Download the file now to the downloads dir
  # If the file is an archive (zip, gz, tar, tar.gz, dmg), extract to
  # AsProject/remote_files/@name/@location
  # Check the location again...
  raise UsageError.new("The RemoteFileTask can only handle HTTP requests at this time, it seems you were trying: #{uri.scheme}") if uri.scheme != 'http'
  raise UsageError.new('HTTP redirect too deep') if limit == 0
  
  response = nil
  t = Thread.new {
    response = Net::HTTP.get_response(uri.host, uri.path)
  }
  # TODO: Add actual bytesLoaded vs bytesTotal
  # To the output...
  while t.alive?
    print(".")
    $stdout.flush
    sleep(0.2)
  end
  
  if(response.is_a? Net::HTTPSuccess)
    return response
  elsif(response.is_a? Net::HTTPRedirection)
    return fetch(response['location'], limit - 1)
  else
    if(response.nil?)
      raise UsageError.new("Network connection failed!")
    else
      puts response.to_s
      return response.error!
    end
  end
end

#get_remote_file(uri, target) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/tasks/remote_file_task.rb', line 261

def get_remote_file(uri, target)
  if(!File.exists?(target))
    puts "Fetching #{uri.to_s} into #{target} now!"
    response = fetch(uri.to_s)
  
    File.makedirs(File.dirname(target))
    # Store the downloaded file on disk
    File.open(target, 'wb') do |f|
      f.write(response.body)
    end
  
    puts ""
    puts "Downloaded #{(response.body.size / 1024).to_s} KB"
    puts "to: #{target}"
    puts ""
  end
end

#is_dmg?(file) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/tasks/remote_file_task.rb', line 208

def is_dmg?(file)
  return (file.split('.').pop == 'dmg')
end

#is_exe?(file) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/tasks/remote_file_task.rb', line 191

def is_exe?(file)
  return (file.split('.').pop == 'exe')
end

#is_gzip?(file) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/tasks/remote_file_task.rb', line 204

def is_gzip?(file)
  return (file.split('.').pop == 'gz')
end

#is_targz?(file) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
202
# File 'lib/tasks/remote_file_task.rb', line 199

def is_targz?(file)
  parts = file.split('.')
  return (parts.pop == 'gz' && parts.pop == 'tar')
end

#is_zip?(file) ⇒ Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/tasks/remote_file_task.rb', line 195

def is_zip?(file)
  return (file.split('.').pop == 'zip')
end

#make_downloaded_dir(file) ⇒ Object



212
213
214
215
# File 'lib/tasks/remote_file_task.rb', line 212

def make_downloaded_dir(file)
  dir = File.dirname(file)
  File.makedirs(dir)
end

#remote_file_dirObject



217
218
219
# File 'lib/tasks/remote_file_task.rb', line 217

def remote_file_dir
  return File.join(@user.downloads, @name.to_s)
end

#remote_locationObject



221
222
223
# File 'lib/tasks/remote_file_task.rb', line 221

def remote_location
  return File.join(remote_file_dir, extracted_file)
end

#unpack_dmg(dmg_file, dir) ⇒ Object

This is actually not unpacking the FlashPlayer Binary file as expected… OSX is treated the player binary as if it is a regular text file, but if it is copied manually, the command works fine!?



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/tasks/remote_file_task.rb', line 164

def unpack_dmg(dmg_file, dir)
  # 1) Mount the dmg in place
  # 2) Recursively Copy it's contents to asproject_home
  # 3) Unmount the dmg
  if(mounted_path.nil?)
    raise StandardError.new('DMG file downloaded, but the RemoteFileTask needs a mounted_path in order to mount it')
  end

  if(!File.exists?(full_mounted_path))
    system("hdiutil mount #{dmg_file}")
  end
  
  mounted_target = File.join(full_mounted_path, extracted_file)

  # Copy the DMG contents using system copy rather than ruby utils
  # Because OS X does something special with .app files that the
  # Ruby FileUtils and File classes break...
  from = full_mounted_path
  to = File.join(@user.downloads, @name.to_s)
  if(File.exists?(from))
    `ditto '#{from}' #{to}`
  end
  if(File.exists?(full_mounted_path))
    system("hdiutil unmount -force \"#{full_mounted_path}\"")
  end
end

#unpack_downloaded_file(file_name, expected_file) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/tasks/remote_file_task.rb', line 122

def unpack_downloaded_file(file_name, expected_file)
  # Remove previously downloaded files...
  dir = File.dirname(file_name)
  Dir[dir + '/*'].each do |child|
    if(child != '.' && child != '..' && child != file_name)
      FileUtils.rm_rf(child)
    end
  end

  if(!File.exists?(expected_file))
    if(is_zip?(file_name))
      unpack_zip(file_name, dir)
    elsif(is_targz?(file_name))
      unpack_targz(file_name, dir)
    elsif(is_dmg?(file_name))
      unpack_dmg(file_name, dir)
    elsif(!is_exe?(file_name))
      raise UsageError.new("RemoteFileTask does not know how to unpack files of type: #{file_name}")
    end
  end
end

#unpack_targz(tgz_file, dir) ⇒ Object



154
155
156
157
# File 'lib/tasks/remote_file_task.rb', line 154

def unpack_targz(tgz_file, dir)
  tar = Zlib::GzipReader.new(File.open(tgz_file, 'rb'))
  Minitar.unpack(tar, dir)
end

#unpack_zip(zip_file, dir) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/tasks/remote_file_task.rb', line 144

def unpack_zip(zip_file, dir)
  Zip::ZipFile::open(zip_file) do |zf|
     zf.each do |e|
       fpath = File.join(dir, e.name)
       FileUtils.mkdir_p(File.dirname(fpath))
       zf.extract(e, fpath)
     end
   end
end