Class: Shell

Inherits:
ArcadiaExt show all
Defined in:
ext/ae-shell/ae-shell.rb

Constant Summary collapse

@@next_number =
0

Instance Attribute Summary

Attributes inherited from ArcadiaExt

#arcadia, #name

Instance Method Summary collapse

Methods inherited from ArcadiaExt

#add_to_conf_property, #array_conf, #conf, #conf_array, #conf_default, #del_from_conf_property, #exec, #float_frame, #frame, #frame_def_visible?, #frame_domain, #frame_domain_default, #frame_visible?, #initialize, #maximize, #maximized?, #resize, #restore_default_conf

Constructor Details

This class inherits a constructor from ArcadiaExt

Instance Method Details

#on_before_build(_event) ⇒ Object



12
13
14
15
16
# File 'ext/ae-shell/ae-shell.rb', line 12

def on_before_build(_event)
  Arcadia.attach_listener(self, SystemExecEvent)
  #Arcadia.attach_listener(self, RunRubyFileEvent)
  Arcadia.attach_listener(self, RunCmdEvent)
end

#on_build(_event) ⇒ Object



18
19
20
21
22
# File 'ext/ae-shell/ae-shell.rb', line 18

def on_build(_event)
  @run_threads = Array.new
  @stdout_blocking = self.conf('stdout_blocking') == 'yes' 
  @stderr_blocking = self.conf('stderr_blocking') == 'yes' 
end

#on_run_cmd(_event) ⇒ Object



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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'ext/ae-shell/ae-shell.rb', line 135

def on_run_cmd(_event)
  if _event.cmd
    #p "_event.cmd = #{_event.cmd}"
    begin
      output_mark = Arcadia.console(self,'msg'=>"Running #{_event.title} as #{_event.lang}...", 'level'=>'debug') # info?
      start_time = Time.now
      @arcadia['pers']['run.file.last']=_event.file if _event.persistent
      @arcadia['pers']['run.cmd.last']=_event.cmd if _event.persistent
      if Arcadia.is_windows?
        # use win32-process gem to startup a child process [not sure if linux needs something like this, too]
        require 'win32/process'
        require 'ruby-wmi'
        output_file_name = "out_#{@@next_number += 1}_#{Process.pid}.txt"
        output = File.open(output_file_name, 'wb')
        child = Process.create :command_line => _event.cmd,  :startup_info => {:stdout => output, :stderr => output}
        #----
        abort_action = proc{
          Process.kill(9,child.process_id)
        }
        alive_check = proc{
          WMI::Win32_Process.find(:first, :conditions => {:ProcessId => child.process_id})
        }
        Arcadia.process_event(SubProcessEvent.new(self,'pid'=>child.process_id, 'name'=>_event.file,'abort_action'=>abort_action, 'alive_check'=>alive_check))
        #----
        timer=nil
        procy = proc {
          still_alive = WMI::Win32_Process.find(:first, :conditions => {:ProcessId => child.process_id})
          if !still_alive #&& File.exists?(output_file_name)
            output.close
            timer.stop
            File.open(output_file_name, 'r') do |f|
              _readed = f.read
              _readed.strip!
              _readed += "\n" + "Done with #{_event.title} in #{Time.now - start_time}s"
              output_mark = Arcadia.console(self,'msg'=>_readed, 'level'=>'debug', 'mark'=>output_mark)
              _event.add_result(self, 'output'=>_readed)
            end
            File.delete output_file_name
          end
        }

        timer=TkAfter.new(1000,-1,procy) # -1 = repeating every 1000ms...
        timer.start
      else
        require "open3"
        #_cmd_ = "|#{_event.cmd} 2>&1"
        _cmd_ = _event.cmd
        Thread.new {
          Thread.current.abort_on_exception = true
          begin
#              th = Thread.current
            fi = nil
            fi_pid = nil
            abort_action = proc{
              ArcadiaUtils.unix_child_pids(fi_pid).each {|pid|
                Process.kill(9,pid.to_i)
              }
              Process.kill(9,fi_pid.to_i)
              #Kernel.system("kill -9 #{unix_child_pids(fi_pid).join(' ')} #{fi_pid}") if fi
            }
              
            alive_check = proc{
              num = `ps -p #{fi_pid}|wc -l` if fi_pid
              num.to_i > 1 && fi_pid
            }
            

            #Arcadia.console(self,'msg'=>"#{th}", 'level'=>'debug', 'abort_action'=>abort_action)

            Open3.popen3(_cmd_){|stdin, stdout, stderr, th|
              fi_pid = th.pid if th
              output_mark = Arcadia.console(self,'msg'=>" [pid #{fi_pid}]", 'level'=>'debug', 'mark'=>output_mark, 'append'=>true)
  	           Arcadia.process_event(SubProcessEvent.new(self, 'pid'=>fi_pid, 'name'=>_event.file,'abort_action'=>abort_action, 'alive_check'=>alive_check))
              
              if stdout  
                if @stdout_blocking
                  output_dump = stdout.read
                  if output_dump && output_dump.length > 0 
                    output_mark = Arcadia.console(self,'msg'=>output_dump, 'level'=>'error', 'mark'=>output_mark)
                    _event.add_result(self, 'output'=>output_dump)
                  end
                else
                  stdout.each do |output_dump|
                    output_mark = Arcadia.console(self,'msg'=>output_dump, 'level'=>'debug', 'mark'=>output_mark)
                    _event.add_result(self, 'output'=>output_dump)
                  end
                end
              end
              
              if stderr
                if @stderr_blocking
                  current_buffer = '<<current buffer>>'
                  output_dump = stderr.read
                  if output_dump && output_dump.length > 0 
                    if !_event.persistent
                      output_dump.gsub!(_event.file, current_buffer)
                    end
                    output_mark = Arcadia.console(self,'msg'=>output_dump, 'level'=>'error', 'mark'=>output_mark)
                    _event.add_result(self, 'output'=>output_dump)
                  end
                else
                  stderr.each do |output_dump|
                    if !_event.persistent
                      output_dump.gsub!(_event.file, current_buffer)
                    end
                    output_mark = Arcadia.console(self,'msg'=>output_dump, 'level'=>'error', 'mark'=>output_mark)
                    _event.add_result(self, 'output'=>output_dump)
                  end
                end
              end
            }
            output_mark = Arcadia.console(self,'msg'=>"\nEnd running #{_event.title}:", 'level'=>'debug', 'mark'=>output_mark)


#              open(_cmd_, "r"){|f|
#                fi = f
#                fi_pid = fi.pid
#    	           Arcadia.process_event(SubProcessEvent.new(self,'name'=>_event.file,'abort_action'=>abort_action, 'alive_check'=>alive_check))
#                _readed = f.read
#                
#                #f.each{|line|
#                #  output_mark = Arcadia.console(self,'msg'=>line, 'level'=>'debug', 'mark'=>output_mark)
#                #}
#                #output_mark = Arcadia.console(self,'msg'=>"\nEnd running #{_event.file}:", 'level'=>'debug', 'mark'=>output_mark)
#                
#                output_dump="#{_readed}\nEnd running #{_event.file}:"
#                output_mark = Arcadia.console(self,'msg'=>output_dump, 'level'=>'debug', 'mark'=>output_mark)
#                _event.add_result(self, 'output'=>_readed)
#              }
            #p "da cancellare #{ _event.file } #{_event.file[-2..-1] == '~~'} #{_event.persistent == false}  #{File.exist?(_event.file)}"
            if _event.persistent == false && File.exist?(_event.file) 
              if File.basename(_event.file)[0..1] == '~~'
                File.delete(_event.file)
              elsif File.basename(File.dirname(_event.file))[0..1] == '~~'
                Dir["#{File.dirname(_event.file)}/*"].each{|file|
                  File.delete(file)
                }
                Dir.delete(File.dirname(_event.file))
              end
            end
          rescue Exception => e
            output_mark = Arcadia.console(self,'msg'=>e.to_s, 'level'=>'debug', 'mark'=>output_mark)
          end
        }
      end
    rescue Exception => e
      output_mark = Arcadia.console(self,'msg'=>e.to_s, 'level'=>'debug', 'mark'=>output_mark)
    end
  end
end

#on_system_exec(_event) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'ext/ae-shell/ae-shell.rb', line 24

def on_system_exec(_event)
  begin
    _cmd_ = "#{_event.command}"
    if Arcadia.is_windows?
      io = IO.popen(_cmd_)
      Arcadia.console(self,'msg'=>io.read, 'level'=>'debug')
    else
      Process.fork{
        open(_cmd_,"r"){|f|
          Arcadia.console(self,'msg'=>f.read, 'level'=>'debug')
        }
      }
    end
  rescue Exception => e
    Arcadia.console(self,'msg'=>e, 'level'=>'debug')
  end
end

#on_system_exec_bo(_event) ⇒ Object

def unix_child_pids(_ppid)

  ret = Array.new
  readed = ''
  open("|ps -o pid,ppid ax | grep #{_ppid}", "r"){|f|  readed = f.read  }
  apids = readed.split
  apids.each_with_index do |v,i|
    ret << v if i % 2 == 0 && v != _ppid.to_s
  end
  subpids = Array.new
  ret.each{|ccp|
    subpids.concat(unix_child_pids(ccp))
  }
  ret.concat(subpids)
end


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'ext/ae-shell/ae-shell.rb', line 301

def on_system_exec_bo(_event)
  command = "#{_event.command} 2>&1"
  (RUBY_PLATFORM.include?('mswin32'))?_cmd="cmd":_cmd='sh'
  if Arcadia.is_windows?
    Thread.new{
      Arcadia.console(self,'msg'=>'begin', 'level'=>'debug')
      #Arcadia.new_debug_msg(self, 'inizio')
      @io = IO.popen(_cmd,'r+')
      @io.puts(command)
      result = ''
      while line = @io.gets
        result << line
      end
      #Arcadia.new_debug_msg(self, result)
      Arcadia.console(self,'msg'=>result, 'level'=>'debug')

    }
  else
    Process.fork{
      open(_cmd_,"r"){|f|
        Arcadia.console(self,'msg'=>f.read, 'level'=>'debug')
        #Arcadia.new_debug_msg(self, f.read)
      }
    }
  end
end