Update version according to OSCI-856
[packages/precise/mcollective.git] / lib / mcollective / pluginpackager.rb
1 module MCollective
2   module PluginPackager
3     # Plugin definition classes
4     autoload :AgentDefinition, "mcollective/pluginpackager/agent_definition"
5     autoload :StandardDefinition, "mcollective/pluginpackager/standard_definition"
6
7     # Package implementation plugins
8     def self.load_packagers
9       PluginManager.find_and_load("pluginpackager")
10     end
11
12     def self.[](klass)
13       const_get("#{klass}")
14     end
15
16     # Fetch and return metadata from plugin DDL
17     def self.get_metadata(path, type)
18       ddl = DDL.new("package", type.to_sym, false)
19
20       begin
21         ddl_file = File.read(Dir.glob(File.join(path, type, "*.ddl")).first)
22       rescue Exception
23         raise "failed to load ddl file in plugin directory : #{File.join(path, type)}"
24       end
25       ddl.instance_eval ddl_file
26
27       return ddl.meta, ddl.requirements[:mcollective]
28     end
29
30     # Checks if a directory is present and not empty
31     def self.check_dir_present(path)
32       (File.directory?(path) && !Dir.glob(File.join(path, "*")).empty?)
33     end
34
35     # Quietly calls a block if verbose parameter is false
36     def self.execute_verbosely(verbose, &block)
37       unless verbose
38         old_stdout = $stdout.clone
39         $stdout.reopen(File.new("/dev/null", "w"))
40         begin
41           block.call
42         rescue Exception => e
43           $stdout.reopen old_stdout
44           raise e
45         ensure
46           $stdout.reopen old_stdout
47         end
48       else
49         block.call
50       end
51     end
52
53     # Checks if a build tool is present on the system
54     def self.command_available?(build_tool)
55       ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
56         builder = File.join(path, build_tool)
57         if File.exists?(builder)
58           return true
59         end
60       end
61       false
62     end
63
64     def self.safe_system(*args)
65       raise(RuntimeError, "Failed: #{args.join(' ')}") unless system *args
66     end
67
68     # Filter out platform specific dependencies
69     # Given a list of dependencies named -
70     # debian::foo
71     # redhat::bar
72     # PluginPackager.filter_dependencies('debian', dependencies)
73     # will return foo.
74     def self.filter_dependencies(prefix, dependencies)
75       dependencies.map do |dependency|
76         if dependency[:name] =~ /^(\w+)::(\w+)/
77           if prefix == $1
78             dependency[:name] = $2
79             dependency
80           else
81             nil
82           end
83         else
84           dependency
85         end
86       end.reject{ |dependency| dependency == nil }
87     end
88   end
89 end