1
- import subprocess
2
1
from dataclasses import dataclass , field , fields
3
2
from functools import cached_property
4
3
from json import loads
5
4
from os import PathLike
6
5
from re import split
6
+ from subprocess import CompletedProcess , run as subprocess_run
7
7
from typing import Callable , Literal , Optional , TypeVar , Union
8
8
from urllib .error import HTTPError , URLError
9
9
from urllib .request import urlopen
@@ -197,7 +197,7 @@ def start(self) -> None:
197
197
# pull means running a separate command before starting
198
198
if self .pull :
199
199
pull_cmd = [* base_cmd , "pull" ]
200
- self ._call_command (cmd = pull_cmd )
200
+ self ._run_command (cmd = pull_cmd )
201
201
202
202
up_cmd = [* base_cmd , "up" ]
203
203
@@ -214,7 +214,7 @@ def start(self) -> None:
214
214
if self .services :
215
215
up_cmd .extend (self .services )
216
216
217
- self ._call_command (cmd = up_cmd )
217
+ self ._run_command (cmd = up_cmd )
218
218
219
219
def stop (self , down = True ) -> None :
220
220
"""
@@ -225,7 +225,7 @@ def stop(self, down=True) -> None:
225
225
down_cmd += ["down" , "--volumes" ]
226
226
else :
227
227
down_cmd += ["stop" ]
228
- self ._call_command (cmd = down_cmd )
228
+ self ._run_command (cmd = down_cmd )
229
229
230
230
def get_logs (self , * services : str ) -> tuple [str , str ]:
231
231
"""
@@ -239,11 +239,7 @@ def get_logs(self, *services: str) -> tuple[str, str]:
239
239
"""
240
240
logs_cmd = [* self .compose_command_property , "logs" , * services ]
241
241
242
- result = subprocess .run (
243
- logs_cmd ,
244
- cwd = self .context ,
245
- capture_output = True ,
246
- )
242
+ result = self ._run_command (cmd = logs_cmd )
247
243
return result .stdout .decode ("utf-8" ), result .stderr .decode ("utf-8" )
248
244
249
245
def get_containers (self , include_all = False ) -> list [ComposeContainer ]:
@@ -259,7 +255,7 @@ def get_containers(self, include_all=False) -> list[ComposeContainer]:
259
255
cmd = [* self .compose_command_property , "ps" , "--format" , "json" ]
260
256
if include_all :
261
257
cmd = [* cmd , "-a" ]
262
- result = subprocess . run (cmd , cwd = self . context , check = True , stdout = subprocess . PIPE )
258
+ result = self . _run_command (cmd = cmd )
263
259
stdout = split (r"\r?\n" , result .stdout .decode ("utf-8" ))
264
260
265
261
containers = []
@@ -322,22 +318,22 @@ def exec_in_container(
322
318
if not service_name :
323
319
service_name = self .get_container ().Service
324
320
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
- )
321
+ result = self ._run_command (cmd = exec_cmd )
331
322
332
323
return (result .stdout .decode ("utf-8" ), result .stderr .decode ("utf-8" ), result .returncode )
333
324
334
- def _call_command (
325
+ def _run_command (
335
326
self ,
336
327
cmd : Union [str , list [str ]],
337
328
context : Optional [str ] = None ,
338
- ) -> None :
329
+ ) -> CompletedProcess [ bytes ] :
339
330
context = context or self .context
340
- subprocess .call (cmd , cwd = context )
331
+ return subprocess_run (
332
+ cmd ,
333
+ capture_output = True ,
334
+ check = True ,
335
+ cwd = context ,
336
+ )
341
337
342
338
def get_service_port (
343
339
self ,
0 commit comments