<commit_before>require 'pathname'
require 'tempfile'
require File.expand_path('../interaction', __FILE__)

module Geordi
  module FirefoxForSelenium
    extend Geordi::Interaction

    FIREFOX_FOR_SELENIUM_BASE_PATH = Pathname.new('~/bin/firefoxes').expand_path
    FIREFOX_FOR_SELENIUM_PROFILE_NAME = 'firefox-for-selenium'
    FIREFOX_VERSION_FILE = Pathname.new('.firefox-version')

    def self.install(version)
      Installer.new(version).run
    end

    def self.path_from_config
      version = FIREFOX_VERSION_FILE.exist? && File.read(FIREFOX_VERSION_FILE).strip

      if version and version != 'system'
        unless FirefoxForSelenium.binary(version).exist?
          warn "Firefox #{ version } not found"

          note strip_heredoc(<<-INSTRUCTIONS)
          Install it with
            geordi firefox --setup #{ version }
          INSTRUCTIONS

          prompt 'Run tests anyway?', 'n', /y|yes/ or fail 'Cancelled.'
        end

        path(version)
      end
    end

    def self.path(version)
      FIREFOX_FOR_SELENIUM_BASE_PATH.join(version)
    end

    def self.binary(version, name = "firefox")
      path(version).join(name)
    end

    def self.setup_firefox
      path = path_from_config

      if path
        ENV['PATH'] = "#{path}:#{ENV['PATH']}"
        note 'Firefox for Selenium set up'
      end
    end


    class Installer
      include Geordi::Interaction

      def initialize(version)
        @version = version
      end

      def run
        say_hello
        check_if_run_before
        download_firefox
        create_separate_profile # do this before the patching because the patched binary calls firefox with a profile that does not yet exist
        patch_old_firefox
        configure_old_firefox
        kkthxbb
      end


      private

      def execute_command(cmd)
        system(cmd) or raise "Error while executing command: #{cmd}"
      end

      def run_firefox_for_selenium(args = '')
        execute_command("PATH=#{path}:$PATH firefox #{args}")
      end

      def path
        FirefoxForSelenium.path(@version)
      end

      def download_url
        "ftp://ftp.mozilla.org/pub/firefox/releases/#{@version}/"
      end

      def binary
        FirefoxForSelenium.binary(@version)
      end

      def original_binary
        FirefoxForSelenium.binary(@version, "firefox-original")
      end

      def say_hello
        execute_command('clear')

        puts strip_heredoc(<<-HELLO)
        Whenever Firefox updates, Selenium breaks. This is annoying. This
        script will help you create an unchanging version of Firefox for your
        Selenium tests.

        In particular, this new copy of Firefox will have the following
        properties:

        - It won't update itself with a newer version
        - It can co-exist with your regular Firefox installation (which you can
          update at will)
        - It will use a profile separate from the one you use for regular<commit_msg>When installing an old firefox, access the mozilla repo over HTTPS instead of the (mostly broken) FTP<commit_after>require 'pathname'
require 'tempfile'
require File.expand_path('../interaction', __FILE__)

module Geordi
  module FirefoxForSelenium
    extend Geordi::Interaction

    FIREFOX_FOR_SELENIUM_BASE_PATH = Pathname.new('~/bin/firefoxes').expand_path
    FIREFOX_FOR_SELENIUM_PROFILE_NAME = 'firefox-for-selenium'
    FIREFOX_VERSION_FILE = Pathname.new('.firefox-version')

    def self.install(version)
      Installer.new(version).run
    end

    def self.path_from_config
      version = FIREFOX_VERSION_FILE.exist? && File.read(FIREFOX_VERSION_FILE).strip

      if version and version != 'system'
        unless FirefoxForSelenium.binary(version).exist?
          warn "Firefox #{ version } not found"

          note strip_heredoc(<<-INSTRUCTIONS)
          Install it with
            geordi firefox --setup #{ version }
          INSTRUCTIONS

          prompt 'Run tests anyway?', 'n', /y|yes/ or fail 'Cancelled.'
        end

        path(version)
      end
    end

    def self.path(version)
      FIREFOX_FOR_SELENIUM_BASE_PATH.join(version)
    end

    def self.binary(version, name = "firefox")
      path(version).join(name)
    end

    def self.setup_firefox
      path = path_from_config

      if path
        ENV['PATH'] = "#{path}:#{ENV['PATH']}"
        note 'Firefox for Selenium set up'
      end
    end


    class Installer
      include Geordi::Interaction

      def initialize(version)
        @version = version
      end

      def run
        say_hello
        check_if_run_before
        download_firefox
        create_separate_profile # do this before the patching because the patched binary calls firefox with a profile that does not yet exist
        patch_old_firefox
        configure_old_firefox
        kkthxbb
      end


      private

      def execute_command(cmd)
        system(cmd) or raise "Error while executing command: #{cmd}"
      end

      def run_firefox_for_selenium(args = '')
        execute_command("PATH=#{path}:$PATH firefox #{args}")
      end

      def path
        FirefoxForSelenium.path(@version)
      end

      def download_url
        "https://ftp.mozilla.org/pub/firefox/releases/#{@version}/"
      end

      def binary
        FirefoxForSelenium.binary(@version)
      end

      def original_binary
        FirefoxForSelenium.binary(@version, "firefox-original")
      end

      def say_hello
        execute_command('clear')

        puts strip_heredoc(<<-HELLO)
        Whenever Firefox updates, Selenium breaks. This is annoying. This
        script will help you create an unchanging version of Firefox for your
        Selenium tests.

        In particular, this new copy of Firefox will have the following
        properties:

        - It won't update itself with a newer version
        - It can co-exist with your regular Firefox installation (which you can
          update at will)
        - It will use a profile separate from the one you use for regular
{'fraction_non_alphanumeric': 0.07538104705102718, 'fraction_numerical': 0.0, 'mean_word_length': 2.4281658148779104, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5430593', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1712, 'n_words': 489}