<commit_before>module Awsborn
  class SecurityError < StandardError ; end
  class ServerError < StandardError ; end

  class << self
    attr_writer :access_key_id, :secret_access_key, :logger, :remote_chef_path
    attr_accessor :verbose

    Awsborn.verbose = true

    def access_key_id
      @access_key_id ||= ENV['AMAZON_ACCESS_KEY_ID'] 
    end
    
    def secret_access_key
      @secret_access_key ||= ENV['AMAZON_SECRET_ACCESS_KEY']
    end

    def remote_chef_path
      @remote_chef_path ||= '/etc/chef'
    end
  
    def logger
      unless defined? @logger
        dir = [File.dirname(File.expand_path($0)), '/tmp'].find { |d| File.writable?(d) }
        if dir
          file = File.open(File.join(dir, 'awsborn.log'), 'a')
          file.sync = true
        end
        @logger = Logger.new(file || $stdout)
        @logger.level = Logger::INFO
      end
      @logger
    end
  
    # @throws Interrupt
    def wait_for (message, sleep_seconds = 5, max_wait_seconds = nil)
      stdout_sync = $stdout.sync
      $stdout.sync = true

      start_at = Time.now
      print "Waiting for #{message}.." if Awsborn.verbose
      result = yield
      while ! result
        if max_wait_seconds && Time.now > start_at + max_wait_seconds
          raise Interrupt, "Timed out after #{Time.now - start_at} seconds."
        end
        print "." if Awsborn.verbose
        sleep sleep_seconds
        result = yield
      end
      verbose_output "OK!"
      result
    ensure
      $stdout.sync = stdout_sync
    end
  
    def verbose_output(message)
      puts message if Awsborn.verbose
    end
    
  end
end
<commit_msg>Fetch secret key from keychain (OS X only) if missing.<commit_after>module Awsborn
  class SecurityError < StandardError ; end
  class ServerError < StandardError ; end

  class << self
    attr_writer :access_key_id, :secret_access_key, :logger, :remote_chef_path
    attr_accessor :verbose

    Awsborn.verbose = true

    def access_key_id
      @access_key_id ||= ENV['AMAZON_ACCESS_KEY_ID'] 
    end
    
    def secret_access_key
      unless @secret_access_key
        @secret_access_key = ENV['AMAZON_SECRET_ACCESS_KEY']
        if @secret_access_key.to_s == ''
          @secret_access_key = secret_access_key_from_keychain(access_key_id)
        end
      end
      @secret_access_key
    end

    def secret_access_key_from_keychain (key_id)
      @credentials ||= {}
      unless @credentials[key_id]
        dump = `security -q find-generic-password -a "#{key_id}" -g 2>&1`
        secret_key = dump[/password: "(.*)"/, 1]
        @credentials[key_id] = secret_key
      end
      @credentials[key_id]
    end

    def remote_chef_path
      @remote_chef_path ||= '/etc/chef'
    end
  
    def logger
      unless defined? @logger
        dir = [File.dirname(File.expand_path($0)), '/tmp'].find { |d| File.writable?(d) }
        if dir
          file = File.open(File.join(dir, 'awsborn.log'), 'a')
          file.sync = true
        end
        @logger = Logger.new(file || $stdout)
        @logger.level = Logger::INFO
      end
      @logger
    end
  
    # @throws Interrupt
    def wait_for (message, sleep_seconds = 5, max_wait_seconds = nil)
      stdout_sync = $stdout.sync
      $stdout.sync = true

      start_at = Time.now
      print "Waiting for #{message}.." if Awsborn.verbose
      result = yield
      while ! result
        if max_wait_seconds && Time.now > start_at + max_wait_seconds
          raise Interrupt, "Timed out after #{Time.now - start_at} seconds."
        end
        print "." if Awsborn.verbose
        sleep sleep_seconds
        result = yield
      end
      verbose_output "OK!"
      result
    ensure
      $stdout.sync = stdout_sync
    end
  
    def verbose_output(message)
      puts message if Awsborn.verbose
    end
    
  end
end

{'fraction_non_alphanumeric': 0.0947533281127643, 'fraction_numerical': 0.0018271991647089533, 'mean_word_length': 2.3006029285099054, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 6, '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': '317620', 'n_tokens_mistral': 1257, 'n_tokens_neox': 1222, 'n_words': 292}