|
|
|
@ -151,31 +151,44 @@ class scanDirectory:
|
|
|
|
|
class process:
|
|
|
|
|
"""Execute system command by Popen
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
#execute program and get result
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
|
|
execute program and get result:
|
|
|
|
|
if process("/bin/gzip","/boot/somefile").success():
|
|
|
|
|
print "Gzip success"
|
|
|
|
|
|
|
|
|
|
#unzip and process unzip data by cpio (list files)
|
|
|
|
|
unzip and process unzip data by cpio (list files):
|
|
|
|
|
processGzip = process("/bin/gzip","-dc","/boot/initrd")
|
|
|
|
|
processCpio = process("/bin/cpio","-tf",stdin=processGzip)
|
|
|
|
|
filelist = processCpio.readlines()
|
|
|
|
|
|
|
|
|
|
#execute command and send data
|
|
|
|
|
execute command and send data:
|
|
|
|
|
processGrub = process("/sbin/grub")
|
|
|
|
|
processGrub.write("root (hd0,0)\n")
|
|
|
|
|
processGrub.write("setup (hd0)\n")
|
|
|
|
|
processGrub.write("quit\n")
|
|
|
|
|
isok = processGrub.success()
|
|
|
|
|
|
|
|
|
|
#union stdout and stderr
|
|
|
|
|
union stdout and stderr:
|
|
|
|
|
process("/bin/ls","/",stderr=STDOUT)
|
|
|
|
|
|
|
|
|
|
result to stdout:
|
|
|
|
|
process("/bin/ls",stdout=None)
|
|
|
|
|
|
|
|
|
|
get data from keyboard:
|
|
|
|
|
process("/bin/cat",stdin=None)
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,command,*params,**kwarg):
|
|
|
|
|
if not "stdin" in kwarg:
|
|
|
|
|
stdin=self._defaultStdin
|
|
|
|
|
else:
|
|
|
|
|
stdin=kwarg["stdin"].getStdout
|
|
|
|
|
if kwarg["stdin"] == None:
|
|
|
|
|
stdin = self._keyboardStdin
|
|
|
|
|
else:
|
|
|
|
|
stdin=kwarg["stdin"].getStdout
|
|
|
|
|
self.stdout = kwarg.get("stdout",PIPE)
|
|
|
|
|
self.envdict = kwarg.get("envdict",None)
|
|
|
|
|
|
|
|
|
|
self.stderr = kwarg.get("stderr",PIPE)
|
|
|
|
|
self.command = [command] + list(params)
|
|
|
|
|
self.stdin = stdin
|
|
|
|
@ -188,14 +201,19 @@ class process:
|
|
|
|
|
"""Open pipe if it not open"""
|
|
|
|
|
if not self.pipe:
|
|
|
|
|
self.pipe = Popen(self.command,
|
|
|
|
|
stdout=PIPE,
|
|
|
|
|
stdout=self.stdout,
|
|
|
|
|
stdin=self.stdin(),
|
|
|
|
|
stderr=self.stderr)
|
|
|
|
|
stderr=self.stderr,
|
|
|
|
|
env=self.envdict)
|
|
|
|
|
|
|
|
|
|
def _defaultStdin(self):
|
|
|
|
|
"""Return default stdin"""
|
|
|
|
|
return PIPE
|
|
|
|
|
|
|
|
|
|
def _keyboardStdin(self):
|
|
|
|
|
"""Return keyboard stdin"""
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def getStdout(self):
|
|
|
|
|
"""Get current stdout"""
|
|
|
|
|
self.close()
|
|
|
|
|