Skip to content

Commit

Permalink
Refactor(export) : add a litle refactoring (#1646)
Browse files Browse the repository at this point in the history
  • Loading branch information
TLAIDI committed Jul 21, 2023
1 parent e4290a8 commit 0154d6b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 67 deletions.
7 changes: 3 additions & 4 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
115 changes: 57 additions & 58 deletions antarest/study/storage/abstract_storage_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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")
10 changes: 5 additions & 5 deletions antarest/study/storage/variantstudy/variant_study_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 0154d6b

Please sign in to comment.