Sha256: f2bc5840e7ad8e106fae9ab76ac83918d449b75a95dc1d365a864f9e14c241e2

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

require "mini_magick/logger"
require "timeout"

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

    def initialize(whiny = true)
      @whiny = whiny
    end

    def run(command)
      stdout, stderr, code = execute(command)

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

      $stderr.print(stderr)

      stdout
    end

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

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

    private

    def execute_open3(*command)
      require "open3"
      Open3.capture3(*command)
    end

    def execute_posix_spawn(*command)
      require "posix-spawn"
      pid, stdin, stdout, stderr = POSIX::Spawn.popen4(*command)
      Process.waitpid(pid)

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

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mini_magick-4.2.3 lib/mini_magick/shell.rb
mini_magick-4.2.1 lib/mini_magick/shell.rb
mini_magick-4.2.0 lib/mini_magick/shell.rb
mini_magick-4.1.1 lib/mini_magick/shell.rb
mini_magick-4.1.0 lib/mini_magick/shell.rb
mini_magick-4.0.4 lib/mini_magick/shell.rb
mini_magick-4.0.3 lib/mini_magick/shell.rb