Skip to content

Commit 4aa0b52

Browse files
committed
refactor!(run): Match subprocess.Popen
1 parent 8855627 commit 4aa0b52

File tree

4 files changed

+109
-13
lines changed

4 files changed

+109
-13
lines changed

libvcs/_internal/run.py

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,20 @@
1313
import os
1414
import subprocess
1515
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
1730

1831
from .. import exc
1932
from ..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
146204
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,
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

libvcs/cmd/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def run(
181181
if no_optional_locks is True:
182182
cli_args.append("--no-optional-locks")
183183

184-
return run(cmd=cli_args, **kwargs)
184+
return run(args=cli_args, **kwargs)
185185

186186
def clone(
187187
self,

libvcs/cmd/hg.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import pathlib
33
from typing import Optional, Sequence, Union
44

5-
from ..types import StrOrBytesPath, StrPath
65
from libvcs._internal.run import run
76

7+
from ..types import StrOrBytesPath, StrPath
8+
89
_CMD = Union[StrOrBytesPath, Sequence[StrOrBytesPath]]
910

1011

@@ -156,7 +157,7 @@ def run(
156157
if help is True:
157158
cli_args.append("--help")
158159

159-
return run(cmd=cli_args, **kwargs)
160+
return run(args=cli_args, **kwargs)
160161

161162
def clone(
162163
self,

libvcs/cmd/svn.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import pathlib
22
from typing import Literal, Optional, Sequence, Union
33

4-
from ..types import StrOrBytesPath, StrPath
54
from libvcs._internal.run import run
65

6+
from ..types import StrOrBytesPath, StrPath
7+
78
_CMD = Union[StrOrBytesPath, Sequence[StrOrBytesPath]]
89

910
DepthLiteral = Union[Literal["infinity", "empty", "files", "immediates"], None]
@@ -107,7 +108,7 @@ def run(
107108
if config_option is not None:
108109
cli_args.append("--config-option {config_option}")
109110

110-
return run(cmd=cli_args, **kwargs)
111+
return run(args=cli_args, **kwargs)
111112

112113
def checkout(
113114
self,

0 commit comments

Comments
 (0)