Class: Puppet::Parser::TypeLoader

Inherits:
Object
  • Object
show all
Includes:
Node::Environment::Helper
Defined in:
lib/vendor/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.



126
127
128
129
# File 'lib/vendor/puppet/parser/type_loader.rb', line 126

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

Instance Method Details

#import(file, current_file = nil) ⇒ Object

Import our files.



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/vendor/puppet/parser/type_loader.rb', line 63

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|
    unless Puppet::Util.absolute_path?(file)
      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
# File 'lib/vendor/puppet/parser/type_loader.rb', line 96

def import_all
  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 => 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



122
123
124
# File 'lib/vendor/puppet/parser/type_loader.rb', line 122

def known_resource_types
  environment.known_resource_types
end

#parse_file(file) ⇒ Object



151
152
153
154
155
156
# File 'lib/vendor/puppet/parser/type_loader.rb', line 151

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.



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

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