Skip to content

Commit

Permalink
feat(python): Execute with stdout / stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Sep 9, 2022
1 parent e5fec05 commit 29318f6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/python/itkwasm/itkwasm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""itkwasm: Python interface to itk-wasm WebAssembly modules."""

__version__ = "1.0b1"
__version__ = "1.0b2"

from .image import Image, ImageType
from .mesh import Mesh, MeshType
from .pointset import PointSet, PointSetType
from .pipeline import Pipeline

__all__ = [
"Pipeline",
"Image",
"ImageType",
"Mesh",
Expand Down
37 changes: 37 additions & 0 deletions src/python/itkwasm/itkwasm/pipeline.py
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()
4 changes: 3 additions & 1 deletion src/python/itkwasm/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ dynamic = ["version"]
requires-python = ">=3.7"
dependencies = [
"numpy",
"wasmer",
"wasmer_compiler_cranelift",
]

[project.urls]
Expand All @@ -39,7 +41,7 @@ Issues = "https://github.com/InsightSoftwareConsortium/itk-wasm/issues"

[project.optional-dependencies]
test = [
"itk>=5.3rc4.post3",
"itk>=5.3rc4.post2",
"pytest >=2.7.3",
]
doc = ["sphinx"]
Expand Down
Binary file not shown.
17 changes: 17 additions & 0 deletions src/python/itkwasm/test/test_pipeline.py
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([])

0 comments on commit 29318f6

Please sign in to comment.