<commit_before>defmodule Mix.Appsignal.Helper do
  @moduledoc """
  Helper functions for downloading and compiling the AppSignal agent library.
  """

  require Logger

  @max_retries 5

  def ensure_downloaded do

    info = Poison.decode!(File.read!("agent.json"))
    arch = map_arch(:erlang.system_info(:system_architecture))
    arch_config = info["triples"][arch]

    System.put_env("LIB_DIR", priv_dir())

    unless has_file("appsignal-agent") and has_file("appsignal_extension.h") and has_file("appsignal_extension.so") do

      Logger.info "Downloading agent release from #{arch_config["download_url"]}"

      File.mkdir_p!(priv_dir())

      download_file(arch_config["download_url"])
      |> verify_checksum(arch_config["checksum"])
      |> extract
    else
      :ok
    end
  end

  defp download_file(url) do
    filename = :filename.join("/tmp", :filename.basename(url))
    case System.cmd("curl", ["-s", "-S", "--retry", Integer.to_string(@max_retries), "-f", "-o", filename, url], stderr_to_stdout: true) do
      {_, 0} ->
        filename
      {result, exitcode} ->
        IO.binwrite(result)
        raise Mix.Error, message: """
        Download failed with code #{exitcode}
        """
    end<commit_msg>compile: Only download the agent file when it is not there yet<commit_after>defmodule Mix.Appsignal.Helper do
  @moduledoc """
  Helper functions for downloading and compiling the AppSignal agent library.
  """

  require Logger

  @max_retries 5

  def ensure_downloaded do

    info = Poison.decode!(File.read!("agent.json"))
    arch = map_arch(:erlang.system_info(:system_architecture))
    arch_config = info["triples"][arch]

    System.put_env("LIB_DIR", priv_dir())

    unless has_file("appsignal-agent") and has_file("appsignal_extension.h") and has_file("appsignal_extension.so") do

      File.mkdir_p!(priv_dir())

      download_file(arch_config["download_url"])
      |> verify_checksum(arch_config["checksum"])
      |> extract
    else
      :ok
    end
  end

  defp download_file(url) do
    filename = :filename.join("/tmp", :filename.basename(url))
    case File.exists?(filename) do
      true ->
        filename
      false ->
        Logger.info "Downloading agent release from #{url}"
        case System.cmd("curl", ["-s", "-S", "--retry", Integer.to_string(@max_retries), "-f", "-o", filename, url], stderr_to_stdout: true) do
          {_, 0} ->
            filename
          {result, exitcode} ->
            IO.binwrite(result)
            raise Mix.Error, message: """
            Download failed with code #{exitcode}
            """
        end
    end
{'fraction_non_alphanumeric': 0.13457872664887532, 'fraction_numerical': 0.0015249714067861228, 'mean_word_length': 3.0808709175738724, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '456518', 'n_tokens_mistral': 827, 'n_tokens_neox': 790, 'n_words': 183}