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

#conf, #conf_array, #conf_default, #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

#is_windows?Boolean

Returns:

  • (Boolean)


313
314
315
# File 'ext/ae-shell/ae-shell.rb', line 313

def is_windows?
  RUBY_PLATFORM =~ /mingw|mswin/
end

#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
# File 'ext/ae-shell/ae-shell.rb', line 135

def on_run_cmd(_event)
  if _event.cmd
    begin
      output_mark = Arcadia.console(self,'msg'=>"Running #{_event.title}...", '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 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
                  output_dump = stderr.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
                  stderr.each do |output_dump|
                    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 && _event.file[-2..-1] == '~~'
              File.delete(_event.file) if File.exist?(_event.file)
            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 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


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
# File 'ext/ae-shell/ae-shell.rb', line 286

def on_system_exec_bo(_event)
  command = "#{_event.command} 2>&1"
  (RUBY_PLATFORM.include?('mswin32'))?_cmd="cmd":_cmd='sh'
  if 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

#run(_filename = nil) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'ext/ae-shell/ae-shell.rb', line 335

def run(_filename=nil)
  if _filename
    begin
      @arcadia['pers']['run.file.last']=_filename
      @run_threads << Thread.new do
        _cmd_ = "|"+$arcadia['conf']['shell.ruby']+" "+_filename+" 2>&1"
        #  Arcadia.new_debug_msg(self, _cmd_)
        @cmd = open(_cmd_,"r"){|f|
          Arcadia.console(self, 'msg'=>f.read ,'level'=>'debug')
          #Arcadia.new_debug_msg(self, f.read)
        }
      end
    rescue Exception => e
      Arcadia.console(self, 'msg'=>e ,'level'=>'debug')
      #Arcadia.new_debug_msg(self, e)
    end
  end
end

#run_currentObject



321
322
323
324
# File 'ext/ae-shell/ae-shell.rb', line 321

def run_current
  current_editor = $arcadia['editor'].raised
  run(current_editor.file) if current_editor
end

#run_lastObject



317
318
319
# File 'ext/ae-shell/ae-shell.rb', line 317

def run_last
  run($arcadia['pers']['run.file.last'])
end

#stopObject



326
327
328
329
330
331
332
333
# File 'ext/ae-shell/ae-shell.rb', line 326

def stop
  @run_threads.each{|t|
    if t.alive?
      t.kill
    end
  }
  debug_quit if @adw
end