Add ability get input from keyboard.

develop
Mike Hiretsky 14 years ago
parent 237aa8dfaf
commit 242ab6556e

@ -151,35 +151,44 @@ class scanDirectory:
class process: class process:
"""Execute system command by Popen """Execute system command by Popen
Example: Examples:
#execute program and get result
execute program and get result:
if process("/bin/gzip","/boot/somefile").success(): if process("/bin/gzip","/boot/somefile").success():
print "Gzip 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") processGzip = process("/bin/gzip","-dc","/boot/initrd")
processCpio = process("/bin/cpio","-tf",stdin=processGzip) processCpio = process("/bin/cpio","-tf",stdin=processGzip)
filelist = processCpio.readlines() filelist = processCpio.readlines()
#execute command and send data execute command and send data:
processGrub = process("/sbin/grub") processGrub = process("/sbin/grub")
processGrub.write("root (hd0,0)\n") processGrub.write("root (hd0,0)\n")
processGrub.write("setup (hd0)\n") processGrub.write("setup (hd0)\n")
processGrub.write("quit\n") processGrub.write("quit\n")
isok = processGrub.success() isok = processGrub.success()
#union stdout and stderr union stdout and stderr:
process("/bin/ls","/",stderr=STDOUT) 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): def __init__(self,command,*params,**kwarg):
if not "stdin" in kwarg: if not "stdin" in kwarg:
stdin=self._defaultStdin stdin=self._defaultStdin
else: else:
stdin=kwarg["stdin"].getStdout if kwarg["stdin"] == None:
if not "stdout" in kwarg: stdin = self._keyboardStdin
self.stdout=PIPE else:
else: stdin=kwarg["stdin"].getStdout
self.stdout=kwarg["stdout"] self.stdout = kwarg.get("stdout",PIPE)
self.envdict = kwarg.get("envdict",None)
self.stderr = kwarg.get("stderr",PIPE) self.stderr = kwarg.get("stderr",PIPE)
self.command = [command] + list(params) self.command = [command] + list(params)
self.stdin = stdin self.stdin = stdin
@ -194,12 +203,17 @@ class process:
self.pipe = Popen(self.command, self.pipe = Popen(self.command,
stdout=self.stdout, stdout=self.stdout,
stdin=self.stdin(), stdin=self.stdin(),
stderr=self.stderr) stderr=self.stderr,
env=self.envdict)
def _defaultStdin(self): def _defaultStdin(self):
"""Return default stdin""" """Return default stdin"""
return PIPE return PIPE
def _keyboardStdin(self):
"""Return keyboard stdin"""
return None
def getStdout(self): def getStdout(self):
"""Get current stdout""" """Get current stdout"""
self.close() self.close()

Loading…
Cancel
Save