From 43166cc4a581611b9f13639e831005bb7075bd69 Mon Sep 17 00:00:00 2001 From: Michael Woolnough Date: Fri, 8 Nov 2024 10:54:09 +0000 Subject: [PATCH] Delint'd. --- softpack_core/schemas/environment.py | 29 +++++++++++++++++++++------ softpack_core/service.py | 3 ++- tests/integration/test_environment.py | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/softpack_core/schemas/environment.py b/softpack_core/schemas/environment.py index 1039570..034a70b 100644 --- a/softpack_core/schemas/environment.py +++ b/softpack_core/schemas/environment.py @@ -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.""" @@ -344,11 +349,13 @@ 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)) @@ -356,8 +363,13 @@ def load_initial_environments(cls) -> None: 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: @@ -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: @@ -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() ) @@ -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. @@ -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", ) @@ -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 @@ -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 diff --git a/softpack_core/service.py b/softpack_core/service.py index 2d82c7f..538ce87 100644 --- a/softpack_core/service.py +++ b/softpack_core/service.py @@ -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, @@ -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 = [] diff --git a/tests/integration/test_environment.py b/tests/integration/test_environment.py index 25b1d17..38dcdfc 100644 --- a/tests/integration/test_environment.py +++ b/tests/integration/test_environment.py @@ -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)