Module: Kmc::PackageDownloads

Defined in:
lib/kmc/package_downloads.rb

Class Method Summary collapse

Class Method Details

.download_and_unzip_package(package, opts = {}) ⇒ Object

Downloads and unzips a package. This will call #download! on its own, and will return the location where the package was downloaded to as a Pathname.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kmc/package_downloads.rb', line 7

def download_and_unzip_package(package, opts = {})
  download_file = download_package(package, opts)
  output_path = Pathname.new(download_file.path).parent.to_s

  return output_path if package.do_not_unzip

  Util.log "Unzipping ..."
  unzip_file(download_file.path, output_path)

  File.delete(File.absolute_path(download_file))

  output_path
end

.download_package(package, opts = {}) ⇒ Object

Downloads the zipfile for a package using its URL, unless a cached version is found first. Uses DownloadUrl to intelligently resolve download URLs.

Returns the file downloaded, which is created in a temp directory.



25
26
27
28
29
30
31
# File 'lib/kmc/package_downloads.rb', line 25

def download_package(package, opts = {})
  download_uri, downloaded_file = fetch_package_file(package)

  save_to_cache(package, downloaded_file) if opts[:cache_after_download]

  download_to_tempdir(download_file_name(download_uri), downloaded_file)
end

.download_to_tempdir(file_name, file_contents) ⇒ Object

Places ‘file_contents` into a file called `file_name` in a temporary directory.



48
49
50
51
52
53
54
55
56
# File 'lib/kmc/package_downloads.rb', line 48

def download_to_tempdir(file_name, file_contents)
  tmpdir = Dir.mktmpdir

  file = File.new(File.join(tmpdir, file_name), 'wb+')
  file.write(file_contents)
  file.close

  file
end

.unzip_file(zipfile_path, output_path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kmc/package_downloads.rb', line 33

def unzip_file(zipfile_path, output_path)
  Zip::File.open(zipfile_path) do |zip_file|
    zip_file.each do |entry|
      destination = File.join(output_path, entry.name)
      parent_dir = File.expand_path('..', destination)

      FileUtils.mkdir_p(parent_dir) unless File.exists?(parent_dir)

      entry.extract(destination)
    end
  end
end