Class: VMC::Cli::ZipUtil

Inherits:
Object show all
Defined in:
lib/cli/zip_util.rb

Constant Summary collapse

PACK_EXCLUSION_GLOBS =
['..', '.', '*~', '#*#', '*.log']
BLOCKSIZE_TO_READ =
1024 * 1000

Class Method Summary collapse

Class Method Details

.entry_lines(file) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cli/zip_util.rb', line 21

def entry_lines(file)
  contents = nil
  unless VMC::Cli::Config.nozip
    contents = `unzip -l #{file} 2> #{to_dev_null}`
    contents = nil if $? != 0
  end
  # Do Ruby version if told to or native version failed
  unless contents
    entries = []
    Zip::ZipFile.foreach(file) { |zentry| entries << zentry }
    contents = entries.join("\n")
  end
  contents
end

.get_files_to_pack(dir) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/cli/zip_util.rb', line 51

def get_files_to_pack(dir)
  Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).select do |f|
    process = true
    PACK_EXCLUSION_GLOBS.each { |e| process = false if File.fnmatch(e, File.basename(f)) }
    process && File.exists?(f)
  end
end

.pack(dir, zipfile) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cli/zip_util.rb', line 59

def pack(dir, zipfile)
  unless VMC::Cli::Config.nozip
    excludes = PACK_EXCLUSION_GLOBS.map { |e| "\\#{e}" }
    excludes = excludes.join(' ')
    Dir.chdir(dir) do
      `zip -y -q -r #{zipfile} . -x #{excludes} 2> #{to_dev_null}`
      return unless $? != 0
    end
  end
  # Do Ruby version if told to or native version failed
  Zip::ZipFile::open(zipfile, true) do |zf|
    get_files_to_pack(dir).each do |f|
      zf.add(f.sub("#{dir}/",''), f)
    end
  end
end

.tar(path) ⇒ Object

not a valid tar file, since tar files include last modified date which breaks it for use in hashing. this method just wraps up everything for use in hashing.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cli/zip_util.rb', line 80

def tar(path)
  tar_filename = Pathname.new(path).realpath.to_path + '.tar'
  File.open(tar_filename, 'wb') do |tarfile|
    get_files_to_pack(path).each do |file|
      if File.file?(file)
        File.open(file, 'rb') do |f|
          while buffer = f.read(BLOCKSIZE_TO_READ)
            tarfile.write buffer
          end
        end
      else
        tarfile.write file.gsub(path, '')
      end
    end
  end

  tar_filename

end

.to_dev_nullObject



13
14
15
16
17
18
19
# File 'lib/cli/zip_util.rb', line 13

def to_dev_null
  if WINDOWS
    'nul'
  else
    '/dev/null'
  end
end

.unpack(file, dest) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cli/zip_util.rb', line 36

def unpack(file, dest)
  unless VMC::Cli::Config.nozip
    FileUtils.mkdir(dest)
    `unzip -q #{file} -d #{dest} 2> #{to_dev_null}`
    return unless $? != 0
  end
  # Do Ruby version if told to or native version failed
  Zip::ZipFile.foreach(file) do |zentry|
    epath = "#{dest}/#{zentry}"
    dirname = File.dirname(epath)
    FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
    zentry.extract(epath) unless File.exists?(epath)
  end
end