1- import subprocess
21from dataclasses import dataclass , field , fields
32from functools import cached_property
43from json import loads
54from os import PathLike
65from re import split
6+ from subprocess import CompletedProcess
7+ from subprocess import run as subprocess_run
78from typing import Callable , Literal , Optional , TypeVar , Union
89from urllib .error import HTTPError , URLError
910from urllib .request import urlopen
@@ -197,7 +198,7 @@ def start(self) -> None:
197198 # pull means running a separate command before starting
198199 if self .pull :
199200 pull_cmd = [* base_cmd , "pull" ]
200- self ._call_command (cmd = pull_cmd )
201+ self ._run_command (cmd = pull_cmd )
201202
202203 up_cmd = [* base_cmd , "up" ]
203204
@@ -214,7 +215,7 @@ def start(self) -> None:
214215 if self .services :
215216 up_cmd .extend (self .services )
216217
217- self ._call_command (cmd = up_cmd )
218+ self ._run_command (cmd = up_cmd )
218219
219220 def stop (self , down = True ) -> None :
220221 """
@@ -225,7 +226,7 @@ def stop(self, down=True) -> None:
225226 down_cmd += ["down" , "--volumes" ]
226227 else :
227228 down_cmd += ["stop" ]
228- self ._call_command (cmd = down_cmd )
229+ self ._run_command (cmd = down_cmd )
229230
230231 def get_logs (self , * services : str ) -> tuple [str , str ]:
231232 """
@@ -239,11 +240,7 @@ def get_logs(self, *services: str) -> tuple[str, str]:
239240 """
240241 logs_cmd = [* self .compose_command_property , "logs" , * services ]
241242
242- result = subprocess .run (
243- logs_cmd ,
244- cwd = self .context ,
245- capture_output = True ,
246- )
243+ result = self ._run_command (cmd = logs_cmd )
247244 return result .stdout .decode ("utf-8" ), result .stderr .decode ("utf-8" )
248245
249246 def get_containers (self , include_all = False ) -> list [ComposeContainer ]:
@@ -259,7 +256,7 @@ def get_containers(self, include_all=False) -> list[ComposeContainer]:
259256 cmd = [* self .compose_command_property , "ps" , "--format" , "json" ]
260257 if include_all :
261258 cmd = [* cmd , "-a" ]
262- result = subprocess . run (cmd , cwd = self . context , check = True , stdout = subprocess . PIPE )
259+ result = self . _run_command (cmd = cmd )
263260 stdout = split (r"\r?\n" , result .stdout .decode ("utf-8" ))
264261
265262 containers = []
@@ -322,22 +319,22 @@ def exec_in_container(
322319 if not service_name :
323320 service_name = self .get_container ().Service
324321 exec_cmd = [* self .compose_command_property , "exec" , "-T" , service_name , * command ]
325- result = subprocess .run (
326- exec_cmd ,
327- cwd = self .context ,
328- capture_output = True ,
329- check = True ,
330- )
322+ result = self ._run_command (cmd = exec_cmd )
331323
332324 return (result .stdout .decode ("utf-8" ), result .stderr .decode ("utf-8" ), result .returncode )
333325
334- def _call_command (
326+ def _run_command (
335327 self ,
336328 cmd : Union [str , list [str ]],
337329 context : Optional [str ] = None ,
338- ) -> None :
330+ ) -> CompletedProcess [ bytes ] :
339331 context = context or self .context
340- subprocess .call (cmd , cwd = context )
332+ return subprocess_run (
333+ cmd ,
334+ capture_output = True ,
335+ check = True ,
336+ cwd = context ,
337+ )
341338
342339 def get_service_port (
343340 self ,
0 commit comments