diff options
author | Luke Kanies <luke@madstop.com> | 2009-05-17 17:10:16 -0500 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-05-20 18:29:04 +1000 |
commit | d489a2b9e2806beefd41627adf7e20d23b0924d6 (patch) | |
tree | afdfa61e1bd6d2704826467abb5b022050494684 /lib/puppet/util/autoload.rb | |
parent | 5f1c228767432591a7e4b77666e9b1c7d70a095c (diff) | |
download | puppet-d489a2b9e2806beefd41627adf7e20d23b0924d6.tar.gz puppet-d489a2b9e2806beefd41627adf7e20d23b0924d6.tar.xz puppet-d489a2b9e2806beefd41627adf7e20d23b0924d6.zip |
Adding modulepath caching to the Autoloader
There's more caching to add, but this simplifies
the interface to the list of paths and then caches
that list so we aren't constantly searching the
filesystem.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet/util/autoload.rb')
-rw-r--r-- | lib/puppet/util/autoload.rb | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/lib/puppet/util/autoload.rb b/lib/puppet/util/autoload.rb index 0c80f8b06..7c176779a 100644 --- a/lib/puppet/util/autoload.rb +++ b/lib/puppet/util/autoload.rb @@ -1,9 +1,11 @@ require 'puppet/util/warnings' +require 'puppet/util/cacher' # Autoload paths, either based on names or all at once. class Puppet::Util::Autoload include Puppet::Util include Puppet::Util::Warnings + include Puppet::Util::Cacher @autoloaders = {} @loaded = [] @@ -70,9 +72,9 @@ class Puppet::Util::Autoload def load(name) path = name.to_s + ".rb" - eachdir do |dir| + searchpath.each do |dir| file = File.join(dir, path) - next unless FileTest.exists?(file) + next unless FileTest.exist?(file) begin Kernel.load file, @wrap name = symbolize(name) @@ -108,7 +110,7 @@ class Puppet::Util::Autoload # so that already-loaded files don't get reloaded unnecessarily. def loadall # Load every instance of everything we can find. - eachdir do |dir| + searchpath.each do |dir| Dir.glob("#{dir}/*.rb").each do |file| name = File.basename(file).sub(".rb", '').intern next if loaded?(name) @@ -125,22 +127,25 @@ class Puppet::Util::Autoload end end - # Yield each subdir in turn. - def eachdir - searchpath.each do |dir| - subdir = File.join(dir, @path) - yield subdir if FileTest.directory?(subdir) + # The list of directories to search through for loadable plugins. + # We have to hard-code the ttl because this library is used by + # so many other classes it's hard to get the load-order such that + # the defaults load before this. + cached_attr(:searchpath, :ttl => 15) do + search_directories.collect { |d| File.join(d, @path) }.find_all { |d| FileTest.directory?(d) } + end + + def module_directories + Puppet.settings.value(:modulepath, Puppet[:environment]).find_all do |dir| + FileTest.directory?(dir) + end.collect do |dir| + Dir.entries(dir) + end.flatten.collect { |d| [File.join(d, "plugins"), File.join(d, "lib")] }.flatten.find_all do |d| + FileTest.directory?(d) end end - # The list of directories to search through for loadable plugins. - def searchpath - # JJM: Search for optional lib directories in each module bundle. - module_lib_dirs = Puppet[:modulepath].split(":").collect do |d| - Dir.glob("%s/*/{plugins,lib}" % d).select do |f| - FileTest.directory?(f) - end - end.flatten - [module_lib_dirs, Puppet[:libdir], $:].flatten + def search_directories + [module_directories, Puppet[:libdir], $:].flatten end end |