diff --git a/pym/cl_utils.py b/pym/cl_utils.py index 3251ea4..1ffc955 100644 --- a/pym/cl_utils.py +++ b/pym/cl_utils.py @@ -151,35 +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 not "stdout" in kwarg: - self.stdout=PIPE - else: - self.stdout=kwarg["stdout"] + 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 @@ -194,12 +203,17 @@ class process: self.pipe = Popen(self.command, 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()