diff --git a/antarest/study/service.py b/antarest/study/service.py index 873f2dc583..b7f772bb7d 100644 --- a/antarest/study/service.py +++ b/antarest/study/service.py @@ -166,6 +166,7 @@ remove_from_cache, study_matcher, ) +from antarest.study.storage.abstract_storage_service import export_study_flat from antarest.study.storage.variantstudy.model.command.icommand import ICommand from antarest.study.storage.variantstudy.model.command.replace_matrix import ( ReplaceMatrix, @@ -1221,9 +1222,7 @@ def export_study_flat( if study.archived: self.storage_service.get_storage(study).unarchive(study) try: - return self.storage_service.get_storage( - study - ).export_study_flat( + return export_study_flat( path_study=path_study, dest=dest, outputs=len(output_list or []) > 0, @@ -1234,7 +1233,7 @@ def export_study_flat( shutil.rmtree(study.path) snapshot_path = path_study / "snapshot" output_src_path = path_study / "output" - self.storage_service.get_storage(study).export_study_flat( + export_study_flat( path_study=snapshot_path, dest=dest, outputs=len(output_list or []) > 0, diff --git a/antarest/study/storage/abstract_storage_service.py b/antarest/study/storage/abstract_storage_service.py index bcb397c55c..69f3156755 100644 --- a/antarest/study/storage/abstract_storage_service.py +++ b/antarest/study/storage/abstract_storage_service.py @@ -53,6 +53,61 @@ logger = logging.getLogger(__name__) +def export_study_flat( + path_study: Path, + dest: Path, + outputs: bool = True, + output_list_filter: Optional[List[str]] = None, + output_src_path: Optional[Path] = None, +) -> None: + """ + Export study to destination + + Args: + path_study: Study source path + dest: Destination path. + outputs: List of outputs to keep. + output_list_filter: List of outputs to keep (None indicate all outputs). + output_src_path: Denormalize the study (replace matrix links by real matrices). + + """ + start_time = time.time() + output_src_path = output_src_path or path_study / "output" + output_dest_path = dest / "output" + ignore_patterns = ( + lambda directory, contents: ["output"] + if str(directory) == str(path_study) + else [] + ) + + shutil.copytree(src=path_study, dst=dest, ignore=ignore_patterns) + + if outputs and output_src_path.is_dir(): + if output_dest_path.exists(): + shutil.rmtree(output_dest_path) + if output_list_filter is not None: + os.mkdir(output_dest_path) + for output in output_list_filter: + zip_path = output_src_path / f"{output}.zip" + if zip_path.exists(): + with ZipFile(zip_path) as zf: + zf.extractall(output_dest_path / output) + else: + shutil.copytree( + src=output_src_path / output, + dst=output_dest_path / output, + ) + else: + shutil.copytree( + src=output_src_path, + dst=output_dest_path, + ) + + stop_time = time.time() + duration = "{:.3f}".format(stop_time - start_time) + logger.info(f"Study {path_study} exported (flat mode) in {duration}s") + + class AbstractStorageService(IStudyStorageService[T], ABC): def __init__( self, @@ -279,13 +334,13 @@ def export_study( if metadata.type != "rawstudy": snapshot_path = path_study / "snapshot" output_src_path = path_study / "output" - self.export_study_flat( + export_study_flat( path_study=snapshot_path, dest=tmp_study_path, outputs=outputs, output_src_path=output_src_path, ) - self.export_study_flat(path_study, tmp_study_path, outputs) + export_study_flat(path_study, tmp_study_path, outputs) stopwatch = StopWatch() zip_dir(tmp_study_path, target) stopwatch.log_elapsed( @@ -382,59 +437,3 @@ def unarchive_study_output( exc_info=e, ) return False - - def export_study_flat( - self, - path_study: Path, - dest: Path, - outputs: bool = True, - output_list_filter: Optional[List[str]] = None, - output_src_path: Optional[Path] = None, - ) -> None: - """ - Export study to destination - Args: - path_study: Study source path - dest: Destination path. - outputs: List of outputs to keep. - output_list_filter: List of outputs to keep (None indicate all outputs). - output_src_path: Denormalize the study (replace matrix links by real matrices). - - Returns: None - - """ - start_time = time.time() - output_src_path = output_src_path or path_study / "output" - output_dest_path = dest / "output" - ignore_patterns = ( - lambda directory, contents: ["output"] - if str(directory) == str(path_study) - else [] - ) - - shutil.copytree(src=path_study, dst=dest, ignore=ignore_patterns) - - if outputs and output_src_path.is_dir(): - if output_dest_path.exists(): - shutil.rmtree(output_dest_path) - if output_list_filter is not None: - os.mkdir(output_dest_path) - for output in output_list_filter: - zip_path = output_src_path / f"{output}.zip" - if zip_path.exists(): - with ZipFile(zip_path) as zf: - zf.extractall(output_dest_path / output) - else: - shutil.copytree( - src=output_src_path / output, - dst=output_dest_path / output, - ) - else: - shutil.copytree( - src=output_src_path, - dst=output_dest_path, - ) - - stop_time = time.time() - duration = "{:.3f}".format(stop_time - start_time) - logger.info(f"Study {path_study} exported (flat mode) in {duration}s") diff --git a/antarest/study/storage/variantstudy/variant_study_service.py b/antarest/study/storage/variantstudy/variant_study_service.py index b889f8aef0..9d8ea14dfe 100644 --- a/antarest/study/storage/variantstudy/variant_study_service.py +++ b/antarest/study/storage/variantstudy/variant_study_service.py @@ -57,6 +57,7 @@ ) from antarest.study.storage.abstract_storage_service import ( AbstractStorageService, + export_study_flat, ) from antarest.study.storage.patch_service import PatchService from antarest.study.storage.rawstudy.model.filesystem.config.model import ( @@ -79,9 +80,8 @@ ) from antarest.study.storage.variantstudy.command_factory import CommandFactory from antarest.study.storage.variantstudy.model.command.icommand import ICommand -from antarest.study.storage.variantstudy.model.command.update_config import ( - UpdateConfig, -) + + from antarest.study.storage.variantstudy.model.dbmodel import ( CommandBlock, VariantStudy, @@ -843,7 +843,7 @@ def _generate( path_study = Path(parent_study.path) snapshot_path = path_study / SNAPSHOT_RELATIVE_PATH output_src_path = path_study / "output" - self.export_study_flat( + export_study_flat( snapshot_path, dst_path, outputs=False, @@ -854,7 +854,7 @@ def _generate( if parent_study.archived: self.raw_study_service.unarchive(parent_study) try: - self.raw_study_service.export_study_flat( + export_study_flat( path_study=path_study, dest=dst_path, outputs=False,