Skip to content

Commit

Permalink
fix(service): adapt code and tests for new antares-launcher behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle authored and laurent-laporte-pro committed Dec 19, 2023
1 parent 7b843c3 commit 9e98ea3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
19 changes: 7 additions & 12 deletions antarest/launcher/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import shutil
import zipfile
from datetime import datetime, timedelta
from http import HTTPStatus
from pathlib import Path
Expand Down Expand Up @@ -522,6 +523,12 @@ def _import_output(
log_paths,
output_true_path / log_name,
)
if additional_logs and output_is_zipped:
with zipfile.ZipFile(output_true_path, "a") as zf:
for log_paths in additional_logs.values():
for path in log_paths:
dest_name = path.name[: path.name.rfind("-")] + ".log"
zf.write(filename=path, arcname=dest_name)

if study_id:
zip_path: Optional[Path] = None
Expand All @@ -535,18 +542,6 @@ def _import_output(
final_output_path = zip_path or output_true_path
with db():
try:
if additional_logs and output_is_zipped:
for log_name, log_paths in additional_logs.items():
log_type = LogType.from_filename(log_name)
log_suffix = log_name
if log_type:
log_suffix = log_type.to_suffix()
self.study_service.save_logs(
study_id,
job_id,
log_suffix,
concat_files_to_str(log_paths),
)
return self.study_service.import_output(
study_id,
final_output_path,
Expand Down
5 changes: 3 additions & 2 deletions antarest/study/storage/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ def fix_study_root(study_path: Path) -> None:

def retrieve_output_path(job_path: Path) -> Path:
inside_study_output_path = job_path / "output"
if job_path.exists() and len([file for file in os.listdir(str(job_path)) if ".zip" in file]) == 1:
return job_path / [file for file in os.listdir(str(job_path)) if ".zip" in file][0]
output_already_zipped_path = job_path.with_suffix(".zip")
if output_already_zipped_path.exists():
return output_already_zipped_path
elif inside_study_output_path.exists() and len(os.listdir(inside_study_output_path)) == 1:
return inside_study_output_path / os.listdir(str(inside_study_output_path))[0]
else:
Expand Down
8 changes: 3 additions & 5 deletions tests/launcher/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,17 +721,15 @@ def test_manage_output(self, tmp_path: Path) -> None:
)

output_path = tmp_path / "output"
zipped_output_path = tmp_path / "zipped_output"
os.mkdir(output_path)
os.mkdir(zipped_output_path)
new_output_path = output_path / "new_output"
os.mkdir(new_output_path)
(new_output_path / "log").touch()
(new_output_path / "data").touch()
additional_log = tmp_path / "output.log"
additional_log.write_text("some log")
new_output_zipped_path = zipped_output_path / "test.zip"
with ZipFile(new_output_zipped_path, "w", ZIP_DEFLATED) as output_data:
zipped_path = tmp_path / "test.zip"
with ZipFile(zipped_path, "w", ZIP_DEFLATED) as output_data:
output_data.writestr("some output", "0\n1")
job_id = "job_id"
zipped_job_id = "zipped_job_id"
Expand Down Expand Up @@ -768,7 +766,7 @@ def test_manage_output(self, tmp_path: Path) -> None:

launcher_service._import_output(
zipped_job_id,
zipped_output_path,
zipped_path,
{
"out.log": [additional_log],
"antares-out": [additional_log],
Expand Down
4 changes: 3 additions & 1 deletion tests/launcher/test_slurm_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,9 @@ def test_import_study_output(launcher_config, tmp_path) -> None:
xpansion_zip_dir = launcher_config.launcher.slurm.local_workspace / "OUTPUT" / "2"
xpansion_zip_dir.mkdir(parents=True)
(xpansion_zip_dir / "input" / "links").mkdir(parents=True)
xpansion_output_file = xpansion_zip_dir / "xpansion.zip"
xpansion_out_put_dir = xpansion_zip_dir / "output"
xpansion_out_put_dir.mkdir(parents=True)
xpansion_output_file = xpansion_out_put_dir / "xpansion.zip"
with zipfile.ZipFile(xpansion_output_file, "w") as zipf:
zipf.write(xpansion_dir / "something_else", "some_file.txt")
slurm_launcher._import_study_output("2", "cpp")
Expand Down

0 comments on commit 9e98ea3

Please sign in to comment.