class Puppet::ModuleTool::Applications::Unpacker

Public Class Methods

harmonize_ownership(source, target) click to toggle source
   # File lib/puppet/module_tool/applications/unpacker.rb
16 def self.harmonize_ownership(source, target)
17   unless Puppet::Util::Platform.windows?
18     source = Pathname.new(source) unless source.respond_to?(:stat)
19     target = Pathname.new(target) unless target.respond_to?(:stat)
20 
21     FileUtils.chown_R(source.stat.uid, source.stat.gid, target)
22   end
23 end
new(filename, options = {}) click to toggle source
   # File lib/puppet/module_tool/applications/unpacker.rb
25 def initialize(filename, options = {})
26   @filename = Pathname.new(filename)
27   super(options)
28   @module_path = Pathname(options[:target_dir])
29 end
unpack(filename, target) click to toggle source
   # File lib/puppet/module_tool/applications/unpacker.rb
 9 def self.unpack(filename, target)
10   app = self.new(filename, :target_dir => target)
11   app.unpack
12   app.sanity_check
13   app.move_into(target)
14 end

Public Instance Methods

module_name() click to toggle source

@api private

   # File lib/puppet/module_tool/applications/unpacker.rb
77 def module_name
78   metadata = Puppet::Util::Json.load((root_dir + 'metadata.json').read)
79   metadata['name'][/-(.*)/, 1]
80 end
move_into(dir) click to toggle source

@api private

   # File lib/puppet/module_tool/applications/unpacker.rb
83 def move_into(dir)
84   dir = Pathname.new(dir)
85   dir.rmtree if dir.exist?
86   FileUtils.mv(root_dir, dir)
87 ensure
88   FileUtils.rmtree(tmpdir)
89 end
root_dir() click to toggle source

@api private

   # File lib/puppet/module_tool/applications/unpacker.rb
63 def root_dir
64   return @root_dir if @root_dir
65 
66   # Grab the first directory containing a metadata.json file
67   metadata_file = Dir["#{tmpdir}/**/metadata.json"].sort_by(&:length)[0]
68 
69   if metadata_file
70     @root_dir = Pathname.new(metadata_file).dirname
71   else
72     raise _("No valid metadata.json found!")
73   end
74 end
run() click to toggle source
   # File lib/puppet/module_tool/applications/unpacker.rb
31 def run
32   unpack
33   sanity_check
34   module_dir = @module_path + module_name
35   move_into(module_dir)
36 
37   # Return the Pathname object representing the directory where the
38   # module release archive was unpacked the to.
39   return module_dir
40 end
sanity_check() click to toggle source

@api private Error on symlinks and other junk

   # File lib/puppet/module_tool/applications/unpacker.rb
44 def sanity_check
45   symlinks = Dir.glob("#{tmpdir}/**/*", File::FNM_DOTMATCH).map { |f| Pathname.new(f) }.select {|p| Puppet::FileSystem.symlink? p}
46   tmpdirpath = Pathname.new tmpdir
47 
48   symlinks.each do |s|
49     Puppet.warning _("Symlinks in modules are unsupported. Please investigate symlink %{from}->%{to}.") % { from: s.relative_path_from(tmpdirpath), to: Puppet::FileSystem.readlink(s) }
50   end
51 end
tmpdir() click to toggle source

Obtain a suitable temporary path for unpacking tarballs

@api private @return [String] path to temporary unpacking location

   # File lib/puppet/module_tool/applications/unpacker.rb
95 def tmpdir
96   @dir ||= Dir.mktmpdir('tmp', Puppet::Forge::Cache.base_path)
97 end
unpack() click to toggle source

@api private

   # File lib/puppet/module_tool/applications/unpacker.rb
54 def unpack
55   begin
56     Puppet::ModuleTool::Tar.instance.unpack(@filename.to_s, tmpdir, [@module_path.stat.uid, @module_path.stat.gid].join(':'))
57   rescue Puppet::ExecutionFailure => e
58     raise RuntimeError, _("Could not extract contents of module archive: %{message}") % { message: e.message }
59   end
60 end