<commit_before>require "turbot/command/base"
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/hash/slice'
require 'zip'
require 'json-schema'
require 'open3'
require 'base64'
require 'shellwords'

# manage bots (create, destroy)
#
class Turbot::Command::Bots < Turbot::Command::Base

  # bots
  #
  # list your bots
  #
  #Example:
  #
  # $ turbot bots
  # === My Bots
  # example
  # example2
  #
  def index
    validate_arguments!
    bots = api.list_bots
    unless bots.empty?
      styled_header("Bots")
      styled_array(bots.map{|bot| bot['bot_id']})
    else
      display("You have no bots.")
    end
  end

  alias_command "list", "bots"

  # bots:info
  #
  # show detailed bot information
  #
  # -s, --shell  # output more shell friendly key/value pairs
  #
  #Examples:
  #
  # $ turbot bots:info
  # === example
  # Last run status: OK
  # Last run ended: 2001/01/01
  # ...
  #
  # $ turbot bots:info --shell
  # last_run_status: OK
  # last_run_ended: 2001/01/01
  # ...
  #
  def info
    validate_arguments!
    bot_data = api.get_bot(bot)
    unless options[:shell]
      styled_header(bot_data["name"])
    end

    if options[:shell]
      bot_data.keys.sort_by { |a| a.to_s }.each do |key|
        hputs("#{key}=#{bot_data[key]}")
      end
    else
      data = {}
      if bot_data["last_run_status"]
        data["Last run status"] = bot_data["last_run_status"]
      end
      if bot_data["last_run_ended"]
        data["Last run ended"] = format_date(bot_data["last_run_ended"]) if bot_data["last_run_ended"]
      end
      data["Git URL"] = bot_data["git_url"]
      data["Repo Size"] = format_bytes(bot_data["repo_size"]) if bot_data["repo_size"]
      styled_hash(data)
    end
  end

  alias_command "info", "bots:info"


  # bots:generate --bot name_of_bot
  #
  # Generate stub code for a bot in specified language
  #
  #   -l, --language LANGUAGE # language to generate (currently `ruby` (default) or `python`)

  # $ turbot bots:generate --language=ruby --bot my_amazing_bot
  # Created new bot template at my_amazing_bot!

  def generate
    puts "running generate"
    validate_arguments!
    language = options[:language] || "ruby"
    puts "Generating #{language} code..."
    manifest_template = File.expand_path("../../../../templates/manifest.json", __FILE__)
    scraper_template = File.expand_path("../../../../templates/#{language}", __FILE__)
    license_template = File.expand_path("../../../../templates/LICENSE.txt", __FILE__)
    manifest = open(manifest_template).read.sub("{{bot_id}}", bot)
    scraper_name = case language
      when "ruby"
        "scraper.rb"
      when "python"
        "scraper.py"
      end

    manifest = manifest.sub("{{scraper_name}}", scraper_name)

    # Language-specific stuff
    FileUtils.cp_r(scraper_template, bot)

    # Same for all languages
    FileUtils.cp(license_template, "#{bot}/LICENSE.txt")
    open("#{bot}/manifest.json", "w") do |f|
      f.write(JSON.pretty_generate(JSON.parse(manifest)))
    end

    api.create_bot(bot, manifest)

    puts "Created new bot template at #{bot}!"
  end


  # bots:register
  #
  # Register a bot with turbot. Must be run from a folder containing scraper and manifest.json

  # $ turbot bots:register
  # Registered my_amazing_bot!

  def register
    working_dir = Dir.pwd
    manifest = parsed_manifest(working_dir)
    api.create_bot(bot, manifest)
  end

  # bots:push
  #
  # Push bot code to the turbot server. Must be run from a local bot checkout.
  #
  # $ turbot bots:push
  # Creating example... done

  def push
    validate_arguments!

    working_dir = Dir.pwd
    manifest = parsed_manifest(working_dir)
    #archive_file = File.join(working_dir, 'tmp', "#{manifest['bot_id']}.zip")
    archive = Tempfile.new(bot)
    archive_path = "#{archive.path}.zip"<commit_msg>Useful CLI messages for bots:register<commit_after>require "turbot/command/base"
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/hash/slice'
require 'zip'
require 'json-schema'
require 'open3'
require 'base64'
require 'shellwords'

# manage bots (create, destroy)
#
class Turbot::Command::Bots < Turbot::Command::Base

  # bots
  #
  # list your bots
  #
  #Example:
  #
  # $ turbot bots
  # === My Bots
  # example
  # example2
  #
  def index
    validate_arguments!
    bots = api.list_bots
    unless bots.empty?
      styled_header("Bots")
      styled_array(bots.map{|bot| bot['bot_id']})
    else
      display("You have no bots.")
    end
  end

  alias_command "list", "bots"

  # bots:info
  #
  # show detailed bot information
  #
  # -s, --shell  # output more shell friendly key/value pairs
  #
  #Examples:
  #
  # $ turbot bots:info
  # === example
  # Last run status: OK
  # Last run ended: 2001/01/01
  # ...
  #
  # $ turbot bots:info --shell
  # last_run_status: OK
  # last_run_ended: 2001/01/01
  # ...
  #
  def info
    validate_arguments!
    bot_data = api.get_bot(bot)
    unless options[:shell]
      styled_header(bot_data["name"])
    end

    if options[:shell]
      bot_data.keys.sort_by { |a| a.to_s }.each do |key|
        hputs("#{key}=#{bot_data[key]}")
      end
    else
      data = {}
      if bot_data["last_run_status"]
        data["Last run status"] = bot_data["last_run_status"]
      end
      if bot_data["last_run_ended"]
        data["Last run ended"] = format_date(bot_data["last_run_ended"]) if bot_data["last_run_ended"]
      end
      data["Git URL"] = bot_data["git_url"]
      data["Repo Size"] = format_bytes(bot_data["repo_size"]) if bot_data["repo_size"]
      styled_hash(data)
    end
  end

  alias_command "info", "bots:info"


  # bots:generate --bot name_of_bot
  #
  # Generate stub code for a bot in specified language
  #
  #   -l, --language LANGUAGE # language to generate (currently `ruby` (default) or `python`)

  # $ turbot bots:generate --language=ruby --bot my_amazing_bot
  # Created new bot template at my_amazing_bot!

  def generate
    puts "running generate"
    validate_arguments!
    language = options[:language] || "ruby"
    puts "Generating #{language} code..."
    manifest_template = File.expand_path("../../../../templates/manifest.json", __FILE__)
    scraper_template = File.expand_path("../../../../templates/#{language}", __FILE__)
    license_template = File.expand_path("../../../../templates/LICENSE.txt", __FILE__)
    manifest = open(manifest_template).read.sub("{{bot_id}}", bot)
    scraper_name = case language
      when "ruby"
        "scraper.rb"
      when "python"
        "scraper.py"
      end

    manifest = manifest.sub("{{scraper_name}}", scraper_name)

    # Language-specific stuff
    FileUtils.cp_r(scraper_template, bot)

    # Same for all languages
    FileUtils.cp(license_template, "#{bot}/LICENSE.txt")
    open("#{bot}/manifest.json", "w") do |f|
      f.write(JSON.pretty_generate(JSON.parse(manifest)))
    end

    api.create_bot(bot, manifest)

    puts "Created new bot template at #{bot}!"
  end


  # bots:register
  #
  # Register a bot with turbot. Must be run from a folder containing scraper and manifest.json

  # $ turbot bots:register
  # Registered my_amazing_bot!

  def register
    working_dir = Dir.pwd
    manifest = parsed_manifest(working_dir)
    response = api.create_bot(bot, manifest)
    if response.is_a? Turbot::API::FailureResponse
      error(response.message)
    else
      puts "Registered #{bot}!"
    end
  end

  # bots:push
  #
  # Push bot code to the turbot server. Must be run from a local bot checkout.
  #
  # $ turbot bots:push
  # Creating example... done

  def push
    validate_arguments!

    working_dir = Dir.pwd
    manifest = parsed_manifest(working_dir)
    #archive_file = File.join(working_dir, 'tmp', "#{manifest['bot_id']}.zip")
    archive = Tempfile.new(bot)
    archive_path = "#{archive.path}.zip"
{'fraction_non_alphanumeric': 0.13374825352470468, 'fraction_numerical': 0.0050806554045471864, 'mean_word_length': 3.037948717948718, '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': '2102846', 'n_tokens_mistral': 2745, 'n_tokens_neox': 2660, 'n_words': 613}