Skip to content

Commit

Permalink
Eagerly create softpack.yml when build queuedi
Browse files Browse the repository at this point in the history
  • Loading branch information
sersorrel committed Jan 26, 2024
1 parent e36a9f3 commit 5fc386a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
25 changes: 19 additions & 6 deletions softpack_core/schemas/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
],
},
},
)
Expand Down Expand Up @@ -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"
Expand Down
34 changes: 29 additions & 5 deletions tests/integration/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import pygit2
import pytest
import yaml
from fastapi import UploadFile

from softpack_core.artifacts import Artifacts, app
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
],
},
},
Expand Down
12 changes: 7 additions & 5 deletions tests/integration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5fc386a

Please sign in to comment.