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/instance.py

95 lines
2.3 KiB
Python
Raw Normal View History

2021-10-22 15:50:26 +00:00
import functools
import re
from pathlib import Path
2021-10-28 14:48:54 +00:00
from typing import Generator, List, Tuple, Optional
2021-10-22 15:50:26 +00:00
import attr
2021-10-28 13:53:32 +00:00
from ruamel.yaml.comments import CommentedMap
2021-10-22 15:50:26 +00:00
from ._constants import COMPOSE_FILE_NAME
2021-10-27 11:33:26 +00:00
from .config import KiwiConfig
from .misc import YAML
2021-10-22 15:50:26 +00:00
_RE_CONFDIR = re.compile(r"^\s*\$(?:CONFDIR|{CONFDIR})/+(.*)$", flags=re.UNICODE)
@attr.s
class Service:
name: str = attr.ib()
2021-11-03 15:32:01 +00:00
content: CommentedMap = attr.ib()
2021-10-22 15:50:26 +00:00
2021-10-28 13:53:32 +00:00
@property
def configs(self) -> Generator[Path, None, None]:
2021-11-03 15:32:01 +00:00
if "volumes" not in self.content:
2021-10-28 13:53:32 +00:00
return
2021-10-22 15:50:26 +00:00
2021-11-03 15:32:01 +00:00
for volume in self.content["volumes"]:
2021-10-28 13:53:32 +00:00
host_part = volume.split(":")[0]
cd_match = _RE_CONFDIR.match(host_part)
2021-10-22 15:50:26 +00:00
2021-10-28 13:53:32 +00:00
if cd_match:
2021-10-28 14:48:54 +00:00
yield Path(cd_match.group(1))
2021-10-22 15:50:26 +00:00
2021-10-28 13:53:32 +00:00
@attr.s
class Services:
content: List[Service] = attr.ib()
def __str__(self) -> str:
return YAML().dump({
"services": {
2021-11-03 15:32:01 +00:00
service.name: service.content
2021-10-28 13:53:32 +00:00
for service in self.content
}
2021-10-28 14:48:54 +00:00
}).strip()
2021-11-06 02:45:27 +00:00
def __bool__(self) -> bool:
return bool(self.content)
def filter_existing(self, service_names: List[str]):
return Services([
service
for service in self.content
if service.name in service_names
])
2021-10-22 15:50:26 +00:00
@attr.s
2021-11-03 15:32:01 +00:00
class Project:
directory: Path = attr.ib()
2021-10-26 10:28:06 +00:00
2021-10-28 13:53:32 +00:00
@staticmethod
2021-10-26 13:59:38 +00:00
@functools.lru_cache(maxsize=10)
2021-11-03 15:32:01 +00:00
def _parse_compose_file(directory: Path) -> CommentedMap:
2021-10-26 13:59:38 +00:00
with open(directory.joinpath(COMPOSE_FILE_NAME), "r") as cf:
2021-10-27 11:33:26 +00:00
return YAML().load(cf)
2021-10-26 13:59:38 +00:00
2021-11-06 02:45:27 +00:00
@property
def name(self) -> str:
return self.directory.name
@property
def services(self) -> Services:
2021-11-03 15:32:01 +00:00
yml = Project._parse_compose_file(self.directory)
2021-11-06 02:45:27 +00:00
return Services([
2021-10-28 13:53:32 +00:00
Service(name, description)
2021-10-26 13:59:38 +00:00
for name, description in yml["services"].items()
2021-11-06 02:45:27 +00:00
])
2021-11-03 15:32:01 +00:00
@attr.s
class Instance:
directory: Path = attr.ib(default=Path('.'))
@property
def config(self) -> KiwiConfig:
"""shorthand: get the current configuration"""
return KiwiConfig.from_directory(self.directory)
def get_project(self, project_name: str) -> Optional[Project]:
for project in self.config.projects:
if project.name == project_name:
return Project(self.directory.joinpath(project.name))