summaryrefslogtreecommitdiffstats
path: root/lib/puppet/autoload.rb
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-08-14 06:21:03 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-08-14 06:21:03 +0000
commit12452ee9ca294563f2e2724ff36f179004f9846f (patch)
treebee6053e8164f4a8dbf214f1898fafecb1d61f2f /lib/puppet/autoload.rb
parent4d6120a1f77cfe76fafbe32caa5d853449562376 (diff)
downloadpuppet-12452ee9ca294563f2e2724ff36f179004f9846f.tar.gz
puppet-12452ee9ca294563f2e2724ff36f179004f9846f.tar.xz
puppet-12452ee9ca294563f2e2724ff36f179004f9846f.zip
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
Diffstat (limited to 'lib/puppet/autoload.rb')
-rw-r--r--lib/puppet/autoload.rb101
1 files changed, 101 insertions, 0 deletions
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$