Class: Puppet::Util::Autoload
Overview
Autoload paths, either based on names or all at once.
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#requiredopts, #set_options, #symbolize_options
Constructor Details
#initialize(obj, path, options = {}) ⇒ Autoload
Returns a new instance of Autoload.
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/puppet/util/autoload.rb', line 186
def initialize(obj, path, options = {})
@path = path.to_s
raise ArgumentError, "Autoload paths cannot be fully qualified" if Puppet::Util.absolute_path?(@path)
@object = obj
self.class[obj] = self
set_options(options)
@wrap = true unless defined?(@wrap)
end
|
Class Attribute Details
.autoloaders ⇒ Object
14
15
16
|
# File 'lib/puppet/util/autoload.rb', line 14
def autoloaders
@autoloaders
end
|
15
16
17
|
# File 'lib/puppet/util/autoload.rb', line 15
def loaded
@loaded
end
|
Instance Attribute Details
184
185
186
|
# File 'lib/puppet/util/autoload.rb', line 184
def object
@object
end
|
184
185
186
|
# File 'lib/puppet/util/autoload.rb', line 184
def objwarn
@objwarn
end
|
184
185
186
|
# File 'lib/puppet/util/autoload.rb', line 184
def path
@path
end
|
184
185
186
|
# File 'lib/puppet/util/autoload.rb', line 184
def wrap
@wrap
end
|
Class Method Details
.changed?(name) ⇒ Boolean
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/puppet/util/autoload.rb', line 42
def changed?(name)
name = cleanpath(name).chomp('.rb')
return true unless loaded.include?(name)
file, old_mtime = loaded[name]
environment = Puppet.lookup(:current_environment)
return true unless file == get_file(name, environment)
begin
old_mtime.to_i != File.mtime(file).to_i
rescue Errno::ENOENT
true
end
end
|
.cleanpath(path) ⇒ Object
Normalize a path. This converts ALT_SEPARATOR to SEPARATOR on Windows and eliminates unnecessary parts of a path.
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/puppet/util/autoload.rb', line 169
def cleanpath(path)
if Puppet::Util.absolute_path?(path)
File.expand_path(path)
else
Pathname.new(path).cleanpath.to_s
end
end
|
.files_in_dir(dir, path) ⇒ Object
97
98
99
100
101
102
|
# File 'lib/puppet/util/autoload.rb', line 97
def files_in_dir(dir, path)
dir = Pathname.new(File.expand_path(dir))
Dir.glob(File.join(dir, path, "*.rb")).collect do |file|
Pathname.new(file).relative_path_from(dir).to_s
end
end
|
.files_to_load(path) ⇒ Object
93
94
95
|
# File 'lib/puppet/util/autoload.rb', line 93
def files_to_load(path)
search_directories(nil).map {|dir| files_in_dir(dir, path) }.flatten.uniq
end
|
.gem_directories ⇒ Object
159
160
161
|
# File 'lib/puppet/util/autoload.rb', line 159
def gem_directories
gem_source.directories
end
|
.get_file(name, env) ⇒ Object
Get the correct file to load for a given path returns nil if no file is found
87
88
89
90
91
|
# File 'lib/puppet/util/autoload.rb', line 87
def get_file(name, env)
name = name + '.rb' unless name =~ /\.rb$/
path = search_directories(env).find { |dir| Puppet::FileSystem.exist?(File.join(dir, name)) }
path and File.join(path, name)
end
|
149
150
151
152
153
154
155
156
157
|
# File 'lib/puppet/util/autoload.rb', line 149
def libdirs()
if (Puppet.settings.app_defaults_initialized?)
Puppet[:libdir].split(File::PATH_SEPARATOR)
else
[]
end
end
|
.load_file(name, env) ⇒ Object
Load a single plugin by name. We use ‘load’ here so we can reload a given plugin.
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/puppet/util/autoload.rb', line 57
def load_file(name, env)
file = get_file(name.to_s, env)
return false unless file
begin
mark_loaded(name, file)
Kernel.load file, @wrap
return true
rescue SystemExit,NoMemoryError
raise
rescue Exception => detail
message = "Could not autoload #{name}: #{detail}"
Puppet.log_exception(detail, message)
raise Puppet::Error, message, detail.backtrace
end
end
|
.loadall(path) ⇒ Object
73
74
75
76
77
78
79
|
# File 'lib/puppet/util/autoload.rb', line 73
def loadall(path)
files_to_load(path).each do |file|
name = file.chomp(".rb")
load_file(name, nil) unless loaded?(name)
end
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.
27
28
29
30
|
# File 'lib/puppet/util/autoload.rb', line 27
def loaded?(path)
path = cleanpath(path).chomp('.rb')
loaded.include?(path)
end
|
.mark_loaded(name, 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.
35
36
37
38
39
40
|
# File 'lib/puppet/util/autoload.rb', line 35
def mark_loaded(name, file)
name = cleanpath(name).chomp('.rb')
ruby_file = name + ".rb"
$LOADED_FEATURES << ruby_file unless $LOADED_FEATURES.include?(ruby_file)
loaded[name] = [file, File.mtime(file)]
end
|
.module_directories(env) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/puppet/util/autoload.rb', line 104
def module_directories(env)
$env_module_directories ||= {}
if Puppet.settings.app_defaults_initialized?
env ||= Puppet.lookup(:environments).get(Puppet[:environment])
if env
$env_module_directories[env] ||= env.modulepath.collect do |dir|
Dir.entries(dir).reject { |f| f =~ /^\./ }.collect { |f| File.join(dir, f, "lib") }
end.flatten.find_all do |d|
FileTest.directory?(d)
end
else
[]
end
else
[]
end
end
|
.reload_changed ⇒ Object
81
82
83
|
# File 'lib/puppet/util/autoload.rb', line 81
def reload_changed
loaded.keys.each { |file| load_file(file, nil) if changed?(file) }
end
|
.search_directories(env) ⇒ Object
163
164
165
|
# File 'lib/puppet/util/autoload.rb', line 163
def search_directories(env)
[gem_directories, module_directories(env), libdirs(), $LOAD_PATH].flatten
end
|
Instance Method Details
#changed?(name) ⇒ Boolean
220
221
222
|
# File 'lib/puppet/util/autoload.rb', line 220
def changed?(name)
self.class.changed?(expand(name))
end
|
#expand(name) ⇒ Object
228
229
230
|
# File 'lib/puppet/util/autoload.rb', line 228
def expand(name)
::File.join(@path, name.to_s)
end
|
#files_to_load ⇒ Object
224
225
226
|
# File 'lib/puppet/util/autoload.rb', line 224
def files_to_load
self.class.files_to_load(@path)
end
|
#load(name, env = nil) ⇒ Object
198
199
200
|
# File 'lib/puppet/util/autoload.rb', line 198
def load(name, env = nil)
self.class.load_file(expand(name), env)
end
|
Load all instances from a path of Autoload.search_directories matching the relative path this Autoloader was initialized with. For example, if we have created a Puppet::Util::Autoload for Puppet::Type::User with a path of ‘puppet/provider/user’, the search_directories path will be searched for all ruby files matching puppet/provider/user/*.rb and they will then be loaded from the first directory in the search path providing them. So earlier entries in the search path may shadow later entries.
This uses require, rather than load, so that already-loaded files don’t get reloaded unnecessarily.
212
213
214
|
# File 'lib/puppet/util/autoload.rb', line 212
def loadall
self.class.loadall(@path)
end
|
#loaded?(name) ⇒ Boolean
216
217
218
|
# File 'lib/puppet/util/autoload.rb', line 216
def loaded?(name)
self.class.loaded?(expand(name))
end
|