<commit_before>require 'hosts'
require 'optparse'
require 'shellwords'

class HostManager
  def initialize(hostsfile_path, hosts, section_name, ip, verbose = false)
    @hostsfile = Hosts::File.read(hostsfile_path)
    @hosts = hosts
    @section = init_section("maphosts - #{section_name}")
    @ip = ip
    @dirty = false
    @verbose = verbose
  end

  def patch
    migrate_hosts(@section, @hostsfile, @hosts)

    @hosts.each do |hostname|
      host = @section.elements.find { |h| h.respond_to?(:name) && h.name == hostname }

      if !host
        log '+', hostname, "will be added"
        @section.elements << Hosts::Entry.new(@ip, hostname)
        @dirty = true
      else
        if host.address != @ip
          log '*', hostname, "is pointing to #{host.address} instead of #{@ip}"
          host.address = @ip
          @dirty = true
        else
          log '*', hostname, "is already set up correctly"
        end
      end
    end

    if !@dirty
      nil
    else
      # Strip whitespace at end of file
      if !@hostsfile.elements.last.nil? && @hostsfile.elements.last.kind_of?(Hosts::EmptyElement)
        @hostsfile.elements.delete @hostsfile.elements.last
      end

      @hostsfile
    end
  end

  private
    def log(symbol, hostname, status)
      if @verbose
        puts "#{symbol} #{hostname} #{status}"
      end
    end

    def init_section(identifier)
      @section = @hostsfile.elements.find {|e| e.respond_to?(:name) && e.name == identifier}

      if !@section
        @section = Hosts::Section.new(identifier)
        @hostsfile.elements << @section
      end

      @section
    end

    def migrate_hosts(section, hostsfile, hosts)
      hosts.each do |hostname|
        host = hostsfile.elements.find { |h| h.respond_to?(:name) && h.name == hostname }

        if host<commit_msg>Abort application if patched hostsfile will be empty<commit_after>require 'hosts'
require 'optparse'
require 'shellwords'

class HostManager
  def initialize(hostsfile_path, hosts, section_name, ip, verbose = false)
    @hostsfile = Hosts::File.read(hostsfile_path)
    @hosts = hosts
    @section = init_section("maphosts - #{section_name}")
    @ip = ip
    @dirty = false
    @verbose = verbose
  end

  def patch
    migrate_hosts(@section, @hostsfile, @hosts)

    @hosts.each do |hostname|
      host = @section.elements.find { |h| h.respond_to?(:name) && h.name == hostname }

      if !host
        log '+', hostname, "will be added"
        @section.elements << Hosts::Entry.new(@ip, hostname)
        @dirty = true
      else
        if host.address != @ip
          log '*', hostname, "is pointing to #{host.address} instead of #{@ip}"
          host.address = @ip
          @dirty = true
        else
          log '*', hostname, "is already set up correctly"
        end
      end
    end

    if !@dirty
      nil
    else
      # Strip whitespace at end of file
      if !@hostsfile.elements.last.nil? && @hostsfile.elements.last.kind_of?(Hosts::EmptyElement)
        @hostsfile.elements.delete @hostsfile.elements.last
      end

      if @hostsfile.to_s.empty?
        raise "The patched hostsfile is empty. This is most likely due to a bug. No worry, the file has not been written."
      end

      @hostsfile
    end
  end

  private
    def log(symbol, hostname, status)
      if @verbose
        puts "#{symbol} #{hostname} #{status}"
      end
    end

    def init_section(identifier)
      @section = @hostsfile.elements.find {|e| e.respond_to?(:name) && e.name == identifier}

      if !@section
        @section = Hosts::Section.new(identifier)
        @hostsfile.elements << @section
      end

      @section
    end

    def migrate_hosts(section, hostsfile, hosts)
      hosts.each do |hostname|
        host = hostsfile.elements.find { |h| h.respond_to?(:name) && h.name == hostname }

        if host
{'fraction_non_alphanumeric': 0.11914011914011914, 'fraction_numerical': 0.0, 'mean_word_length': 2.4267968056787934, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7561225', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1183, 'n_words': 325}