From 12452ee9ca294563f2e2724ff36f179004f9846f Mon Sep 17 00:00:00 2001 From: luke Date: Mon, 14 Aug 2006 06:21:03 +0000 Subject: Merging r1468 from the implementations branch with r1438 from when the branch was first created. git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1469 980ebf18-57e1-0310-9a29-db15c13687c0 --- lib/puppet/autoload.rb | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 lib/puppet/autoload.rb (limited to 'lib/puppet/autoload.rb') diff --git a/lib/puppet/autoload.rb b/lib/puppet/autoload.rb new file mode 100644 index 000000000..4e5d07e31 --- /dev/null +++ b/lib/puppet/autoload.rb @@ -0,0 +1,101 @@ +# I have no idea what's going on here, but... +unless defined? MissingSourceFile + class MissingSourceFile < RuntimeError + end +end +# Autoload paths, either based on names or all at once. +class Puppet::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 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) + + path = File.join(@path, name.to_s + ".rb") + + begin + Kernel.load path, @wrap + @loaded[name] = true + return true + rescue MissingSourceFile + return false + 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] + end + return false + end + end + + def loaded?(name) + name = symbolize(name) + @loaded[name] + end + + def loadall + # 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 + begin + Kernel.load file, @wrap + @loaded[name] = true + rescue => detail + #if Puppet[:debug] + # puts detail.backtrace + #end + warn "Could not autoload %s: %s" % [file.inspect, detail] + end + end + end + end + end +end + +# $Id$ -- cgit