Sha256: 4aeae3a60ceba01cbef63eb012e5ee438bc75c0033d5e9f0c47816b871a43489

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

require "timeout"
require "benchmark"

module MiniMagick
  ##
  # Sends commands to the shell (more precisely, it sends commands directly to
  # the operating system).
  #
  # @private
  #
  class Shell

    def run(command, options = {})
      stdout, stderr, status = execute(command, stdin: options[:stdin])

      case status
      when 1
        fail MiniMagick::Error, "`#{command.join(" ")}` failed with error:\n#{stderr}"
      when 127
        fail MiniMagick::Error, stderr
      end if options.fetch(:whiny, MiniMagick.whiny)

      $stderr.print(stderr) unless options[:stderr] == false

      [stdout, stderr, status]
    end

    def execute(command, options = {})
      stdout, stderr, status =
        log(command.join(" ")) do
          Timeout.timeout(MiniMagick.timeout) do
            send("execute_#{MiniMagick.shell_api.gsub("-", "_")}", command, options)
          end
        end

      [stdout, stderr, status.exitstatus]
    rescue Errno::ENOENT, IOError
      ["", "executable not found: \"#{command.first}\"", 127]
    end

    private

    def execute_open3(command, options = {})
      require "open3"

      Open3.capture3(*command, binmode: true, stdin_data: options[:stdin].to_s)
    end

    def execute_posix_spawn(command, options = {})
      require "posix-spawn"

      pid, stdin, stdout, stderr = POSIX::Spawn.popen4(*command)
      [stdin, stdout, stderr].each(&:binmode)
      stdin.write(options[:stdin].to_s)
      Process.waitpid(pid)

      [stdout.read, stderr.read, $?]
    end

    def log(command, &block)
      value = nil
      duration = Benchmark.realtime { value = block.call }
      MiniMagick.logger.debug "[%.2fs] %s" % [duration, command]
      value
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mini_magick-4.6.0 lib/mini_magick/shell.rb
mini_magick-4.5.1 lib/mini_magick/shell.rb
mini_magick-4.5.0 lib/mini_magick/shell.rb