1515import asyncio
1616import subprocess
1717import sys
18- from typing import (
19- List ,
20- Optional ,
21- Tuple ,
22- Union ,
23- IO ,
24- Any ,
25- cast ,
26- NamedTuple ,
27- )
18+ from typing import List , Optional , Tuple , Union , IO , Any , cast , NamedTuple
2819
2920from collections .abc import AsyncIterable
3021
31- CommandOutput = NamedTuple ("CommandOutput" , [
32- ('out' , Optional [str ]),
33- ('err' , Optional [str ]),
34- ('exit_code' , int ),
35- ])
22+ CommandOutput = NamedTuple (
23+ "CommandOutput" , [('out' , Optional [str ]), ('err' , Optional [str ]), ('exit_code' , int )]
24+ )
3625
3726BOLD = 1
3827DIM = 2
@@ -52,11 +41,7 @@ def highlight(text: str, color_code: int, bold: bool = False) -> str:
5241 Returns:
5342 The highlighted string.
5443 """
55- return '{}\033 [{}m{}\033 [0m' .format (
56- '\033 [1m' if bold else '' ,
57- color_code ,
58- text ,
59- )
44+ return '{}\033 [{}m{}\033 [0m' .format ('\033 [1m' if bold else '' , color_code , text )
6045
6146
6247class TeeCapture :
@@ -70,9 +55,9 @@ def __init__(self, out_pipe: Optional[IO[str]] = None) -> None:
7055 self .out_pipe = out_pipe
7156
7257
73- async def _async_forward (async_chunks : AsyncIterable ,
74- out : Optional [Union [TeeCapture , IO [str ]]]
75- ) -> Optional [str ]:
58+ async def _async_forward (
59+ async_chunks : AsyncIterable , out : Optional [Union [TeeCapture , IO [str ]]]
60+ ) -> Optional [str ]:
7661 """Prints/captures output from the given asynchronous iterable.
7762
7863 Args:
@@ -99,9 +84,9 @@ async def _async_forward(async_chunks: AsyncIterable,
9984
10085
10186async def _async_wait_for_process (
102- future_process : Any ,
103- out : Optional [Union [TeeCapture , IO [str ]]] = sys .stdout ,
104- err : Optional [Union [TeeCapture , IO [str ]]] = sys .stderr
87+ future_process : Any ,
88+ out : Optional [Union [TeeCapture , IO [str ]]] = sys .stdout ,
89+ err : Optional [Union [TeeCapture , IO [str ]]] = sys .stderr ,
10590) -> CommandOutput :
10691 """Awaits the creation and completion of an asynchronous process.
10792
@@ -122,8 +107,7 @@ async def _async_wait_for_process(
122107 return CommandOutput (output , err_output , process .returncode )
123108
124109
125- def abbreviate_command_arguments_after_switches (cmd : Tuple [str , ...]
126- ) -> Tuple [str , ...]:
110+ def abbreviate_command_arguments_after_switches (cmd : Tuple [str , ...]) -> Tuple [str , ...]:
127111 result = [cmd [0 ]]
128112 for i in range (1 , len (cmd )):
129113 if not cmd [i ].startswith ('-' ):
@@ -133,13 +117,15 @@ def abbreviate_command_arguments_after_switches(cmd: Tuple[str, ...]
133117 return tuple (result )
134118
135119
136- def run_cmd (* cmd : Optional [str ],
137- out : Optional [Union [TeeCapture , IO [str ]]] = sys .stdout ,
138- err : Optional [Union [TeeCapture , IO [str ]]] = sys .stderr ,
139- raise_on_fail : bool = True ,
140- log_run_to_stderr : bool = True ,
141- abbreviate_non_option_arguments : bool = False ,
142- ** kwargs ) -> CommandOutput :
120+ def run_cmd (
121+ * cmd : Optional [str ],
122+ out : Optional [Union [TeeCapture , IO [str ]]] = sys .stdout ,
123+ err : Optional [Union [TeeCapture , IO [str ]]] = sys .stderr ,
124+ raise_on_fail : bool = True ,
125+ log_run_to_stderr : bool = True ,
126+ abbreviate_non_option_arguments : bool = False ,
127+ ** kwargs ,
128+ ) -> CommandOutput :
143129 """Invokes a subprocess and waits for it to finish.
144130
145131 Args:
@@ -212,12 +198,14 @@ def run_cmd(*cmd: Optional[str],
212198 return result
213199
214200
215- def run_shell (cmd : str ,
216- out : Optional [Union [TeeCapture , IO [str ]]] = sys .stdout ,
217- err : Optional [Union [TeeCapture , IO [str ]]] = sys .stderr ,
218- raise_on_fail : bool = True ,
219- log_run_to_stderr : bool = True ,
220- ** kwargs ) -> CommandOutput :
201+ def run_shell (
202+ cmd : str ,
203+ out : Optional [Union [TeeCapture , IO [str ]]] = sys .stdout ,
204+ err : Optional [Union [TeeCapture , IO [str ]]] = sys .stderr ,
205+ raise_on_fail : bool = True ,
206+ log_run_to_stderr : bool = True ,
207+ ** kwargs ,
208+ ) -> CommandOutput :
221209 """Invokes a shell command and waits for it to finish.
222210
223211 Args:
@@ -296,9 +284,7 @@ def output_of(*cmd: Optional[str], **kwargs) -> str:
296284 subprocess.CalledProcessError: The process returned a non-zero error
297285 code and raise_on_fail was set.
298286 """
299- result = cast (
300- str ,
301- run_cmd (* cmd , log_run_to_stderr = False , out = TeeCapture (), ** kwargs ).out )
287+ result = cast (str , run_cmd (* cmd , log_run_to_stderr = False , out = TeeCapture (), ** kwargs ).out )
302288
303289 # Strip final newline.
304290 if result .endswith ('\n ' ):
0 commit comments