1
1
import json
2
2
from pathlib import Path
3
- from typing import List , Union , Dict
3
+ from typing import List , Union , Dict , Tuple
4
4
from .interface_types import InterfaceTypes
5
5
from .pipeline_input import PipelineInput
6
6
from .pipeline_output import PipelineOutput
7
7
from .text_stream import TextStream
8
8
from .binary_stream import BinaryStream
9
+ from .text_file import TextFile
10
+ from .binary_file import BinaryFile
9
11
10
12
from wasmer import engine , wasi , Store , Module , ImportObject , Instance
11
13
from wasmer_compiler_cranelift import Compiler
@@ -26,9 +28,21 @@ def __init__(self, pipeline: Union[str, Path, bytes]):
26
28
self .module = Module (self .store , wasm_bytes )
27
29
self .wasi_version = wasi .get_version (self .module , strict = True )
28
30
29
- def run (self , args : List [str ], outputs : List [PipelineOutput ]= [], inputs : List [PipelineInput ]= [], preopen_directories = [], map_directories = {}, environments = {} ) -> List [PipelineOutput ]:
31
+ def run (self , args : List [str ], outputs : List [PipelineOutput ]= [], inputs : List [PipelineInput ]= []) -> Tuple [PipelineOutput ]:
30
32
"""Run the itk-wasm pipeline."""
31
33
34
+ preopen_directories = set ()
35
+ map_directories = {}
36
+ # Todo: expose?
37
+ environments = {}
38
+ for index , input_ in enumerate (inputs ):
39
+ if input_ .type == InterfaceTypes .TextFile or input_ .type == InterfaceTypes .BinaryFile :
40
+ preopen_directories .add (str (input_ .data .path .parent ))
41
+ for index , output in enumerate (outputs ):
42
+ if output .type == InterfaceTypes .TextFile or output .type == InterfaceTypes .BinaryFile :
43
+ preopen_directories .add (str (output .data .path .parent ))
44
+ preopen_directories = list (preopen_directories )
45
+
32
46
wasi_state = wasi .StateBuilder ('itk-wasm-pipeline' )
33
47
wasi_state .arguments (args )
34
48
wasi_state .environments (environments )
@@ -60,6 +74,10 @@ def run(self, args: List[str], outputs: List[PipelineOutput]=[], inputs: List[Pi
60
74
array_ptr = self ._set_input_array (data_array , index , 0 )
61
75
data_json = { "size" : len (data_array ), "data" : f"data:application/vnd.itk.address,0:{ array_ptr } " }
62
76
self ._set_input_json (data_json , index )
77
+ elif input_ .type == InterfaceTypes .TextFile :
78
+ pass
79
+ elif input_ .type == InterfaceTypes .BinaryFile :
80
+ pass
63
81
else :
64
82
raise ValueError (f'Unexpected/not yet supported input.type { input_ .type } ' )
65
83
@@ -80,13 +98,17 @@ def run(self, args: List[str], outputs: List[PipelineOutput]=[], inputs: List[Pi
80
98
data_size = self .output_array_size (0 , index , 0 )
81
99
data_array = bytes (memoryview (self .memory .buffer )[data_ptr :data_ptr + data_size ])
82
100
output_data = PipelineOutput (InterfaceTypes .BinaryStream , BinaryStream (data_array ))
101
+ elif output .type == InterfaceTypes .TextFile :
102
+ output_data = PipelineOutput (InterfaceTypes .TextFile , TextFile (output .data .path ))
103
+ elif output .type == InterfaceTypes .BinaryFile :
104
+ output_data = PipelineOutput (InterfaceTypes .BinaryFile , BinaryFile (output .data .path ))
83
105
populated_outputs .append (output_data )
84
106
85
107
delayed_exit = instance .exports .itk_wasm_delayed_exit
86
108
delayed_exit (return_code )
87
109
88
110
# Should we be returning the return_code?
89
- return populated_outputs
111
+ return tuple ( populated_outputs )
90
112
91
113
def _set_input_array (self , data_array : bytes , input_index : int , sub_index : int ) -> int :
92
114
data_ptr = 0
0 commit comments