<commit_before>require 'tutum'

module KumoTutum
  def self.uuid_from_uri(uri)
    uri.split('/')[-1]
  end

  class TutumApi < ::Tutum
    # Note: Tutum handles the options in a "very" different way.
    def initialize(options = {})
      authorize(options)
      super options
    end

    def stack_by_name(name)
      stacks.list['objects'].find { |s| s['name'].eql? name }
    end

    def services_by_stack_name(stack_name)
      stack = stack_by_name(stack_name)
      return [] unless stack
      stack['services'].collect do |uri|
        services.get(KumoTutum.uuid_from_uri(uri))
      end
    end

    def containers_by_stack_name(stack_name)
      services_by_stack_name(stack_name).collect do |data|
        data['containers'].collect do |uri|
          containers.get(KumoTutum.uuid_from_uri(uri))
        end
      end.flatten
    end

    def nodes_by_stack_name(stack_name)
      containers_by_stack_name(stack_name).collect do |data|
        nodes.get(KumoTutum.uuid_from_uri(data['node']))
      end
    end

    private

    def authorize(options)
      if options[:tutum_auth].nil?
        if ENV['TUTUM_USER'] && ENV['TUTUM_APIKEY']
          options[:username] ||= ENV['TUTUM_USER']
          options[:api_key] ||= ENV['TUTUM_APIKEY']
        else
          if read_basic_auth != nil
            options[:tutum_auth] = "Basic #{read_basic_auth}"
          else
            options[:username] ||= read_user_id
            options[:api_key] ||= read_api_key
          end
        end
      end
      options
    end

    def read_basic_auth
      return unless tutum_config['basic_auth']
      tutum_config['basic_auth'][/\"([^@]+)\"/, 1]
    end

    def read_user_id
      tutum_config['user']
    end

    def read_api_key
      tutum_config['apikey']<commit_msg>Correctly check for the non-existence of the auth env vars<commit_after>require 'tutum'

module KumoTutum
  def self.uuid_from_uri(uri)
    uri.split('/')[-1]
  end

  class TutumApi < ::Tutum
    # Note: Tutum handles the options in a "very" different way.
    def initialize(options = {})
      authorize(options)
      super options
    end

    def stack_by_name(name)
      stacks.list['objects'].find { |s| s['name'].eql? name }
    end

    def services_by_stack_name(stack_name)
      stack = stack_by_name(stack_name)
      return [] unless stack
      stack['services'].collect do |uri|
        services.get(KumoTutum.uuid_from_uri(uri))
      end
    end

    def containers_by_stack_name(stack_name)
      services_by_stack_name(stack_name).collect do |data|
        data['containers'].collect do |uri|
          containers.get(KumoTutum.uuid_from_uri(uri))
        end
      end.flatten
    end

    def nodes_by_stack_name(stack_name)
      containers_by_stack_name(stack_name).collect do |data|
        nodes.get(KumoTutum.uuid_from_uri(data['node']))
      end
    end

    private

    def authorize(options)
      if options[:tutum_auth].nil?
        if !ENV['TUTUM_USER'].nil? && !ENV['TUTUM_APIKEY'].nil?
          options[:username] ||= ENV['TUTUM_USER']
          options[:api_key]  ||= ENV['TUTUM_APIKEY']
        else
          if read_basic_auth != nil
            options[:tutum_auth] = "Basic #{read_basic_auth}"
          else
            options[:username] ||= read_user_id
            options[:api_key]  ||= read_api_key
          end
        end
      end
      options
    end

    def read_basic_auth
      return unless tutum_config['basic_auth']
      tutum_config['basic_auth'][/\"([^@]+)\"/, 1]
    end

    def read_user_id
      tutum_config['user']
    end

    def read_api_key
      tutum_config['apikey']
{'fraction_non_alphanumeric': 0.11196698762035763, 'fraction_numerical': 0.0011004126547455295, 'mean_word_length': 2.540408958130477, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5143504', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1202, 'n_words': 230}