<commit_before>defmodule Farmbot.Target.UpdateHandler do
  @moduledoc "Handles prep and post OTA update."

  @behaviour Farmbot.System.UpdateHandler
  use Farmbot.Logger

  # Update Handler callbacks

  def apply_firmware(fw_file_path) do
    meta_bin
    |> String.trim()
    |> String.split("\n")
    |> Enum.map(&String.split(&1, "="))
    |> Map.new(fn([key, val]) ->
      {key, val |> String.trim_leading("\"") |> String.trim_trailing("\"")}
    end)
    |> log_meta
    Nerves.Firmware.upgrade_and_finalize(fw_file_path)
  end

  defp log_meta(meta_map) do
    target = "target: #{meta_map["meta-platform"]}"
    product = "product: #{meta_map["meta-product"]}"
    version = "version: #{meta_map["meta-version"]}"
    create_time = "created: #{meta_map["meta-creation-date"]}"
    msg = """
    Applying Firmware:
    #{create_time}
    #{target}
    #{product}
    #{version}
    """
    Logger.debug 1, msg
  end

  def before_update, do: :ok

  def post_update do
    alias Farmbot.Firmware.UartHandler.Update
    hw = Farmbot.System.ConfigStorage.get_config_value(:string, "settings", "firmware_hardware")
    Update.maybe_update_firmware(hw)
    :ok
  end

  def setup(:prod) do
    file = "#{:code.priv_dir(:farmbot)}/fwup-key.pub"
    Application.put_env(:nerves_firmware, :pub_key_path, file)
    if File.exists?(file), do: :ok, else: {:error, :no_pub_file}
  end

  def setup(_) do
    :ok
  end

  def requires_reboot? do
    !Nerves.Firmware.allow_upgrade?
  end
end
<commit_msg>Add call to fwup<commit_after>defmodule Farmbot.Target.UpdateHandler do
  @moduledoc "Handles prep and post OTA update."

  @behaviour Farmbot.System.UpdateHandler
  use Farmbot.Logger

  # Update Handler callbacks

  def apply_firmware(fw_file_path) do
    {meta_bin, 0} = System.cmd("fwup", ~w"-i #{fw_file_path} -m")
    meta_bin
    |> String.trim()
    |> String.split("\n")
    |> Enum.map(&String.split(&1, "="))
    |> Map.new(fn([key, val]) ->
      {key, val |> String.trim_leading("\"") |> String.trim_trailing("\"")}
    end)
    |> log_meta()

    Nerves.Firmware.upgrade_and_finalize(fw_file_path)
  end

  defp log_meta(meta_map) do
    target = "target: #{meta_map["meta-platform"]}"
    product = "product: #{meta_map["meta-product"]}"
    version = "version: #{meta_map["meta-version"]}"
    create_time = "created: #{meta_map["meta-creation-date"]}"
    msg = """
    Applying Firmware:
    #{create_time}
    #{target}
    #{product}
    #{version}
    """
    Logger.debug 1, msg
  end

  def before_update, do: :ok

  def post_update do
    alias Farmbot.Firmware.UartHandler.Update
    hw = Farmbot.System.ConfigStorage.get_config_value(:string, "settings", "firmware_hardware")
    Update.maybe_update_firmware(hw)
    :ok
  end

  def setup(:prod) do
    file = "#{:code.priv_dir(:farmbot)}/fwup-key.pub"
    Application.put_env(:nerves_firmware, :pub_key_path, file)
    if File.exists?(file), do: :ok, else: {:error, :no_pub_file}
  end

  def setup(_) do
    :ok
  end

  def requires_reboot? do
    !Nerves.Firmware.allow_upgrade?
  end
end

{'fraction_non_alphanumeric': 0.15254237288135594, 'fraction_numerical': 0.0016297262059973925, 'mean_word_length': 3.5601783060921246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '3287049', 'n_tokens_mistral': 1171, 'n_tokens_neox': 1113, 'n_words': 215}