Class: PuppetForge::Unpacker

Inherits:
Object
  • Object
show all
Defined in:
lib/shared/puppet_forge/unpacker.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, target, tmpdir) ⇒ Unpacker

Returns a new instance of Unpacker.

Parameters:

  • filename (String)

    the file to unpack

  • target (String)

    the target directory to unpack into



31
32
33
34
35
# File 'lib/shared/puppet_forge/unpacker.rb', line 31

def initialize(filename, target, tmpdir)
  @filename = filename
  @target = target
  @tmpdir = tmpdir
end

Class Method Details

.harmonize_ownership(source, target) ⇒ Object

Set the owner/group of the target directory to those of the source Note: don’t call this function on Microsoft Windows

Parameters:

  • source (Pathname)

    source of the permissions

  • target (Pathname)

    target of the permissions change



25
26
27
# File 'lib/shared/puppet_forge/unpacker.rb', line 25

def self.harmonize_ownership(source, target)
    FileUtils.chown_R(source.stat.uid, source.stat.gid, target)
end

.unpack(filename, target, tmpdir) ⇒ Hash{:symbol => Array<String>}

Unpack a tar file into a specified directory

Parameters:

  • filename (String)

    the file to unpack

  • target (String)

    the target directory to unpack into

Returns:

  • (Hash{:symbol => Array<String>})

    a hash with file-category keys pointing to lists of filenames. The categories are :valid, :invalid and :symlink



13
14
15
16
17
18
# File 'lib/shared/puppet_forge/unpacker.rb', line 13

def self.unpack(filename, target, tmpdir)
  inst = self.new(filename, target, tmpdir)
  file_lists = inst.unpack
  inst.move_into(Pathname.new(target))
  file_lists
end

Instance Method Details

#move_into(dir) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
50
51
52
# File 'lib/shared/puppet_forge/unpacker.rb', line 47

def move_into(dir)
  dir.rmtree if dir.exist?
  FileUtils.mv(root_dir, dir)
ensure
  FileUtils.rmtree(@tmpdir)
end

#root_dirObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/shared/puppet_forge/unpacker.rb', line 55

def root_dir
  return @root_dir if @root_dir

  # Grab the first directory containing a metadata.json file
   = Dir["#{@tmpdir}/**/metadata.json"].sort_by(&:length)[0]

  if 
    @root_dir = Pathname.new().dirname
  else
    raise "No valid metadata.json found!"
  end
end

#unpackObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
41
42
43
44
# File 'lib/shared/puppet_forge/unpacker.rb', line 38

def unpack
  begin
    PuppetForge::Tar.instance.unpack(@filename, @tmpdir)
  rescue PuppetForge::ExecutionFailure => e
    raise RuntimeError, "Could not extract contents of module archive: #{e.message}"
  end
end