From 60259df4a2810ee6306a44002cbfede6a396f51f Mon Sep 17 00:00:00 2001 From: Mohamed Abdel Wedoud Date: Mon, 27 May 2024 02:08:21 +0200 Subject: [PATCH] feat(archive-api): replace "get_archive_path" using "find/create_archive_path" --- antarest/study/service.py | 4 +-- .../storage/rawstudy/raw_study_service.py | 25 ++++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/antarest/study/service.py b/antarest/study/service.py index 16ff611022..552ec2f9de 100644 --- a/antarest/study/service.py +++ b/antarest/study/service.py @@ -1142,7 +1142,7 @@ def delete_study(self, uuid: str, children: bool, params: RequestParameters) -> self.storage_service.get_storage(study).delete(study) else: if isinstance(study, RawStudy): - os.unlink(self.storage_service.raw_study_service.get_archive_path(study)) + os.unlink(self.storage_service.raw_study_service.find_archive_path(study)) logger.info("study %s deleted by user %s", uuid, params.get_user_id()) @@ -1956,7 +1956,7 @@ def unarchive_task(notifier: TaskUpdateNotifier) -> TaskResult: self.storage_service.raw_study_service.unarchive(study_to_archive) study_to_archive.archived = False - os.unlink(self.storage_service.raw_study_service.get_archive_path(study_to_archive)) + os.unlink(self.storage_service.raw_study_service.find_archive_path(study_to_archive)) self.repository.save(study_to_archive) self.event_bus.push( Event( diff --git a/antarest/study/storage/rawstudy/raw_study_service.py b/antarest/study/storage/rawstudy/raw_study_service.py index a88f82a3a8..ebb1272131 100644 --- a/antarest/study/storage/rawstudy/raw_study_service.py +++ b/antarest/study/storage/rawstudy/raw_study_service.py @@ -138,7 +138,7 @@ def exists(self, study: RawStudy) -> bool: Returns: true if study presents in disk, false else. """ if study.archived: - archive_path = self.get_archive_path(study) + archive_path = self.find_archive_path(study) return archive_path.is_file() path = self.get_study_path(study) @@ -373,7 +373,7 @@ def set_reference_output(self, study: RawStudy, output_id: str, status: bool) -> remove_from_cache(self.cache, study.id) def archive(self, study: RawStudy) -> Path: - archive_path = self.get_archive_path(study) + archive_path = self.create_archive_path(study) new_study_path = self.export_study(study, archive_path) shutil.rmtree(study.path) remove_from_cache(cache=self.cache, root_id=study.id) @@ -391,12 +391,12 @@ def unarchive(self, study: RawStudy) -> None: Raises: BadArchiveContent: If the archive is corrupted or in an unknown format. """ - with open(self.get_archive_path(study), mode="rb") as fh: + with open(self.find_archive_path(study), mode="rb") as fh: self.import_study(study, fh) - def get_archive_path(self, study: RawStudy) -> Path: + def find_archive_path(self, study: RawStudy) -> Path: """ - Get archive path of a study. + Fetch for archive path of a study if it exists else raise an incorrectly archived study. Args: study: The study to get the archive path for. @@ -409,6 +409,19 @@ def get_archive_path(self, study: RawStudy) -> Path: path = archive_dir.joinpath(f"{study.id}{suffix}") if path.is_file(): return path + raise FileNotFoundError(f"Study {study.id} archiving process is corrupted (no archive file found).") + + def create_archive_path(self, study: RawStudy) -> Path: + """ + Create archive path of a study. + + Args: + study: The study to get the archive path for. + + Returns: + The full path of the archive file (7z). + """ + archive_dir: Path = self.config.storage.archive_dir return archive_dir.joinpath(f"{study.id}.7z") def get_study_path(self, metadata: Study) -> Path: @@ -421,7 +434,7 @@ def get_study_path(self, metadata: Study) -> Path: """ if metadata.archived: - return self.get_archive_path(metadata) + return self.find_archive_path(metadata) return Path(metadata.path) def initialize_additional_data(self, raw_study: RawStudy) -> bool: