|
|
|
@ -232,10 +232,14 @@ class process:
|
|
|
|
|
|
|
|
|
|
def read(self):
|
|
|
|
|
"""Read all data"""
|
|
|
|
|
try:
|
|
|
|
|
self._open()
|
|
|
|
|
if self.cacheresult is None:
|
|
|
|
|
self.cacheresult = self.pipe.communicate()[0]
|
|
|
|
|
return self.cacheresult
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
self.kill()
|
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
|
|
|
|
|
def readlines(self):
|
|
|
|
|
"""Read lines"""
|
|
|
|
@ -247,15 +251,18 @@ class process:
|
|
|
|
|
self.iter = iter(self.readlines())
|
|
|
|
|
return self.iter
|
|
|
|
|
|
|
|
|
|
def kill(self):
|
|
|
|
|
"""Kill this process"""
|
|
|
|
|
if self.pipe:
|
|
|
|
|
self.pipe.kill()
|
|
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
|
"""Next string from stdout"""
|
|
|
|
|
return self.__iter__().next()
|
|
|
|
|
|
|
|
|
|
def returncode(self):
|
|
|
|
|
"""Get return code"""
|
|
|
|
|
self._open()
|
|
|
|
|
if self.pipe.returncode is None:
|
|
|
|
|
self.cacheresult = self.pipe.communicate()[0]
|
|
|
|
|
self.read()
|
|
|
|
|
return self.pipe.returncode
|
|
|
|
|
|
|
|
|
|
def success(self):
|
|
|
|
@ -266,6 +273,56 @@ class process:
|
|
|
|
|
"""Failed or not"""
|
|
|
|
|
return self.returncode() != 0
|
|
|
|
|
|
|
|
|
|
class processProgress(process):
|
|
|
|
|
"""Execute system command by Popen for parse stdout."""
|
|
|
|
|
|
|
|
|
|
def __init__(self,command,*params,**kwarg):
|
|
|
|
|
process.__init__(self,command,*params,**kwarg)
|
|
|
|
|
self.readsize = kwarg.get("readsize",10)
|
|
|
|
|
self.init(**kwarg)
|
|
|
|
|
|
|
|
|
|
def init(self,**kwarg):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def read(self):
|
|
|
|
|
"""Read data with parsing ability"""
|
|
|
|
|
try:
|
|
|
|
|
self.processInit()
|
|
|
|
|
self._open()
|
|
|
|
|
if self.cacheresult is None:
|
|
|
|
|
self.cacheresult = ""
|
|
|
|
|
self.buf = ""
|
|
|
|
|
part = self.pipe.stdout.read(1)
|
|
|
|
|
while part:
|
|
|
|
|
self.buf += part
|
|
|
|
|
if self.processStdout():
|
|
|
|
|
self.processDraw()
|
|
|
|
|
self.cacheresult += part
|
|
|
|
|
part = self.pipe.stdout.read(self.readsize)
|
|
|
|
|
self.pipe.poll()
|
|
|
|
|
self.processEnd(self.success())
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
self.pipe.kill()
|
|
|
|
|
self.processEnd(False)
|
|
|
|
|
raise KeyboardInterrupt()
|
|
|
|
|
return self.cacheresult
|
|
|
|
|
|
|
|
|
|
def processInit(self):
|
|
|
|
|
"""Called when read first byte"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def processDraw(self):
|
|
|
|
|
"""Called when processStdout return True"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def processStdout(self):
|
|
|
|
|
"""Called when read readsize byte from stdout"""
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def processEnd(self,res=True):
|
|
|
|
|
"""Called when process end"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def runOsCommand(cmd,in_str=None, env_dict=None):
|
|
|
|
|
"""Выполняет внешнюю программу
|
|
|
|
|
|
|
|
|
|