Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import string |
| 4 | |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 5 | MAXFD = 256 # Max number of file descriptors (os.getdtablesize()???) |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 7 | _active = [] |
| 8 | |
| 9 | def _cleanup(): |
| 10 | for inst in _active[:]: |
| 11 | inst.poll() |
| 12 | |
| 13 | class Popen3: |
| 14 | def __init__(self, cmd, capturestderr=0): |
Guido van Rossum | 971ee13 | 1995-08-07 20:17:23 +0000 | [diff] [blame] | 15 | cmd = ['/bin/sh', '-c', cmd] |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 16 | p2cread, p2cwrite = os.pipe() |
| 17 | c2pread, c2pwrite = os.pipe() |
Guido van Rossum | caa9f23 | 1997-04-21 14:15:55 +0000 | [diff] [blame] | 18 | if capturestderr: |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 19 | errout, errin = os.pipe() |
| 20 | self.pid = os.fork() |
| 21 | if self.pid == 0: |
| 22 | # Child |
| 23 | os.close(0) |
| 24 | os.close(1) |
| 25 | if os.dup(p2cread) <> 0: |
| 26 | sys.stderr.write('popen2: bad read dup\n') |
| 27 | if os.dup(c2pwrite) <> 1: |
| 28 | sys.stderr.write('popen2: bad write dup\n') |
| 29 | if capturestderr: |
| 30 | os.close(2) |
| 31 | if os.dup(errin) <> 2: pass |
| 32 | for i in range(3, MAXFD): |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 33 | try: |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 34 | os.close(i) |
| 35 | except: pass |
| 36 | try: |
| 37 | os.execv(cmd[0], cmd) |
| 38 | finally: |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 39 | os._exit(1) |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 40 | # Shouldn't come here, I guess |
| 41 | os._exit(1) |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 42 | os.close(p2cread) |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 43 | self.tochild = os.fdopen(p2cwrite, 'w') |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 44 | os.close(c2pwrite) |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 45 | self.fromchild = os.fdopen(c2pread, 'r') |
Guido van Rossum | caa9f23 | 1997-04-21 14:15:55 +0000 | [diff] [blame] | 46 | if capturestderr: |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 47 | os.close(errin) |
| 48 | self.childerr = os.fdopen(errout, 'r') |
Guido van Rossum | caa9f23 | 1997-04-21 14:15:55 +0000 | [diff] [blame] | 49 | else: |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 50 | self.childerr = None |
| 51 | self.sts = -1 # Child not completed yet |
| 52 | _active.append(self) |
| 53 | def poll(self): |
| 54 | if self.sts < 0: |
| 55 | try: |
| 56 | pid, sts = os.waitpid(self.pid, os.WNOHANG) |
| 57 | if pid == self.pid: |
| 58 | self.sts = sts |
| 59 | _active.remove(self) |
| 60 | except os.error: |
| 61 | pass |
| 62 | return self.sts |
| 63 | def wait(self): |
| 64 | pid, sts = os.waitpid(self.pid, 0) |
| 65 | if pid == self.pid: |
| 66 | self.sts = sts |
| 67 | _active.remove(self) |
| 68 | return self.sts |
Guido van Rossum | caa9f23 | 1997-04-21 14:15:55 +0000 | [diff] [blame] | 69 | |
| 70 | def popen2(cmd): |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 71 | _cleanup() |
| 72 | inst = Popen3(cmd, 0) |
| 73 | return inst.fromchild, inst.tochild |
Guido van Rossum | caa9f23 | 1997-04-21 14:15:55 +0000 | [diff] [blame] | 74 | |
| 75 | def popen3(cmd): |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 76 | _cleanup() |
| 77 | inst = Popen3(cmd, 1) |
| 78 | return inst.fromchild, inst.tochild, inst.childerr |
| 79 | |
| 80 | def _test(): |
| 81 | teststr = "abc\n" |
| 82 | print "testing popen2..." |
| 83 | r, w = popen2('cat') |
| 84 | w.write(teststr) |
| 85 | w.close() |
| 86 | assert r.read() == teststr |
| 87 | print "testing popen3..." |
| 88 | r, w, e = popen3('cat') |
| 89 | w.write(teststr) |
| 90 | w.close() |
| 91 | assert r.read() == teststr |
| 92 | assert e.read() == "" |
| 93 | _cleanup() |
| 94 | assert not _active |
| 95 | print "All OK" |
| 96 | |
| 97 | if __name__ == '__main__': |
| 98 | _test() |