Skip to content

Commit

Permalink
Delint'd.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjkw31 committed Nov 8, 2024
1 parent 90a8331 commit 43166cc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
29 changes: 23 additions & 6 deletions softpack_core/schemas/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class InvalidInputError(Error):
"""Invalid input data."""


@strawberry.type
class WriteArtifactFailure(Error):
"""Artifact failed to be created."""


@strawberry.type
class EnvironmentNotFoundError(Error):
"""Environment not found."""
Expand Down Expand Up @@ -344,20 +349,27 @@ class Environment:

@classmethod
def init(cls, branch: str | None) -> None:
"""Initialises Environment."""
artifacts.clone_repo(branch)
cls.load_initial_environments()

@classmethod
def load_initial_environments(cls) -> None:
"""Loads the environments from the repo."""
environment_folders = artifacts.iter()
cls.environments = list(
filter(None, map(cls.from_artifact, environment_folders))
)

cls.environments.sort(key=lambda x: x.full_path())

def full_path(cls) -> Path:
"""Return a Path containing the file path and name."""
return Path(cls.path, cls.name)

@classmethod
def iter(cls) -> list["Environment"]:
"""Return a list of all Enviroments."""
return cls.environments

def has_requested_recipes(self) -> bool:
Expand Down Expand Up @@ -461,7 +473,11 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore
if not isinstance(response, CreateEnvironmentSuccess):
return response

cls.insert_new_env(Environment.get_env(env.path, env.name))
environment = Environment.get_env(Path(env.path), env.name)
if environment is not None:
cls.insert_new_env(environment)
else:
return EnvironmentNotFoundError(path=env.path, name=env.name)

response = cls.submit_env_to_builder(env)
if response is not None:
Expand All @@ -473,6 +489,7 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore

@classmethod
def insert_new_env(cls, env: "Environment") -> None:
"""Inserts an enviroment into the correct, sorted position."""
bisect.insort(
Environment.environments, env, key=lambda x: x.full_path()
)
Expand Down Expand Up @@ -665,6 +682,8 @@ async def add_tag(
if isinstance(response, WriteArtifactSuccess):
return AddTagSuccess(message="Tag successfully added")

return WriteArtifactFailure

@classmethod
def read_metadata(cls, path: str, name: str) -> Box:
"""Read an environments metadata.
Expand All @@ -689,7 +708,7 @@ async def store_metadata(
environment path given.
"""
return await Environment.write_artifacts(
environment_path,
str(environment_path),
[(artifacts.meta_file, metadata.to_yaml())],
"update metadata",
)
Expand Down Expand Up @@ -953,7 +972,8 @@ async def write_artifacts(
)

@classmethod
def env_index_from_path(cls, folder_path: str) -> Optional["Environment"]:
def env_index_from_path(cls, folder_path: str) -> Optional[int]:
"""Return the index of a folder_path from the list of environments."""
return next(
(
i
Expand All @@ -963,9 +983,6 @@ def env_index_from_path(cls, folder_path: str) -> Optional["Environment"]:
None,
)

def full_path(cls):
return Path(cls.path, cls.name)

@classmethod
async def update_from_module(
cls, file: Upload, module_path: str, environment_path: str
Expand Down
3 changes: 2 additions & 1 deletion softpack_core/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ async def fulfil_recipe( # type: ignore[no-untyped-def]
continue

await Environment.write_artifacts(
Path(env.path, env.name),
str(Path(env.path, env.name)),
[
(
Artifacts.environments_file,
Expand Down Expand Up @@ -426,6 +426,7 @@ async def recipe_description( # type: ignore[no-untyped-def]
async def buildStatus( # type: ignore[no-untyped-def]
request: Request,
):
"""Return the avg wait seconds and a map of names to build status."""
statuses = BuildStatus.get_all()
if isinstance(statuses, BuilderError):
statuses = []
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ async def test_create(
Environment.get_env(
testable_env_input.path, testable_env_input.name + "-3"
)
== None
is None
)

result = Environment.create(testable_env_input)
Expand Down

0 comments on commit 43166cc

Please sign in to comment.