Skip to content

Commit ca7dd3a

Browse files
authored
Separate dir creation from compilation (PolusAI#293)
* Separate dir creation from compilation * Fix lints and update test * Minor updates
1 parent 060a85b commit ca7dd3a

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

src/sophios/api/pythonapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ def run(self) -> None:
758758

759759
post_compile.cwl_docker_extract(args, self.process_name)
760760
rose_tree = post_compile.remove_entrypoints(args, rose_tree)
761-
761+
post_compile.find_and_create_output_dirs(rose_tree)
762762
# Do NOT capture stdout and/or stderr and pipe warnings and errors into a black hole.
763763
retval = run_local_module.run_local(args, rose_tree, args.cachedir, args.cwl_runner, True)
764764

src/sophios/compiler.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -880,11 +880,6 @@ def compile_workflow_once(yaml_tree_ast: YamlTree,
880880
newval['format'] = in_format
881881
new_keyval = {key: newval}
882882
elif 'Directory' == in_dict['type']:
883-
if not args.ignore_dir_path:
884-
ldir = Path(in_dict['value'])
885-
if not ldir.is_absolute():
886-
ldir = Path('autogenerated') / ldir
887-
ldir.mkdir(parents=True, exist_ok=True)
888883
newval = {'class': 'Directory', 'location': in_dict['value']}
889884
new_keyval = {key: newval}
890885
# TODO: Check for all valid types?

src/sophios/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def main() -> None:
196196
pc.cwl_docker_extract(args, yaml_stem)
197197
rose_tree = pc.remove_entrypoints(args, rose_tree)
198198
io.write_to_disk(rose_tree, Path('autogenerated/'), True, args.inputs_file)
199-
199+
pc.find_and_create_output_dirs(rose_tree)
200200
run_local.run_local(args, rose_tree, args.cachedir, args.cwl_runner, False)
201201

202202
# Finally, since there is an output file copying bug in cwltool,

src/sophios/post_compile.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,55 @@
11
import argparse
22
from pathlib import Path
33
import subprocess as sub
4-
4+
from typing import Dict, Union
55
from . import plugins
66
from .wic_types import RoseTree
77

88

9+
def find_output_dirs(data: Union[RoseTree, Dict, list]) -> list:
10+
"""
11+
Recursively searches through a nested structure and finds all dictionaries
12+
that contain the key 'location', and a key 'class' with a value of 'Directory'.
13+
14+
Args:
15+
data (any): The data to search through, which can be a dictionary, list,
16+
or any other structure.
17+
18+
Returns:
19+
list: A list of location values.
20+
"""
21+
results = []
22+
if isinstance(data, Dict):
23+
if "class" in data and data["class"] == "Directory" and "location" in data:
24+
results.append(data["location"])
25+
for value in data.values():
26+
results.extend(find_output_dirs(value))
27+
elif isinstance(data, list):
28+
for item in data:
29+
results.extend(find_output_dirs(item))
30+
31+
return results
32+
33+
34+
def create_output_dirs(output_dirs: list, basepath: str = 'autogenerated') -> None:
35+
"""
36+
Creates all the directories that are needed for the outputs of a workflow.
37+
"""
38+
for output_dir in output_dirs:
39+
dir_path = Path(output_dir)
40+
if not dir_path.is_absolute():
41+
dir_path = Path(basepath) / dir_path
42+
dir_path.mkdir(parents=True, exist_ok=True)
43+
44+
45+
def find_and_create_output_dirs(rose_tree: RoseTree, basepath: str = 'autogenerated') -> None:
46+
"""
47+
Finds all output directories in the workflow and creates them.
48+
"""
49+
output_dirs = find_output_dirs(rose_tree.data.workflow_inputs_file)
50+
create_output_dirs(output_dirs, basepath)
51+
52+
953
def cwl_docker_extract(args: argparse.Namespace, file_name: str) -> None:
1054
"""Helper function to do the cwl_docker_extract"""
1155
# cwl-docker-extract recursively `docker pull`s all images in all subworkflows.

tests/test_examples.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from sophios import auto_gen_header
2323
from sophios.cli import get_args
2424
from sophios.utils_yaml import wic_loader
25-
from sophios.post_compile import cwl_docker_extract, remove_entrypoints
25+
from sophios.post_compile import cwl_docker_extract, remove_entrypoints, find_and_create_output_dirs
2626
from sophios.wic_types import NodeData, StepId, Yaml, YamlTree, Json
2727
from sophios.utils_graphs import get_graph_reps
2828

@@ -223,6 +223,7 @@ def run_workflows(yml_path_str: str, yml_path: Path, cwl_runner: str, args: argp
223223
rose_tree = sophios.plugins.cwl_update_outputs_optional_rosetree(rose_tree)
224224
sophios.input_output.write_to_disk(rose_tree, Path('autogenerated/'), True, args.inputs_file)
225225
# NOTE: Do not use --cachedir; we want to actually test everything.
226+
find_and_create_output_dirs(rose_tree)
226227
retval = sophios.run_local.run_local(args, rose_tree, None, cwl_runner, True)
227228
assert retval == 0
228229

0 commit comments

Comments
 (0)