1 | n/a | import collections |
---|
2 | n/a | import subprocess |
---|
3 | n/a | import warnings |
---|
4 | n/a | |
---|
5 | n/a | from . import compat |
---|
6 | n/a | from . import protocols |
---|
7 | n/a | from . import transports |
---|
8 | n/a | from .coroutines import coroutine |
---|
9 | n/a | from .log import logger |
---|
10 | n/a | |
---|
11 | n/a | |
---|
12 | n/a | class BaseSubprocessTransport(transports.SubprocessTransport): |
---|
13 | n/a | |
---|
14 | n/a | def __init__(self, loop, protocol, args, shell, |
---|
15 | n/a | stdin, stdout, stderr, bufsize, |
---|
16 | n/a | waiter=None, extra=None, **kwargs): |
---|
17 | n/a | super().__init__(extra) |
---|
18 | n/a | self._closed = False |
---|
19 | n/a | self._protocol = protocol |
---|
20 | n/a | self._loop = loop |
---|
21 | n/a | self._proc = None |
---|
22 | n/a | self._pid = None |
---|
23 | n/a | self._returncode = None |
---|
24 | n/a | self._exit_waiters = [] |
---|
25 | n/a | self._pending_calls = collections.deque() |
---|
26 | n/a | self._pipes = {} |
---|
27 | n/a | self._finished = False |
---|
28 | n/a | |
---|
29 | n/a | if stdin == subprocess.PIPE: |
---|
30 | n/a | self._pipes[0] = None |
---|
31 | n/a | if stdout == subprocess.PIPE: |
---|
32 | n/a | self._pipes[1] = None |
---|
33 | n/a | if stderr == subprocess.PIPE: |
---|
34 | n/a | self._pipes[2] = None |
---|
35 | n/a | |
---|
36 | n/a | # Create the child process: set the _proc attribute |
---|
37 | n/a | try: |
---|
38 | n/a | self._start(args=args, shell=shell, stdin=stdin, stdout=stdout, |
---|
39 | n/a | stderr=stderr, bufsize=bufsize, **kwargs) |
---|
40 | n/a | except: |
---|
41 | n/a | self.close() |
---|
42 | n/a | raise |
---|
43 | n/a | |
---|
44 | n/a | self._pid = self._proc.pid |
---|
45 | n/a | self._extra['subprocess'] = self._proc |
---|
46 | n/a | |
---|
47 | n/a | if self._loop.get_debug(): |
---|
48 | n/a | if isinstance(args, (bytes, str)): |
---|
49 | n/a | program = args |
---|
50 | n/a | else: |
---|
51 | n/a | program = args[0] |
---|
52 | n/a | logger.debug('process %r created: pid %s', |
---|
53 | n/a | program, self._pid) |
---|
54 | n/a | |
---|
55 | n/a | self._loop.create_task(self._connect_pipes(waiter)) |
---|
56 | n/a | |
---|
57 | n/a | def __repr__(self): |
---|
58 | n/a | info = [self.__class__.__name__] |
---|
59 | n/a | if self._closed: |
---|
60 | n/a | info.append('closed') |
---|
61 | n/a | if self._pid is not None: |
---|
62 | n/a | info.append('pid=%s' % self._pid) |
---|
63 | n/a | if self._returncode is not None: |
---|
64 | n/a | info.append('returncode=%s' % self._returncode) |
---|
65 | n/a | elif self._pid is not None: |
---|
66 | n/a | info.append('running') |
---|
67 | n/a | else: |
---|
68 | n/a | info.append('not started') |
---|
69 | n/a | |
---|
70 | n/a | stdin = self._pipes.get(0) |
---|
71 | n/a | if stdin is not None: |
---|
72 | n/a | info.append('stdin=%s' % stdin.pipe) |
---|
73 | n/a | |
---|
74 | n/a | stdout = self._pipes.get(1) |
---|
75 | n/a | stderr = self._pipes.get(2) |
---|
76 | n/a | if stdout is not None and stderr is stdout: |
---|
77 | n/a | info.append('stdout=stderr=%s' % stdout.pipe) |
---|
78 | n/a | else: |
---|
79 | n/a | if stdout is not None: |
---|
80 | n/a | info.append('stdout=%s' % stdout.pipe) |
---|
81 | n/a | if stderr is not None: |
---|
82 | n/a | info.append('stderr=%s' % stderr.pipe) |
---|
83 | n/a | |
---|
84 | n/a | return '<%s>' % ' '.join(info) |
---|
85 | n/a | |
---|
86 | n/a | def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs): |
---|
87 | n/a | raise NotImplementedError |
---|
88 | n/a | |
---|
89 | n/a | def set_protocol(self, protocol): |
---|
90 | n/a | self._protocol = protocol |
---|
91 | n/a | |
---|
92 | n/a | def get_protocol(self): |
---|
93 | n/a | return self._protocol |
---|
94 | n/a | |
---|
95 | n/a | def is_closing(self): |
---|
96 | n/a | return self._closed |
---|
97 | n/a | |
---|
98 | n/a | def close(self): |
---|
99 | n/a | if self._closed: |
---|
100 | n/a | return |
---|
101 | n/a | self._closed = True |
---|
102 | n/a | |
---|
103 | n/a | for proto in self._pipes.values(): |
---|
104 | n/a | if proto is None: |
---|
105 | n/a | continue |
---|
106 | n/a | proto.pipe.close() |
---|
107 | n/a | |
---|
108 | n/a | if (self._proc is not None |
---|
109 | n/a | # the child process finished? |
---|
110 | n/a | and self._returncode is None |
---|
111 | n/a | # the child process finished but the transport was not notified yet? |
---|
112 | n/a | and self._proc.poll() is None |
---|
113 | n/a | ): |
---|
114 | n/a | if self._loop.get_debug(): |
---|
115 | n/a | logger.warning('Close running child process: kill %r', self) |
---|
116 | n/a | |
---|
117 | n/a | try: |
---|
118 | n/a | self._proc.kill() |
---|
119 | n/a | except ProcessLookupError: |
---|
120 | n/a | pass |
---|
121 | n/a | |
---|
122 | n/a | # Don't clear the _proc reference yet: _post_init() may still run |
---|
123 | n/a | |
---|
124 | n/a | # On Python 3.3 and older, objects with a destructor part of a reference |
---|
125 | n/a | # cycle are never destroyed. It's not more the case on Python 3.4 thanks |
---|
126 | n/a | # to the PEP 442. |
---|
127 | n/a | if compat.PY34: |
---|
128 | n/a | def __del__(self): |
---|
129 | n/a | if not self._closed: |
---|
130 | n/a | warnings.warn("unclosed transport %r" % self, ResourceWarning, |
---|
131 | n/a | source=self) |
---|
132 | n/a | self.close() |
---|
133 | n/a | |
---|
134 | n/a | def get_pid(self): |
---|
135 | n/a | return self._pid |
---|
136 | n/a | |
---|
137 | n/a | def get_returncode(self): |
---|
138 | n/a | return self._returncode |
---|
139 | n/a | |
---|
140 | n/a | def get_pipe_transport(self, fd): |
---|
141 | n/a | if fd in self._pipes: |
---|
142 | n/a | return self._pipes[fd].pipe |
---|
143 | n/a | else: |
---|
144 | n/a | return None |
---|
145 | n/a | |
---|
146 | n/a | def _check_proc(self): |
---|
147 | n/a | if self._proc is None: |
---|
148 | n/a | raise ProcessLookupError() |
---|
149 | n/a | |
---|
150 | n/a | def send_signal(self, signal): |
---|
151 | n/a | self._check_proc() |
---|
152 | n/a | self._proc.send_signal(signal) |
---|
153 | n/a | |
---|
154 | n/a | def terminate(self): |
---|
155 | n/a | self._check_proc() |
---|
156 | n/a | self._proc.terminate() |
---|
157 | n/a | |
---|
158 | n/a | def kill(self): |
---|
159 | n/a | self._check_proc() |
---|
160 | n/a | self._proc.kill() |
---|
161 | n/a | |
---|
162 | n/a | @coroutine |
---|
163 | n/a | def _connect_pipes(self, waiter): |
---|
164 | n/a | try: |
---|
165 | n/a | proc = self._proc |
---|
166 | n/a | loop = self._loop |
---|
167 | n/a | |
---|
168 | n/a | if proc.stdin is not None: |
---|
169 | n/a | _, pipe = yield from loop.connect_write_pipe( |
---|
170 | n/a | lambda: WriteSubprocessPipeProto(self, 0), |
---|
171 | n/a | proc.stdin) |
---|
172 | n/a | self._pipes[0] = pipe |
---|
173 | n/a | |
---|
174 | n/a | if proc.stdout is not None: |
---|
175 | n/a | _, pipe = yield from loop.connect_read_pipe( |
---|
176 | n/a | lambda: ReadSubprocessPipeProto(self, 1), |
---|
177 | n/a | proc.stdout) |
---|
178 | n/a | self._pipes[1] = pipe |
---|
179 | n/a | |
---|
180 | n/a | if proc.stderr is not None: |
---|
181 | n/a | _, pipe = yield from loop.connect_read_pipe( |
---|
182 | n/a | lambda: ReadSubprocessPipeProto(self, 2), |
---|
183 | n/a | proc.stderr) |
---|
184 | n/a | self._pipes[2] = pipe |
---|
185 | n/a | |
---|
186 | n/a | assert self._pending_calls is not None |
---|
187 | n/a | |
---|
188 | n/a | loop.call_soon(self._protocol.connection_made, self) |
---|
189 | n/a | for callback, data in self._pending_calls: |
---|
190 | n/a | loop.call_soon(callback, *data) |
---|
191 | n/a | self._pending_calls = None |
---|
192 | n/a | except Exception as exc: |
---|
193 | n/a | if waiter is not None and not waiter.cancelled(): |
---|
194 | n/a | waiter.set_exception(exc) |
---|
195 | n/a | else: |
---|
196 | n/a | if waiter is not None and not waiter.cancelled(): |
---|
197 | n/a | waiter.set_result(None) |
---|
198 | n/a | |
---|
199 | n/a | def _call(self, cb, *data): |
---|
200 | n/a | if self._pending_calls is not None: |
---|
201 | n/a | self._pending_calls.append((cb, data)) |
---|
202 | n/a | else: |
---|
203 | n/a | self._loop.call_soon(cb, *data) |
---|
204 | n/a | |
---|
205 | n/a | def _pipe_connection_lost(self, fd, exc): |
---|
206 | n/a | self._call(self._protocol.pipe_connection_lost, fd, exc) |
---|
207 | n/a | self._try_finish() |
---|
208 | n/a | |
---|
209 | n/a | def _pipe_data_received(self, fd, data): |
---|
210 | n/a | self._call(self._protocol.pipe_data_received, fd, data) |
---|
211 | n/a | |
---|
212 | n/a | def _process_exited(self, returncode): |
---|
213 | n/a | assert returncode is not None, returncode |
---|
214 | n/a | assert self._returncode is None, self._returncode |
---|
215 | n/a | if self._loop.get_debug(): |
---|
216 | n/a | logger.info('%r exited with return code %r', |
---|
217 | n/a | self, returncode) |
---|
218 | n/a | self._returncode = returncode |
---|
219 | n/a | if self._proc.returncode is None: |
---|
220 | n/a | # asyncio uses a child watcher: copy the status into the Popen |
---|
221 | n/a | # object. On Python 3.6, it is required to avoid a ResourceWarning. |
---|
222 | n/a | self._proc.returncode = returncode |
---|
223 | n/a | self._call(self._protocol.process_exited) |
---|
224 | n/a | self._try_finish() |
---|
225 | n/a | |
---|
226 | n/a | # wake up futures waiting for wait() |
---|
227 | n/a | for waiter in self._exit_waiters: |
---|
228 | n/a | if not waiter.cancelled(): |
---|
229 | n/a | waiter.set_result(returncode) |
---|
230 | n/a | self._exit_waiters = None |
---|
231 | n/a | |
---|
232 | n/a | @coroutine |
---|
233 | n/a | def _wait(self): |
---|
234 | n/a | """Wait until the process exit and return the process return code. |
---|
235 | n/a | |
---|
236 | n/a | This method is a coroutine.""" |
---|
237 | n/a | if self._returncode is not None: |
---|
238 | n/a | return self._returncode |
---|
239 | n/a | |
---|
240 | n/a | waiter = self._loop.create_future() |
---|
241 | n/a | self._exit_waiters.append(waiter) |
---|
242 | n/a | return (yield from waiter) |
---|
243 | n/a | |
---|
244 | n/a | def _try_finish(self): |
---|
245 | n/a | assert not self._finished |
---|
246 | n/a | if self._returncode is None: |
---|
247 | n/a | return |
---|
248 | n/a | if all(p is not None and p.disconnected |
---|
249 | n/a | for p in self._pipes.values()): |
---|
250 | n/a | self._finished = True |
---|
251 | n/a | self._call(self._call_connection_lost, None) |
---|
252 | n/a | |
---|
253 | n/a | def _call_connection_lost(self, exc): |
---|
254 | n/a | try: |
---|
255 | n/a | self._protocol.connection_lost(exc) |
---|
256 | n/a | finally: |
---|
257 | n/a | self._loop = None |
---|
258 | n/a | self._proc = None |
---|
259 | n/a | self._protocol = None |
---|
260 | n/a | |
---|
261 | n/a | |
---|
262 | n/a | class WriteSubprocessPipeProto(protocols.BaseProtocol): |
---|
263 | n/a | |
---|
264 | n/a | def __init__(self, proc, fd): |
---|
265 | n/a | self.proc = proc |
---|
266 | n/a | self.fd = fd |
---|
267 | n/a | self.pipe = None |
---|
268 | n/a | self.disconnected = False |
---|
269 | n/a | |
---|
270 | n/a | def connection_made(self, transport): |
---|
271 | n/a | self.pipe = transport |
---|
272 | n/a | |
---|
273 | n/a | def __repr__(self): |
---|
274 | n/a | return ('<%s fd=%s pipe=%r>' |
---|
275 | n/a | % (self.__class__.__name__, self.fd, self.pipe)) |
---|
276 | n/a | |
---|
277 | n/a | def connection_lost(self, exc): |
---|
278 | n/a | self.disconnected = True |
---|
279 | n/a | self.proc._pipe_connection_lost(self.fd, exc) |
---|
280 | n/a | self.proc = None |
---|
281 | n/a | |
---|
282 | n/a | def pause_writing(self): |
---|
283 | n/a | self.proc._protocol.pause_writing() |
---|
284 | n/a | |
---|
285 | n/a | def resume_writing(self): |
---|
286 | n/a | self.proc._protocol.resume_writing() |
---|
287 | n/a | |
---|
288 | n/a | |
---|
289 | n/a | class ReadSubprocessPipeProto(WriteSubprocessPipeProto, |
---|
290 | n/a | protocols.Protocol): |
---|
291 | n/a | |
---|
292 | n/a | def data_received(self, data): |
---|
293 | n/a | self.proc._pipe_data_received(self.fd, data) |
---|