Skip to content

Commit d3804fa

Browse files
committed
if files are in a workflow result, the files are copied to the workflow directory
1 parent 3cd9880 commit d3804fa

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

pydra/engine/helpers.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import subprocess as sp
1111

1212
from .specs import Runtime, File, attr_fields
13-
from .helpers_file import is_existing_file, hash_file
13+
from .helpers_file import is_existing_file, hash_file, copyfile, is_existing_file
1414

1515

1616
def ensure_list(obj, tuple2list=False):
@@ -112,13 +112,27 @@ def save(task_path: Path, result=None, task=None):
112112
raise ValueError("Nothing to be saved")
113113
task_path.mkdir(parents=True, exist_ok=True)
114114
if result:
115+
if Path(task_path).name.startswith("Workflow"):
116+
# copy files to the workflow directory
117+
result = copyfile_workflow(wf_path=task_path, result=result)
115118
with (task_path / "_result.pklz").open("wb") as fp:
116119
cp.dump(result, fp)
117120
if task:
118121
with (task_path / "_task.pklz").open("wb") as fp:
119122
cp.dump(task, fp)
120123

121124

125+
def copyfile_workflow(wf_path, result):
126+
""" if file in the wf results, the file will be copied to the workflow directory"""
127+
for field in attr_fields(result.output):
128+
value = getattr(result.output, field.name)
129+
if is_existing_file(value):
130+
new_path = wf_path / value.name
131+
copyfile(originalfile=value, newfile=new_path, copy=True, use_hardlink=True)
132+
setattr(result.output, field.name, new_path)
133+
return result
134+
135+
122136
def task_hash(task):
123137
"""
124138
Calculate the checksum of a task.

pydra/engine/helpers_file.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ def is_local_file(f):
516516

517517
def is_existing_file(f):
518518
""" checking if an object is an existing file"""
519+
if not f:
520+
return False
519521
try:
520522
return Path(f).exists()
521523
except TypeError:

pydra/engine/tests/test_shelltask.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,8 @@ def test_shell_cmd_outputspec_wf_1(plugin):
20182018
res = wf.result()
20192019
assert res.output.stdout == ""
20202020
assert res.output.newfile.exists()
2021+
# checking if the file was copied to the wf dir
2022+
assert res.output.newfile.parent == wf.output_dir
20212023

20222024

20232025
def no_fsl():

0 commit comments

Comments
 (0)