Attention une mise à jour du service Gitlab va être effectuée le mardi 30 novembre entre 17h30 et 18h00. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes. Cette mise à jour intermédiaire en version 14.0.12 nous permettra de rapidement pouvoir mettre à votre disposition une version plus récente.

Commit 773c8168 authored by Jérémie Gaidamour's avatar Jérémie Gaidamour
Browse files

[dev] Split input_loader file. Moved hash functionalities to lib/hash/hash.rb

parent fdc21ae7
#!/usr/bin/ruby
require 'pp'
require 'pathname'
require 'yaml'
require 'json'
# Monkey patching Ruby's Hash class
# Merge a and b. If a and b are hashes, they are recursively merged.
# - a and b might be strings or nil.
......@@ -23,8 +18,8 @@ class ::Hash
# Returns a new hash containing the contents of other_hash and the contents of hash. The value for entries with duplicate keys will be that of other_hash:
# a = {"key": "value_a"}
# b = {"key": "value_b"}
# pp a.deep_merge(b) => {:key=>"value_b"}
# pp b.deep_merge(a) => {:key=>"value_a"}
# a.deep_merge(b) -> {:key=>"value_b"}
# b.deep_merge(a) -> {:key=>"value_a"}
def deep_merge(other_hash)
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
self.merge(other_hash, &merger)
......@@ -91,39 +86,5 @@ class ::Hash
def self.from_array(array, value)
return array.reverse.inject(value) { |a, n| { n => a } }
end
end
global_hash = {} # the global data structure
Dir.chdir("../input/grid5000/")
# Recursively list the .yaml files.
# The order in which the results are returned depends on the system (http://ruby-doc.org/core-2.2.3/Dir.html).
# => List deepest files first as they have lowest priority when hash keys are duplicated.
list_of_yaml_files = Dir['**/*.y*ml'].sort_by { |x| -x.count('/') }
list_of_yaml_files.each { |filename|
# Load YAML
file_hash = YAML::load_file(filename)
if not file_hash
puts "Error loading '#{filename}'"
next
end
# Expand the hash
file_hash.expand_angle_brackets()
# Inject the file content into the global_hash, at the right place
path_hierarchy = File.dirname(filename).split('/') # Split the file path (path relative to input/)
file_hash = Hash.from_array(path_hierarchy, file_hash) # Build the nested hash hierarchy according to the file path
global_hash = global_hash.deep_merge(file_hash) # Merge global_hash and file_hash. The value for entries with duplicate keys will be that of file_hash
}
#pp global_hash
#pp data
#puts JSON.generate(data)
end
# Load a hierarchy of YAML file into a Ruby hash
require 'yaml'
require '../lib/hash/hash.rb'
def load_yaml_file_hierarchy(directory)
global_hash = {} # the global data structure
Dir.chdir(directory) {
# Recursively list the .yaml files.
# The order in which the results are returned depends on the system (http://ruby-doc.org/core-2.2.3/Dir.html).
# => List deepest files first as they have lowest priority when hash keys are duplicated.
list_of_yaml_files = Dir['**/*.y*ml'].sort_by { |x| -x.count('/') }
list_of_yaml_files.each { |filename|
# Load YAML
file_hash = YAML::load_file(filename)
if not file_hash
puts "Error loading '#{filename}'"
next
end
# Expand the hash
file_hash.expand_angle_brackets()
# Inject the file content into the global_hash, at the right place
path_hierarchy = File.dirname(filename).split('/') # Split the file path (path relative to input/)
file_hash = Hash.from_array(path_hierarchy, file_hash) # Build the nested hash hierarchy according to the file path
global_hash = global_hash.deep_merge(file_hash) # Merge global_hash and file_hash. The value for entries with duplicate keys will be that of file_hash
}
}
return global_hash
end
#!/usr/bin/ruby
require_relative "input_loader"
require "test/unit"
require "hashdiff"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment