1313import os
1414import subprocess
1515import 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
1730
1831from .. import exc
1932from ..types import StrOrBytesPath
@@ -143,15 +156,97 @@ def __call__(self, output: Union[str, bytes], timestamp: datetime.datetime):
143156 ...
144157
145158
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
146204def 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 ,
148242 shell : bool = False ,
149243 cwd : Optional [StrOrBytesPath ] = None ,
244+ * ,
245+ # custom
150246 log_in_real_time : bool = True ,
151247 check_returncode : bool = True ,
152248 callback : Optional [ProgressCallbackProtocol ] = None ,
153- * args ,
154- ** kwargs
249+ ** kwargs ,
155250):
156251 """Run 'cmd' in a shell and return the combined contents of stdout and
157252 stderr (Blocking). Throws an exception if the command exits non-zero.
@@ -189,13 +284,12 @@ def progress_cb(output, timestamp):
189284 run(['git', 'pull'], callback=progress_cb)
190285 """
191286 proc = subprocess .Popen (
192- cmd ,
287+ args ,
193288 shell = shell ,
194289 stderr = subprocess .PIPE ,
195290 stdout = subprocess .PIPE ,
196291 cwd = cwd ,
197- * args ,
198- ** kwargs
292+ ** kwargs ,
199293 )
200294
201295 all_output = []
@@ -220,5 +314,5 @@ def progress_cb(output, timestamp):
220314 all_output = console_to_str (b"" .join (stderr_lines ))
221315 output = "" .join (all_output )
222316 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 )
224318 return output
0 commit comments