Skip to content

Commit

Permalink
feat: add optional followlinks to datasamples spec (#426)
Browse files Browse the repository at this point in the history
Signed-off-by: ThibaultFy <[email protected]>
  • Loading branch information
ThibaultFy authored Aug 21, 2024
1 parent a7a2631 commit ce8452a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
13 changes: 12 additions & 1 deletion substra/sdk/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import enum
import json
import logging
import pathlib
import typing
import uuid
Expand All @@ -14,6 +15,8 @@

from substra.sdk import utils

logger = logging.getLogger(__name__)

_SERVER_NAMES = {
"dataset": "data_manager",
"summary_task": "task",
Expand Down Expand Up @@ -142,11 +145,14 @@ class DataSampleSpec(_Spec):
"""Specification to create one or many data samples
To create one data sample, use the 'path' field, otherwise use
the 'paths' field.
Use 'followlinks' to follow symbolic links recursively. Note that it will lead to infinite
loops if a symbolic link points to a parent directory.
"""

path: Optional[pathlib.Path] = None # Path to the data sample if only one
paths: Optional[List[pathlib.Path]] = None # Path to the data samples if several
data_manager_keys: typing.List[str]
followlinks: Optional[bool] = False

type_: typing.ClassVar[Type] = Type.DataSample

Expand Down Expand Up @@ -186,8 +192,13 @@ def build_request_kwargs(self, local):
# redefine kwargs builder to handle the local paths
# Serialize and deserialize to prevent errors eg with pathlib.Path
data = json.loads(self.model_dump_json(exclude_unset=True))
if self.followlinks:
logger.warning(
"The 'followlinks' option is enabled for your datasample registration. It may lead to infinite loops "
"if a symbolic link points to a parent directory."
)
if local:
with utils.extract_data_sample_files(data) as (data, files):
with utils.extract_data_sample_files(data, followlinks=self.followlinks) as (data, files):
yield (data, files)
else:
yield data, None
Expand Down
12 changes: 6 additions & 6 deletions substra/sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,25 @@ def extract_files(data, file_attributes):
f.close()


def zip_folder(fp, path):
def zip_folder(fp, path, followlinks=False):
zipf = zipfile.ZipFile(fp, "w", zipfile.ZIP_DEFLATED)
for root, _, files in os.walk(path):
for root, _, files in os.walk(path, followlinks=followlinks):
for f in files:
abspath = os.path.join(root, f)
archive_path = os.path.relpath(abspath, start=path)
zipf.write(abspath, arcname=archive_path)
zipf.close()


def zip_folder_in_memory(path):
def zip_folder_in_memory(path, followlinks=False):
fp = io.BytesIO()
zip_folder(fp, path)
zip_folder(fp, path, followlinks=followlinks)
fp.seek(0)
return fp


@contextlib.contextmanager
def extract_data_sample_files(data):
def extract_data_sample_files(data, followlinks=False):
# handle data sample specific case; paths and path cases
data = copy.deepcopy(data)

Expand All @@ -81,7 +81,7 @@ def extract_data_sample_files(data):
for k, f in folders.items():
if not os.path.isdir(f):
raise exceptions.LoadDataException(f"Paths '{f}' is not an existing directory")
files[k] = zip_folder_in_memory(f)
files[k] = zip_folder_in_memory(f, followlinks=followlinks)

try:
yield (data, files)
Expand Down

0 comments on commit ce8452a

Please sign in to comment.