Module: BrickAndMortar::Download

Defined in:
lib/brick_and_mortar/download.rb

Constant Summary collapse

'././@LongLink'

Class Method Summary collapse

Class Method Details

.deflate_tar(tar_string, destination = Dir.pwd) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/brick_and_mortar/download.rb', line 41

def self.deflate_tar(tar_string, destination = Dir.pwd)
  first_directory = nil
  Gem::Package::TarReader.new(tar_string) do |tar|
    dest = nil
    tar.each do |entry|
      if entry.full_name == TAR_LONGLINK
        dest = File.join destination, entry.read.strip
        next
      end
      dest ||= File.join destination, entry.full_name
      if entry.directory?
        FileUtils.rm_rf dest unless File.directory? dest
        FileUtils.mkdir_p dest, :mode => entry.header.mode, :verbose => false
        first_directory = dest unless first_directory
      elsif entry.file?
        FileUtils.rm_rf dest unless File.file? dest
        dir = File.dirname dest
        unless File.exist?(dir)
          FileUtils.mkpath dir
          first_directory = dir unless first_directory
        end
        File.open dest, "wb" do |f|
          f.print entry.read
        end
        FileUtils.chmod entry.header.mode, dest, :verbose => false
        unless first_directory
          puts "WARNING: File \"#{dest}\" created before a root directory"
        end
      elsif entry.header.typeflag == '2' #Symlink!
        dir = File.dirname dest
        unless File.exist?(dir)
          FileUtils.mkpath dir
          first_directory = dir unless first_directory
        end
        File.symlink entry.header.linkname, dest
        unless first_directory
          puts "WARNING: Symlink \"#{dest}\" to \"#{entry.header.linkname}\" created before a root directory"
        end
      end
      dest = nil
    end
  end
  first_directory
end

.deflate_tar_bz2(archive, destination = Dir.pwd) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/brick_and_mortar/download.rb', line 88

def self.deflate_tar_bz2(archive, destination = Dir.pwd)
  Bzip2::FFI::Reader.open(archive) do |reader|
    File.open("#{destination}.tar", 'w') do |tar|
      tar.write reader.read
    end
  end
  first_directory = File.open("#{destination}.tar", 'r') do |tar|
    deflate_tar(tar)
  end
  FileUtils.rm "#{destination}.tar"
  first_directory
end

.deflate_tar_gz(tar_gz_archive, destination = Dir.pwd) ⇒ Object



85
86
87
# File 'lib/brick_and_mortar/download.rb', line 85

def self.deflate_tar_gz(tar_gz_archive, destination = Dir.pwd)
  deflate_tar(Zlib::GzipReader.open(tar_gz_archive), destination)
end

.get_and_unpack_tar_bz2(url, name = nil, options = {}) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/brick_and_mortar/download.rb', line 114

def self.get_and_unpack_tar_bz2(url, name = nil, options = {})
  unless name
    name = name_from_path url
  end
  uncompressed_dir_name = deflate_tar_bz2(open(url))
  FileUtils.mv uncompressed_dir_name, name
end

.get_and_unpack_tar_gz(url, name = nil, options = {}) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/brick_and_mortar/download.rb', line 107

def self.get_and_unpack_tar_gz(url, name = nil, options = {})
  unless name
    name = name_from_path url
  end
  uncompressed_dir_name = deflate_tar_gz(open(url))
  FileUtils.mv uncompressed_dir_name, name
end

.get_and_unpack_zip(url, name = nil, options = {}) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/brick_and_mortar/download.rb', line 100

def self.get_and_unpack_zip(url, name = nil, options = {})
  unless name
    name = name_from_path url
  end
  unzipped_dir_name = unzip(open(url))
  FileUtils.mv unzipped_dir_name, name
end

.name_from_path(url) ⇒ Object



17
18
19
# File 'lib/brick_and_mortar/download.rb', line 17

def self.name_from_path(url)
  File.basename(url, File.extname(url))
end

.unzip(zip_file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/brick_and_mortar/download.rb', line 20

def self.unzip(zip_file)
  root_dir_name = nil
  Zip::File.open(zip_file) do |z|
    z.each do |entry|
      next unless entry.file?
      fpath = entry.to_s
      unless root_dir_name
        root_dir_name = File.dirname fpath
      end
      unless File.exist?(entry.name)
        FileUtils::mkdir_p(File.dirname(entry.name))
        # puts "Extracting #{entry.name} to #{fpath}"
        entry.extract(entry.name)
      end
    end
  end
  root_dir_name
end