forked from ipython/ipython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_process_common.py
More file actions
270 lines (226 loc) · 9.05 KB
/
Copy path_process_common.py
File metadata and controls
270 lines (226 loc) · 9.05 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
"""Common utilities for the various process_* implementations.
This file is only meant to be imported by the platform-specific implementations
of subprocess utilities, and it contains tools that are common to all of them.
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2010-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import shlex
import subprocess
import sys
from typing import IO, TypeVar
from collections.abc import Callable
_T = TypeVar("_T")
from .encoding import DEFAULT_ENCODING
#-----------------------------------------------------------------------------
# Function definitions
#-----------------------------------------------------------------------------
def read_no_interrupt(stream: IO[bytes]) -> bytes | None:
"""Read from a pipe ignoring EINTR errors.
This is necessary because when reading from pipes with GUI event loops
running in the background, often interrupts are raised that stop the
command from completing."""
import errno
try:
return stream.read()
except OSError as err:
if err.errno != errno.EINTR:
raise
return None
def process_handler(
cmd: str | list[str],
callback: Callable[[subprocess.Popen[bytes]], _T],
stderr: int = subprocess.PIPE,
) -> _T | None:
"""Open a command in a shell subprocess and execute a callback.
This function provides common scaffolding for creating subprocess.Popen()
calls. It creates a Popen object and then calls the callback with it.
Parameters
----------
cmd : str or list
A command to be executed by the system, using :class:`subprocess.Popen`.
If a string is passed, it will be run in the system shell. If a list is
passed, it will be used directly as arguments.
callback : callable
A one-argument function that will be called with the Popen object.
stderr : file descriptor number, optional
By default this is set to ``subprocess.PIPE``, but you can also pass the
value ``subprocess.STDOUT`` to force the subprocess' stderr to go into
the same file descriptor as its stdout. This is useful to read stdout
and stderr combined in the order they are generated.
Returns
-------
The return value of the provided callback is returned.
"""
sys.stdout.flush()
sys.stderr.flush()
# On win32, close_fds can't be true when using pipes for stdin/out/err
if sys.platform == "win32" and stderr != subprocess.PIPE:
close_fds = False
else:
close_fds = True
# Determine if cmd should be run with system shell.
shell = isinstance(cmd, str)
# On POSIX systems run shell commands with user-preferred shell.
executable = None
if shell and os.name == 'posix' and 'SHELL' in os.environ:
executable = os.environ['SHELL']
p = subprocess.Popen(cmd, shell=shell,
executable=executable,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=stderr,
close_fds=close_fds)
try:
out = callback(p)
except KeyboardInterrupt:
print('^C')
sys.stdout.flush()
sys.stderr.flush()
out = None
finally:
# Make really sure that we don't leave processes behind, in case the
# call above raises an exception
# We start by assuming the subprocess finished (to avoid NameErrors
# later depending on the path taken)
if p.returncode is None:
try:
p.terminate()
p.poll()
except OSError:
pass
# One last try on our way out
if p.returncode is None:
try:
p.kill()
except OSError:
pass
return out
def getoutput(cmd: str | list[str]) -> str:
"""Run a command and return its stdout/stderr as a string.
Parameters
----------
cmd : str or list
A command to be executed in the system shell.
Returns
-------
output : str
A string containing the combination of stdout and stderr from the
subprocess, in whatever order the subprocess originally wrote to its
file descriptors (so the order of the information in this string is the
correct order as would be seen if running the command in a terminal).
"""
out = process_handler(cmd, lambda p: p.communicate()[0], subprocess.STDOUT)
if out is None:
return ''
return out.decode(DEFAULT_ENCODING, "replace")
def getoutputerror(cmd: str | list[str]) -> tuple[str, str]:
"""Return (standard output, standard error) of executing cmd in a shell.
Accepts the same arguments as os.system().
Parameters
----------
cmd : str or list
A command to be executed in the system shell.
Returns
-------
stdout : str
stderr : str
"""
return get_output_error_code(cmd)[:2]
def get_output_error_code(cmd: str | list[str]) -> tuple[str, str, int | None]:
"""Return (standard output, standard error, return code) of executing cmd
in a shell.
Accepts the same arguments as os.system().
Parameters
----------
cmd : str or list
A command to be executed in the system shell.
Returns
-------
stdout : str
stderr : str
returncode: int
"""
result = process_handler(cmd, lambda p: (p.communicate(), p))
if result is None:
return '', '', None
(out, err), p = result
return out.decode(DEFAULT_ENCODING, "replace"), err.decode(DEFAULT_ENCODING, "replace"), p.returncode
def arg_split(commandline: str, posix: bool = False, strict: bool = True) -> list[str]:
"""Split a command line's arguments in a shell-like manner.
This is a modified version of the standard library's shlex.split()
function, but with a default of posix=False for splitting, so that quotes
in inputs are respected.
if strict=False, then any errors shlex.split would raise will result in the
unparsed remainder being the last element of the list, rather than raising.
This is because we sometimes use arg_split to parse things other than
command-line args.
"""
lex = shlex.shlex(commandline, posix=posix)
lex.whitespace_split = True
# Extract tokens, ensuring that things like leaving open quotes
# does not cause this to raise. This is important, because we
# sometimes pass Python source through this (e.g. %timeit f(" ")),
# and it shouldn't raise an exception.
# It may be a bad idea to parse things that are not command-line args
# through this function, but we do, so let's be safe about it.
lex.commenters='' #fix for GH-1269
tokens = []
while True:
try:
tokens.append(next(lex))
except StopIteration:
break
except ValueError:
if strict:
raise
# couldn't parse, get remaining blob as last token
tokens.append(lex.token)
break
return tokens
def arg_split_with_quotes(
commandline: str, strict: bool = True
) -> list[tuple[str, bool]]:
"""Split a command line and report which tokens were originally quoted.
Returns a list of ``(token, was_quoted)`` pairs. ``token`` is the unquoted
form, as ``shlex.split(posix=True)`` returns, and ``was_quoted`` is True
if that token had any single- or double-quote characters in ``commandline``.
Useful for callers like ``%run`` that want to honor shell quoting when
deciding wether to apply further expansion (glob, tilde) to a token.
Detection is shlex-based on both passes so the quote semantics are the
same on Posix and Windows. If ``strict`` is False, malformed input (e.g.
an unbalanced quote) returns whatever was parsed so far instead of raising.
"""
def _tokenize(s: str, posix: bool) -> list[str]:
lex = shlex.shlex(s, posix=posix)
lex.whitespace_split = True
lex.commenters = ''
out = []
while True:
try:
out.append(next(lex))
except StopIteration:
break
except ValueError:
if strict:
raise
out.append(lex.token)
break
return out
raw_tokens = _tokenize(commandline, posix=False)
clean_tokens = _tokenize(commandline, posix=True)
if len(raw_tokens) != len(clean_tokens):
# If the two passes disagree (exotic input) report nothing as quoted
# so callers get the legacy, non-quote-aware behavior.
return [(t, False) for t in clean_tokens]
return [
(clean, ("'" in raw) or ('"' in raw))
for clean, raw in zip(clean_tokens, raw_tokens)
]