<commit_before>module DockerHelpers
  module Container
    def coerce_links(v)
      v = Array(v)
      if v.empty?
        nil
      else
        # Parse docker input of /source:/container_name/dest into source:dest
        v.map do |link|
          if link =~ %r{^/(?<source>.+):/#{name}/(?<dest>.+)}
            link = "#{Regexp.last_match[:source]}:#{Regexp.last_match[:dest]}"
          end
          link
        end
      end
    end

    def coerce_log_opts(v)
      case v
      when Hash, nil
        v
      else
        Array(v).each_with_object({}) do |log_opt, memo|
          key, value = log_opt.split('=', 2)
          memo[key] = value
        end
      end
    end

    def coerce_ulimits(v)
      if v.nil?
        v
      else
        Array(v).map do |u|
          u = "#{u['Name']}=#{u['Soft']}:#{u['Hard']}" if u.is_a?(Hash)
          u
        end
      end
    end

    def coerce_volumes(v)
      case v
      when nil, Hash
        v
      else
        Array(v).sort.each_with_object({}) { |volume, h| h[volume] = {} }
      end
    end

    def default_network_mode
      case api_version
      when '1.20'
        'default'
      when '1.19'
        'bridge'
      else
        ''
      end
    end

    def state
      container ? container.info['State'] : {}
    end

    def wait_running_state(v)
      i = 0
      tries = 20
      until state['Running'] == v
        i += 1
        break if i == tries
        sleep 1
      end
    end

    # port_bindings and exposed_ports really handle this
    # TODO: infer `port` from `port_bindings` and `exposed_ports`
    def port(ports = Chef::NOT_PASSED)
      if ports != Chef::NOT_PASSED
        ports = Array(ports)
        ports = nil if ports.empty?
        @port = ports
        port_bindings to_port_bindings(ports)
        exposed_ports to_port_exposures(ports)
      end
      @port
    end

    # log_driver and log_opts really handle this<commit_msg>check if the container finished before we were able to check state<commit_after>module DockerHelpers
  module Container
    def coerce_links(v)
      v = Array(v)
      if v.empty?
        nil
      else
        # Parse docker input of /source:/container_name/dest into source:dest
        v.map do |link|
          if link =~ %r{^/(?<source>.+):/#{name}/(?<dest>.+)}
            link = "#{Regexp.last_match[:source]}:#{Regexp.last_match[:dest]}"
          end
          link
        end
      end
    end

    def coerce_log_opts(v)
      case v
      when Hash, nil
        v
      else
        Array(v).each_with_object({}) do |log_opt, memo|
          key, value = log_opt.split('=', 2)
          memo[key] = value
        end
      end
    end

    def coerce_ulimits(v)
      if v.nil?
        v
      else
        Array(v).map do |u|
          u = "#{u['Name']}=#{u['Soft']}:#{u['Hard']}" if u.is_a?(Hash)
          u
        end
      end
    end

    def coerce_volumes(v)
      case v
      when nil, Hash
        v
      else
        Array(v).sort.each_with_object({}) { |volume, h| h[volume] = {} }
      end
    end

    def default_network_mode
      case api_version
      when '1.20'
        'default'
      when '1.19'
        'bridge'
      else
        ''
      end
    end

    def state
      container ? container.info['State'] : {}
    end

    def wait_running_state(v)
      i = 0
      tries = 20
      until state['Running'] == v || state['FinishedAt'] != '0001-01-01T00:00:00Z'
        i += 1
        break if i == tries
        sleep 1
      end
    end

    # port_bindings and exposed_ports really handle this
    # TODO: infer `port` from `port_bindings` and `exposed_ports`
    def port(ports = Chef::NOT_PASSED)
      if ports != Chef::NOT_PASSED
        ports = Array(ports)
        ports = nil if ports.empty?
        @port = ports
        port_bindings to_port_bindings(ports)
        exposed_ports to_port_exposures(ports)
      end
      @port
    end

    # log_driver and log_opts really handle this
{'fraction_non_alphanumeric': 0.11211664152840624, 'fraction_numerical': 0.009552538964303671, 'mean_word_length': 1.681266846361186, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4415564', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1303, 'n_words': 338}