From cbe4e422e06ad1e6cdfb50e61f8c00a9227b0e74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Thu, 27 Jan 2022 15:26:10 +0100 Subject: [PATCH] renamed generic "parent" props --- kiwi_scp/instance.py | 2 +- kiwi_scp/project.py | 12 ++++++------ kiwi_scp/service.py | 9 +++++++-- kiwi_scp/services.py | 35 +++++++++++++++++++++++++---------- tests/test_project.py | 4 ++-- tests/test_service.py | 8 ++++---- tests/test_services.py | 2 +- 7 files changed, 46 insertions(+), 26 deletions(-) diff --git a/kiwi_scp/instance.py b/kiwi_scp/instance.py index 9f41977..6799280 100644 --- a/kiwi_scp/instance.py +++ b/kiwi_scp/instance.py @@ -27,7 +27,7 @@ class Instance: for project in self.config.projects: yield Project( directory=self.directory.joinpath(project.name), - parent=self, + parent_instance=self, ) def get_projects(self, project_names: Sequence[str]) -> Dict[str, Project]: diff --git a/kiwi_scp/project.py b/kiwi_scp/project.py index 5b9515d..5f7bca9 100644 --- a/kiwi_scp/project.py +++ b/kiwi_scp/project.py @@ -18,7 +18,7 @@ if TYPE_CHECKING: @attr.s class Project: directory: Path = attr.ib() - parent: "Instance" = attr.ib() + parent_instance: "Instance" = attr.ib() @staticmethod @functools.lru_cache(maxsize=10) @@ -32,14 +32,14 @@ class Project: @property def config(self) -> Optional[ProjectConfig]: - return self.parent.config.get_project_config(self.name) + return self.parent_instance.config.get_project_config(self.name) @property def process_kwargs(self) -> Dict[str, Any]: directory: Path = self.directory project_name: str = self.name - kiwi_hub_name: str = self.parent.config.network.name - target_root_dir: Path = self.parent.config.storage.directory + kiwi_hub_name: str = self.parent_instance.config.network.name + target_root_dir: Path = self.parent_instance.config.storage.directory conf_dir: Path = target_root_dir.joinpath(CONF_DIRECTORY_NAME) target_dir: Path = target_root_dir.joinpath(project_name) @@ -54,7 +54,7 @@ class Project: }, } - result["env"].update(self.parent.config.environment) + result["env"].update(self.parent_instance.config.environment) return result @@ -66,6 +66,6 @@ class Project: Service( name=name, content=content, - parent=self, + parent_project=self, ) for name, content in yml["services"].items() ]) diff --git a/kiwi_scp/service.py b/kiwi_scp/service.py index 243c2a6..2631f34 100644 --- a/kiwi_scp/service.py +++ b/kiwi_scp/service.py @@ -11,6 +11,7 @@ from ruamel.yaml import CommentedMap from .executable import COMPOSE_EXE if TYPE_CHECKING: + from .instance import Instance from .project import Project _logger = logging.getLogger(__name__) @@ -20,10 +21,14 @@ _logger = logging.getLogger(__name__) class Service: name: str = attr.ib() content: CommentedMap = attr.ib() - parent: "Project" = attr.ib() + parent_project: "Project" = attr.ib() _RE_CONFIGDIR = re.compile(r"^\s*\$(?:CONFIGDIR|{CONFIGDIR})/+(.*)$", flags=re.UNICODE) + @property + def parent_instance(self) -> "Instance": + return self.parent_project.parent_instance + @property def configs(self) -> Generator[Path, None, None]: if "volumes" not in self.content: @@ -42,7 +47,7 @@ class Service: 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, + **self.parent_project.process_kwargs, ) return True diff --git a/kiwi_scp/services.py b/kiwi_scp/services.py index 0d6759b..7cacab5 100644 --- a/kiwi_scp/services.py +++ b/kiwi_scp/services.py @@ -1,14 +1,18 @@ -from typing import List, Generator +from pathlib import Path +from typing import List, Generator, Optional, TYPE_CHECKING import attr -from .service import Service from .yaml import YAML +if TYPE_CHECKING: + from .instance import Instance + from .service import Service + @attr.s class Services: - content: List[Service] = attr.ib() + content: List["Service"] = attr.ib() def __str__(self) -> str: return YAML().dump({ @@ -21,14 +25,25 @@ class Services: def __bool__(self) -> bool: return bool(self.content) - # def copy_configs(self): - # configs = ( - # config - # for service in self.content - # for config in service.configs - # ) + @property + def parent_instance(self) -> Optional["Instance"]: + if not self: + return + + return self.content[0].parent_instance + + @property + def configs(self) -> Generator[Path, None, None]: + for service in self.content: + yield from service.configs + + # def copy_configs(self) -> None: + # instance = self.parent_instance # - # print(list(configs)) + # if instance is None: + # return + # + # print(list(self.configs)) # # # Rootkit("rsync"). diff --git a/tests/test_project.py b/tests/test_project.py index 6247950..79531ba 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -13,7 +13,7 @@ class TestDefault: def test_example(self): p = Project( directory=Path("example/hello-world.project"), - parent=None, + parent_instance=None, ) ss = p.services @@ -31,7 +31,7 @@ class TestDefault: def test_empty(self): p = Project( directory=Path("nonexistent"), - parent=None, + parent_instance=None, ) with pytest.raises(FileNotFoundError) as exc_info: diff --git a/tests/test_service.py b/tests/test_service.py index cf82dfe..064edad 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -10,7 +10,7 @@ class TestDefault: s = Service( name="s", content=CommentedMap(), - parent=None, + parent_project=None, ) assert s.name == "s" @@ -22,7 +22,7 @@ class TestDefault: content=CommentedMap({ "image": "repo/image:tag", }), - parent=None, + parent_project=None, ) assert s.name == "s" @@ -39,7 +39,7 @@ class TestDefault: "$TARGETDIR/other/dir:/path/to/other/mountpoint", ] }), - parent=None, + parent_project=None, ) assert s.name == "s" @@ -55,7 +55,7 @@ class TestDefault: "$CONFIGDIR/other/config:/path/to/other/config", ] }), - parent=None, + parent_project=None, ) assert s.name == "s" diff --git a/tests/test_services.py b/tests/test_services.py index 151d242..686d6e0 100644 --- a/tests/test_services.py +++ b/tests/test_services.py @@ -9,7 +9,7 @@ class TestServices: s = Service( name="s", content=CommentedMap(), - parent=None, + parent_project=None, ) ss = Services([s])