Class: Openlayer::TarFileHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/openlayer/services/tar_file_helper.rb

Constant Summary collapse

LOGGER =
Logger.new($stdout)

Class Method Summary collapse

Class Method Details

.add_directory_to_tar(tar, folder, base_name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/openlayer/services/tar_file_helper.rb', line 40

def self.add_directory_to_tar(tar, folder, base_name)
  Dir[File.join(folder, "**/*")].each do |file|
    mode = File.stat(file).mode
    relative_file = File.join(base_name, file.sub("#{folder}/", ""))

    if File.directory?(file)
      tar.mkdir(relative_file, mode)
    else
      tar.add_file_simple(relative_file, mode, File.size(file)) do |io|
        File.open(file, "rb") { |f| io.write(f.read) }
      end
    end
  end
end

.create_tar_from_folders(folders, tar_file_name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/openlayer/services/tar_file_helper.rb', line 12

def self.create_tar_from_folders(folders, tar_file_name)
  File.open(tar_file_name, "wb") do |file|
    Gem::Package::TarWriter.new(Zlib::GzipWriter.open(file)) do |tar|
      folders.each do |folder|
        base_name = File.basename(folder)
        add_directory_to_tar(tar, folder, base_name)
      end
    end
  end
end

.validate_structure(file_path, required_structure) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/openlayer/services/tar_file_helper.rb', line 23

def self.validate_structure(file_path, required_structure)
  tarfile_structure = []
  begin
    Gem::Package::TarReader.new(Zlib::GzipReader.open(file_path)).each do |entry|
      tarfile_structure << entry.full_name
    end
  rescue Zlib::GzipFile::Error => e
    stacktrace = e.backtrace.join("\n")
    LOGGER.error("ERROR: #{e.message}\n #{stacktrace}")
    return nil
  end

  required_structure.each do |required_file|
    raise Error, "Missing file: #{required_file}" unless tarfile_structure.include?(required_file)
  end
end