Beremiz.py
changeset 48 6b30cfee163e
parent 46 6f2689a2438b
child 49 45dc6a944ab6
equal deleted inserted replaced
47:fd45c291fed0 48:6b30cfee163e
    26 
    26 
    27 import wx
    27 import wx
    28 
    28 
    29 import types
    29 import types
    30 
    30 
       
    31 import time
       
    32 
    31 import os, re, platform, sys, time, traceback, getopt, commands
    33 import os, re, platform, sys, time, traceback, getopt, commands
    32 
    34 
    33 from plugger import PluginsRoot
    35 from plugger import PluginsRoot
       
    36 
       
    37 from wxPopen import wxPopen3
    34 
    38 
    35 class LogPseudoFile:
    39 class LogPseudoFile:
    36     """ Base class for file like objects to facilitate StdOut for the Shell."""
    40     """ Base class for file like objects to facilitate StdOut for the Shell."""
    37     def __init__(self, output = None):
    41     def __init__(self, output = None):
    38         self.red_white = wx.TextAttr("RED", "WHITE")
    42         self.red_white = wx.TextAttr("RED", "WHITE")
    61         self.output.SetValue("")
    65         self.output.SetValue("")
    62     
    66     
    63     def isatty(self):
    67     def isatty(self):
    64         return false
    68         return false
    65 
    69 
    66     def LogCommand(self, Command, sz_limit = 100):
    70     def LogCommand(self, Command, sz_limit = 100, no_stdout=False):
    67 
    71         self.errlen = 0
    68         import os, popen2, select, signal
    72         self.exitcode = None
    69         
    73         self.outdata = ""
    70         child = popen2.Popen3(Command, 1) # capture stdout and stderr from command
    74         self.errdata = ""
    71         child.tochild.close()             # don't need to talk to child
    75         
    72         outfile = child.fromchild 
    76         def output(v):
    73         outfd = outfile.fileno()
    77             self.outdata += v
    74         errfile = child.childerr
    78             if not no_stdout:
    75         errfd = errfile.fileno()
    79                 self.write(v)
    76         outdata = errdata = ''
    80 
    77         outeof = erreof = 0
    81         def errors(v):
    78         outlen = errlen = 0
    82             self.errdata += v
    79         while 1:
    83             self.errlen += 1
    80             ready = select.select([outfd,errfd],[],[]) # wait for input
    84             if self.errlen > sz_limit:
    81             if outfd in ready[0]:
    85                 p.kill()
    82                 outchunk = outfile.readline()
    86             self.write_warning(v)
    83                 if outchunk == '': outeof = 1 
    87 
    84                 else : outlen += 1
    88         def fin(pid,ecode):
    85                 outdata += outchunk
    89             self.exitcode = ecode
    86                 self.write(outchunk)
    90             if self.exitcode != 0:
    87             if errfd in ready[0]:
    91                 self.write("pid %d exited with status %d\n"%(pid,ecode))
    88                 errchunk = errfile.readline()
    92 
    89                 if errchunk == '': erreof = 1 
    93         def spin(p):
    90                 else : errlen += 1
    94             while not p.finished:
    91                 errdata += errchunk
    95                 wx.Yield()
    92                 self.write_warning(errchunk)
    96                 time.sleep(0.01)
    93             if outeof and erreof : break
    97 
    94             if errlen > sz_limit or outlen > sz_limit : 
    98         input = []
    95                 os.kill(child.pid, signal.SIGTERM)
    99         p = wxPopen3(Command, input, output, errors, fin, self.output)
    96                 self.write_error("Output size reached limit -- killed\n")
   100         spin(p)
    97                 break
   101 
    98         err = child.wait()
   102         return (self.exitcode, self.outdata, self.errdata)
    99         return (err, outdata, errdata)
       
   100 
   103 
   101 [ID_BEREMIZ, ID_BEREMIZMAINSPLITTER, 
   104 [ID_BEREMIZ, ID_BEREMIZMAINSPLITTER, 
   102  ID_BEREMIZSECONDSPLITTER, ID_BEREMIZLEFTPANEL, 
   105  ID_BEREMIZSECONDSPLITTER, ID_BEREMIZLEFTPANEL, 
   103  ID_BEREMIZPARAMSPANEL, ID_BEREMIZLOGCONSOLE, 
   106  ID_BEREMIZPARAMSPANEL, ID_BEREMIZLOGCONSOLE, 
   104  ID_BEREMIZPLUGINTREE, ID_BEREMIZPLUGINCHILDS, 
   107  ID_BEREMIZPLUGINTREE, ID_BEREMIZPLUGINCHILDS,