annotate corebio/_future/subprocess.py @ 7:8d676bbd1f2d

Uploaded
author davidmurphy
date Mon, 16 Jan 2012 07:03:36 -0500
parents c55bdc2fb9fa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1 # subprocess - Subprocesses with accessible I/O streams
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
2 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
3 # For more information about this module, see PEP 324.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
4 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
5 # Copyright (c) 2003-2004 by Peter Astrand <astrand@lysator.liu.se>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
6 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
7 # By obtaining, using, and/or copying this software and/or its
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
8 # associated documentation, you agree that you have read, understood,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
9 # and will comply with the following terms and conditions:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
10 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
11 # Permission to use, copy, modify, and distribute this software and
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
12 # its associated documentation for any purpose and without fee is
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
13 # hereby granted, provided that the above copyright notice appears in
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
14 # all copies, and that both that copyright notice and this permission
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
15 # notice appear in supporting documentation, and that the name of the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
16 # author not be used in advertising or publicity pertaining to
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
17 # distribution of the software without specific, written prior
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
18 # permission.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
19 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
20 # THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
21 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
23 # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
24 # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
25 # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
26 # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
27
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
28 r"""subprocess - Subprocesses with accessible I/O streams
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
29
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
30 This module allows you to spawn processes, connect to their
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
31 input/output/error pipes, and obtain their return codes. This module
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
32 intends to replace several other, older modules and functions, like:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
33
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
34 os.system
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
35 os.spawn*
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
36 os.popen*
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
37 popen2.*
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
38 commands.*
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
39
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
40 Information about how the subprocess module can be used to replace these
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
41 modules and functions can be found below.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
42
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
43
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
44
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
45 Using the subprocess module
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
46 ===========================
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
47 This module defines one class called Popen:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
48
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
49 class Popen(args, bufsize=0, executable=None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
50 stdin=None, stdout=None, stderr=None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
51 preexec_fn=None, close_fds=False, shell=False,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
52 cwd=None, env=None, universal_newlines=False,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
53 startupinfo=None, creationflags=0):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
54
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
55
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
56 Arguments are:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
57
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
58 args should be a string, or a sequence of program arguments. The
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
59 program to execute is normally the first item in the args sequence or
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
60 string, but can be explicitly set by using the executable argument.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
61
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
62 On UNIX, with shell=False (default): In this case, the Popen class
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
63 uses os.execvp() to execute the child program. args should normally
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
64 be a sequence. A string will be treated as a sequence with the string
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
65 as the only item (the program to execute).
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
66
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
67 On UNIX, with shell=True: If args is a string, it specifies the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
68 command string to execute through the shell. If args is a sequence,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
69 the first item specifies the command string, and any additional items
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
70 will be treated as additional shell arguments.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
71
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
72 On Windows: the Popen class uses CreateProcess() to execute the child
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
73 program, which operates on strings. If args is a sequence, it will be
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
74 converted to a string using the list2cmdline method. Please note that
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
75 not all MS Windows applications interpret the command line the same
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
76 way: The list2cmdline is designed for applications using the same
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
77 rules as the MS C runtime.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
78
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
79 bufsize, if given, has the same meaning as the corresponding argument
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
80 to the built-in open() function: 0 means unbuffered, 1 means line
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
81 buffered, any other positive value means use a buffer of
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
82 (approximately) that size. A negative bufsize means to use the system
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
83 default, which usually means fully buffered. The default value for
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
84 bufsize is 0 (unbuffered).
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
85
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
86 stdin, stdout and stderr specify the executed programs' standard
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
87 input, standard output and standard error file handles, respectively.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
88 Valid values are PIPE, an existing file descriptor (a positive
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
89 integer), an existing file object, and None. PIPE indicates that a
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
90 new pipe to the child should be created. With None, no redirection
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
91 will occur; the child's file handles will be inherited from the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
92 parent. Additionally, stderr can be STDOUT, which indicates that the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
93 stderr data from the applications should be captured into the same
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
94 file handle as for stdout.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
95
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
96 If preexec_fn is set to a callable object, this object will be called
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
97 in the child process just before the child is executed.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
98
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
99 If close_fds is true, all file descriptors except 0, 1 and 2 will be
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
100 closed before the child process is executed.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
101
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
102 if shell is true, the specified command will be executed through the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
103 shell.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
104
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
105 If cwd is not None, the current directory will be changed to cwd
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
106 before the child is executed.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
107
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
108 If env is not None, it defines the environment variables for the new
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
109 process.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
110
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
111 If universal_newlines is true, the file objects stdout and stderr are
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
112 opened as a text files, but lines may be terminated by any of '\n',
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
113 the Unix end-of-line convention, '\r', the Macintosh convention or
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
114 '\r\n', the Windows convention. All of these external representations
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
115 are seen as '\n' by the Python program. Note: This feature is only
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
116 available if Python is built with universal newline support (the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
117 default). Also, the newlines attribute of the file objects stdout,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
118 stdin and stderr are not updated by the communicate() method.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
119
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
120 The startupinfo and creationflags, if given, will be passed to the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
121 underlying CreateProcess() function. They can specify things such as
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
122 appearance of the main window and priority for the new process.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
123 (Windows only)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
124
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
125
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
126 This module also defines two shortcut functions:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
127
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
128 call(*args, **kwargs):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
129 Run command with arguments. Wait for command to complete, then
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
130 return the returncode attribute.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
131
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
132 The arguments are the same as for the Popen constructor. Example:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
133
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
134 retcode = call(["ls", "-l"])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
135
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
136
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
137 Exceptions
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
138 ----------
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
139 Exceptions raised in the child process, before the new program has
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
140 started to execute, will be re-raised in the parent. Additionally,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
141 the exception object will have one extra attribute called
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
142 'child_traceback', which is a string containing traceback information
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
143 from the childs point of view.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
144
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
145 The most common exception raised is OSError. This occurs, for
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
146 example, when trying to execute a non-existent file. Applications
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
147 should prepare for OSErrors.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
148
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
149 A ValueError will be raised if Popen is called with invalid arguments.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
150
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
151
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
152 Security
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
153 --------
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
154 Unlike some other popen functions, this implementation will never call
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
155 /bin/sh implicitly. This means that all characters, including shell
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
156 metacharacters, can safely be passed to child processes.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
157
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
158
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
159 Popen objects
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
160 =============
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
161 Instances of the Popen class have the following methods:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
162
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
163 poll()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
164 Check if child process has terminated. Returns returncode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
165 attribute.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
166
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
167 wait()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
168 Wait for child process to terminate. Returns returncode attribute.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
169
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
170 communicate(input=None)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
171 Interact with process: Send data to stdin. Read data from stdout
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
172 and stderr, until end-of-file is reached. Wait for process to
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
173 terminate. The optional stdin argument should be a string to be
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
174 sent to the child process, or None, if no data should be sent to
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
175 the child.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
176
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
177 communicate() returns a tuple (stdout, stderr).
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
178
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
179 Note: The data read is buffered in memory, so do not use this
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
180 method if the data size is large or unlimited.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
181
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
182 The following attributes are also available:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
183
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
184 stdin
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
185 If the stdin argument is PIPE, this attribute is a file object
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
186 that provides input to the child process. Otherwise, it is None.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
187
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
188 stdout
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
189 If the stdout argument is PIPE, this attribute is a file object
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
190 that provides output from the child process. Otherwise, it is
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
191 None.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
192
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
193 stderr
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
194 If the stderr argument is PIPE, this attribute is file object that
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
195 provides error output from the child process. Otherwise, it is
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
196 None.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
197
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
198 pid
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
199 The process ID of the child process.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
200
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
201 returncode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
202 The child return code. A None value indicates that the process
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
203 hasn't terminated yet. A negative value -N indicates that the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
204 child was terminated by signal N (UNIX only).
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
205
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
206
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
207 Replacing older functions with the subprocess module
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
208 ====================================================
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
209 In this section, "a ==> b" means that b can be used as a replacement
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
210 for a.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
211
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
212 Note: All functions in this section fail (more or less) silently if
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
213 the executed program cannot be found; this module raises an OSError
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
214 exception.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
215
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
216 In the following examples, we assume that the subprocess module is
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
217 imported with "from subprocess import *".
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
218
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
219
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
220 Replacing /bin/sh shell backquote
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
221 ---------------------------------
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
222 output=`mycmd myarg`
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
223 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
224 output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
225
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
226
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
227 Replacing shell pipe line
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
228 -------------------------
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
229 output=`dmesg | grep hda`
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
230 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
231 p1 = Popen(["dmesg"], stdout=PIPE)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
232 p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
233 output = p2.communicate()[0]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
234
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
235
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
236 Replacing os.system()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
237 ---------------------
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
238 sts = os.system("mycmd" + " myarg")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
239 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
240 p = Popen("mycmd" + " myarg", shell=True)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
241 sts = os.waitpid(p.pid, 0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
242
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
243 Note:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
244
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
245 * Calling the program through the shell is usually not required.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
246
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
247 * It's easier to look at the returncode attribute than the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
248 exitstatus.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
249
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
250 A more real-world example would look like this:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
251
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
252 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
253 retcode = call("mycmd" + " myarg", shell=True)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
254 if retcode < 0:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
255 print >>sys.stderr, "Child was terminated by signal", -retcode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
256 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
257 print >>sys.stderr, "Child returned", retcode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
258 except OSError, e:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
259 print >>sys.stderr, "Execution failed:", e
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
260
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
261
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
262 Replacing os.spawn*
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
263 -------------------
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
264 P_NOWAIT example:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
265
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
266 pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
267 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
268 pid = Popen(["/bin/mycmd", "myarg"]).pid
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
269
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
270
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
271 P_WAIT example:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
272
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
273 retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
274 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
275 retcode = call(["/bin/mycmd", "myarg"])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
276
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
277
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
278 Vector example:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
279
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
280 os.spawnvp(os.P_NOWAIT, path, args)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
281 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
282 Popen([path] + args[1:])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
283
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
284
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
285 Environment example:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
286
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
287 os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
288 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
289 Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
290
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
291
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
292 Replacing os.popen*
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
293 -------------------
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
294 pipe = os.popen(cmd, mode='r', bufsize)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
295 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
296 pipe = Popen(cmd, shell=True, bufsize=bufsize, stdout=PIPE).stdout
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
297
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
298 pipe = os.popen(cmd, mode='w', bufsize)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
299 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
300 pipe = Popen(cmd, shell=True, bufsize=bufsize, stdin=PIPE).stdin
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
301
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
302
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
303 (child_stdin, child_stdout) = os.popen2(cmd, mode, bufsize)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
304 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
305 p = Popen(cmd, shell=True, bufsize=bufsize,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
306 stdin=PIPE, stdout=PIPE, close_fds=True)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
307 (child_stdin, child_stdout) = (p.stdin, p.stdout)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
308
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
309
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
310 (child_stdin,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
311 child_stdout,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
312 child_stderr) = os.popen3(cmd, mode, bufsize)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
313 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
314 p = Popen(cmd, shell=True, bufsize=bufsize,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
315 stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
316 (child_stdin,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
317 child_stdout,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
318 child_stderr) = (p.stdin, p.stdout, p.stderr)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
319
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
320
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
321 (child_stdin, child_stdout_and_stderr) = os.popen4(cmd, mode, bufsize)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
322 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
323 p = Popen(cmd, shell=True, bufsize=bufsize,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
324 stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
325 (child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
326
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
327
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
328 Replacing popen2.*
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
329 ------------------
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
330 Note: If the cmd argument to popen2 functions is a string, the command
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
331 is executed through /bin/sh. If it is a list, the command is directly
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
332 executed.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
333
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
334 (child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
335 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
336 p = Popen(["somestring"], shell=True, bufsize=bufsize
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
337 stdin=PIPE, stdout=PIPE, close_fds=True)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
338 (child_stdout, child_stdin) = (p.stdout, p.stdin)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
339
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
340
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
341 (child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, mode)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
342 ==>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
343 p = Popen(["mycmd", "myarg"], bufsize=bufsize,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
344 stdin=PIPE, stdout=PIPE, close_fds=True)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
345 (child_stdout, child_stdin) = (p.stdout, p.stdin)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
346
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
347 The popen2.Popen3 and popen3.Popen4 basically works as subprocess.Popen,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
348 except that:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
349
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
350 * subprocess.Popen raises an exception if the execution fails
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
351 * the capturestderr argument is replaced with the stderr argument.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
352 * stdin=PIPE and stdout=PIPE must be specified.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
353 * popen2 closes all filedescriptors by default, but you have to specify
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
354 close_fds=True with subprocess.Popen.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
355
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
356
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
357 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
358
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
359 import sys
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
360 mswindows = (sys.platform == "win32")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
361
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
362 import os
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
363 import types
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
364 import traceback
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
365
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
366 if mswindows:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
367 import threading
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
368 import msvcrt
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
369 if 0: # <-- change this to use pywin32 instead of the _subprocess driver
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
370 import pywintypes
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
371 from win32api import GetStdHandle, STD_INPUT_HANDLE, \
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
372 STD_OUTPUT_HANDLE, STD_ERROR_HANDLE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
373 from win32api import GetCurrentProcess, DuplicateHandle, \
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
374 GetModuleFileName, GetVersion
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
375 from win32con import DUPLICATE_SAME_ACCESS, SW_HIDE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
376 from win32pipe import CreatePipe
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
377 from win32process import CreateProcess, STARTUPINFO, \
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
378 GetExitCodeProcess, STARTF_USESTDHANDLES, \
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
379 STARTF_USESHOWWINDOW, CREATE_NEW_CONSOLE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
380 from win32event import WaitForSingleObject, INFINITE, WAIT_OBJECT_0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
381 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
382 from _subprocess import *
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
383 class STARTUPINFO:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
384 dwFlags = 0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
385 hStdInput = None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
386 hStdOutput = None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
387 hStdError = None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
388 class pywintypes:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
389 error = IOError
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
390 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
391 import select
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
392 import errno
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
393 import fcntl
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
394 import pickle
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
395
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
396 __all__ = ["Popen", "PIPE", "STDOUT", "call"]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
397
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
398 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
399 MAXFD = os.sysconf("SC_OPEN_MAX")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
400 except:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
401 MAXFD = 256
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
402
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
403 # True/False does not exist on 2.2.0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
404 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
405 False
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
406 except NameError:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
407 False = 0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
408 True = 1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
409
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
410 _active = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
411
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
412 def _cleanup():
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
413 for inst in _active[:]:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
414 inst.poll()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
415
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
416 PIPE = -1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
417 STDOUT = -2
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
418
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
419
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
420 def call(*args, **kwargs):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
421 """Run command with arguments. Wait for command to complete, then
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
422 return the returncode attribute.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
423
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
424 The arguments are the same as for the Popen constructor. Example:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
425
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
426 retcode = call(["ls", "-l"])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
427 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
428 return Popen(*args, **kwargs).wait()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
429
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
430
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
431 def list2cmdline(seq):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
432 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
433 Translate a sequence of arguments into a command line
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
434 string, using the same rules as the MS C runtime:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
435
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
436 1) Arguments are delimited by white space, which is either a
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
437 space or a tab.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
438
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
439 2) A string surrounded by double quotation marks is
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
440 interpreted as a single argument, regardless of white space
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
441 contained within. A quoted string can be embedded in an
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
442 argument.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
443
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
444 3) A double quotation mark preceded by a backslash is
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
445 interpreted as a literal double quotation mark.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
446
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
447 4) Backslashes are interpreted literally, unless they
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
448 immediately precede a double quotation mark.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
449
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
450 5) If backslashes immediately precede a double quotation mark,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
451 every pair of backslashes is interpreted as a literal
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
452 backslash. If the number of backslashes is odd, the last
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
453 backslash escapes the next double quotation mark as
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
454 described in rule 3.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
455 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
456
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
457 # See
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
458 # http://msdn.microsoft.com/library/en-us/vccelng/htm/progs_12.asp
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
459 result = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
460 needquote = False
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
461 for arg in seq:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
462 bs_buf = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
463
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
464 # Add a space to separate this argument from the others
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
465 if result:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
466 result.append(' ')
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
467
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
468 needquote = (" " in arg) or ("\t" in arg)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
469 if needquote:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
470 result.append('"')
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
471
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
472 for c in arg:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
473 if c == '\\':
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
474 # Don't know if we need to double yet.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
475 bs_buf.append(c)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
476 elif c == '"':
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
477 # Double backspaces.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
478 result.append('\\' * len(bs_buf)*2)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
479 bs_buf = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
480 result.append('\\"')
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
481 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
482 # Normal char
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
483 if bs_buf:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
484 result.extend(bs_buf)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
485 bs_buf = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
486 result.append(c)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
487
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
488 # Add remaining backspaces, if any.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
489 if bs_buf:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
490 result.extend(bs_buf)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
491
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
492 if needquote:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
493 result.extend(bs_buf)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
494 result.append('"')
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
495
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
496 return ''.join(result)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
497
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
498
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
499 class Popen(object):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
500 def __init__(self, args, bufsize=0, executable=None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
501 stdin=None, stdout=None, stderr=None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
502 preexec_fn=None, close_fds=False, shell=False,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
503 cwd=None, env=None, universal_newlines=False,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
504 startupinfo=None, creationflags=0):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
505 """Create new Popen instance."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
506 _cleanup()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
507
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
508 if not isinstance(bufsize, (int, long)):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
509 raise TypeError("bufsize must be an integer")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
510
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
511 if mswindows:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
512 if preexec_fn is not None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
513 raise ValueError("preexec_fn is not supported on Windows "
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
514 "platforms")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
515 if close_fds:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
516 raise ValueError("close_fds is not supported on Windows "
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
517 "platforms")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
518 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
519 # POSIX
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
520 if startupinfo is not None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
521 raise ValueError("startupinfo is only supported on Windows "
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
522 "platforms")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
523 if creationflags != 0:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
524 raise ValueError("creationflags is only supported on Windows "
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
525 "platforms")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
526
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
527 self.stdin = None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
528 self.stdout = None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
529 self.stderr = None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
530 self.pid = None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
531 self.returncode = None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
532 self.universal_newlines = universal_newlines
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
533
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
534 # Input and output objects. The general principle is like
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
535 # this:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
536 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
537 # Parent Child
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
538 # ------ -----
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
539 # p2cwrite ---stdin---> p2cread
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
540 # c2pread <--stdout--- c2pwrite
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
541 # errread <--stderr--- errwrite
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
542 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
543 # On POSIX, the child objects are file descriptors. On
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
544 # Windows, these are Windows file handles. The parent objects
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
545 # are file descriptors on both platforms. The parent objects
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
546 # are None when not using PIPEs. The child objects are None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
547 # when not redirecting.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
548
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
549 (p2cread, p2cwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
550 c2pread, c2pwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
551 errread, errwrite) = self._get_handles(stdin, stdout, stderr)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
552
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
553 self._execute_child(args, executable, preexec_fn, close_fds,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
554 cwd, env, universal_newlines,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
555 startupinfo, creationflags, shell,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
556 p2cread, p2cwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
557 c2pread, c2pwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
558 errread, errwrite)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
559
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
560 if p2cwrite:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
561 self.stdin = os.fdopen(p2cwrite, 'wb', bufsize)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
562 if c2pread:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
563 if universal_newlines:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
564 self.stdout = os.fdopen(c2pread, 'rU', bufsize)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
565 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
566 self.stdout = os.fdopen(c2pread, 'rb', bufsize)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
567 if errread:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
568 if universal_newlines:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
569 self.stderr = os.fdopen(errread, 'rU', bufsize)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
570 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
571 self.stderr = os.fdopen(errread, 'rb', bufsize)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
572
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
573 _active.append(self)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
574
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
575
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
576 def _translate_newlines(self, data):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
577 data = data.replace("\r\n", "\n")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
578 data = data.replace("\r", "\n")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
579 return data
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
580
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
581
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
582 if mswindows:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
583 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
584 # Windows methods
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
585 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
586 def _get_handles(self, stdin, stdout, stderr):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
587 """Construct and return tupel with IO objects:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
588 p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
589 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
590 if stdin == None and stdout == None and stderr == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
591 return (None, None, None, None, None, None)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
592
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
593 p2cread, p2cwrite = None, None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
594 c2pread, c2pwrite = None, None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
595 errread, errwrite = None, None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
596
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
597 if stdin == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
598 p2cread = GetStdHandle(STD_INPUT_HANDLE)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
599 elif stdin == PIPE:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
600 p2cread, p2cwrite = CreatePipe(None, 0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
601 # Detach and turn into fd
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
602 p2cwrite = p2cwrite.Detach()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
603 p2cwrite = msvcrt.open_osfhandle(p2cwrite, 0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
604 elif type(stdin) == types.IntType:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
605 p2cread = msvcrt.get_osfhandle(stdin)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
606 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
607 # Assuming file-like object
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
608 p2cread = msvcrt.get_osfhandle(stdin.fileno())
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
609 p2cread = self._make_inheritable(p2cread)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
610
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
611 if stdout == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
612 c2pwrite = GetStdHandle(STD_OUTPUT_HANDLE)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
613 elif stdout == PIPE:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
614 c2pread, c2pwrite = CreatePipe(None, 0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
615 # Detach and turn into fd
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
616 c2pread = c2pread.Detach()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
617 c2pread = msvcrt.open_osfhandle(c2pread, 0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
618 elif type(stdout) == types.IntType:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
619 c2pwrite = msvcrt.get_osfhandle(stdout)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
620 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
621 # Assuming file-like object
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
622 c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
623 c2pwrite = self._make_inheritable(c2pwrite)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
624
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
625 if stderr == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
626 errwrite = GetStdHandle(STD_ERROR_HANDLE)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
627 elif stderr == PIPE:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
628 errread, errwrite = CreatePipe(None, 0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
629 # Detach and turn into fd
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
630 errread = errread.Detach()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
631 errread = msvcrt.open_osfhandle(errread, 0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
632 elif stderr == STDOUT:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
633 errwrite = c2pwrite
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
634 elif type(stderr) == types.IntType:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
635 errwrite = msvcrt.get_osfhandle(stderr)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
636 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
637 # Assuming file-like object
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
638 errwrite = msvcrt.get_osfhandle(stderr.fileno())
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
639 errwrite = self._make_inheritable(errwrite)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
640
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
641 return (p2cread, p2cwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
642 c2pread, c2pwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
643 errread, errwrite)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
644
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
645
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
646 def _make_inheritable(self, handle):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
647 """Return a duplicate of handle, which is inheritable"""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
648 return DuplicateHandle(GetCurrentProcess(), handle,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
649 GetCurrentProcess(), 0, 1,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
650 DUPLICATE_SAME_ACCESS)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
651
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
652
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
653 def _find_w9xpopen(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
654 """Find and return absolut path to w9xpopen.exe"""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
655 w9xpopen = os.path.join(os.path.dirname(GetModuleFileName(0)),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
656 "w9xpopen.exe")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
657 if not os.path.exists(w9xpopen):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
658 # Eeek - file-not-found - possibly an embedding
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
659 # situation - see if we can locate it in sys.exec_prefix
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
660 w9xpopen = os.path.join(os.path.dirname(sys.exec_prefix),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
661 "w9xpopen.exe")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
662 if not os.path.exists(w9xpopen):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
663 raise RuntimeError("Cannot locate w9xpopen.exe, which is "
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
664 "needed for Popen to work with your "
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
665 "shell or platform.")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
666 return w9xpopen
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
667
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
668
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
669 def _execute_child(self, args, executable, preexec_fn, close_fds,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
670 cwd, env, universal_newlines,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
671 startupinfo, creationflags, shell,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
672 p2cread, p2cwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
673 c2pread, c2pwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
674 errread, errwrite):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
675 """Execute program (MS Windows version)"""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
676
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
677 if not isinstance(args, types.StringTypes):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
678 args = list2cmdline(args)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
679
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
680 # Process startup details
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
681 default_startupinfo = STARTUPINFO()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
682 if startupinfo == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
683 startupinfo = default_startupinfo
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
684 if not None in (p2cread, c2pwrite, errwrite):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
685 startupinfo.dwFlags |= STARTF_USESTDHANDLES
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
686 startupinfo.hStdInput = p2cread
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
687 startupinfo.hStdOutput = c2pwrite
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
688 startupinfo.hStdError = errwrite
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
689
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
690 if shell:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
691 default_startupinfo.dwFlags |= STARTF_USESHOWWINDOW
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
692 default_startupinfo.wShowWindow = SW_HIDE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
693 comspec = os.environ.get("COMSPEC", "cmd.exe")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
694 args = comspec + " /c " + args
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
695 if (GetVersion() >= 0x80000000L or
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
696 os.path.basename(comspec).lower() == "command.com"):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
697 # Win9x, or using command.com on NT. We need to
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
698 # use the w9xpopen intermediate program. For more
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
699 # information, see KB Q150956
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
700 # (http://web.archive.org/web/20011105084002/http://support.microsoft.com/support/kb/articles/Q150/9/56.asp)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
701 w9xpopen = self._find_w9xpopen()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
702 args = '"%s" %s' % (w9xpopen, args)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
703 # Not passing CREATE_NEW_CONSOLE has been known to
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
704 # cause random failures on win9x. Specifically a
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
705 # dialog: "Your program accessed mem currently in
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
706 # use at xxx" and a hopeful warning about the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
707 # stability of your system. Cost is Ctrl+C wont
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
708 # kill children.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
709 creationflags |= CREATE_NEW_CONSOLE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
710
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
711 # Start the process
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
712 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
713 hp, ht, pid, tid = CreateProcess(executable, args,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
714 # no special security
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
715 None, None,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
716 # must inherit handles to pass std
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
717 # handles
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
718 1,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
719 creationflags,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
720 env,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
721 cwd,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
722 startupinfo)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
723 except pywintypes.error, e:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
724 # Translate pywintypes.error to WindowsError, which is
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
725 # a subclass of OSError. FIXME: We should really
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
726 # translate errno using _sys_errlist (or simliar), but
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
727 # how can this be done from Python?
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
728 raise WindowsError(*e.args)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
729
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
730 # Retain the process handle, but close the thread handle
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
731 self._handle = hp
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
732 self.pid = pid
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
733 ht.Close()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
734
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
735 # Child is launched. Close the parent's copy of those pipe
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
736 # handles that only the child should have open. You need
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
737 # to make sure that no handles to the write end of the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
738 # output pipe are maintained in this process or else the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
739 # pipe will not close when the child process exits and the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
740 # ReadFile will hang.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
741 if p2cread != None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
742 p2cread.Close()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
743 if c2pwrite != None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
744 c2pwrite.Close()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
745 if errwrite != None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
746 errwrite.Close()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
747
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
748
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
749 def poll(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
750 """Check if child process has terminated. Returns returncode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
751 attribute."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
752 if self.returncode == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
753 if WaitForSingleObject(self._handle, 0) == WAIT_OBJECT_0:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
754 self.returncode = GetExitCodeProcess(self._handle)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
755 _active.remove(self)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
756 return self.returncode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
757
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
758
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
759 def wait(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
760 """Wait for child process to terminate. Returns returncode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
761 attribute."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
762 if self.returncode == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
763 obj = WaitForSingleObject(self._handle, INFINITE)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
764 self.returncode = GetExitCodeProcess(self._handle)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
765 _active.remove(self)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
766 return self.returncode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
767
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
768
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
769 def _readerthread(self, fh, buffer):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
770 buffer.append(fh.read())
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
771
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
772
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
773 def communicate(self, input=None):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
774 """Interact with process: Send data to stdin. Read data from
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
775 stdout and stderr, until end-of-file is reached. Wait for
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
776 process to terminate. The optional input argument should be a
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
777 string to be sent to the child process, or None, if no data
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
778 should be sent to the child.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
779
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
780 communicate() returns a tuple (stdout, stderr)."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
781 stdout = None # Return
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
782 stderr = None # Return
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
783
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
784 if self.stdout:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
785 stdout = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
786 stdout_thread = threading.Thread(target=self._readerthread,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
787 args=(self.stdout, stdout))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
788 stdout_thread.setDaemon(True)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
789 stdout_thread.start()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
790 if self.stderr:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
791 stderr = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
792 stderr_thread = threading.Thread(target=self._readerthread,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
793 args=(self.stderr, stderr))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
794 stderr_thread.setDaemon(True)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
795 stderr_thread.start()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
796
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
797 if self.stdin:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
798 if input != None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
799 self.stdin.write(input)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
800 self.stdin.close()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
801
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
802 if self.stdout:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
803 stdout_thread.join()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
804 if self.stderr:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
805 stderr_thread.join()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
806
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
807 # All data exchanged. Translate lists into strings.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
808 if stdout != None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
809 stdout = stdout[0]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
810 if stderr != None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
811 stderr = stderr[0]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
812
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
813 # Translate newlines, if requested. We cannot let the file
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
814 # object do the translation: It is based on stdio, which is
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
815 # impossible to combine with select (unless forcing no
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
816 # buffering).
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
817 if self.universal_newlines and hasattr(open, 'newlines'):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
818 if stdout:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
819 stdout = self._translate_newlines(stdout)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
820 if stderr:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
821 stderr = self._translate_newlines(stderr)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
822
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
823 self.wait()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
824 return (stdout, stderr)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
825
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
826 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
827 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
828 # POSIX methods
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
829 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
830 def _get_handles(self, stdin, stdout, stderr):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
831 """Construct and return tupel with IO objects:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
832 p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
833 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
834 p2cread, p2cwrite = None, None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
835 c2pread, c2pwrite = None, None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
836 errread, errwrite = None, None
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
837
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
838 if stdin == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
839 pass
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
840 elif stdin == PIPE:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
841 p2cread, p2cwrite = os.pipe()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
842 elif type(stdin) == types.IntType:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
843 p2cread = stdin
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
844 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
845 # Assuming file-like object
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
846 p2cread = stdin.fileno()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
847
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
848 if stdout == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
849 pass
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
850 elif stdout == PIPE:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
851 c2pread, c2pwrite = os.pipe()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
852 elif type(stdout) == types.IntType:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
853 c2pwrite = stdout
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
854 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
855 # Assuming file-like object
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
856 c2pwrite = stdout.fileno()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
857
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
858 if stderr == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
859 pass
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
860 elif stderr == PIPE:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
861 errread, errwrite = os.pipe()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
862 elif stderr == STDOUT:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
863 errwrite = c2pwrite
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
864 elif type(stderr) == types.IntType:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
865 errwrite = stderr
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
866 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
867 # Assuming file-like object
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
868 errwrite = stderr.fileno()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
869
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
870 return (p2cread, p2cwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
871 c2pread, c2pwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
872 errread, errwrite)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
873
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
874
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
875 def _set_cloexec_flag(self, fd):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
876 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
877 cloexec_flag = fcntl.FD_CLOEXEC
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
878 except AttributeError:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
879 cloexec_flag = 1
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
880
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
881 old = fcntl.fcntl(fd, fcntl.F_GETFD)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
882 fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
883
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
884
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
885 def _close_fds(self, but):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
886 for i in range(3, MAXFD):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
887 if i == but:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
888 continue
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
889 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
890 os.close(i)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
891 except:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
892 pass
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
893
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
894
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
895 def _execute_child(self, args, executable, preexec_fn, close_fds,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
896 cwd, env, universal_newlines,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
897 startupinfo, creationflags, shell,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
898 p2cread, p2cwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
899 c2pread, c2pwrite,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
900 errread, errwrite):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
901 """Execute program (POSIX version)"""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
902
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
903 if isinstance(args, types.StringTypes):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
904 args = [args]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
905
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
906 if shell:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
907 args = ["/bin/sh", "-c"] + args
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
908
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
909 if executable == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
910 executable = args[0]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
911
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
912 # For transferring possible exec failure from child to parent
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
913 # The first char specifies the exception type: 0 means
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
914 # OSError, 1 means some other error.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
915 errpipe_read, errpipe_write = os.pipe()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
916 self._set_cloexec_flag(errpipe_write)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
917
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
918 self.pid = os.fork()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
919 if self.pid == 0:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
920 # Child
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
921 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
922 # Close parent's pipe ends
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
923 if p2cwrite:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
924 os.close(p2cwrite)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
925 if c2pread:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
926 os.close(c2pread)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
927 if errread:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
928 os.close(errread)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
929 os.close(errpipe_read)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
930
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
931 # Dup fds for child
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
932 if p2cread:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
933 os.dup2(p2cread, 0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
934 if c2pwrite:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
935 os.dup2(c2pwrite, 1)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
936 if errwrite:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
937 os.dup2(errwrite, 2)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
938
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
939 # Close pipe fds. Make sure we doesn't close the same
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
940 # fd more than once.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
941 if p2cread:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
942 os.close(p2cread)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
943 if c2pwrite and c2pwrite not in (p2cread,):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
944 os.close(c2pwrite)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
945 if errwrite and errwrite not in (p2cread, c2pwrite):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
946 os.close(errwrite)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
947
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
948 # Close all other fds, if asked for
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
949 if close_fds:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
950 self._close_fds(but=errpipe_write)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
951
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
952 if cwd != None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
953 os.chdir(cwd)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
954
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
955 if preexec_fn:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
956 apply(preexec_fn)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
957
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
958 if env == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
959 os.execvp(executable, args)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
960 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
961 os.execvpe(executable, args, env)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
962
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
963 except:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
964 exc_type, exc_value, tb = sys.exc_info()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
965 # Save the traceback and attach it to the exception object
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
966 exc_lines = traceback.format_exception(exc_type,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
967 exc_value,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
968 tb)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
969 exc_value.child_traceback = ''.join(exc_lines)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
970 os.write(errpipe_write, pickle.dumps(exc_value))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
971
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
972 # This exitcode won't be reported to applications, so it
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
973 # really doesn't matter what we return.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
974 os._exit(255)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
975
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
976 # Parent
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
977 os.close(errpipe_write)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
978 if p2cread and p2cwrite:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
979 os.close(p2cread)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
980 if c2pwrite and c2pread:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
981 os.close(c2pwrite)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
982 if errwrite and errread:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
983 os.close(errwrite)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
984
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
985 # Wait for exec to fail or succeed; possibly raising exception
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
986 data = os.read(errpipe_read, 1048576) # Exceptions limited to 1 MB
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
987 os.close(errpipe_read)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
988 if data != "":
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
989 os.waitpid(self.pid, 0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
990 child_exception = pickle.loads(data)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
991 raise child_exception
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
992
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
993
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
994 def _handle_exitstatus(self, sts):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
995 if os.WIFSIGNALED(sts):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
996 self.returncode = -os.WTERMSIG(sts)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
997 elif os.WIFEXITED(sts):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
998 self.returncode = os.WEXITSTATUS(sts)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
999 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1000 # Should never happen
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1001 raise RuntimeError("Unknown child exit status!")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1002
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1003 _active.remove(self)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1004
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1005
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1006 def poll(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1007 """Check if child process has terminated. Returns returncode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1008 attribute."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1009 if self.returncode == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1010 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1011 pid, sts = os.waitpid(self.pid, os.WNOHANG)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1012 if pid == self.pid:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1013 self._handle_exitstatus(sts)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1014 except os.error:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1015 pass
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1016 return self.returncode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1017
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1018
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1019 def wait(self):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1020 """Wait for child process to terminate. Returns returncode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1021 attribute."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1022 if self.returncode == None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1023 pid, sts = os.waitpid(self.pid, 0)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1024 self._handle_exitstatus(sts)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1025 return self.returncode
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1026
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1027
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1028 def communicate(self, input=None):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1029 """Interact with process: Send data to stdin. Read data from
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1030 stdout and stderr, until end-of-file is reached. Wait for
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1031 process to terminate. The optional input argument should be a
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1032 string to be sent to the child process, or None, if no data
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1033 should be sent to the child.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1034
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1035 communicate() returns a tuple (stdout, stderr)."""
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1036 read_set = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1037 write_set = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1038 stdout = None # Return
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1039 stderr = None # Return
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1040
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1041 if self.stdin:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1042 # Flush stdio buffer. This might block, if the user has
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1043 # been writing to .stdin in an uncontrolled fashion.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1044 self.stdin.flush()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1045 if input:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1046 write_set.append(self.stdin)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1047 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1048 self.stdin.close()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1049 if self.stdout:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1050 read_set.append(self.stdout)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1051 stdout = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1052 if self.stderr:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1053 read_set.append(self.stderr)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1054 stderr = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1055
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1056 while read_set or write_set:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1057 rlist, wlist, xlist = select.select(read_set, write_set, [])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1058
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1059 if self.stdin in wlist:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1060 # When select has indicated that the file is writable,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1061 # we can write up to PIPE_BUF bytes without risk
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1062 # blocking. POSIX defines PIPE_BUF >= 512
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1063 bytes_written = os.write(self.stdin.fileno(), input[:512])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1064 input = input[bytes_written:]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1065 if not input:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1066 self.stdin.close()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1067 write_set.remove(self.stdin)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1068
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1069 if self.stdout in rlist:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1070 data = os.read(self.stdout.fileno(), 1024)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1071 if data == "":
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1072 self.stdout.close()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1073 read_set.remove(self.stdout)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1074 stdout.append(data)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1075
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1076 if self.stderr in rlist:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1077 data = os.read(self.stderr.fileno(), 1024)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1078 if data == "":
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1079 self.stderr.close()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1080 read_set.remove(self.stderr)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1081 stderr.append(data)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1082
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1083 # All data exchanged. Translate lists into strings.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1084 if stdout != None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1085 stdout = ''.join(stdout)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1086 if stderr != None:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1087 stderr = ''.join(stderr)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1088
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1089 # Translate newlines, if requested. We cannot let the file
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1090 # object do the translation: It is based on stdio, which is
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1091 # impossible to combine with select (unless forcing no
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1092 # buffering).
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1093 if self.universal_newlines and hasattr(open, 'newlines'):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1094 if stdout:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1095 stdout = self._translate_newlines(stdout)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1096 if stderr:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1097 stderr = self._translate_newlines(stderr)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1098
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1099 self.wait()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1100 return (stdout, stderr)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1101
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1102
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1103 def _demo_posix():
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1104 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1105 # Example 1: Simple redirection: Get process list
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1106 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1107 plist = Popen(["ps"], stdout=PIPE).communicate()[0]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1108 print "Process list:"
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1109 print plist
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1110
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1111 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1112 # Example 2: Change uid before executing child
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1113 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1114 if os.getuid() == 0:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1115 p = Popen(["id"], preexec_fn=lambda: os.setuid(100))
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1116 p.wait()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1117
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1118 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1119 # Example 3: Connecting several subprocesses
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1120 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1121 print "Looking for 'hda'..."
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1122 p1 = Popen(["dmesg"], stdout=PIPE)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1123 p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1124 print repr(p2.communicate()[0])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1125
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1126 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1127 # Example 4: Catch execution error
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1128 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1129 print
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1130 print "Trying a weird file..."
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1131 try:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1132 print Popen(["/this/path/does/not/exist"]).communicate()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1133 except OSError, e:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1134 if e.errno == errno.ENOENT:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1135 print "The file didn't exist. I thought so..."
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1136 print "Child traceback:"
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1137 print e.child_traceback
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1138 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1139 print "Error", e.errno
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1140 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1141 print >>sys.stderr, "Gosh. No error."
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1142
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1143
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1144 def _demo_windows():
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1145 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1146 # Example 1: Connecting several subprocesses
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1147 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1148 print "Looking for 'PROMPT' in set output..."
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1149 p1 = Popen("set", stdout=PIPE, shell=True)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1150 p2 = Popen('find "PROMPT"', stdin=p1.stdout, stdout=PIPE)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1151 print repr(p2.communicate()[0])
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1152
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1153 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1154 # Example 2: Simple execution of program
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1155 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1156 print "Executing calc..."
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1157 p = Popen("calc")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1158 p.wait()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1159
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1160
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1161 if __name__ == "__main__":
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1162 if mswindows:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1163 _demo_windows()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1164 else:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1165 _demo_posix()