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