github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/modules/BOSH.Sysprep/print_conflicted_registry_keys_from_pol_txt.rb (about)

     1  #!/usr/bin/env ruby
     2  
     3  def convert_to_utf8(name)
     4    `iconv -f UTF-16LE -t UTF-8 #{name} > /tmp/registry-tmp`
     5    File.read '/tmp/registry-tmp', encoding: 'bom|utf-8'
     6  end
     7  
     8  def merge(a, b)
     9    a_no_comment = a.split("\r\n").reject {|x| x[0] == ';' }.join("\r\n").strip
    10    b_no_comment = b.split("\r\n").reject {|x| x[0] == ';' }.join("\r\n").strip
    11    a_no_comment.strip + "\r\n\r\n" + b_no_comment.strip
    12  end
    13  
    14  registry_a_name = 'registry-cis-machine.txt'
    15  registry_b_name = 'registry-ms-baseline-machine.txt'
    16  
    17  registry_a_contents = convert_to_utf8(registry_a_name)
    18  registry_b_contents = convert_to_utf8(registry_b_name)
    19  
    20  before_uniq = merge(registry_a_contents, registry_b_contents).split("\r\n\r\n").sort
    21  
    22  puts "before uniq: #{before_uniq.size}"
    23  after_uniq = before_uniq.uniq do |entry|
    24    lines = entry.split "\r\n"
    25    reg_key = (lines[1] + lines[2]).downcase
    26    lines[0] + reg_key + lines[3]
    27  end
    28  puts "after uniq: #{after_uniq.size}"
    29  
    30  library = after_uniq.group_by {|x| x.split("\r\n")[1..2].join('\\')}
    31  dups = library.keys.select {|k| library[k].size > 1}
    32  
    33  if(dups.nil?)
    34   puts "found 0 conflicts"
    35  else
    36   puts "found #{dups.size} conflicts"
    37   dups.each {|x| puts x}
    38  end
    39  
    40  merged_file = "registry-merged.txt"
    41  File.write merged_file, after_uniq.join("\r\n\r\n")
    42  puts "wrote merged registry files with duplicates removed to #{merged_file}"