<commit_before>require 'cocaine'

module Dbsync
  class Sync
    class << self
      def notify(message="")
        $stdout.puts "[#{Time.now.strftime('%T')}] [dbsync] #{message}"
      end
    end


    def initialize(ssh_config, db_config, options={})
      ssh_config  = ssh_config.with_indifferent_access
      db_config   = db_config.with_indifferent_access

      @verbose = !!options[:verbose]

      @remote_host  = ssh_config[:remote_host] || raise_missing("remote_host")
      @remote_dir   = ssh_config[:remote_dir]  || raise_missing("remote_dir")
      @local_dir    = ssh_config[:local_dir]   || raise_missing("local_dir")
      @filename     = ssh_config[:filename]    || raise_missing("filename")

      @db_username  = db_config[:username]
      @db_password  = db_config[:password]
      @db_host      = db_config[:host]
      @db_database  = db_config[:database]

      @remote_file = "#{@remote_host}:" + File.join(@remote_dir, @filename)
      @local_file  = File.expand_path(File.join(@local_dir, @filename))
    end


    # Update the local dump file from the remote source (using rsync).
    def fetch
      notify "Updating '#{@local_file}' from '#{@remote_file}' via rsync..."

      line = Cocaine::CommandLine.new('rsync', '-v :remote :local')
      line.run({
        :remote => @remote_file,
        :local  => @local_file
      })
    end


    # Update the local database with the local dump file.
    def merge
      notify "Dumping data from '#{@local_file}' into '#{@db_database}'"

      options = ""
      options += "-u :username " if @db_username.present?
      options += "-p:password "  if @db_password.present?
      options += "-h :host "     if @db_host.present?

      line = Cocaine::CommandLine.new('mysql', "#{options} :database < :local")
      line.run({
        :username   => @db_username,
        :password   => <PASSWORD>,
        :host       => @db_host,
        :database   => @db_database,
        :local      => @local_file
      })
    end


    # Update the local dump file, and update the local database.
    def pull
      fetch
      merge
    end


    # Copy the remote dump file to a local destination.<commit_msg>ellipses<commit_after>require 'cocaine'

module Dbsync
  class Sync
    class << self
      def notify(message="")
        $stdout.puts "[#{Time.now.strftime('%T')}] [dbsync] #{message}"
      end
    end


    def initialize(ssh_config, db_config, options={})
      ssh_config  = ssh_config.with_indifferent_access
      db_config   = db_config.with_indifferent_access

      @verbose = !!options[:verbose]

      @remote_host  = ssh_config[:remote_host] || raise_missing("remote_host")
      @remote_dir   = ssh_config[:remote_dir]  || raise_missing("remote_dir")
      @local_dir    = ssh_config[:local_dir]   || raise_missing("local_dir")
      @filename     = ssh_config[:filename]    || raise_missing("filename")

      @db_username  = db_config[:username]
      @db_password  = db_config[:password]
      @db_host      = db_config[:host]
      @db_database  = db_config[:database]

      @remote_file = "#{@remote_host}:" + File.join(@remote_dir, @filename)
      @local_file  = File.expand_path(File.join(@local_dir, @filename))
    end


    # Update the local dump file from the remote source (using rsync).
    def fetch
      notify "Updating '#{@local_file}' from '#{@remote_file}' via rsync..."

      line = Cocaine::CommandLine.new('rsync', '-v :remote :local')
      line.run({
        :remote => @remote_file,
        :local  => @local_file
      })
    end


    # Update the local database with the local dump file.
    def merge
      notify "Dumping data from '#{@local_file}' into '#{@db_database}'..."

      options = ""
      options += "-u :username " if @db_username.present?
      options += "-p:password "  if @db_password.present?
      options += "-h :host "     if @db_host.present?

      line = Cocaine::CommandLine.new('mysql', "#{options} :database < :local")
      line.run({
        :username   => @db_username,
        :password   => <PASSWORD>,
        :host       => @db_host,
        :database   => @db_database,
        :local      => @local_file
      })
    end


    # Update the local dump file, and update the local database.
    def pull
      fetch
      merge
    end


    # Copy the remote dump file to a local destination.
{'fraction_non_alphanumeric': 0.13337924701561066, 'fraction_numerical': 0.0, 'mean_word_length': 2.6675084175084174, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '2084561', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1341, 'n_words': 317}