~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/util/SubProcess.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Enhanced subprocess.Popen subclass, supporting:
3
 
    * .communicate() with timeout
4
 
    * kill/terminate/send_signal (like in Py 2.6) for Py 2.4 / 2.5
5
 
 
6
 
Sample usage:
7
 
    out, err = Popen(...).communicate(input, timeout=300)
8
 
"""
9
 
 
10
 
import os
11
 
import subprocess
12
 
import threading
13
 
import signal
14
 
 
15
 
if subprocess.mswindows:
16
 
    try:
17
 
        # Python >= 2.6 should have this:
18
 
        from _subprocess import TerminateProcess
19
 
    except ImportError:
20
 
        # otherwise you need win32 extensions:
21
 
        from win32process import TerminateProcess
22
 
else:
23
 
    import select
24
 
    import errno
25
 
 
26
 
class Popen(subprocess.Popen):
27
 
    # send_signal, terminate, kill copied from Python 2.6
28
 
    # (we want to support Python >= 2.4)
29
 
    if subprocess.mswindows:
30
 
        def send_signal(self, sig):
31
 
            """Send a signal to the process
32
 
            """
33
 
            if sig == signal.SIGTERM:
34
 
                self.terminate()
35
 
            else:
36
 
                raise ValueError("Only SIGTERM is supported on Windows")
37
 
 
38
 
        def terminate(self):
39
 
            """Terminates the process
40
 
            """
41
 
            TerminateProcess(self._handle, 1)
42
 
 
43
 
        kill = terminate
44
 
 
45
 
    else: # POSIX
46
 
        def send_signal(self, sig):
47
 
            """Send a signal to the process
48
 
            """
49
 
            os.kill(self.pid, sig)
50
 
 
51
 
        def terminate(self):
52
 
            """Terminate the process with SIGTERM
53
 
            """
54
 
            self.send_signal(signal.SIGTERM)
55
 
 
56
 
        def kill(self):
57
 
            """Kill the process with SIGKILL
58
 
            """
59
 
            self.send_signal(signal.SIGKILL)
60
 
 
61
 
    def communicate(self, input=None, timeout=None):
62
 
        """Interact with process: Send data to stdin.  Read data from
63
 
        stdout and stderr, until end-of-file is reached.  Wait for
64
 
        process to terminate.  The optional input argument should be a
65
 
        string to be sent to the child process, or None, if no data
66
 
        should be sent to the child.
67
 
 
68
 
        communicate() returns a tuple (stdout, stderr)."""
69
 
 
70
 
        self.timeout = timeout
71
 
 
72
 
        # Optimization: If we are only using one pipe, or no pipe at
73
 
        # all, using select() or threads is unnecessary.
74
 
        if [self.stdin, self.stdout, self.stderr].count(None) >= 2:
75
 
            stdout = None
76
 
            stderr = None
77
 
            if self.stdin:
78
 
                if input:
79
 
                    self._fo_write_no_intr(self.stdin, input)
80
 
                self.stdin.close()
81
 
            elif self.stdout:
82
 
                stdout = self._fo_read_no_intr(self.stdout)
83
 
                self.stdout.close()
84
 
            elif self.stderr:
85
 
                stderr = self._fo_read_no_intr(self.stderr)
86
 
                self.stderr.close()
87
 
            self.wait()
88
 
            return (stdout, stderr)
89
 
 
90
 
        return self._communicate(input)
91
 
 
92
 
    if subprocess.mswindows:
93
 
        def _communicate(self, input):
94
 
            stdout = None # Return
95
 
            stderr = None # Return
96
 
 
97
 
            if self.stdout:
98
 
                stdout = []
99
 
                stdout_thread = threading.Thread(target=self._readerthread,
100
 
                                                 args=(self.stdout, stdout))
101
 
                stdout_thread.setDaemon(True)
102
 
                stdout_thread.start()
103
 
            if self.stderr:
104
 
                stderr = []
105
 
                stderr_thread = threading.Thread(target=self._readerthread,
106
 
                                                 args=(self.stderr, stderr))
107
 
                stderr_thread.setDaemon(True)
108
 
                stderr_thread.start()
109
 
 
110
 
            if self.stdin:
111
 
                if input is not None:
112
 
                    self.stdin.write(input)
113
 
                self.stdin.close()
114
 
 
115
 
            if self.stdout:
116
 
                stdout_thread.join(self.timeout)
117
 
            if self.stderr:
118
 
                stderr_thread.join(self.timeout)
119
 
 
120
 
            # if the threads are still alive, that means the thread join timed out
121
 
            timed_out = (self.stdout and stdout_thread.isAlive() or
122
 
                         self.stderr and stderr_thread.isAlive())
123
 
            if timed_out:
124
 
                self.kill()
125
 
            else:
126
 
                self.wait()
127
 
 
128
 
            # All data exchanged.  Translate lists into strings.
129
 
            if stdout is not None:
130
 
                stdout = stdout[0]
131
 
            if stderr is not None:
132
 
                stderr = stderr[0]
133
 
 
134
 
            # Translate newlines, if requested.  We cannot let the file
135
 
            # object do the translation: It is based on stdio, which is
136
 
            # impossible to combine with select (unless forcing no
137
 
            # buffering).
138
 
            if self.universal_newlines and hasattr(file, 'newlines'):
139
 
                if stdout:
140
 
                    stdout = self._translate_newlines(stdout)
141
 
                if stderr:
142
 
                    stderr = self._translate_newlines(stderr)
143
 
 
144
 
            return (stdout, stderr)
145
 
 
146
 
    else: # POSIX
147
 
        def _communicate(self, input):
148
 
            timed_out = False
149
 
            read_set = []
150
 
            write_set = []
151
 
            stdout = None # Return
152
 
            stderr = None # Return
153
 
 
154
 
            if self.stdin:
155
 
                # Flush stdio buffer.  This might block, if the user has
156
 
                # been writing to .stdin in an uncontrolled fashion.
157
 
                self.stdin.flush()
158
 
                if input:
159
 
                    write_set.append(self.stdin)
160
 
                else:
161
 
                    self.stdin.close()
162
 
            if self.stdout:
163
 
                read_set.append(self.stdout)
164
 
                stdout = []
165
 
            if self.stderr:
166
 
                read_set.append(self.stderr)
167
 
                stderr = []
168
 
 
169
 
            input_offset = 0
170
 
            while read_set or write_set:
171
 
                try:
172
 
                    rlist, wlist, xlist = select.select(read_set, write_set, [], self.timeout)
173
 
                except select.error, e:
174
 
                    if e.args[0] == errno.EINTR:
175
 
                        continue
176
 
                    raise
177
 
 
178
 
                timed_out = not (rlist or wlist or xlist)
179
 
                if timed_out:
180
 
                    break
181
 
 
182
 
                if self.stdin in wlist:
183
 
                    # When select has indicated that the file is writable,
184
 
                    # we can write up to PIPE_BUF bytes without risk
185
 
                    # blocking.  POSIX defines PIPE_BUF >= 512
186
 
                    chunk = input[input_offset:input_offset + 512]
187
 
                    bytes_written = os.write(self.stdin.fileno(), chunk)
188
 
                    input_offset += bytes_written
189
 
                    if input_offset >= len(input):
190
 
                        self.stdin.close()
191
 
                        write_set.remove(self.stdin)
192
 
 
193
 
                if self.stdout in rlist:
194
 
                    data = os.read(self.stdout.fileno(), 1024)
195
 
                    if data == "":
196
 
                        self.stdout.close()
197
 
                        read_set.remove(self.stdout)
198
 
                    stdout.append(data)
199
 
 
200
 
                if self.stderr in rlist:
201
 
                    data = os.read(self.stderr.fileno(), 1024)
202
 
                    if data == "":
203
 
                        self.stderr.close()
204
 
                        read_set.remove(self.stderr)
205
 
                    stderr.append(data)
206
 
 
207
 
            # All data exchanged.  Translate lists into strings.
208
 
            if stdout is not None:
209
 
                stdout = ''.join(stdout)
210
 
            if stderr is not None:
211
 
                stderr = ''.join(stderr)
212
 
 
213
 
            # Translate newlines, if requested.  We cannot let the file
214
 
            # object do the translation: It is based on stdio, which is
215
 
            # impossible to combine with select (unless forcing no
216
 
            # buffering).
217
 
            if self.universal_newlines and hasattr(file, 'newlines'):
218
 
                if stdout:
219
 
                    stdout = self._translate_newlines(stdout)
220
 
                if stderr:
221
 
                    stderr = self._translate_newlines(stderr)
222
 
 
223
 
            if timed_out:
224
 
                self.kill()
225
 
            else:
226
 
                self.wait()
227
 
            return (stdout, stderr)
228
 
 
229
 
 
230
 
def exec_cmd(cmd, input=None, timeout=None):
231
 
    p = Popen(cmd, shell=True,
232
 
              close_fds=not subprocess.mswindows,
233
 
              bufsize=1024,
234
 
              stdin=subprocess.PIPE,
235
 
              stdout=subprocess.PIPE,
236
 
              stderr=subprocess.PIPE)
237
 
    data, errors = p.communicate(input, timeout=timeout)
238
 
    return data, errors, p.returncode
239
 
 
240
 
 
241
 
if __name__ == '__main__':
242
 
    print exec_cmd("python", "import time ; time.sleep(20) ; print 'never!' ;", timeout=10)
243
 
    print exec_cmd("python", "import time ; time.sleep(20) ; print '20s gone' ;")
244