summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/autoload.rb
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-02-07 23:56:59 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-02-07 23:56:59 +0000
commit6d8068eddd0d29ec53f62557eb53f6ebb8e40591 (patch)
tree8c93181b9325fee95d7ecdc6e79341ff6d3604b0 /lib/puppet/util/autoload.rb
parent162602323406117444ce4375ead91d8f92f2b31a (diff)
downloadpuppet-6d8068eddd0d29ec53f62557eb53f6ebb8e40591.tar.gz
puppet-6d8068eddd0d29ec53f62557eb53f6ebb8e40591.tar.xz
puppet-6d8068eddd0d29ec53f62557eb53f6ebb8e40591.zip
Moving some of the stand-alone classes into the util/ subdirectory, to clean up the top-level namespace a bit. This is a lot of file modifications, but most of them just change class names and file paths.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2178 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/util/autoload.rb')
-rw-r--r--lib/puppet/util/autoload.rb107
1 files changed, 107 insertions, 0 deletions
diff --git a/lib/puppet/util/autoload.rb b/lib/puppet/util/autoload.rb
new file mode 100644
index 000000000..f171254af
--- /dev/null
+++ b/lib/puppet/util/autoload.rb
@@ -0,0 +1,107 @@
+# Autoload paths, either based on names or all at once.
+class Puppet::Util::Autoload
+ include Puppet::Util
+
+ @autoloaders = {}
+
+ attr_accessor :object, :path, :objwarn, :wrap
+
+
+ class << self
+ attr_reader :autoloaders
+ private :autoloaders
+ end
+ Puppet::Util.classproxy self, :autoloaders, "[]", "[]=", :clear
+
+ attr_reader :loaded
+ private :loaded
+
+ Puppet::Util.proxy self, :loaded, :clear
+
+ def handle_libdir
+ dir = Puppet[:libdir]
+
+ $: << dir unless $:.include?(dir)
+ end
+
+ def initialize(obj, path, options = {})
+ @path = path.to_s
+ @object = obj
+
+ self.class[obj] = self
+
+ options.each do |opt, value|
+ opt = opt.intern if opt.is_a? String
+ begin
+ self.send(opt.to_s + "=", value)
+ rescue NoMethodError
+ raise ArgumentError, "%s is not a valid option" % opt
+ end
+ end
+
+ unless defined? @wrap
+ @wrap = true
+ end
+
+ @loaded = {}
+ end
+
+ def load(name)
+ name = symbolize(name)
+ handle_libdir()
+
+ path = File.join(@path, name.to_s + ".rb")
+
+ begin
+ Kernel.load path, @wrap
+ @loaded[name] = true
+ return true
+ rescue LoadError => detail
+ # I have no idea what's going on here, but different versions
+ # of ruby are raising different errors on missing files.
+ unless detail.to_s =~ /^no such file/i
+ warn "Could not autoload %s: %s" % [name, detail]
+ if Puppet[:trace]
+ puts detail.backtrace
+ end
+ end
+ return false
+ end
+ end
+
+ def loaded?(name)
+ name = symbolize(name)
+ @loaded[name]
+ end
+
+ def loadall
+ handle_libdir()
+ # Load every instance of everything we can find.
+ $:.each do |dir|
+ fdir = File.join(dir, @path)
+ if FileTest.exists?(fdir) and FileTest.directory?(fdir)
+ Dir.glob("#{fdir}/*.rb").each do |file|
+ # Load here, rather than require, so that facts
+ # can be reloaded. This has some short-comings, I
+ # believe, but it works as long as real classes
+ # aren't used.
+ name = File.basename(file).sub(".rb", '').intern
+ next if @loaded.include? name
+ next if $".include?(File.join(@path, name.to_s + ".rb"))
+ filepath = File.join(@path, name.to_s + ".rb")
+ begin
+ Kernel.require filepath
+ @loaded[name] = true
+ rescue => detail
+ if Puppet[:trace]
+ puts detail.backtrace
+ end
+ warn "Could not autoload %s: %s" % [file.inspect, detail]
+ end
+ end
+ end
+ end
+ end
+end
+
+# $Id$