Class: Puppet::Parser::TypeLoader

Inherits:
Object
  • Object
show all
Includes:
Node::Environment::Helper
Defined in:
lib/puppet/parser/type_loader.rb

Defined Under Namespace

Classes: Helper

Instance Method Summary collapse

Methods included from Node::Environment::Helper

#environment, #environment=

Constructor Details

#initialize(env) ⇒ TypeLoader

Returns a new instance of TypeLoader.



128
129
130
131
# File 'lib/puppet/parser/type_loader.rb', line 128

def initialize(env)
  self.environment = env
  @loading_helper = Helper.new
end

Instance Method Details

#import(file, current_file = nil) ⇒ Object

Import our files.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/puppet/parser/type_loader.rb', line 62

def import(file, current_file = nil)
  return if Puppet[:ignoreimport]

  # use a path relative to the file doing the importing
  if current_file
    dir = current_file.sub(%r{[^/]+$},'').sub(/\/$/, '')
  else
    dir = "."
  end
  if dir == ""
    dir = "."
  end

  pat = file
  modname, files = Puppet::Parser::Files.find_manifests(pat, :cwd => dir, :environment => environment)
  if files.size == 0
    raise Puppet::ImportError.new("No file(s) found for import of '#{pat}'")
  end

  loaded_asts = []
  files.each do |file|
    regex = Puppet.features.microsoft_windows? ? /^[A-Za-z]:#{File::SEPARATOR}/ : /^#{File::SEPARATOR}/
    unless file =~ regex
      file = File.join(dir, file)
    end
    @loading_helper.do_once(file) do
      loaded_asts << parse_file(file)
    end
  end
  loaded_asts.inject([]) do |loaded_types, ast|
    loaded_types + known_resource_types.import_ast(ast, modname)
  end
end

#import_allObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/puppet/parser/type_loader.rb', line 96

def import_all
  require 'find'

  module_names = []
  # Collect the list of all known modules
  environment.modulepath.each do |path|
    Dir.chdir(path) do
      Dir.glob("*").each do |dir|
        next unless FileTest.directory?(dir)
        module_names << dir
      end
    end
  end

  module_names.uniq!
  # And then load all files from each module, but (relying on system
  # behavior) only load files from the first module of a given name.  E.g.,
  # given first/foo and second/foo, only files from first/foo will be loaded.
  module_names.each do |name|
    mod = Puppet::Module.new(name, environment)
    Find.find(File.join(mod.path, "manifests")) do |path|
      if path =~ /\.pp$/ or path =~ /\.rb$/
        import(path)
      end
    end
  end
end

#known_resource_typesObject



124
125
126
# File 'lib/puppet/parser/type_loader.rb', line 124

def known_resource_types
  environment.known_resource_types
end

#parse_file(file) ⇒ Object



153
154
155
156
157
158
# File 'lib/puppet/parser/type_loader.rb', line 153

def parse_file(file)
  Puppet.debug("importing '#{file}' in environment #{environment}")
  parser = Puppet::Parser::Parser.new(environment)
  parser.file = file
  return parser.parse
end

#try_load_fqname(type, fqname) ⇒ Object

Try to load the object with the given fully qualified name.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/puppet/parser/type_loader.rb', line 134

def try_load_fqname(type, fqname)
  return nil if fqname == "" # special-case main.
  name2files(fqname).each do |filename|
    begin
      imported_types = import(filename)
      if result = imported_types.find { |t| t.type == type and t.name == fqname }
        Puppet.debug "Automatically imported #{fqname} from #{filename} into #{environment}"
        return result
      end
    rescue Puppet::ImportError => detail
      # We couldn't load the item
      # I'm not convienced we should just drop these errors, but this
      # preserves existing behaviours.
    end
  end
  # Nothing found.
  return nil
end