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, #destroy_frame, #exec, #float_frame, #frame, #frame_def_visible?, #frame_domain, #frame_domain_default, #frame_raised?, #frame_title, #frame_visible?, #hide_frame, #hinner_dialog, #hinner_splitted_dialog, #hinner_splitted_dialog_titled, #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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'ext/ae-shell/ae-shell.rb', line 135

def on_run_cmd(_event)
  if _event.cmd
    begin
      if _event.prompt
        hd_args = {
          'type'=>'ok_cancel',
          'msg'=>_event.title,
          'title' => _event.title, 
          'prompt' => _event.prompt, 
          'level' => 'info'
        }
          
        hd_event = HinnerDialogEvent.new(self, hd_args).go!
      end
      if _event.file
        process_name = _event.file
      else
        process_name = "#{_event.cmd[0..8]} ..."
      end
      output_mark = Arcadia.console(self,'msg'=>"Running #{_event.title} as ... #{_event.lang}", 'level'=>'info') # 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? && RUBY_VERSION < '1.9'
        # 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}
        RunCmdStartedEvent.new(self, 'parent'=>_event).shot!
        #----
        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'=>process_name,'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)
            begin
              output.close
              timer.stop
              File.open(output_file_name, 'r') do |f|
                _readed = f.read
                _readed.strip!
                
                _readed += "\n" + Arcadia.text('ext.shell.done', [_event.title, Time.now - start_time])
                output_mark = Arcadia.console(self,'msg'=>_readed, 'level'=>'debug', 'mark'=>output_mark)
                _event.add_result(self, 'output'=>_readed)
              end
              File.delete output_file_name
            ensure
              RunCmdEndedEvent.new(self, 'parent'=>_event).shot!
            end
          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
            fi = nil
            fi_pid = nil

            Open3.popen3(_cmd_){|stdin, stdout, stderr, th|
              fi_pid = th.pid if th
              output_mark = Arcadia.console(self,'msg'=>" [pid #{fi_pid}]", 'level'=>'info', 'mark'=>output_mark, 'append'=>true)
              RunCmdStartedEvent.new(self, 'parent'=>_event).shot!
              alive_check = proc{th.status != false}
              abort_action = proc{Process.kill(9,fi_pid.to_i); th.exit}
  	           Arcadia.process_event(SubProcessEvent.new(self, 'pid'=>fi_pid, 'name'=>process_name, 'abort_action'=>abort_action, 'alive_check'=>alive_check))

              to = Thread.new(stdout) do |tout|
                begin
                  while (line = tout.gets)
                    output_mark = Arcadia.console(self,'msg'=>line, 'level'=>'debug', 'mark'=>output_mark)
                    _event.add_result(self, 'output'=>line)
                  end
                  RunCmdEndedEvent.new(self, 'parent'=>_event).shot!
                rescue Exception => e
                  output_mark = Arcadia.console(self,'msg'=>e.to_s, 'level'=>'debug', 'mark'=>output_mark)
                end
              end

              input_break = false
              ti = Thread.new(stdin, input_break) do |tin|
                begin
                  while (tin && !tin.closed? && !input_break)
                    if th.status != false && to.status == 'sleep'
                      begin
                        input = Arcadia.console_input(self, fi_pid)
                        tin.puts(input) if input != nil
                        sleep(0.1)
                      rescue Exception => e
                        output_mark = Arcadia.console(self,'msg'=>e.to_s, 'level'=>'debug', 'mark'=>output_mark)
                      end
                    else
                      sleep(0.1)
                    end
                  end
                rescue Exception => e
                  output_mark = Arcadia.console(self,'msg'=>e.to_s, 'level'=>'debug', 'mark'=>output_mark)
                end
              end

              te = Thread.new(stderr) do |terr|
                begin
                  while (line = terr.gets)
                    output_mark = Arcadia.console(self,'msg'=>line, 'level'=>'error', 'mark'=>output_mark)
                    _event.add_result(self, 'output'=>line)
                    if line && line.length > 0
                      _event.flag = Event::FLAG_ERROR
                    end
                  end
                rescue Exception => e
                  output_mark = Arcadia.console(self,'msg'=>e.to_s, 'level'=>'debug', 'mark'=>output_mark)
                end
              end
              to.join
              te.join
              input_event = Arcadia.console_input_event
              if input_event != nil
                input_event.break
                input_break = true
                ti.join
              else
                ti.exit
              end
              te.exit
            }
            output_mark = Arcadia.console(self,'msg'=>"\n"+Arcadia.text('ext.shell.done.1', [_event.title]), 'level'=>'info', '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


330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'ext/ae-shell/ae-shell.rb', line 330

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