<commit_before>require 'childprocess'
require 'tempfile'
require 'shellwords'

require 'aruba/errors'
require 'aruba/processes/basic_process'
require 'aruba/platform'

module Aruba
  module Processes
    class SpawnProcess < BasicProcess
      # Use as default launcher
      def self.match?(mode)
        true
      end

      # Create process
      #
      # @params [String] cmd
      #   Command string
      #
      # @params [Integer] exit_timeout
      #   The timeout until we expect the command to be finished
      #
      # @params [Integer] io_wait
      #   The timeout until we expect the io to be finished
      #
      # @params [String] working_directory
      #   The directory where the command will be executed
      def initialize(cmd, exit_timeout, io_wait, working_directory, environment = ENV.to_hash.dup, main_class = nil)
        super

        @exit_timeout = exit_timeout
        @io_wait      = io_wait
        @process      = nil
        @stdout_cache       = nil
        @stderr_cache       = nil
      end

      # Run the command
      #
      # @yield [SpawnProcess]
      #   Run code for process which was started
      #
      # rubocop:disable Metrics/MethodLength
      # rubocop:disable Metrics/CyclomaticComplexity
      def start
        cmd = Aruba.platform.command_string.new(command)

        @process   = ChildProcess.build(*[cmd.to_a, arguments].flatten)
        @stdout_file = Tempfile.new("aruba-stdout")
        @stderr_file = Tempfile.new("aruba-stderr")
        @exit_status = nil
        @duplex      = true

        before_run

        @process.io.stdout = @stdout_file
        @process.io.stderr = @stderr_file
        @process.duplex    = @duplex
        @process.cwd       = @working_directory

        @process.environment.update(environment)

        begin
          with_local_env(environment) do
            @process.start
          end
        rescue ChildProcess::LaunchError => e
          raise LaunchError, "It tried to start #{cmd}. " + e.message
        end

        after_run

        yield self if block_given?
      end
      # rubocop:enable Metrics/MethodLength
      # rubocop:enable Metrics/CyclomaticComplexity

      def stdin
        return if @process.nil?

        @process.io.stdin
      end

      def stdout(opts = {})
        return @stdout_cache if @process.nil?

        wait_for_io = opts.fetch(:wait_for_io, @io_wait)

        wait_for_io wait_for_io do
          @process.io.stdout.flush
          open(@stdout_file).read
        end
      end

      def stderr(opts = {})
        return @stderr_cache if @process.nil?

        wait_for_io = opts.fetch(:wait_for_io, @io_wait)

        wait_for_io wait_for_io do
          @process.io.stderr.flush
          open(@stderr_file).read
        end
      end

      def read_stdout
        # rubocop:disable Metrics/LineLength
        Aruba::Platform.deprecated('The use of "#read_stdout" is deprecated. Use "#stdout" instead. To reduce the time to wait for io, pass `:wait_for_io => 0` or some suitable for your use case')
        # rubocop:enable Metrics/LineLength

        stdout(:wait_for_io => 0)
      end

      def write(input)<commit_msg>Add some documentation for SpawnProcess<commit_after>require 'childprocess'
require 'tempfile'
require 'shellwords'

require 'aruba/errors'
require 'aruba/processes/basic_process'
require 'aruba/platform'

module Aruba
  module Processes
    class SpawnProcess < BasicProcess
      # Use as default launcher
      def self.match?(mode)
        true
      end

      # Create process
      #
      # @params [String] cmd
      #   Command string
      #
      # @params [Integer] exit_timeout
      #   The timeout until we expect the command to be finished
      #
      # @params [Integer] io_wait
      #   The timeout until we expect the io to be finished
      #
      # @params [String] working_directory
      #   The directory where the command will be executed
      def initialize(cmd, exit_timeout, io_wait, working_directory, environment = ENV.to_hash.dup, main_class = nil)
        super

        @exit_timeout = exit_timeout
        @io_wait      = io_wait
        @process      = nil
        @stdout_cache       = nil
        @stderr_cache       = nil
      end

      # Run the command
      #
      # @yield [SpawnProcess]
      #   Run code for process which was started
      #
      # rubocop:disable Metrics/MethodLength
      # rubocop:disable Metrics/CyclomaticComplexity
      def start
        cmd = Aruba.platform.command_string.new(command)

        @process   = ChildProcess.build(*[cmd.to_a, arguments].flatten)
        @stdout_file = Tempfile.new("aruba-stdout")
        @stderr_file = Tempfile.new("aruba-stderr")
        @exit_status = nil
        @duplex      = true

        before_run

        @process.io.stdout = @stdout_file
        @process.io.stderr = @stderr_file
        @process.duplex    = @duplex
        @process.cwd       = @working_directory

        @process.environment.update(environment)

        begin
          with_local_env(environment) do
            @process.start
          end
        rescue ChildProcess::LaunchError => e
          raise LaunchError, "It tried to start #{cmd}. " + e.message
        end

        after_run

        yield self if block_given?
      end
      # rubocop:enable Metrics/MethodLength
      # rubocop:enable Metrics/CyclomaticComplexity

      # Access to stdout of process
      def stdin
        return if @process.nil?

        @process.io.stdin
      end

      # Access to stdout of process
      #
      # @param [Hash] opts
      #   Options
      #
      # @option [Integer] wait_for_io
      #   Wait for IO to be finished
      #
      # @return [String]
      #   The content of stdout
      def stdout(opts = {})
        return @stdout_cache if @process.nil?

        wait_for_io = opts.fetch(:wait_for_io, @io_wait)

        wait_for_io wait_for_io do
          @process.io.stdout.flush
          open(@stdout_file).read
        end
      end

      # Access to stderr of process
      #
      # @param [Hash] opts
      #   Options
      #
      # @option [Integer] wait_for_io
      #   Wait for IO to be finished
      #
      # @return [String]
      #   The content of stderr
      def stderr(opts = {})
        return @stderr_cache if @process.nil?

        wait_for_io = opts.fetch(:wait_for_io, @io_wait)

        wait_for_io wait_for_io do
          @process.io.stderr.flush
          open(@stderr_file).read
        end
      end

      def read_stdout
        # rubocop:disable Metrics/LineLength
        Aruba::Platform.deprecated('The use of "#read_stdout" is deprecated. Use "#stdout" instead. To reduce the time to wait for io, pass `:wait_for_io => 0` or some suitable for your use case')
        # rubocop:enable Metrics/LineLength

        stdout(:wait_for_io => 0)
      end

      def write(input)
{'fraction_non_alphanumeric': 0.08014492753623188, 'fraction_numerical': 0.0005797101449275362, 'mean_word_length': 2.022777047744196, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '1349284', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1994, 'n_words': 530}