]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/popen2.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / popen2.py
CommitLineData
4710c53d 1"""Spawn a command with pipes to its stdin, stdout, and optionally stderr.\r
2\r
3The normal os.popen(cmd, mode) call spawns a shell command and provides a\r
4file interface to just the input or output of the process depending on\r
5whether mode is 'r' or 'w'. This module provides the functions popen2(cmd)\r
6and popen3(cmd) which return two or three pipes to the spawned command.\r
7"""\r
8\r
9import os\r
10import sys\r
11import warnings\r
12warnings.warn("The popen2 module is deprecated. Use the subprocess module.",\r
13 DeprecationWarning, stacklevel=2)\r
14\r
15__all__ = ["popen2", "popen3", "popen4"]\r
16\r
17try:\r
18 MAXFD = os.sysconf('SC_OPEN_MAX')\r
19except (AttributeError, ValueError):\r
20 MAXFD = 256\r
21\r
22_active = []\r
23\r
24def _cleanup():\r
25 for inst in _active[:]:\r
26 if inst.poll(_deadstate=sys.maxint) >= 0:\r
27 try:\r
28 _active.remove(inst)\r
29 except ValueError:\r
30 # This can happen if two threads create a new Popen instance.\r
31 # It's harmless that it was already removed, so ignore.\r
32 pass\r
33\r
34class Popen3:\r
35 """Class representing a child process. Normally, instances are created\r
36 internally by the functions popen2() and popen3()."""\r
37\r
38 sts = -1 # Child not completed yet\r
39\r
40 def __init__(self, cmd, capturestderr=False, bufsize=-1):\r
41 """The parameter 'cmd' is the shell command to execute in a\r
42 sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments\r
43 will be passed directly to the program without shell intervention (as\r
44 with os.spawnv()). If 'cmd' is a string it will be passed to the shell\r
45 (as with os.system()). The 'capturestderr' flag, if true, specifies\r
46 that the object should capture standard error output of the child\r
47 process. The default is false. If the 'bufsize' parameter is\r
48 specified, it specifies the size of the I/O buffers to/from the child\r
49 process."""\r
50 _cleanup()\r
51 self.cmd = cmd\r
52 p2cread, p2cwrite = os.pipe()\r
53 c2pread, c2pwrite = os.pipe()\r
54 if capturestderr:\r
55 errout, errin = os.pipe()\r
56 self.pid = os.fork()\r
57 if self.pid == 0:\r
58 # Child\r
59 os.dup2(p2cread, 0)\r
60 os.dup2(c2pwrite, 1)\r
61 if capturestderr:\r
62 os.dup2(errin, 2)\r
63 self._run_child(cmd)\r
64 os.close(p2cread)\r
65 self.tochild = os.fdopen(p2cwrite, 'w', bufsize)\r
66 os.close(c2pwrite)\r
67 self.fromchild = os.fdopen(c2pread, 'r', bufsize)\r
68 if capturestderr:\r
69 os.close(errin)\r
70 self.childerr = os.fdopen(errout, 'r', bufsize)\r
71 else:\r
72 self.childerr = None\r
73\r
74 def __del__(self):\r
75 # In case the child hasn't been waited on, check if it's done.\r
76 self.poll(_deadstate=sys.maxint)\r
77 if self.sts < 0:\r
78 if _active is not None:\r
79 # Child is still running, keep us alive until we can wait on it.\r
80 _active.append(self)\r
81\r
82 def _run_child(self, cmd):\r
83 if isinstance(cmd, basestring):\r
84 cmd = ['/bin/sh', '-c', cmd]\r
85 os.closerange(3, MAXFD)\r
86 try:\r
87 os.execvp(cmd[0], cmd)\r
88 finally:\r
89 os._exit(1)\r
90\r
91 def poll(self, _deadstate=None):\r
92 """Return the exit status of the child process if it has finished,\r
93 or -1 if it hasn't finished yet."""\r
94 if self.sts < 0:\r
95 try:\r
96 pid, sts = os.waitpid(self.pid, os.WNOHANG)\r
97 # pid will be 0 if self.pid hasn't terminated\r
98 if pid == self.pid:\r
99 self.sts = sts\r
100 except os.error:\r
101 if _deadstate is not None:\r
102 self.sts = _deadstate\r
103 return self.sts\r
104\r
105 def wait(self):\r
106 """Wait for and return the exit status of the child process."""\r
107 if self.sts < 0:\r
108 pid, sts = os.waitpid(self.pid, 0)\r
109 # This used to be a test, but it is believed to be\r
110 # always true, so I changed it to an assertion - mvl\r
111 assert pid == self.pid\r
112 self.sts = sts\r
113 return self.sts\r
114\r
115\r
116class Popen4(Popen3):\r
117 childerr = None\r
118\r
119 def __init__(self, cmd, bufsize=-1):\r
120 _cleanup()\r
121 self.cmd = cmd\r
122 p2cread, p2cwrite = os.pipe()\r
123 c2pread, c2pwrite = os.pipe()\r
124 self.pid = os.fork()\r
125 if self.pid == 0:\r
126 # Child\r
127 os.dup2(p2cread, 0)\r
128 os.dup2(c2pwrite, 1)\r
129 os.dup2(c2pwrite, 2)\r
130 self._run_child(cmd)\r
131 os.close(p2cread)\r
132 self.tochild = os.fdopen(p2cwrite, 'w', bufsize)\r
133 os.close(c2pwrite)\r
134 self.fromchild = os.fdopen(c2pread, 'r', bufsize)\r
135\r
136\r
137if sys.platform[:3] == "win" or sys.platform == "os2emx":\r
138 # Some things don't make sense on non-Unix platforms.\r
139 del Popen3, Popen4\r
140\r
141 def popen2(cmd, bufsize=-1, mode='t'):\r
142 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
143 be a sequence, in which case arguments will be passed directly to the\r
144 program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
145 string it will be passed to the shell (as with os.system()). If\r
146 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
147 file objects (child_stdout, child_stdin) are returned."""\r
148 w, r = os.popen2(cmd, mode, bufsize)\r
149 return r, w\r
150\r
151 def popen3(cmd, bufsize=-1, mode='t'):\r
152 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
153 be a sequence, in which case arguments will be passed directly to the\r
154 program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
155 string it will be passed to the shell (as with os.system()). If\r
156 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
157 file objects (child_stdout, child_stdin, child_stderr) are returned."""\r
158 w, r, e = os.popen3(cmd, mode, bufsize)\r
159 return r, w, e\r
160\r
161 def popen4(cmd, bufsize=-1, mode='t'):\r
162 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
163 be a sequence, in which case arguments will be passed directly to the\r
164 program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
165 string it will be passed to the shell (as with os.system()). If\r
166 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
167 file objects (child_stdout_stderr, child_stdin) are returned."""\r
168 w, r = os.popen4(cmd, mode, bufsize)\r
169 return r, w\r
170else:\r
171 def popen2(cmd, bufsize=-1, mode='t'):\r
172 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
173 be a sequence, in which case arguments will be passed directly to the\r
174 program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
175 string it will be passed to the shell (as with os.system()). If\r
176 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
177 file objects (child_stdout, child_stdin) are returned."""\r
178 inst = Popen3(cmd, False, bufsize)\r
179 return inst.fromchild, inst.tochild\r
180\r
181 def popen3(cmd, bufsize=-1, mode='t'):\r
182 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
183 be a sequence, in which case arguments will be passed directly to the\r
184 program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
185 string it will be passed to the shell (as with os.system()). If\r
186 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
187 file objects (child_stdout, child_stdin, child_stderr) are returned."""\r
188 inst = Popen3(cmd, True, bufsize)\r
189 return inst.fromchild, inst.tochild, inst.childerr\r
190\r
191 def popen4(cmd, bufsize=-1, mode='t'):\r
192 """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
193 be a sequence, in which case arguments will be passed directly to the\r
194 program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
195 string it will be passed to the shell (as with os.system()). If\r
196 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
197 file objects (child_stdout_stderr, child_stdin) are returned."""\r
198 inst = Popen4(cmd, bufsize)\r
199 return inst.fromchild, inst.tochild\r
200\r
201 __all__.extend(["Popen3", "Popen4"])\r