Skip to content

Commit

Permalink
feat(export): export using "7zip" instead of "zip"
Browse files Browse the repository at this point in the history
  • Loading branch information
mabw-rte committed Apr 18, 2024
1 parent 42062f4 commit 6220f9f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 11 additions & 0 deletions antarest/core/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import py7zr
import redis
from py7zr import SevenZipFile

from antarest.core.config import RedisConfig
from antarest.core.exceptions import ShouldNotHappenException
Expand Down Expand Up @@ -180,6 +181,16 @@ def zip_dir(dir_path: Path, zip_path: Path, remove_source_dir: bool = False) ->
shutil.rmtree(dir_path)


def seven_zip_dir(dir_path: Path, seven_zip_path: Path, remove_source_dir: bool = False) -> None:
with SevenZipFile(seven_zip_path, "w") as szf:
for root, _, files in os.walk(dir_path):
for file in files:
file_path = os.path.join(root, file)
szf.write(file_path, arcname=os.path.relpath(file_path, dir_path))
if remove_source_dir:
shutil.rmtree(dir_path)


def unzip(dir_path: Path, zip_path: Path, remove_source_zip: bool = False) -> None:
with zipfile.ZipFile(zip_path, mode="r") as zipf:
zipf.extractall(dir_path)
Expand Down
10 changes: 5 additions & 5 deletions antarest/study/storage/abstract_storage_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from antarest.core.exceptions import BadOutputError, StudyOutputNotFoundError
from antarest.core.interfaces.cache import CacheConstants, ICache
from antarest.core.model import JSON, PublicMode
from antarest.core.utils.utils import StopWatch, extract_zip, unzip, zip_dir
from antarest.core.utils.utils import StopWatch, extract_zip, seven_zip_dir, unzip, zip_dir
from antarest.login.model import GroupDTO
from antarest.study.common.studystorage import IStudyStorageService, T
from antarest.study.model import (
Expand Down Expand Up @@ -267,24 +267,24 @@ def import_output(

def export_study(self, metadata: T, target: Path, outputs: bool = True) -> Path:
"""
Export and compress the study inside a ZIP file.
Export and compress the study inside a 7zip file.
Args:
metadata: Study metadata object.
target: Path of the file to export to.
outputs: Flag to indicate whether to include the output folder inside the exportation.
Returns:
The ZIP file containing the study files compressed inside.
The 7zip file containing the study files compressed inside.
"""
path_study = Path(metadata.path)
with tempfile.TemporaryDirectory(dir=self.config.storage.tmp_dir) as tmpdir:
logger.info(f"Exporting study {metadata.id} to temporary path {tmpdir}")
tmp_study_path = Path(tmpdir) / "tmp_copy"
self.export_study_flat(metadata, tmp_study_path, outputs)
stopwatch = StopWatch()
zip_dir(tmp_study_path, target)
stopwatch.log_elapsed(lambda x: logger.info(f"Study {path_study} exported (zipped mode) in {x}s"))
seven_zip_dir(tmp_study_path, target)
stopwatch.log_elapsed(lambda x: logger.info(f"Study {path_study} exported (7zip mode) in {x}s"))
return target

def export_output(self, metadata: T, output_id: str, target: Path) -> None:
Expand Down
1 change: 1 addition & 0 deletions antarest/study/storage/study_upgrader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path

from antarest.core.exceptions import StudyValidationError

from .upgrader_710 import upgrade_710
from .upgrader_720 import upgrade_720
from .upgrader_800 import upgrade_800
Expand Down

0 comments on commit 6220f9f

Please sign in to comment.