From aa741354a75c0d3b4f4b7f37a736a0154d45234c Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 6 Jul 2007 22:22:10 +0000 Subject: Fixing #596 -- classes in modules now autoload git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2655 980ebf18-57e1-0310-9a29-db15c13687c0 --- lib/puppet/module.rb | 115 +++++++++++++++++++++++++++++++++++++++ lib/puppet/modules.rb | 113 -------------------------------------- lib/puppet/parser/interpreter.rb | 64 ++++++++++++++++++++-- 3 files changed, 173 insertions(+), 119 deletions(-) create mode 100644 lib/puppet/module.rb delete mode 100644 lib/puppet/modules.rb (limited to 'lib/puppet') diff --git a/lib/puppet/module.rb b/lib/puppet/module.rb new file mode 100644 index 000000000..87f849d17 --- /dev/null +++ b/lib/puppet/module.rb @@ -0,0 +1,115 @@ +# Support for modules +class Puppet::Module + + TEMPLATES = "templates" + FILES = "files" + MANIFESTS = "manifests" + + # Return an array of paths by splitting the +modulepath+ config + # parameter. Only consider paths that are absolute and existing + # directories + def self.modulepath + dirs = Puppet[:modulepath].split(":") + if ENV["PUPPETLIB"] + dirs = ENV["PUPPETLIB"].split(":") + dirs + end + dirs.select do |p| + p =~ /^#{File::SEPARATOR}/ && File::directory?(p) + end + end + + # Find and return the +module+ that +path+ belongs to. If +path+ is + # absolute, or if there is no module whose name is the first component + # of +path+, return +nil+ + def self.find(path) + if path =~ %r/^#{File::SEPARATOR}/ + return nil + end + + modname, rest = path.split(File::SEPARATOR, 2) + return nil if modname.nil? || modname.empty? + + modpath = modulepath.collect { |p| + File::join(p, modname) + }.find { |f| File::directory?(f) } + return nil unless modpath + + return self.new(modname, modpath) + end + + # Instance methods + + # Find the concrete file denoted by +file+. If +file+ is absolute, + # return it directly. Otherwise try to find it as a template in a + # module. If that fails, return it relative to the +templatedir+ config + # param. + # In all cases, an absolute path is returned, which does not + # necessarily refer to an existing file + def self.find_template(file) + if file =~ /^#{File::SEPARATOR}/ + return file + end + + mod = find(file) + if mod + return mod.template(file) + else + return File.join(Puppet[:templatedir], file) + end + end + + # Return a list of manifests (as absolute filenames) that match +pat+ + # with the current directory set to +cwd+. If the first component of + # +pat+ does not contain any wildcards and is an existing module, return + # a list of manifests in that module matching the rest of +pat+ + # Otherwise, try to find manifests matching +pat+ relative to +cwd+ + def self.find_manifests(pat, cwd = nil) + cwd ||= Dir.getwd + mod = find(pat) + if mod + return mod.manifests(pat) + else + abspat = File::expand_path(pat, cwd) + files = Dir.glob(abspat).reject { |f| FileTest.directory?(f) } + if files.size == 0 + files = Dir.glob(abspat + ".pp").reject { |f| FileTest.directory?(f) } + end + return files + end + end + + attr_reader :name, :path + def initialize(name, path) + @name = name + @path = path + end + + def strip(file) + n, rest = file.split(File::SEPARATOR, 2) + rest = nil if rest && rest.empty? + return rest + end + + def template(file) + return File::join(path, TEMPLATES, strip(file)) + end + + def files + return File::join(path, FILES) + end + + def manifests(pat) + rest = strip(pat) + rest ||= "init.pp" + p = File::join(path, MANIFESTS, rest) + files = Dir.glob(p) + if files.size == 0 + files = Dir.glob(p + ".pp") + end + return files + end + + private :initialize +end + +# $Id$ diff --git a/lib/puppet/modules.rb b/lib/puppet/modules.rb deleted file mode 100644 index aa2f75d03..000000000 --- a/lib/puppet/modules.rb +++ /dev/null @@ -1,113 +0,0 @@ -# Support for modules -class Puppet::Module - - TEMPLATES = "templates" - FILES = "files" - MANIFESTS = "manifests" - - # Return an array of paths by splitting the +modulepath+ config - # parameter. Only consider paths that are absolute and existing - # directories - def self.modulepath - dirs = Puppet[:modulepath].split(":") - if ENV["PUPPETLIB"] - dirs = ENV["PUPPETLIB"].split(":") + dirs - end - dirs.select do |p| - p =~ /^#{File::SEPARATOR}/ && File::directory?(p) - end - end - - # Find and return the +module+ that +path+ belongs to. If +path+ is - # absolute, or if there is no module whose name is the first component - # of +path+, return +nil+ - def self.find(path) - if path =~ %r/^#{File::SEPARATOR}/ - return nil - end - - modname, rest = path.split(File::SEPARATOR, 2) - return nil if modname.nil? || modname.empty? - - modpath = modulepath.collect { |p| - File::join(p, modname) - }.find { |f| File::directory?(f) } - return nil unless modpath - - return self.new(modname, modpath) - end - - # Instance methods - - # Find the concrete file denoted by +file+. If +file+ is absolute, - # return it directly. Otherwise try to find it as a template in a - # module. If that fails, return it relative to the +templatedir+ config - # param. - # In all cases, an absolute path is returned, which does not - # necessarily refer to an existing file - def self.find_template(file) - if file =~ /^#{File::SEPARATOR}/ - return file - end - - mod = find(file) - if mod - return mod.template(file) - else - return File.join(Puppet[:templatedir], file) - end - end - - # Return a list of manifests (as absolute filenames) that match +pat+ - # with the current directory set to +cwd+. If the first component of - # +pat+ does not contain any wildcards and is an existing module, return - # a list of manifests in that module matching the rest of +pat+ - # Otherwise, try to find manifests matching +pat+ relative to +cwd+ - def self.find_manifests(pat, cwd = nil) - cwd ||= Dir.getwd - mod = find(pat) - if mod - return mod.manifests(pat) - else - abspat = File::expand_path(pat, cwd) - files = Dir.glob(abspat).reject { |f| FileTest.directory?(f) } - if files.size == 0 - files = Dir.glob(abspat + ".pp").reject { |f| FileTest.directory?(f) } - end - return files - end - end - - attr_reader :name, :path - def initialize(name, path) - @name = name - @path = path - end - - def strip(file) - n, rest = file.split(File::SEPARATOR, 2) - rest = nil if rest && rest.empty? - return rest - end - - def template(file) - return File::join(path, TEMPLATES, strip(file)) - end - - def files - return File::join(path, FILES) - end - - def manifests(pat) - rest = strip(pat) - rest ||= "init.pp" - p = File::join(path, MANIFESTS, rest) - files = Dir.glob(p) - if files.size == 0 - files = Dir.glob(p + ".pp") - end - return files - end - - private :initialize -end diff --git a/lib/puppet/parser/interpreter.rb b/lib/puppet/parser/interpreter.rb index 3a719e03c..11f5aa15d 100644 --- a/lib/puppet/parser/interpreter.rb +++ b/lib/puppet/parser/interpreter.rb @@ -1,7 +1,3 @@ -# The interepreter's job is to convert from a parsed file to the configuration -# for a given client. It really doesn't do any work on its own, it just collects -# and calls out to other objects. - require 'puppet' require 'timeout' require 'puppet/rails' @@ -9,6 +5,9 @@ require 'puppet/util/methodhelper' require 'puppet/parser/parser' require 'puppet/parser/scope' +# The interpreter's job is to convert from a parsed file to the configuration +# for a given client. It really doesn't do any work on its own, it just collects +# and calls out to other objects. class Puppet::Parser::Interpreter class NodeDef include Puppet::Util::MethodHelper @@ -267,12 +266,65 @@ class Puppet::Parser::Interpreter # Find a class definition, relative to the current namespace. def findclass(namespace, name) - fqfind namespace, name, @classtable + find_or_load namespace, name, @classtable end # Find a component definition, relative to the current namespace. def finddefine(namespace, name) - fqfind namespace, name, @definetable + find_or_load namespace, name, @definetable + end + + # Attempt to find the requested object. If it's not yet loaded, + # attempt to load it. + def find_or_load(namespace, name, table) + if namespace == "" + fullname = name.gsub("::", File::SEPARATOR) + else + fullname = ("%s::%s" % [namespace, name]).gsub("::", File::SEPARATOR) + end + + # See if it's already loaded + if result = fqfind(namespace, name, table) + return result + end + + if fullname == "" + return nil + end + + # Nope. Try to load the module itself, to see if that + # loads it. + mod = fullname.scan(/^[\w-]+/).shift + # We couldn't find it, so try to load the base module + begin + @parser.import(mod) + Puppet.info "Autoloaded module %s" % mod + if result = fqfind(namespace, name, table) + return result + end + rescue Puppet::ImportError => detail + # We couldn't load the module + end + + + # If they haven't specified a subclass, then there's no point in looking for + # a separate file. + if ! fullname.include?("/") + return nil + end + + # Nope. Try to load the individual file + begin + @parser.import(fullname) + Puppet.info "Autloaded file %s from module %s" % [fullname, mod] + if result = fqfind(namespace, name, table) + return result + end + rescue Puppet::ImportError => detail + # We couldn't load the file + end + + return nil end # The recursive method used to actually look these objects up. -- cgit