<commit_before>module Specjour
  class RsyncDaemon
    require 'fileutils'
    include SocketHelpers

    # Corresponds to the version of specjour that changed the configuration
    # file.
    CONFIG_VERSION = "0.2.3".freeze
    PID_FILE_NAME = "rsyncd.pid"

    attr_reader :project_path, :project_name

    def initialize(project_path, project_name)
      @project_path = project_path
      @project_name = project_name
    end

    def config_directory
      @config_directory ||= File.join(project_path, ".specjour")
    end

    def config_file
      @config_file ||= File.join(config_directory, "rsyncd.conf")
    end

    def pid
      if File.exists?(pid_file)
        File.read(pid_file).strip.to_i
      end
    end

    def pid_file
      File.join(config_directory, PID_FILE_NAME)
    end

    def start
      write_config
      Dir.chdir(project_path) do
        Kernel.system *command
      end
      Kernel.at_exit { stop }
    end

    def stop
      if pid
        Process.kill("TERM", pid)
        FileUtils.rm(pid_file)
      end
    end

    protected

    def command
      ["rsync", "--daemon", "--config=#{config_file}", "--port=8989"]
    end

    def check_config_version
      File.read(config_file) =~ /\A# (\d+.\d+.\d+)/
      if out_of_date? Regexp.last_match(1)
        Kernel.warn <<-WARN

Specjour has made changes to the way rsync.d is generated.
Back up '#{config_file}'
and re-run the dispatcher to generate the new config file.
        WARN
      end
    end

    def out_of_date?(version)
      CONFIG_VERSION != version
    end

    def write_config
      if File.exists? config_file
        check_config_version
      else
        FileUtils.mkdir_p config_directory
        File.open(config_file, 'w') do |f|<commit_msg>Fix name of config file in warning<commit_after>module Specjour
  class RsyncDaemon
    require 'fileutils'
    include SocketHelpers

    # Corresponds to the version of specjour that changed the configuration
    # file.
    CONFIG_VERSION = "0.2.3".freeze
    CONFIG_FILE_NAME = "rsyncd.conf"
    PID_FILE_NAME = "rsyncd.pid"

    attr_reader :project_path, :project_name

    def initialize(project_path, project_name)
      @project_path = project_path
      @project_name = project_name
    end

    def config_directory
      @config_directory ||= File.join(project_path, ".specjour")
    end

    def config_file
      @config_file ||= File.join(config_directory, CONFIG_FILE_NAME)
    end

    def pid
      if File.exists?(pid_file)
        File.read(pid_file).strip.to_i
      end
    end

    def pid_file
      File.join(config_directory, PID_FILE_NAME)
    end

    def start
      write_config
      Dir.chdir(project_path) do
        Kernel.system *command
      end
      Kernel.at_exit { stop }
    end

    def stop
      if pid
        Process.kill("TERM", pid)
        FileUtils.rm(pid_file)
      end
    end

    protected

    def command
      ["rsync", "--daemon", "--config=#{config_file}", "--port=8989"]
    end

    def check_config_version
      File.read(config_file) =~ /\A# (\d+.\d+.\d+)/
      if out_of_date? Regexp.last_match(1)
        Kernel.warn <<-WARN

Specjour has made changes to the way #{CONFIG_FILE_NAME} is generated.
Back up '#{config_file}'
and re-run the dispatcher to generate the new config file.
        WARN
      end
    end

    def out_of_date?(version)
      CONFIG_VERSION != version
    end

    def write_config
      if File.exists? config_file
        check_config_version
      else
        FileUtils.mkdir_p config_directory
        File.open(config_file, 'w') do |f|
{'fraction_non_alphanumeric': 0.08767047035903144, 'fraction_numerical': 0.004453103256331756, 'mean_word_length': 2.4131054131054133, 'pattern_counts': {'":': 0, '<': 7, '<?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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2240217', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1134, 'n_words': 287}