def start_python(self, cmd_line): outfp, infp = popen2.popen4('"%s" %s' % (sys.executable, cmd_line)) infp.close() data = outfp.read() outfp.close() # try to cleanup the child so we don't appear to leak when running # with regrtest -R. This should be a no-op on Windows. popen2._cleanup() return data
def tearDown(self): for inst in popen2._active: inst.wait() popen2._cleanup() self.assertFalse(popen2._active, "popen2._active not empty") # The os.popen*() API delegates to the subprocess module (on Unix) import subprocess for inst in subprocess._active: inst.wait() subprocess._cleanup() self.assertFalse(subprocess._active, "subprocess._active not empty") reap_children()
def tearDown(self): for inst in popen2._active: inst.wait() popen2._cleanup() self.assertFalse(popen2._active, "popen2._active not empty") # The os.popen*() API delegates to the subprocess module (on Unix) if not due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/15512"): import subprocess for inst in subprocess._active: inst.wait() subprocess._cleanup() self.assertFalse(subprocess._active, "subprocess._active not empty") reap_children()
def __init__(self, cmd, pstdin, pstdout, pstderr, detach): """ Initialization :Parameters: - `cmd`: The command to execute (type must be suitable for the platform) - `pstdin`: Pipe to stdin? - `pstdout`: Pipe from stdout? - `pstderr`: Pipe from stderr (on stdout channel) - `detach`: Detach the process? (evauluated on Win32 only) :Types: - `cmd`: ``list`` or ``basestring`` - `pstdin`: ``bool`` - `pstdout`: ``bool`` - `pstderr`: ``bool`` - `detach`: ``bool`` :exception NotImplementedError: not implemented functions were activated """ detach # pylint. This parameter is ignored popen2._cleanup() if pstdin: p2cread, p2cwrite = os.pipe() if pstdout: c2pread, c2pwrite = os.pipe() self.pid = os.fork() # Child if self.pid == 0: if pstdin: os.dup2(p2cread, 0) if pstdout: os.dup2(c2pwrite, 1) if pstderr and pstdout: os.dup2(c2pwrite, 2) self._run_child(cmd) # Parent else: self.tochild = self.fromchild = None if pstdin: os.close(p2cread) self.tochild = os.fdopen(p2cwrite, 'wb', -1) if pstdout: os.close(c2pwrite) self.fromchild = os.fdopen(c2pread, 'rb', -1) popen2._active.append(self)
def __init__(self, cmd, bufsize=-1): popen2._cleanup() c2pread, c2pwrite = os.pipe() null = open('/dev/null', 'w+') self.pid = os.fork() if self.pid == 0: # Child os.dup2(null.fileno(), sys.__stdin__.fileno()) #os.dup2(p2cread, 0) os.dup2(c2pwrite, 1) os.dup2(c2pwrite, 2) self._run_child(cmd) os.close(c2pwrite) self.fromchild = os.fdopen(c2pread, 'r', bufsize) popen2._active.append(self)
def tearDown(self): for inst in popen2._active: inst.wait() popen2._cleanup() self.assertFalse(popen2._active, "popen2._active not empty") # The os.popen*() API delegates to the subprocess module (on Unix) if not due_to_ironpython_bug( "http://ironpython.codeplex.com/workitem/15512"): import subprocess for inst in subprocess._active: inst.wait() subprocess._cleanup() self.assertFalse(subprocess._active, "subprocess._active not empty") reap_children()
def send_mail(msgbuf, mailcmd): import popen2 popen2._cleanup() log(TRACE, 'Mail command is "%s".' % mailcmd) cmd = popen2.Popen3(mailcmd, 1, bufsize=-1) cmdout, cmdin, cmderr = cmd.fromchild, cmd.tochild, cmd.childerr cmdin.write(msgbuf) cmdin.flush() cmdin.close() log(TRACE) err = string.strip(cmderr.read()) cmderr.close() out = string.strip(cmdout.read()) cmdout.close() r = cmd.wait() log(TRACE, 'r == %s' % r) exitcode = 0 try: if r: if os.WIFEXITED(r): exitcode = os.WEXITSTATUS(r) if os.WIFSIGNALED(r): exitsignal = 'signal %s' % os.WTERMSIG(r) else: exitsignal = 'no signal' if err: errtext = ', err: "%s"' % err else: errtext = '' raise DeliveryError, 'mail command %s exited %s, %s%s' \ % (mailcmd, exitcode, exitsignal, errtext) else: exitcode = 127 raise DeliveryError, 'mail command %s did not exit (rc == %s)' \ % (mailcmd, r) if err: raise DeliveryError, 'mail command %s error: "%s"' % (mailcmd, err) exitcode = 1 except DeliveryError, txt: log(FATAL, 'Fatal: failed sending mail (%s)' % txt) log_exception() sys.exit(exitcode or 1)
def _test(): # same test as popen2._test(), but using the os.popen*() API print "Testing os module:" import popen2 # When the test runs, there shouldn't be any open pipes popen2._cleanup() assert not popen2._active, "Active pipes when test starts " + repr( [c.cmd for c in popen2._active]) cmd = "cat" teststr = "ab cd\n" if os.name == "nt": cmd = "more" # "more" doesn't act the same way across Windows flavors, # sometimes adding an extra newline at the start or the # end. So we strip whitespace off both ends for comparison. expected = teststr.strip() print "testing popen2..." w, r = os.popen2(cmd) w.write(teststr) w.close() got = r.read() if got.strip() != expected: raise ValueError("wrote %r read %r" % (teststr, got)) print "testing popen3..." try: w, r, e = os.popen3([cmd]) except: w, r, e = os.popen3(cmd) w.write(teststr) w.close() got = r.read() if got.strip() != expected: raise ValueError("wrote %r read %r" % (teststr, got)) got = e.read() if got: raise ValueError("unexpected %r on stderr" % (got, )) for inst in popen2._active[:]: inst.wait() popen2._cleanup() if popen2._active: raise ValueError("_active not empty") print "All OK"
def _test(): # same test as popen2._test(), but using the os.popen*() API print "Testing os module:" import popen2 # When the test runs, there shouldn't be any open pipes popen2._cleanup() assert not popen2._active, "Active pipes when test starts " + repr([c.cmd for c in popen2._active]) cmd = "cat" teststr = "ab cd\n" if os.name == "nt": cmd = "more" # "more" doesn't act the same way across Windows flavors, # sometimes adding an extra newline at the start or the # end. So we strip whitespace off both ends for comparison. expected = teststr.strip() print "testing popen2..." w, r = os.popen2(cmd) w.write(teststr) w.close() got = r.read() if got.strip() != expected: raise ValueError("wrote %r read %r" % (teststr, got)) print "testing popen3..." try: w, r, e = os.popen3([cmd]) except: w, r, e = os.popen3(cmd) w.write(teststr) w.close() got = r.read() if got.strip() != expected: raise ValueError("wrote %r read %r" % (teststr, got)) got = e.read() if got: raise ValueError("unexpected %r on stderr" % (got,)) for inst in popen2._active[:]: inst.wait() popen2._cleanup() if popen2._active: raise ValueError("_active not empty") print "All OK"
def setUp(self): popen2._cleanup() # When the test runs, there shouldn't be any open pipes self.assertFalse( popen2._active, "Active pipes when test starts" + repr([c.cmd for c in popen2._active]))
def tearDown(self): for inst in popen2._active: inst.wait() popen2._cleanup() self.assertFalse(popen2._active, "_active not empty") reap_children()
def setUp(self): popen2._cleanup() # When the test runs, there shouldn't be any open pipes self.assertFalse(popen2._active, "Active pipes when test starts" + repr([c.cmd for c in popen2._active]))