<commit_before>require 'open3'

module RubyGpg
  extend self

  Config = Struct.new(:executable, :homedir)
  def config
    @config ||=  Config.new("gpg", "~/.gnupg")
  end

  def gpg_command
    "#{config.executable} --homedir #{config.homedir} --quiet" +
    " --no-secmem-warning --no-permission-warning --no-tty --yes"
  end
  
  def encrypt(file, recipient, opts = {})
    options = {
      :armor => false
    }.merge(opts)
    
    output = output_filename(file, options)
    
    ascii = options[:armor] == true ? "-a " : ""
    
    command = "#{gpg_command} #{ascii}--output #{output}" +
              " --recipient \"#{recipient}\" --encrypt #{file}"
    
    run_command(command)
  end
  
  # Encrypt a string from stdin
  def encrypt_string(string, recipient, opts = {})
    command = gpg_command.dup
    command << " -a" if opts[:armor]
    command << " --encrypt"
    command << " --recipient \"#{recipient}\""
    run_command(command, string)
  end
  
  def decrypt(file, passphrase = nil, opts = {})
    outfile = opts[:output].nil? ? file.gsub(/\.gpg$|\.asc$/, '') : opts[:output]
    command = "#{gpg_command} --output #{outfile}"
    command << " --passphrase #{passphrase}" if passphrase
    command << " --decrypt #{file}"
    run_command(command)
  end
  
  def decrypt_string(string, passphrase = nil)
    command = gpg_command.dup
    command << " --passphrase #{passphrase}" if passphrase
    command << " --decrypt"
    run_command(command, string)
  end
  
  private
  
  def run_command(command, input = nil)
    output = ""
    Open3.popen3(command) do |stdin, stdout, stderr|
      stdin.write(input) if input
      stdin.close_write
      output << stdout.read
      error = stderr.read
      if error && !error.empty?
        raise "GPG command (#{command}) failed with: #{error}"
      end
    end
    output
  end
  
  # Return the output filename to use
  def output_filename(file, opts)
    extension = opts[:armor] ? "asc" : "gpg"<commit_msg>stdin.close<commit_after>require 'open3'

module RubyGpg
  extend self

  Config = Struct.new(:executable, :homedir)
  def config
    @config ||=  Config.new("gpg", "~/.gnupg")
  end

  def gpg_command
    "#{config.executable} --homedir #{config.homedir} --quiet" +
    " --no-secmem-warning --no-permission-warning --no-tty --yes"
  end
  
  def encrypt(file, recipient, opts = {})
    options = {
      :armor => false
    }.merge(opts)
    
    output = output_filename(file, options)
    
    ascii = options[:armor] == true ? "-a " : ""
    
    command = "#{gpg_command} #{ascii}--output #{output}" +
              " --recipient \"#{recipient}\" --encrypt #{file}"
    
    run_command(command)
  end
  
  # Encrypt a string from stdin
  def encrypt_string(string, recipient, opts = {})
    command = gpg_command.dup
    command << " -a" if opts[:armor]
    command << " --encrypt"
    command << " --recipient \"#{recipient}\""
    run_command(command, string)
  end
  
  def decrypt(file, passphrase = nil, opts = {})
    outfile = opts[:output].nil? ? file.gsub(/\.gpg$|\.asc$/, '') : opts[:output]
    command = "#{gpg_command} --output #{outfile}"
    command << " --passphrase #{passphrase}" if passphrase
    command << " --decrypt #{file}"
    run_command(command)
  end
  
  def decrypt_string(string, passphrase = nil)
    command = gpg_command.dup
    command << " --passphrase #{passphrase}" if passphrase
    command << " --decrypt"
    run_command(command, string)
  end
  
  private
  
  def run_command(command, input = nil)
    output = ""
    Open3.popen3(command) do |stdin, stdout, stderr|
      stdin.write(input) if input
      stdin.close
      output << stdout.read
      error = stderr.read
      if error && !error.empty?
        raise "GPG command (#{command}) failed with: #{error}"
      end
    end
    output
  end
  
  # Return the output filename to use
  def output_filename(file, opts)
    extension = opts[:armor] ? "asc" : "gpg"
{'fraction_non_alphanumeric': 0.15460526315789475, 'fraction_numerical': 0.0015182186234817814, 'mean_word_length': 2.639963167587477, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 5, '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': '5647669', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1329, 'n_words': 317}