18
18
from collections .abc import Iterable , Mapping , MutableMapping , Sequence
19
19
20
20
from libvcs import exc
21
- from libvcs ._internal .types import StrOrBytesPath
21
+ from libvcs ._internal .types import StrPath
22
22
23
23
logger = logging .getLogger (__name__ )
24
24
25
- console_encoding = sys .stdout .encoding
26
-
27
-
28
- def console_to_str (s : bytes ) -> str :
29
- """From pypa/pip project, pip.backwardwardcompat. License MIT."""
30
- try :
31
- return s .decode (console_encoding )
32
- except UnicodeDecodeError :
33
- return s .decode ("utf_8" )
34
- except AttributeError : # for tests, #13
35
- return str (s )
36
-
37
25
38
26
if t .TYPE_CHECKING :
39
27
_LoggerAdapter = logging .LoggerAdapter [logging .Logger ]
@@ -99,34 +87,32 @@ def __call__(self, output: t.AnyStr, timestamp: datetime.datetime) -> None:
99
87
_ENV : TypeAlias = Mapping [str , str ]
100
88
else :
101
89
_ENV : TypeAlias = t .Union [
102
- Mapping [bytes , StrOrBytesPath ],
103
- Mapping [str , StrOrBytesPath ],
90
+ Mapping [bytes , StrPath ],
91
+ Mapping [str , StrPath ],
104
92
]
105
93
106
- _CMD = t .Union [StrOrBytesPath , Sequence [StrOrBytesPath ]]
94
+ _CMD = t .Union [StrPath , Sequence [StrPath ]]
107
95
_FILE : TypeAlias = t .Optional [t .Union [int , t .IO [t .Any ]]]
108
96
109
97
110
98
def run (
111
99
args : _CMD ,
112
100
bufsize : int = - 1 ,
113
- executable : StrOrBytesPath | None = None ,
101
+ executable : StrPath | None = None ,
114
102
stdin : _FILE | None = None ,
115
103
stdout : _FILE | None = None ,
116
104
stderr : _FILE | None = None ,
117
105
preexec_fn : t .Callable [[], t .Any ] | None = None ,
118
106
close_fds : bool = True ,
119
107
shell : bool = False ,
120
- cwd : StrOrBytesPath | None = None ,
108
+ cwd : StrPath | None = None ,
121
109
env : _ENV | None = None ,
122
- universal_newlines : bool = False ,
123
110
startupinfo : t .Any | None = None ,
124
111
creationflags : int = 0 ,
125
112
restore_signals : bool = True ,
126
113
start_new_session : bool = False ,
127
114
pass_fds : t .Any = (),
128
115
* ,
129
- text : bool | None = None ,
130
116
encoding : str | None = None ,
131
117
errors : str | None = None ,
132
118
user : str | int | None = None ,
@@ -191,13 +177,12 @@ def progress_cb(output, timestamp):
191
177
shell = shell ,
192
178
cwd = cwd ,
193
179
env = env ,
194
- universal_newlines = universal_newlines ,
195
180
startupinfo = startupinfo ,
196
181
creationflags = creationflags ,
197
182
restore_signals = restore_signals ,
198
183
start_new_session = start_new_session ,
199
184
pass_fds = pass_fds ,
200
- text = text ,
185
+ text = True ,
201
186
encoding = encoding ,
202
187
errors = errors ,
203
188
user = user ,
@@ -206,7 +191,7 @@ def progress_cb(output, timestamp):
206
191
umask = umask ,
207
192
)
208
193
209
- all_output : list [ str ] = []
194
+ all_output : str = ""
210
195
code = None
211
196
line = None
212
197
if log_in_real_time and callback is None :
@@ -220,18 +205,32 @@ def progress_cb(output: t.AnyStr, timestamp: datetime.datetime) -> None:
220
205
code = proc .poll ()
221
206
222
207
if callback and callable (callback ) and proc .stderr is not None :
223
- line = console_to_str (proc .stderr .read (128 ))
208
+ line = str (proc .stderr .read (128 ))
224
209
if line :
225
210
callback (output = line , timestamp = datetime .datetime .now ())
226
211
if callback and callable (callback ):
227
212
callback (output = "\r " , timestamp = datetime .datetime .now ())
228
213
229
- lines = filter (None , (line .strip () for line in proc .stdout .readlines ()))
230
- all_output = console_to_str (b"\n " .join (lines ))
214
+ lines = (
215
+ filter (None , (line .strip () for line in proc .stdout .readlines ()))
216
+ if proc .stdout is not None
217
+ else []
218
+ )
219
+ all_output = "\n " .join (lines )
231
220
if code :
232
- stderr_lines = filter (None , (line .strip () for line in proc .stderr .readlines ()))
233
- all_output = console_to_str (b"" .join (stderr_lines ))
221
+ stderr_lines = (
222
+ filter (None , (line .strip () for line in proc .stderr .readlines ()))
223
+ if proc .stderr is not None
224
+ else []
225
+ )
226
+ all_output = "" .join (stderr_lines )
234
227
output = "" .join (all_output )
235
228
if code != 0 and check_returncode :
236
- raise exc .CommandError (output = output , returncode = code , cmd = args )
229
+ raise exc .CommandError (
230
+ output = output ,
231
+ returncode = code ,
232
+ cmd = [str (arg ) for arg in args ]
233
+ if isinstance (args , (list , tuple ))
234
+ else str (args ),
235
+ )
237
236
return output
0 commit comments