-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python): Execute with stdout / stderr
- Loading branch information
Showing
5 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from pathlib import Path | ||
from typing import List, Union | ||
|
||
from wasmer import engine, wasi, Store, Module, ImportObject, Instance | ||
from wasmer_compiler_cranelift import Compiler | ||
|
||
|
||
class Pipeline: | ||
"""Run an itk-wasm WASI pipeline.""" | ||
|
||
def __init__(self, pipeline: Union[str, Path, bytes]): | ||
"""Compile the pipeline.""" | ||
self.engine = engine.Universal(Compiler) | ||
if isinstance(pipeline, bytes): | ||
wasm_bytes = pipeline | ||
else: | ||
with open(pipeline, 'rb') as fp: | ||
wasm_bytes = fp.read() | ||
self.store = Store(self.engine) | ||
self.module = Module(self.store, wasm_bytes) | ||
self.wasi_version = wasi.get_version(self.module, strict=True) | ||
|
||
def run(self, args: List[str], outputs=[], inputs=[], preopen_directories=[], map_directories={}, environments={}): | ||
"""Run the itk-wasm pipeline.""" | ||
|
||
wasi_state = wasi.StateBuilder('itk-wasm-pipeline') | ||
wasi_state.arguments(args) | ||
wasi_state.environments(environments) | ||
wasi_state.map_directories(map_directories) | ||
wasi_state.preopen_directories(preopen_directories) | ||
wasi_env = wasi_state.finalize() | ||
import_object = wasi_env.generate_import_object(self.store, self.wasi_version) | ||
|
||
instance = Instance(self.module, import_object) | ||
|
||
start = instance.exports._start | ||
start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from itkwasm import Pipeline | ||
|
||
from pathlib import Path | ||
|
||
test_input_dir = Path(__file__).resolve().parent / 'input' | ||
|
||
|
||
def test_stdout_stderr(): | ||
pipeline = Pipeline(test_input_dir / 'stdout-stderr-test.wasi.wasm') | ||
pipeline.run([]) | ||
|
||
def test_pipeline_bytes(): | ||
pipeline_path = test_input_dir / 'stdout-stderr-test.wasi.wasm' | ||
with open(pipeline_path, 'rb') as fp: | ||
wasm_bytes = fp.read() | ||
pipeline = Pipeline(wasm_bytes) | ||
pipeline.run([]) |