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

72 lines
2.1 KiB
Python
Raw Normal View History

2021-12-02 16:08:14 +00:00
import functools
from pathlib import Path
2021-12-02 16:12:30 +00:00
from typing import TYPE_CHECKING, Optional, Dict, Any
2021-12-02 16:08:14 +00:00
import attr
from ruamel.yaml import CommentedMap
2022-01-27 16:57:15 +00:00
from ._constants import COMPOSE_FILE_NAME, CONFIG_DIRECTORY_NAME
2021-12-02 16:08:14 +00:00
from .config import ProjectConfig
from .service import Service
from .services import Services
from .yaml import YAML
2021-12-02 16:12:30 +00:00
if TYPE_CHECKING:
from .instance import Instance
2021-12-02 16:08:14 +00:00
@attr.s
class Project:
directory: Path = attr.ib()
2022-01-27 14:26:10 +00:00
parent_instance: "Instance" = attr.ib()
2021-12-02 16:08:14 +00:00
@staticmethod
@functools.lru_cache(maxsize=10)
def _parse_compose_file(directory: Path) -> CommentedMap:
with open(directory.joinpath(COMPOSE_FILE_NAME), "r") as cf:
return YAML().load(cf)
@property
def name(self) -> str:
return self.directory.name
@property
def config(self) -> Optional[ProjectConfig]:
2022-01-27 14:26:10 +00:00
return self.parent_instance.config.get_project_config(self.name)
2021-12-02 16:08:14 +00:00
@property
def process_kwargs(self) -> Dict[str, Any]:
directory: Path = self.directory
project_name: str = self.name
2022-01-27 14:26:10 +00:00
kiwi_hub_name: str = self.parent_instance.config.network.name
2022-02-21 22:35:48 +00:00
kiwi_instance_dir: Path = self.parent_instance.config.storage.directory
kiwi_config_dir: Path = kiwi_instance_dir.joinpath(CONFIG_DIRECTORY_NAME)
kiwi_project_dir: Path = kiwi_instance_dir.joinpath(project_name)
2021-12-02 16:08:14 +00:00
result: Dict[str, Any] = {
"cwd": str(directory),
"env": {
"COMPOSE_PROJECT_NAME": project_name,
"KIWI_HUB_NAME": kiwi_hub_name,
2022-02-21 22:35:48 +00:00
"KIWI_INSTANCE": str(kiwi_instance_dir),
"KIWI_CONFIG": str(kiwi_config_dir),
"KIWI_PROJECT": str(kiwi_project_dir),
2021-12-02 16:08:14 +00:00
},
}
2022-01-27 14:26:10 +00:00
result["env"].update(self.parent_instance.config.environment)
2021-12-02 16:08:14 +00:00
return result
@property
def services(self) -> Services:
yml = Project._parse_compose_file(self.directory)
return Services([
Service(
name=name,
content=content,
2022-01-27 14:26:10 +00:00
parent_project=self,
2021-12-02 16:08:14 +00:00
) for name, content in yml["services"].items()
2021-12-02 16:12:30 +00:00
])