Skip to content

Commit

Permalink
feat(zip extractor): add support for -z option
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Oct 18, 2023
1 parent 516f94d commit 2b5d277
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
20 changes: 10 additions & 10 deletions antareslauncher/use_cases/retrieve/final_zip_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/retriever/test_final_zip_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2b5d277

Please sign in to comment.