1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 12:53:00 +00:00
kiwi-scp/kiwi_scp/service.py

47 lines
1.2 KiB
Python
Raw Normal View History

2021-12-02 16:08:14 +00:00
import re
import subprocess
from pathlib import Path
2021-12-02 16:12:30 +00:00
from typing import TYPE_CHECKING, Generator
2021-12-02 16:08:14 +00:00
import attr
from ruamel.yaml import CommentedMap
from .executable import COMPOSE_EXE
2021-12-02 16:12:30 +00:00
if TYPE_CHECKING:
from .project import Project
2021-12-02 16:08:14 +00:00
@attr.s
class Service:
name: str = attr.ib()
content: CommentedMap = attr.ib()
parent: "Project" = attr.ib()
_RE_CONFDIR = re.compile(r"^\s*\$(?:CONFDIR|{CONFDIR})/+(.*)$", flags=re.UNICODE)
@property
def configs(self) -> Generator[Path, None, None]:
if "volumes" not in self.content:
return
for volume in self.content["volumes"]:
host_part = volume.split(":")[0]
cd_match = Service._RE_CONFDIR.match(host_part)
if cd_match:
yield Path(cd_match.group(1))
def has_executable(self, exe_name: str) -> bool:
try:
# test if desired executable exists
COMPOSE_EXE.run(
["exec", "-T", self.name, "/bin/sh", "-c", f"command -v {exe_name}"],
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
**self.parent.process_kwargs,
)
return True
except subprocess.CalledProcessError:
2021-12-02 16:12:30 +00:00
return False