13
13
import os
14
14
import subprocess
15
15
import sys
16
- from typing import Optional , Protocol , Union
16
+ from typing import (
17
+ IO ,
18
+ Any ,
19
+ Callable ,
20
+ Iterable ,
21
+ Mapping ,
22
+ Optional ,
23
+ Protocol ,
24
+ Sequence ,
25
+ Union ,
26
+ overload ,
27
+ )
28
+
29
+ from typing_extensions import TypeAlias
17
30
18
31
from .. import exc
19
32
from ..types import StrOrBytesPath
@@ -143,15 +156,97 @@ def __call__(self, output: Union[str, bytes], timestamp: datetime.datetime):
143
156
...
144
157
145
158
159
+ if sys .platform == "win32" :
160
+ _ENV : TypeAlias = Mapping [str , str ]
161
+ else :
162
+ _ENV : TypeAlias = Mapping [bytes , StrOrBytesPath ] | Mapping [str , StrOrBytesPath ]
163
+
164
+ _CMD = Union [StrOrBytesPath , Sequence [StrOrBytesPath ]]
165
+ _FILE : TypeAlias = None | int | IO [Any ]
166
+
167
+ # def run(
168
+ # args: _CMD,
169
+ # bufsize: int = -1,
170
+ # executable: StrOrBytesPath | None = None,
171
+ # stdin: _FILE | None = None,
172
+ # stdout: _FILE | None = None,
173
+ # stderr: _FILE | None = None,
174
+ # preexec_fn: Callable[[], Any] | None = None,
175
+ # close_fds: bool = True,
176
+ # shell: bool = False,
177
+ # cwd: StrOrBytesPath | None = None,
178
+ # env: _ENV | None = None,
179
+ # universal_newlines: bool = False,
180
+ # startupinfo: Any | None = None,
181
+ # creationflags: int = 0,
182
+ # restore_signals: bool = True,
183
+ # start_new_session: bool = False,
184
+ # pass_fds: Any = None,
185
+ # *,
186
+ # text: bool | None = None,
187
+ # encoding: str = "utf-8",
188
+ # errors: str | None = None,
189
+ # user: str | int | None = None,
190
+ # group: str | int | None = None,
191
+ # extra_groups: Iterable[str | int] | None = None,
192
+ # umask: int = -1,
193
+ # pipesize: int = -1,
194
+ #
195
+ #
196
+ # # custom
197
+ # log_in_real_time: bool = True,
198
+ # check_returncode: bool = True,
199
+ # callback: Optional[ProgressCallbackProtocol] = None,
200
+ # ):
201
+
202
+
203
+ @overload
146
204
def run (
147
- cmd : Union [str , list [str ]],
205
+ args : _CMD ,
206
+ bufsize : int = ...,
207
+ executable : StrOrBytesPath | None = ...,
208
+ stdin : _FILE | None = ...,
209
+ stdout : _FILE | None = ...,
210
+ stderr : _FILE | None = ...,
211
+ preexec_fn : Callable [[], Any ] | None = ...,
212
+ close_fds : bool = ...,
213
+ shell : bool = ...,
214
+ cwd : Optional [StrOrBytesPath ] = ...,
215
+ env : _ENV | None = ...,
216
+ universal_newlines : bool = ...,
217
+ startupinfo : Any | None = ...,
218
+ creationflags : int = ...,
219
+ restore_signals : bool = ...,
220
+ start_new_session : bool = ...,
221
+ pass_fds : Any = ...,
222
+ * ,
223
+ text : bool | None = ...,
224
+ encoding : str = "utf-8" ,
225
+ errors : str | None = ...,
226
+ user : str | int | None = ...,
227
+ group : str | int | None = ...,
228
+ extra_groups : Iterable [str | int ] | None = ...,
229
+ umask : int = ...,
230
+ pipesize : int = ...,
231
+ # custom
232
+ log_in_real_time : bool = True ,
233
+ check_returncode : bool = True ,
234
+ callback : Optional [ProgressCallbackProtocol ] = None ,
235
+ ):
236
+ ...
237
+
238
+
239
+ def run (
240
+ # args: Union[str, list[str]],
241
+ args : _CMD ,
148
242
shell : bool = False ,
149
243
cwd : Optional [StrOrBytesPath ] = None ,
244
+ * ,
245
+ # custom
150
246
log_in_real_time : bool = True ,
151
247
check_returncode : bool = True ,
152
248
callback : Optional [ProgressCallbackProtocol ] = None ,
153
- * args ,
154
- ** kwargs
249
+ ** kwargs ,
155
250
):
156
251
"""Run 'cmd' in a shell and return the combined contents of stdout and
157
252
stderr (Blocking). Throws an exception if the command exits non-zero.
@@ -189,13 +284,12 @@ def progress_cb(output, timestamp):
189
284
run(['git', 'pull'], callback=progress_cb)
190
285
"""
191
286
proc = subprocess .Popen (
192
- cmd ,
287
+ args ,
193
288
shell = shell ,
194
289
stderr = subprocess .PIPE ,
195
290
stdout = subprocess .PIPE ,
196
291
cwd = cwd ,
197
- * args ,
198
- ** kwargs
292
+ ** kwargs ,
199
293
)
200
294
201
295
all_output = []
@@ -220,5 +314,5 @@ def progress_cb(output, timestamp):
220
314
all_output = console_to_str (b"" .join (stderr_lines ))
221
315
output = "" .join (all_output )
222
316
if code != 0 and check_returncode :
223
- raise exc .CommandError (output = output , returncode = code , cmd = cmd )
317
+ raise exc .CommandError (output = output , returncode = code , cmd = args )
224
318
return output
0 commit comments