diff --git a/antareslauncher/use_cases/retrieve/final_zip_extractor.py b/antareslauncher/use_cases/retrieve/final_zip_extractor.py index 81d02cb..a9136c7 100644 --- a/antareslauncher/use_cases/retrieve/final_zip_extractor.py +++ b/antareslauncher/use_cases/retrieve/final_zip_extractor.py @@ -29,17 +29,17 @@ def extract_final_zip(self, study: StudyDTO) -> None: # If all files are in the same directory, we can extract the ZIP # file directly in the target directory. target_dir = zip_path.parent + progress_bar = self._display.generate_progress_bar( + names, desc="Extracting archive:", total=len(names) + ) + for file in progress_bar: + zf.extract(member=file, path=target_dir) else: - # Otherwise, we need to create a directory to store the results. - # This situation occurs when the ZIP file contains - # only the simulation results and not the entire study. - target_dir = zip_path.with_suffix("") - - progress_bar = self._display.generate_progress_bar( - names, desc="Extracting archive:", total=len(names) - ) - for file in progress_bar: - zf.extract(member=file, path=target_dir) + # The directory is already an output and does not need to be unzipped. + # All we have to do is rename it by removing the prefix "_finished" + # and the suffix "job_id" that lies before the ".zip". + # If these prefix/suffix prefix change, this code needs to be adapted. + zip_path.rename((zip_path.parent / (zip_path.name[9:zip_path.name.rfind("_")] + ".zip"))) except (OSError, zipfile.BadZipFile) as exc: # If we cannot extract the final ZIP file, either because the file diff --git a/tests/unit/retriever/test_final_zip_extractor.py b/tests/unit/retriever/test_final_zip_extractor.py index 7817dc0..86159a3 100644 --- a/tests/unit/retriever/test_final_zip_extractor.py +++ b/tests/unit/retriever/test_final_zip_extractor.py @@ -131,8 +131,10 @@ def test_extract_final_zip__finished_study__nominal_results(self, finished_study assert finished_study.final_zip_extracted assert not finished_study.with_error - result_dir = Path(finished_study.local_final_zipfile_path).with_suffix("") - assert result_dir.joinpath("simulation.log").is_file() + result_dir = Path(Path(finished_study.local_final_zipfile_path).parent / finished_study.name).with_suffix(".zip") + assert result_dir.exists() + with zipfile.ZipFile(result_dir, "r") as zf: + assert zf.namelist() == ["simulation.log"] @pytest.mark.unit_test def test_extract_final_zip__finished_study__reentrancy(self, finished_study: StudyDTO) -> None: