-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
359 lines (310 loc) · 11.5 KB
/
Copy pathprocess.py
File metadata and controls
359 lines (310 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import asyncio
import dataclasses
import os
import pathlib
import subprocess
import sys
from collections.abc import Callable, Generator
from typing import Literal
@dataclasses.dataclass
class ProcessResult:
exit_code: int
stdout: str
stderr: str
combined_out: str
def start_process(
command: str | pathlib.Path,
args: list[str | pathlib.Path] = [],
*,
cwd: str | pathlib.Path | None = None,
on_line_callback: Callable | None = None,
print_stdout: bool = True,
print_stderr: bool = True,
capture_stdout: bool = False,
capture_stderr: bool = False,
raise_on_exitcode: bool = False,
pty: bool = False,
wait: bool = True,
new_console: bool = False,
) -> ProcessResult | None:
"""
Synchronously start a subprocess.
Args:
command: The command to execute (e.g., 'python').
args: A list of arguments for the command.
cwd: Set current working directory of the process to the given path.
on_line_callback: Optional callback function to call for each complete line of output.
print_stdout: Whether to print output to stdout
print_stderr: Whether to print output to stderr
capture_stdout: Whether to store stdout in return value
capture_stderr: Whether to store stderr in return value
raise_on_exitcode: If True, raises CalledProcessError for non-zero exit codes.
pty: Whether to run process in pseudo-terminal.
wait: If True, waits for process completion; if False, returns immediately.
new_console: If True, starts process in a new detached console window.
Returns:
ProcessResult: The return code and output of the subprocess.
Or None if `new_console` is True or `wait` is False.
Raises:
subprocess.CalledProcessError: On non-zero exit code if `raise_on_exitcode` is True.
"""
command_str = str(command)
args_str = [str(a) for a in args]
cwd_str = None if cwd is None else str(cwd)
if new_console or not wait:
return _start_subprocess(
command_str, args_str, cwd=cwd_str, raise_on_exitcode=raise_on_exitcode, wait=wait, new_console=new_console
)
if pty:
return _win_pty_start_process(
command_str,
args_str,
cwd=cwd_str,
on_line_callback=on_line_callback,
print_output=print_stdout or print_stderr,
capture_output=capture_stdout or capture_stderr,
raise_on_exitcode=raise_on_exitcode,
)
else:
return asyncio.run(
_async_start_process(
command_str,
args_str,
cwd=cwd_str,
on_line_callback=on_line_callback,
print_stdout=print_stdout,
print_stderr=print_stderr,
capture_stdout=capture_stdout,
capture_stderr=capture_stderr,
raise_on_exitcode=raise_on_exitcode,
new_console=new_console,
)
)
def _win_pty_start_process(
command: str,
args: list[str],
*,
cwd: str | None = None,
on_line_callback: Callable | None,
print_output: bool,
capture_output: bool,
raise_on_exitcode: bool,
) -> ProcessResult:
"""
Start a subprocess in windows-specific pseudoterminal.
Args:
command: The command to execute (e.g., 'python').
args: A list of arguments for the command.
cwd: Set current working directory of the process to the given path.
on_line_callback: Optional callback function to call for each complete line of output.
print_output: Whether to print output to stdout.
capture_output: Whether to store process output in return value.
raise_on_exitcode: If True, raises CalledProcessError for non-zero exit codes.
Returns:
ProcessResult: The return code and output of the subprocess.
Raises:
subprocess.CalledProcessError: On non-zero exit code if `raise_on_exitcode` is True.
"""
import msvcrt
import shutil
import signal
import subprocess
import threading
import time
import winpty
command = shutil.which(command) or command
cmdline = subprocess.list2cmdline([command] + args)
term_size = os.get_terminal_size()
pty = winpty.PTY(term_size.columns, term_size.lines, backend=winpty.Backend.WinPTY)
pty.spawn(command, cmdline=cmdline, cwd=cwd)
def input_thread_func() -> None:
try:
while pty.isalive():
if msvcrt.kbhit():
ch = msvcrt.getwch()
try:
pty.write(ch)
except (BrokenPipeError, OSError):
break
else:
time.sleep(0.1)
except Exception:
pass
if sys.stdin.isatty():
input_thread = threading.Thread(target=input_thread_func, daemon=True)
input_thread.start()
line_builder = LineBuilder()
combined_chunks = []
while True:
try:
data = pty.read(blocking=True)
except Exception:
break
if not data:
if pty.iseof():
break
continue
if capture_output:
combined_chunks.append(data)
if print_output:
sys.stdout.write(data)
sys.stdout.flush()
if on_line_callback:
for line in line_builder.feed(data):
on_line_callback(line)
try:
while pty.isalive():
time.sleep(0.1)
except KeyboardInterrupt:
if pty.pid:
os.kill(pty.pid, signal.SIGTERM)
raise
exit_code = pty.get_exitstatus()
result = ProcessResult(
exit_code=exit_code or 0,
stdout="",
stderr="",
combined_out="".join(combined_chunks),
)
if raise_on_exitcode and result.exit_code != 0:
raise subprocess.CalledProcessError(result.exit_code, [command] + args, output=result.combined_out, stderr=None)
return result
async def _async_start_process(
command: str,
args: list[str],
*,
cwd: str | None = None,
on_line_callback: Callable | None,
print_stdout: bool = True,
print_stderr: bool = True,
capture_stdout: bool = False,
capture_stderr: bool = False,
raise_on_exitcode: bool,
new_console: bool = False,
) -> ProcessResult:
"""
Asynchronously start a subprocess.
Args:
command: The command to execute (e.g., 'python').
args: A list of arguments for the command.
cwd: Set current working directory of the process to the given path.
on_line_callback: Optional callback function to call for each complete line of output.
print_stdout: Whether to print output to stdout
print_stderr: Whether to print output to stderr
capture_stdout: Whether to store stdout in return value
capture_stderr: Whether to store stderr in return value
raise_on_exitcode: If True, raises CalledProcessError for non-zero exit codes.
new_console: If True, starts process in a new detached console window.
Returns:
ProcessResult: The return code and output of the subprocess.
Raises:
subprocess.CalledProcessError: On non-zero exit code if `raise_on_exitcode` is True.
"""
creation_flags = 0
if new_console:
creation_flags |= subprocess.CREATE_NEW_CONSOLE
process = await asyncio.create_subprocess_exec(
command,
*args,
cwd=cwd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
creationflags=creation_flags,
)
output_queue = asyncio.Queue[tuple[Literal["stdout", "stderr"], bytes | None]]()
stdout_chunks = []
stderr_chunks = []
combined_chunks = []
async def queue_output(stream: asyncio.StreamReader | None, stream_type: Literal["stdout", "stderr"]) -> None:
if stream is None:
return
while True:
chunk = await stream.read(1024)
if chunk:
await output_queue.put((stream_type, chunk))
else:
await output_queue.put((stream_type, None))
break
stdout_task = asyncio.create_task(queue_output(process.stdout, "stdout"))
stderr_task = asyncio.create_task(queue_output(process.stderr, "stderr"))
line_builder = LineBuilder()
while not output_queue.empty() or not stdout_task.done() or not stderr_task.done():
item = await output_queue.get()
stream_type, chunk = item
if chunk is not None:
decoded_chunk = chunk.decode("utf-8", errors="replace")
if stream_type == "stdout":
if print_stdout:
sys.stdout.write(decoded_chunk)
sys.stdout.flush()
if capture_stdout:
stdout_chunks.append(decoded_chunk)
combined_chunks.append(decoded_chunk)
elif stream_type == "stderr":
if print_stderr:
sys.stderr.write(decoded_chunk)
sys.stderr.flush()
if capture_stderr:
stderr_chunks.append(decoded_chunk)
combined_chunks.append(decoded_chunk)
if on_line_callback:
for line in line_builder.feed(decoded_chunk):
on_line_callback(line)
output_queue.task_done()
await asyncio.gather(stdout_task, stderr_task)
exit_code = await process.wait()
result = ProcessResult(
exit_code=exit_code,
stdout="".join(stdout_chunks),
stderr="".join(stderr_chunks),
combined_out="".join(combined_chunks),
)
if raise_on_exitcode and result.exit_code != 0:
raise subprocess.CalledProcessError(
result.exit_code, [command] + args, output=result.stdout, stderr=result.stderr
)
return result
def _start_subprocess(
command: str,
args: list[str],
*,
cwd: str | None = None,
raise_on_exitcode: bool = False,
wait: bool = True,
new_console: bool = False,
) -> ProcessResult | None:
"""
Start a subprocess.
Args:
command: The command to execute (e.g., 'python').
args: A list of arguments for the command.
cwd: Set current working directory of the process to the given path.
raise_on_exitcode: If True, raises CalledProcessError for non-zero exit codes.
wait: If True, waits for process completion; if False, returns immediately.
new_console: If True, starts process in a new detached console window.
Returns:
ProcessResult: The return code and output of the subprocess.
Or None if `wait` is False.
Raises:
subprocess.CalledProcessError: On non-zero exit code if `raise_on_exitcode` is True.
"""
creation_flags = 0
if new_console:
creation_flags |= subprocess.CREATE_NEW_CONSOLE
process = subprocess.Popen([command] + args, cwd=cwd, creationflags=creation_flags)
if not wait:
return None
exit_code = process.wait()
if raise_on_exitcode and exit_code != 0:
raise subprocess.CalledProcessError(exit_code, [command] + args, output="", stderr="")
return ProcessResult(exit_code, "", "", "")
class LineBuilder:
def __init__(self) -> None:
self.buffer = ""
def feed(self, chunk: str) -> Generator[str, None, None]:
self.buffer += chunk
while "\n" in self.buffer:
index = self.buffer.index("\n")
line = self.buffer[:index]
self.buffer = self.buffer[index + 1 :]
yield line