예제 #1
0
    def __init__(self, cmd, capturestderr=0, bufsize=-1):
        """The parameter 'cmd' is the shell command to execute in a
        sub-process.  Can be either a sequence of executable
        and arguments, or a shell command.
        The 'capturestderr' flag, if true, specifies that
        the object should capture standard error output of the child process.
        The default is false.  If the 'bufsize' parameter is specified, it
        specifies the size of the I/O buffers to/from the child process.
        """
        self.process = shellexecute(cmd)
        self._tochild = self.process.getOutputStream()
        self._fromchild = self.process.getInputStream()
        if capturestderr:
            self._childerr = self.process.getErrorStream()
        else:
            self._childerr = None
        import threading
        self.childWaiterLock = threading.Lock()

        if bufsize > 0:
            self._tochild = BufferedOutputStream(self._tochild, bufsize)
            self._fromchild = BufferedInputStream(self._fromchild, bufsize)
            if self._childerr:
                self._childerr = BufferedInputStream(self._childerr, bufsize)

        self.tochild = PyFile(self._tochild)
        self.fromchild = PyFile(self._fromchild)
        if self._childerr:
            self.childerr = PyFile(self._childerr)
예제 #2
0
파일: popen2.py 프로젝트: jbalint/spark
    def __init__(self, cmd, capturestderr=0, bufsize=-1):
        """The parameter 'cmd' is the shell command to execute in a
        sub-process.  Can be either a sequence of executable
        and arguments, or a shell command.
        The 'capturestderr' flag, if true, specifies that
        the object should capture standard error output of the child process.
        The default is false.  If the 'bufsize' parameter is specified, it
        specifies the size of the I/O buffers to/from the child process.
        """
        self.process = shellexecute( cmd )
        self._tochild = self.process.getOutputStream()
        self._fromchild = self.process.getInputStream()
        if capturestderr:
            self._childerr = self.process.getErrorStream()
        else:
            self._childerr = None
        import threading
        self.childWaiterLock = threading.Lock()

        if bufsize > 0:
            self._tochild = BufferedOutputStream( self._tochild, bufsize )
            self._fromchild = BufferedInputStream( self._fromchild, bufsize )
            if self._childerr:
                self._childerr = BufferedInputStream(
                    self._childerr,
                    bufsize
                    )
                
        self.tochild = FileUtil.wrap(self._tochild)
        self.fromchild = FileUtil.wrap(self._fromchild)
        if self._childerr:
            self.childerr = FileUtil.wrap(self._childerr)
예제 #3
0
 def _testCmds(self, _shellEnv, testCmds, whichEnv):
     """test commands (key) and compare output to expected output (value).
     this actually executes all the commands twice, testing the return
     code by calling system(), and testing some of the output by calling
     execute()
     """
     for cmd, pattern in testCmds:
         dprint("\nExecuting '%s' with %s environment" % (cmd, whichEnv))
         p = javashell.shellexecute(cmd)
         line = PyFile(p.getInputStream()).readlines()[0]
         assert re.match( pattern, line ), \
                 "expected match for %s, got %s" % ( pattern, line )
         dprint("waiting for", cmd, "to complete")
         assert not p.waitFor(), \
                 "%s failed with %s environment" % (cmd, whichEnv)
예제 #4
0
 def _testCmds( self, _shellEnv, testCmds, whichEnv ):
     """test commands (key) and compare output to expected output (value).
     this actually executes all the commands twice, testing the return
     code by calling system(), and testing some of the output by calling
     execute()
     """
     for cmd, pattern in testCmds:
         dprint( "\nExecuting '%s' with %s environment" % (cmd, whichEnv))
         p = javashell.shellexecute(cmd)
         line = FileUtil.wrap(p.getInputStream()).readlines()[0]
         assert re.match( pattern, line ), \
                 "expected match for %s, got %s" % ( pattern, line )
         dprint( "waiting for", cmd, "to complete")
         assert not p.waitFor(), \
                 "%s failed with %s environment" % (cmd, whichEnv)
예제 #5
0
 def testExecuteUnicodeCommandWithRedirection(self):
     process = javashell.shellexecute('nonexcmd 2>&1')
     stdout = process.getOutputStream().toString()
     process.waitFor()
     self.assertNotEqual(stdout, "",
                         "Redirecting 2>&1 failed with unicode cmd")
예제 #6
0
 def testExecuteUnicodeCommandWithRedirection(self):
     process = javashell.shellexecute(u'nonexcmd 2>&1')
     stdout = process.getOutputStream().toString()
     process.waitFor()
     self.assertNotEqual(stdout, "", "Redirecting 2>&1 failed with unicode cmd")