From 5fc386a69d303275314dbe0ef204b3203604fa2c Mon Sep 17 00:00:00 2001 From: ash Date: Fri, 26 Jan 2024 11:22:41 +0000 Subject: [PATCH] Eagerly create softpack.yml when build queuedi --- softpack_core/schemas/environment.py | 25 +++++++++++++++----- tests/integration/test_environment.py | 34 +++++++++++++++++++++++---- tests/integration/utils.py | 12 ++++++---- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/softpack_core/schemas/environment.py b/softpack_core/schemas/environment.py index fd2fd51..721ce08 100644 --- a/softpack_core/schemas/environment.py +++ b/softpack_core/schemas/environment.py @@ -15,6 +15,7 @@ import strawberry from fastapi import UploadFile from strawberry.file_uploads import Upload +import yaml from softpack_core.app import app from softpack_core.artifacts import Artifacts, Package, State @@ -269,10 +270,13 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore "version": str(version), "model": { "description": env.description, - "packages": [{ - "name": pkg.name, - "version": pkg.version, - } for pkg in env.packages], + "packages": [ + { + "name": pkg.name, + "version": pkg.version, + } + for pkg in env.packages + ], }, }, ) @@ -327,8 +331,17 @@ def create_new_env( # Create folder with place-holder file new_folder_path = Path(env.path, env.name) try: - tree_oid = cls.artifacts.create_file( - new_folder_path, env_type, "", True + softpack_definition = dict( + description = env.description, + packages = [pkg.name + ("@" + pkg.version if pkg.version else "") for pkg in env.packages] + ) + ymlData = yaml.dump(softpack_definition) + + tree_oid = cls.artifacts.create_files( + new_folder_path, [ + (env_type, ""), # e.g. .built_by_softpack + (cls.artifacts.environments_file, ymlData) # softpack.yml + ], True ) cls.artifacts.commit_and_push( tree_oid, "create environment folder" diff --git a/tests/integration/test_environment.py b/tests/integration/test_environment.py index d096edd..9d686fd 100644 --- a/tests/integration/test_environment.py +++ b/tests/integration/test_environment.py @@ -10,6 +10,7 @@ import pygit2 import pytest +import yaml from fastapi import UploadFile from softpack_core.artifacts import Artifacts, app @@ -42,19 +43,41 @@ def test_create(httpx_post, testable_env_input: EnvironmentInput) -> None: name="test_env_create2", path="groups/not_already_in_repo", description="description2", - packages=[Package(name="pkg_test2")], + packages=[Package(name="pkg_test2"), Package(name="pkg_test3", version="3.1")], ) ) assert isinstance(result, CreateEnvironmentSuccess) httpx_post.assert_called() - path = Path( + dir = Path( Environment.artifacts.environments_root, testable_env_input.path, testable_env_input.name + "-1", - Environment.artifacts.built_by_softpack_file, ) - assert file_in_remote(path) + builtPath = dir / Environment.artifacts.built_by_softpack_file + ymlPath = dir / Environment.artifacts.environments_file + assert file_in_remote(builtPath) + ymlFile = file_in_remote(ymlPath) + expected_yaml = { + "description": "description", + "packages": ["pkg_test"], + } + assert yaml.safe_load(ymlFile.data.decode()) == expected_yaml + + dir = Path( + Environment.artifacts.environments_root, + "groups/not_already_in_repo", + "test_env_create2-1" + ) + builtPath = dir / Environment.artifacts.built_by_softpack_file + ymlPath = dir / Environment.artifacts.environments_file + assert file_in_remote(builtPath) + ymlFile = file_in_remote(ymlPath) + expected_yaml = { + "description": "description2", + "packages": ["pkg_test2", "pkg_test3@3.1"], + } + assert yaml.safe_load(ymlFile.data.decode()) == expected_yaml result = Environment.create(testable_env_input) assert isinstance(result, CreateEnvironmentSuccess) @@ -93,7 +116,8 @@ def builder_called_correctly( { "name": pkg.name, "version": pkg.version, - } for pkg in testable_env_input.packages + } + for pkg in testable_env_input.packages ], }, }, diff --git a/tests/integration/utils.py b/tests/integration/utils.py index 6948588..49ee51c 100644 --- a/tests/integration/utils.py +++ b/tests/integration/utils.py @@ -171,25 +171,27 @@ def get_user_path_without_environments( return Path(*(artifacts.user_folder(user).parts[1:])) -def file_in_remote(*paths_with_environment: Union[str, Path]) -> bool: +def file_in_remote(*paths_with_environment: Union[str, Path]) -> Union[pygit2.Tree, pygit2.Blob]: temp_dir = tempfile.TemporaryDirectory() app.settings.artifacts.path = Path(temp_dir.name) artifacts = Artifacts() + file = None for path_with_environment in paths_with_environment: path = Path(path_with_environment) - if not file_in_repo(artifacts, path): + file = file_in_repo(artifacts, path) + if not file: return False - return True + return file -def file_in_repo(artifacts: Artifacts, path: Path) -> bool: +def file_in_repo(artifacts: Artifacts, path: Path) -> Union[pygit2.Tree, pygit2.Blob]: current = artifacts.repo.head.peel(pygit2.Tree) for part in path.parts: if part not in current: return False current = current[part] - return True + return current