Class: Puppet::Util::Autoload
- Includes:
- Puppet::Util, FileCache, Cacher, Warnings
- Defined in:
- lib/puppet/util/autoload.rb
Overview
Autoload paths, either based on names or all at once.
Defined Under Namespace
Modules: FileCache
Class Attribute Summary collapse
-
.autoloaders ⇒ Object
readonly
Returns the value of attribute autoloaders.
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
-
#objwarn ⇒ Object
Returns the value of attribute objwarn.
-
#path ⇒ Object
Returns the value of attribute path.
-
#wrap ⇒ Object
Returns the value of attribute wrap.
Attributes included from Cacher::Expirer
Class Method Summary collapse
-
.list_loaded ⇒ Object
List all loaded files.
-
.loaded(file) ⇒ Object
Save the fact that a given path has been loaded.
-
.loaded?(path) ⇒ Boolean
Has a given path been loaded? This is used for testing whether a changed file should be loaded or just ignored.
Instance Method Summary collapse
-
#initialize(obj, path, options = {}) ⇒ Autoload
constructor
A new instance of Autoload.
-
#load(name, env = nil) ⇒ Object
Load a single plugin by name.
-
#loadall ⇒ Object
Load all instances that we can.
-
#loaded(name, file) ⇒ Object
Mark the named object as loaded.
-
#loaded?(name) ⇒ Boolean
Indicate whether the specfied plugin has been loaded.
- #module_directories(env = nil) ⇒ Object
- #search_directories(env = nil) ⇒ Object
-
#searchpath(env = nil) ⇒ Object
The list of directories to search through for loadable plugins.
Methods included from FileCache
clear, #directory_exist?, #file_exist?, #found_file, #found_file?, #found_files, #missing_file, #missing_file?, #missing_files
Methods included from Cacher
Methods included from Cacher::Expirer
#dependent_data_expired?, #expire
Methods included from Warnings
clear_warnings, notice_once, warnonce
Methods included from Puppet::Util
activerecord_version, benchmark, chuser, classproxy, #execfail, #execpipe, execute, logmethods, memory, proxy, recmkdir, secure_open, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, which, withumask
Methods included from POSIX
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Constructor Details
#initialize(obj, path, options = {}) ⇒ Autoload
Returns a new instance of Autoload.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/puppet/util/autoload.rb', line 51 def initialize(obj, path, = {}) @path = path.to_s raise ArgumentError, "Autoload paths cannot be fully qualified" if @path !~ /^\w/ @object = obj self.class[obj] = self .each do |opt, value| opt = opt.intern if opt.is_a? String begin self.send(opt.to_s + "=", value) rescue NoMethodError raise ArgumentError, "#{opt} is not a valid option" end end @wrap = true unless defined?(@wrap) end |
Class Attribute Details
.autoloaders ⇒ Object (readonly)
Returns the value of attribute autoloaders.
17 18 19 |
# File 'lib/puppet/util/autoload.rb', line 17 def autoloaders @autoloaders end |
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
49 50 51 |
# File 'lib/puppet/util/autoload.rb', line 49 def object @object end |
#objwarn ⇒ Object
Returns the value of attribute objwarn.
49 50 51 |
# File 'lib/puppet/util/autoload.rb', line 49 def objwarn @objwarn end |
#path ⇒ Object
Returns the value of attribute path.
49 50 51 |
# File 'lib/puppet/util/autoload.rb', line 49 def path @path end |
#wrap ⇒ Object
Returns the value of attribute wrap.
49 50 51 |
# File 'lib/puppet/util/autoload.rb', line 49 def wrap @wrap end |
Class Method Details
.list_loaded ⇒ Object
List all loaded files.
25 26 27 28 29 |
# File 'lib/puppet/util/autoload.rb', line 25 def self.list_loaded @loaded.sort { |a,b| a[0] <=> b[0] }.collect do |path, hash| "#{path}: #{hash[:file]}" end end |
.loaded(file) ⇒ Object
Save the fact that a given path has been loaded. This is so we can load downloaded plugins if they’ve already been loaded into memory.
44 45 46 47 |
# File 'lib/puppet/util/autoload.rb', line 44 def self.loaded(file) $" << file + ".rb" unless $".include?(file) @loaded << file unless @loaded.include?(file) end |
.loaded?(path) ⇒ Boolean
Has a given path been loaded? This is used for testing whether a changed file should be loaded or just ignored. This is only used in network/client/master, when downloading plugins, to see if a given plugin is currently loaded and thus should be reloaded.
36 37 38 39 |
# File 'lib/puppet/util/autoload.rb', line 36 def self.loaded?(path) path = path.to_s.sub(/\.rb$/, '') @loaded.include?(path) end |
Instance Method Details
#load(name, env = nil) ⇒ Object
Load a single plugin by name. We use ‘load’ here so we can reload a given plugin.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/puppet/util/autoload.rb', line 72 def load(name,env=nil) path = name.to_s + ".rb" searchpath(env).each do |dir| file = File.join(dir, path) next unless file_exist?(file) begin Kernel.load file, @wrap name = symbolize(name) loaded name, file return true rescue SystemExit,NoMemoryError raise rescue Exception => detail puts detail.backtrace if Puppet[:trace] raise Puppet::Error, "Could not autoload #{name}: #{detail}" end end false end |
#loadall ⇒ Object
Load all instances that we can. This uses require, rather than load, so that already-loaded files don’t get reloaded unnecessarily.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/puppet/util/autoload.rb', line 106 def loadall # Load every instance of everything we can find. searchpath.each do |dir| Dir.glob("#{dir}/*.rb").each do |file| name = File.basename(file).sub(".rb", '').intern next if loaded?(name) begin Kernel.require file loaded(name, file) rescue SystemExit,NoMemoryError raise rescue Exception => detail puts detail.backtrace if Puppet[:trace] raise Puppet::Error, "Could not autoload #{file}: #{detail}" end end end end |
#loaded(name, file) ⇒ Object
Mark the named object as loaded. Note that this supports unqualified queries, while we store the result as a qualified query in the class.
95 96 97 |
# File 'lib/puppet/util/autoload.rb', line 95 def loaded(name, file) self.class.loaded(File.join(@path, name.to_s)) end |
#loaded?(name) ⇒ Boolean
Indicate whether the specfied plugin has been loaded.
100 101 102 |
# File 'lib/puppet/util/autoload.rb', line 100 def loaded?(name) self.class.loaded?(File.join(@path, name.to_s)) end |
#module_directories(env = nil) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/puppet/util/autoload.rb', line 130 def module_directories(env=nil) # We have to require this late in the process because otherwise we might have # load order issues. require 'puppet/node/environment' real_env = Puppet::Node::Environment.new(env) # We're using a per-thread cache of said module directories, so that # we don't scan the filesystem each time we try to load something with # this autoload instance. But since we don't want to cache for the eternity # this env_module_directories gets reset after the compilation on the master. # This is also reset after an agent ran. # One of the side effect of this change is that this module directories list will be # shared among all autoload that we have running at a time. But that won't be an issue # as by definition those directories are shared by all autoload. Thread.current[:env_module_directories] ||= {} Thread.current[:env_module_directories][real_env] ||= real_env.modulepath.collect do |dir| Dir.entries(dir).reject { |f| f =~ /^\./ }.collect { |f| File.join(dir, f) } end.flatten.collect { |d| [File.join(d, "plugins"), File.join(d, "lib")] }.flatten.find_all do |d| FileTest.directory?(d) end end |