Module: BashHelper

Included in:
GnomeHelper, MultimediaHelper, SystemHelper
Defined in:
lib/sapis/bash_helper.rb

Overview

<bash_helper.rb> - Part of Sav’s APIs. Copyright © 2011 Saverio Miroddi

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.encode_bash_filenames(*files) ⇒ Object



73
74
75
76
# File 'lib/sapis/bash_helper.rb', line 73

def BashHelper.encode_bash_filenames(*files)
  quoted_filenames = files.map { |file| file.shellescape }
  quoted_filenames.join(' ')
end

.safe_execute(cmd, options = {}) ⇒ Object

REFACTOR: default :out to STDOUT



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sapis/bash_helper.rb', line 27

def self.safe_execute(cmd, options={})
  out = options[:out]

  Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
    exit_status = wait_thr.value.exitstatus

    output = stdout.read

    out << output if out

    if exit_status == 0
      output.chomp unless out
    else
      raise stderr.read.chomp
    end
  end
end

.simple_bash_execute(command, *files_and_options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sapis/bash_helper.rb', line 57

def BashHelper.simple_bash_execute(command, *files_and_options)
  options = files_and_options.last.is_a?(Hash) ? files_and_options.pop : {}
  files   = files_and_options

  output = options.has_key?(:out) ? options[:out] : STDOUT

  files = files.flatten
  command = "#{command} #{encode_bash_filenames(*files)}"

  safe_execute(command, out: output)
end

Instance Method Details

#encode_bash_filenames(*files) ⇒ Object



69
70
71
# File 'lib/sapis/bash_helper.rb', line 69

def encode_bash_filenames(*files)
  BashHelper.encode_bash_filenames(*files)
end

#safe_execute(cmd, options = {}) ⇒ Object



45
46
47
# File 'lib/sapis/bash_helper.rb', line 45

def safe_execute(cmd, options={})
  BashHelper.safe_execute(cmd, options)
end

#simple_bash_execute(command, *files_and_options) ⇒ Object

Encode as single-quoted, space-separated series of filenames. Encoding a slash apparently requires four slashes.



53
54
55
# File 'lib/sapis/bash_helper.rb', line 53

def simple_bash_execute(command, *files_and_options)
  BashHelper.simple_bash_execute(command, *files_and_options)
end