Class: Origen::SiteConfig

Inherits:
Object show all
Defined in:
lib/origen/site_config.rb,
lib/origen/site_config/config.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

TRUE_VALUES =
['true', 'TRUE', '1', 1]
FALSE_VALUES =
['false', 'FALSE', '0', 0]
NON_BOOLEAN_PARAMETERS =

Adding parameters to this array will prevent them from being converted to booleans if they are assigned one of the values in the TRUE_VALUES/FALSE_VALUES arrays

[:lsf_cores, :centralized_site_config_refresh]

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/origen/site_config.rb', line 144

def method_missing(method, *args, &block)
  method = method.to_s
  if method =~ /(.*)!$/
    method = Regexp.last_match(1)
    must_be_present = true
  end
  val = find_val(method)
  if must_be_present && val.nil?
    puts "No value assigned for site_config attribute '#{method}'"
    puts
    fail 'Missing site_config value!'
  end
  define_singleton_method(method) do
    find_val(method)
  end
  val
end

Instance Method Details

#add_as_highest(var, value) ⇒ Object Also known as: []=

Dynamically add a new site variable at the highest priority.



116
117
118
119
120
121
# File 'lib/origen/site_config.rb', line 116

def add_as_highest(var, value)
  # Don't want to override anything, so just shift in a dummy site config instance at the highest level and
  # set the value there.
  c = Config.new(path: :runtime, parent: self, values: { var.to_s => value })
  configs.prepend(Config.new(path: :runtime, parent: self, values: { var.to_s => value }))
end

#add_as_lowest(var, value) ⇒ Object

Dynamically add a new site variable at the lowest priority. Essentially, this sets a new default value.



126
127
128
129
130
# File 'lib/origen/site_config.rb', line 126

def add_as_lowest(var, value)
  # Don't want to override anything, so just shift in a dummy site config at the lowest level and
  # set the value there.
  configs.append(Config.new(path: :runtime, parent: self, values: { var.to_s => value }))
end

#add_site_config_as_highest(site_config_file) ⇒ Object

Adds a new site config file as the highest priority



133
134
135
136
# File 'lib/origen/site_config.rb', line 133

def add_site_config_as_highest(site_config_file)
  # configs.prepend YAML.load_file(File.expand_path('../../../origen_site_config.yml', __FILE__))
  configs.prepend(Config.new(path: site_config_file, parent: self))
end

#add_site_config_as_lowest(site_config_file) ⇒ Object

Adds a new site config file as the highest priority



139
140
141
142
# File 'lib/origen/site_config.rb', line 139

def add_site_config_as_lowest(site_config_file)
  # configs.append YAML.load_file(File.expand_path('../../../origen_site_config.yml', __FILE__))
  configs.append(Config.new(path: site_config_file, parent: self))
end

#all_varsObject Also known as: env



193
194
195
196
197
198
199
# File 'lib/origen/site_config.rb', line 193

def all_vars
  vars = {}
  configs.each do |c|
    vars = c.values.merge(vars)
  end
  vars
end

#centralized_site_config_cache_dirObject



83
84
85
# File 'lib/origen/site_config.rb', line 83

def centralized_site_config_cache_dir
  File.expand_path(find_val('centralized_site_config_cache_dir'))
end

#clearObject



172
173
174
# File 'lib/origen/site_config.rb', line 172

def clear
  configs.clear
end

#env_contains?(val) ⇒ Boolean

Returns:

  • (Boolean)


242
243
244
# File 'lib/origen/site_config.rb', line 242

def env_contains?(val)
  ENV.key?(val)
end

#eval_path(path, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/origen/site_config.rb', line 49

def eval_path(path, options = {})
  # Any leading ~ should be expanded with whatever ~/ points to. This needs to be done now because later ~ will be replaced with just the username.
  path = path.sub(/^~/, File.expand_path('~/'))

  # Gsub the remaining ~ that aren't escaped.
  # If it was escaped, eat the escape character
  path.gsub!(/(?<!\\|\A)~/, "#{Etc.getlogin}")
  path.gsub!(/\\(?=~)/, '')

  # Now, expand the entire path for any other OS-specific symbols.
  # One note, if we still have a leading '~', that means it was escaped at the beginning. So, what we'll do for this is let it expand
  # then replace the leading File.expand_path('~/') with just '~', pretty much the opposite of path.sub(/^~/, File.expand_path('~/'))
  # Note, we can't just take it out, expand, then add it back  because expanding the path on Windows will expand to
  # C:\, or D:\ or whatever, so need to do this 'expand, then unexpand' method.
  if path.start_with?('~')
    path = File.expand_path(path).sub(/^#{Regexp.quote(File.expand_path('~/'))}/, '~')
  else
    path = File.expand_path(path)
  end

  append = find_val('append_dot_origen')
  append = '.origen' if append == true || append.nil?

  gem_append = find_val('append_gems')
  gem_append = 'gems' if gem_append == true || gem_append.nil?

  if append
    unless path.end_with?(append) || (path.end_with?(File.join(append, gem_append)) if gem_append)
      path = File.join(path, append)
    end
  end
  path
end

#find_val(val, options = {}) ⇒ Object Also known as: get, []



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/origen/site_config.rb', line 332

def find_val(val, options = {})
  env = "ORIGEN_#{val.upcase}"
  if ENV.key?(env)
    value = ENV[env]
  else
    config = configs.find { |c| c.has_var?(val) }
    value = config ? config.find_val(val) : nil
  end

  unless NON_BOOLEAN_PARAMETERS.include?(val.to_s.downcase.to_sym)
    if TRUE_VALUES.include?(value)
      return true
    elsif FALSE_VALUES.include?(value)
      return false
    end
  end
  value
end

#gem_install_dirObject Also known as: user_gem_dir

Gets the gem_intall_dir. This is either site_config.home_dir/gems or the site configs gem_install_dir



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/origen/site_config.rb', line 21

def gem_install_dir
  if gems_use_tool_repo && tool_repo_install_dir && !user_install_enable
    path = eval_path(tool_repo_install_dir)
  else
    path = eval_path(find_val('user_gem_dir') || find_val('gem_install_dir') || home_dir)
  end

  append = find_val('append_gems')
  append = 'gems' if append == true || append.nil?

  if append
    unless path.end_with?(append)
      path = File.join(path, append)
    end
  end
  path
end

#get_all(val) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/origen/site_config.rb', line 162

def get_all(val)
  ret = []
  configs.each do |c|
    if c.has_var?(val)
      ret << c[val]
    end
  end
  ret
end

#home_dirObject



45
46
47
# File 'lib/origen/site_config.rb', line 45

def home_dir
  eval_path(find_val('home_dir') || '~/')
end

#inspect_configs(*config_indexes) ⇒ Object Also known as: inspect_config

Inspects the config(s) at the incex given.



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/origen/site_config.rb', line 276

def inspect_configs(*config_indexes)
  config_indexes.each do |i|
    if i.to_i > configs.size
      puts red("Origen::SiteConfig: index #{i} is out of range of the available configs! Total configs: #{configs.size}.")
    elsif i.to_i < 0
      puts red("Origen::SiteConfig: index #{i} is less than 0. This index is ignored.")
    else
      c = configs[i.to_i]
      puts "Inspecting config \##{i}"
      puts "Type: #{c.type}"
      puts "Path: #{c.path}"
      if c.centralized?
        # Add a safeguard in case something happened being bootup and now and the cache is no longer present
        puts "Cached At: #{c.cache_file}" if c.cache_file
        puts "Cached On: #{c.cache_file.ctime}" if c.cache_file
      end

      puts
      puts 'Values from this config:'
      spacing = ' ' * 2
      c.values.each do |var, val|
        if val.is_a?(Array)
          puts "#{var}: ["
          val.each { |v| puts "#{spacing} #{v}" }
          puts ']'
        elsif val.is_a?(Hash)
          puts "#{var}: {"
          val.each { |v| puts "#{spacing} #{v}" }
          puts '}'
        else
          puts "#{var}: #{val}"
        end
      end

      puts
      puts 'Active (highest-level) values from this config:'
      spacing = ' ' * 2
      vars_by_config.select { |k, v| v == c }.map { |k, v| [k, v[k]] }.to_h.each do |var, val|
        if val.is_a?(Array)
          puts "#{var}: ["
          val.each { |v| puts "#{spacing} #{v}" }
          puts ']'
        elsif val.is_a?(Hash)
          puts "#{var}: {"
          val.each { |v| puts "#{spacing} #{v}" }
          puts '}'
        else
          puts "#{var}: #{val}"
        end
      end
      puts
    end
  end
end

#inspect_variable(*vars) ⇒ Object Also known as: inspect_var, inspect_variables, inspect_vars

rubocop:enable Lint/DuplicateMethods



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/origen/site_config.rb', line 255

def inspect_variable(*vars)
  vars.each do |var|
    puts "Inspecting Variable: #{var}"
    if env_contains?(to_env(var))
      puts "Environment Variable (#{to_env(var)}): #{env(to_env(var))}"
    else
      puts "(No enviornment variable #{to_env(var)} defined)"
    end
    configs.each do |c|
      if c.has_var?(var)
        puts "#{c.path} (#{c.type}): #{c[var]}"
      end
    end
    puts
  end
end

#pretty_print_configsObject Also known as: pp_configs



184
185
186
187
188
189
190
# File 'lib/origen/site_config.rb', line 184

def pretty_print_configs
  puts 'The following config files are ordered from last-encountered (highest priority) first-encountered (lowest priority)'
  puts
  configs.each_with_index do |config, i|
    puts "#{i}: #{config.path} (#{config.type})"
  end
end

#pretty_print_env(*vars) ⇒ Object Also known as: pp_env



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/origen/site_config.rb', line 213

def pretty_print_env(*vars)
  puts
  spacing = ' ' * 2
  r = vars.empty? ? nil : Regexp.union(vars.map { |v| Regexp.new(v) })
  all_vars.each do |var, val|
    if !r.nil? && !(var.match r)
      next
    end

    if val.is_a?(Array)
      puts "#{var}: ["
      val.each { |v| puts "#{spacing} #{v}" }
      puts ']'
    elsif val.is_a?(Hash)
      puts "#{var}: {"
      val.each { |v| puts "#{spacing} #{v}" }
      puts '}'
    else
      puts "#{var}: #{val}"
    end
  end
  puts
end

#rebuild!Object



176
177
178
# File 'lib/origen/site_config.rb', line 176

def rebuild!
  configs!
end

#refreshObject



180
181
182
# File 'lib/origen/site_config.rb', line 180

def refresh
  configs.each(&:refresh)
end

#remove_all_instances(var) ⇒ Object Also known as: purge

Dynamically remove all the instances of :var



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/origen/site_config.rb', line 100

def remove_all_instances(var)
  # Iterate though all the site configs, removing every instance of :var
  # Return an array containing the value of :var at each config,
  # from lowest priority to highest.
  # If [] is returned, it implies that there was no instancs of :var to be removed.
  ret = []
  configs.each do |c|
    if c.has_var?(var)
      ret << c.remove_var(var)
    end
  end
  ret
end

#remove_highest(var) ⇒ Object

Dynamically remove the highest instance of :var



88
89
90
91
92
93
94
95
96
97
# File 'lib/origen/site_config.rb', line 88

def remove_highest(var)
  configs.each do |c|
    if c.has_var?(var)
      return c.remove_var(var)
    end
  end

  # return nil if we haven't returned a value yet
  nil
end

#to_env(val) ⇒ Object



238
239
240
# File 'lib/origen/site_config.rb', line 238

def to_env(val)
  "ORIGEN_#{val.upcase}"
end

#user_install_dirObject

Gets the user_install_dir. Like gem_install_dir, this default to somewhere home_dir, unless overridden



41
42
43
# File 'lib/origen/site_config.rb', line 41

def user_install_dir
  eval_path(find_val('user_install_dir') || home_dir)
end

#vars_by_configsObject Also known as: vars_by_config

Gets all config variables as a hash, but the hash’s values are the Config instances which defines the highest priority of each var, instead of the var’s value itself.



204
205
206
207
208
209
210
# File 'lib/origen/site_config.rb', line 204

def vars_by_configs
  vars = {}
  configs.each do |c|
    vars = c.values.map { |k, v| [k, c] }.to_h.merge(vars)
  end
  vars
end