From 0547c662d809ce252a07c73b2f691c0ef5315ea2 Mon Sep 17 00:00:00 2001 From: Axel Farrugia <axel.farrugia@cea.fr> Date: Tue, 18 Feb 2025 11:28:54 +0100 Subject: [PATCH] [Feat](Exports) Add a copy_folder function and allow the copies to be done through simlinks --- aidge_core/export_utils/__init__.py | 2 +- aidge_core/export_utils/code_generation.py | 23 +++++++++++++++++++-- aidge_core/export_utils/export_registry.py | 3 +++ aidge_core/export_utils/scheduler_export.py | 8 +++++-- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/aidge_core/export_utils/__init__.py b/aidge_core/export_utils/__init__.py index 72472eee0..a14fc63b1 100644 --- a/aidge_core/export_utils/__init__.py +++ b/aidge_core/export_utils/__init__.py @@ -1,5 +1,5 @@ from .node_export import ExportNode, ExportNodeCpp -from .code_generation import generate_file, generate_str, copy_file +from .code_generation import generate_file, generate_str, copy_file, copy_folder from .export_registry import ExportLib from .scheduler_export import scheduler_export from .tensor_export import tensor_to_c, generate_input_file diff --git a/aidge_core/export_utils/code_generation.py b/aidge_core/export_utils/code_generation.py index 4f0f4634d..42ae19f79 100644 --- a/aidge_core/export_utils/code_generation.py +++ b/aidge_core/export_utils/code_generation.py @@ -44,10 +44,29 @@ def generate_str(template_path: Union[Path, str], **kwargs) -> str: return Environment(loader=FileSystemLoader( template_path.parent), undefined=StrictUndefined, keep_trailing_newline=True).get_template(template_path.name).render(kwargs) -def copy_file(filename, dst_folder): +def copy_file(filename, dst_folder, symlink=False): + """Copy the given file into the given dst path + The symlink arg allows to make a symbolic link instead of copying the file. + """ # If directory doesn't exist, create it if not os.path.exists(dst_folder): os.makedirs(dst_folder) - shutil.copy(filename, dst_folder) + if symlink: + dst_folder += "/" + os.path.basename(filename) + if not os.path.exists(dst_folder): + os.symlink(filename, dst_folder) + else: + shutil.copy(filename, dst_folder) + +def copy_folder(foldername, dst_folder, symlink=False): + """Copy the given folder into the given dst path + The symlink arg allows to make a symbolic link instead of copying the file. + """ + + if symlink: + os.symlink(foldername, dst_folder) + else: + shutil.copytree(foldername, dst_folder, dirs_exist_ok=True) + \ No newline at end of file diff --git a/aidge_core/export_utils/export_registry.py b/aidge_core/export_utils/export_registry.py index 8927ae516..01329e6a5 100644 --- a/aidge_core/export_utils/export_registry.py +++ b/aidge_core/export_utils/export_registry.py @@ -40,6 +40,9 @@ class ExportLib(aidge_core.OperatorImpl): # key: Path where static file is # Value: Path where to copy the file relative to the export root static_files: Dict[str, str] = {} + # key: Path where static folder is + # Value: Path where to copy the folder relative to the export root + static_folders: Dict[str, str] = {} # Main memory section mem_section = None # Custom forward generation jinja file diff --git a/aidge_core/export_utils/scheduler_export.py b/aidge_core/export_utils/scheduler_export.py index 8aaedc18d..aaca6b76a 100644 --- a/aidge_core/export_utils/scheduler_export.py +++ b/aidge_core/export_utils/scheduler_export.py @@ -2,7 +2,7 @@ import aidge_core import os import shutil from pathlib import Path -from aidge_core.export_utils import ExportLib, generate_file, copy_file +from aidge_core.export_utils import ExportLib, generate_file, copy_file, copy_folder from typing import List, Tuple @@ -208,4 +208,8 @@ def scheduler_export(scheduler, export_folder_path: str, export_lib: ExportLib = if export_lib is not None: # Copy all static files in the export for source, destination in export_lib.static_files.items(): - copy_file(source, str(export_folder / destination)) + copy_file(source, str(export_folder / destination), test_mode) + + # Copy all static folders in the export + for source, destination in export_lib.static_folders.items(): + copy_folder(source, str(export_folder / destination), test_mode) -- GitLab