github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/modules/BOSH.Sysprep/print_conflicted_keys_from_inf_txt.rb (about) 1 #!/usr/bin/env ruby 2 3 class Infs 4 def self.from_string(s) 5 Inf.from_string_array(s.split(/(?=\[.+\])/)) 6 end 7 end 8 9 class Inf 10 attr_accessor :name 11 attr_accessor :contents 12 13 def initialize(name, contents) 14 @name = name 15 @contents = contents 16 end 17 18 def self.from_string_array(sections) 19 sections.map do |x| 20 contents = x.split("\r\n") 21 section_name = contents.first 22 section_contents = contents[1..-1] 23 24 Inf.new(section_name, section_contents) 25 end 26 end 27 28 def uniq() 29 trim_contents = contents.map { |x| x.strip } 30 Inf.new(name, trim_contents.uniq {|text| text.split('=')[0].downcase + text.split('=')[1..-1].join('')}) 31 end 32 33 def sort() 34 Inf.new(name, contents.sort) 35 end 36 37 def size() 38 @contents.size 39 end 40 41 def summary() 42 "section: #{@name} size: #{size}" 43 end 44 end 45 46 def print_section_summary(sections) 47 sections.each do |x| 48 puts x.summary 49 end 50 end 51 52 def convert_to_utf8(name) 53 `iconv -f UTF-16LE -t UTF-8 #{name} > /tmp/registry-tmp` 54 File.read '/tmp/registry-tmp', encoding: 'bom|utf-8' 55 end 56 57 def merge(a, b) 58 a_no_comment = a.split("\r\n").reject {|x| x.strip.gsub(/^;.*/,'').empty?}.join("\r\n").strip 59 b_no_comment = b.split("\r\n").reject {|x| x.strip.gsub(/^;.*/,'').empty?}.join("\r\n").strip 60 61 a_normalize_equals = a_no_comment.gsub(' = ', '=').gsub(" =\r\n", "=\r\n") 62 b_normalize_equals = b_no_comment.gsub(' = ', '=').gsub(" =\r\n", "=\r\n") 63 64 a_infs = Infs.from_string(a_normalize_equals) 65 b_infs = Infs.from_string(b_normalize_equals) 66 67 (a_infs + b_infs).group_by {|x| x.name}.map {|section_name,infs| Inf.new(section_name, infs.map{|x| x.contents}.flatten)} 68 end 69 70 a_contents = convert_to_utf8('GptTmpl-ms-baseline.inf') 71 b_contents = convert_to_utf8('GptTmpl-cis-baseline.inf') 72 73 puts "merged" 74 merged = merge(a_contents, b_contents) 75 76 print_section_summary(merge(a_contents, b_contents)) 77 78 puts "removing dups" 79 80 unique = merged.map do |section| 81 section.uniq 82 end 83 84 sorted = unique.map do |section| 85 section.sort 86 end 87 88 print_section_summary(sorted) 89 90 puts "listing conflicts" 91 sorted.each do |section| 92 elements = section.contents 93 title = section.name 94 95 grouped = elements.group_by {|x| x.split("=")[0]} 96 dups = grouped.keys.select {|k| grouped[k].size > 1} 97 98 if(dups.nil?) 99 puts "found 0 conflicts in section #{title}" 100 else 101 puts "found #{dups.size} conflicts in section #{title}" 102 dups.each {|x| puts x} 103 end 104 end 105 106 new_file = 'GptTmpl-merged.inf' 107 File.write new_file, sorted.map{|section| section.name + "\n" + section.contents.join("\n")}.join("\n") 108 puts "merged files with uniques removed outputted to #{new_file}"