Module: CMD

Defined in:
lib/rbbt/util/cmd.rb

Class Method Summary collapse

Class Method Details

.cmd(cmd, options = {}, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rbbt/util/cmd.rb', line 29

def self.cmd(cmd, options = {}, &block)
  options = Misc.add_defaults options, :stderr => Log::DEBUG
  in_content = options.delete(:in)
  stderr     = options.delete(:stderr)
  pipe       = options.delete(:pipe)
  post       = options.delete(:post)
  log        = options.delete(:log)
  no_fail    = options.delete(:no_fail)
  no_wait    = options.delete(:no_wait)

  dont_close_in  = options.delete(:dont_close_in)

  log = true if log.nil?

  if stderr == true
    stderr = Log::HIGH
  end

  cmd_options = process_cmd_options options
  if cmd =~ /'\{opt\}'/
    cmd.sub!('\'{opt}\'', cmd_options) 
  else
    cmd << " " << cmd_options
  end

  in_content = StringIO.new in_content if String === in_content

  sout, serr, sin = Misc.pipe, Misc.pipe, Misc.pipe

  pid = fork {
    begin
      Misc.purge_pipes(sin.last,sout.last,serr.last)

      sin.last.close
      sout.first.close
      serr.first.close

      if IO === in_content
        in_content.close if in_content.respond_to?(:close) and not in_content.closed?
        in_content.join if in_content.respond_to?(:join) and not in_content.joined?
      end


      STDERR.reopen serr.last
      serr.last.close

      STDIN.reopen sin.first
      sin.first.close

      STDOUT.reopen sout.last
      sout.last.close

      STDOUT.sync = STDERR.sync = true

      exec(ENV, cmd)

      exit(-1)
    rescue Exception
      Log.debug{ "ProcessFailed: #{$!.message}" } if log
      Log.debug{ "Backtrace: \n" + $!.backtrace * "\n" } if log
      raise ProcessFailed, $!.message
    end
  }

  sin.first.close
  sout.last.close
  serr.last.close


  sin = sin.last
  sout = sout.first
  serr = serr.first


  Log.debug{"CMD: [#{pid}] #{cmd}" if log}

  if in_content.respond_to?(:read)
    in_thread = Thread.new(Thread.current) do |parent|
      begin
        begin
          while c = in_content.readpartial(Misc::BLOCK_SIZE)
            sin << c 
          end
        rescue EOFError
        end
        sin.close  unless sin.closed?

        unless dont_close_in
          in_content.close unless in_content.closed? 
          in_content.join if in_content.respond_to? :join 
        end
      rescue
        parent.raise $!
        Process.kill "INT", pid
      ensure
        sin.close  unless sin.closed?
      end
    end
  else
    in_thread = nil
    sin.close
  end

  pids = [pid]

  if pipe
    err_thread = Thread.new do
      while line = serr.gets
        Log.log "STDERRĀ [#{pid}]: " +  line, stderr if Integer === stderr and log
      end
      serr.close
    end

    ConcurrentStream.setup sout, :pids => pids, :threads => [in_thread, err_thread].compact, :autojoin => no_wait, :no_fail => no_fail 

    sout
  else
    err = ""
    err_thread = Thread.new do
      while not serr.eof?
        err << serr.gets if Integer === stderr
      end
      serr.close
    end

    ConcurrentStream.setup sout, :pids => pids, :threads => [in_thread, err_thread].compact, :autojoin => no_wait, :no_fail => no_fail 

    out = StringIO.new sout.read
    sout.close unless sout.closed?

    Process.waitpid pid

    if not $?.success? and not no_fail
      raise ProcessFailed.new "Command [#{pid}] #{cmd} failed with error status #{$?.exitstatus}.\n#{err}"
    else
      Log.log err, stderr if Integer === stderr and log
    end

    out
  end
end

.cmd_log(*args) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/rbbt/util/cmd.rb', line 171

def self.cmd_log(*args)
  all_args = *args

  all_args << {} unless Hash === all_args.last
  all_args.last[:log] = true
  all_args.last[:pipe] = true

  io = cmd(*all_args)
  pid = io.pids.first
  while line = io.gets
    if pid
      Log.debug "STDOUTĀ [#{pid}]: " + line
    else
      Log.debug "STDOUT: " + line
    end
  end
  io.join
  nil
end

.process_cmd_options(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rbbt/util/cmd.rb', line 6

def self.process_cmd_options(options = {})
  string = ""
  options.each do |option, value|
    raise "Invalid option key: #{option.inspect}" if option.to_s !~ /^[a-z_0-9\-=]+$/i
    raise "Invalid option value: #{value.inspect}" if value.to_s.include? "'"

    case 
    when value.nil? || FalseClass === value 
      next
    when TrueClass === value
      string << "#{option} "
    else
      if option.to_s.chars.to_a.last == "="
        string << "#{option}'#{value}' "
      else
        string << "#{option} '#{value}' "
      end
    end
  end

  string.strip
end