Skip to content

Commit 6c451b0

Browse files
committed
remove wasi-vfs dependency
It turns out that wasi-vfs isn't needed after all -- our Wizer initialization step is sufficient to allow CPython to load everything it needs from the host filesystem. As a bonus, skipping the wasi-vfs step ensures that the module size is automatically minimized -- no need to use `rsync` for staging and excluding files by hand. One possible drawback is that an application might try to dynamically load modules from the filesystem at runtime (either built-in Python modules or otherwise). In that case, we may need to either use wasi-vfs or give the developer some other way to pull the needed modules in at Wizer time (e.g. via an app-provided init function which is called from the Wizer init function). Signed-off-by: Joel Dice <[email protected]>
1 parent 6bc9a2e commit 6c451b0

File tree

8 files changed

+16
-34
lines changed

8 files changed

+16
-34
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
[submodule "cpython"]
22
path = cpython
33
url = https://github.com/python/cpython
4-
[submodule "wasi-vfs"]
5-
path = wasi-vfs
6-
url = https://github.com/dicej/wasi-vfs

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ http = "0.2"
1313
spin-sdk = { git = "https://github.com/fermyon/spin" }
1414
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "dde4694aaa6acf9370206527a798ac4ba6a8c5b8" }
1515
pyo3 = { version = "0.17.3", features = [ "abi3-py310" ] }
16-
wasi-vfs = { path = "./wasi-vfs" }
1716
once_cell = "1.16.0"

Makefile

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
WASI_SDK_PATH ?= /opt/wasi-sdk
22

33
.PHONY: build
4-
build: target/config.txt target/lib
4+
build: target/config.txt
55
PYO3_CONFIG_FILE=$$(pwd)/target/config.txt cargo build --release --target=wasm32-wasi
6-
env -i wasi-vfs/target/release/wasi-vfs pack target/wasm32-wasi/release/python_wasi.wasm \
7-
--mapdir lib::$$(pwd)/target/lib \
8-
-o target/wasm32-wasi/release/python-wasi-vfs.wasm
9-
env -i PYTHONUNBUFFERED=1 PYTHONPATH=/py:/site-packages \
10-
$$(which wizer) target/wasm32-wasi/release/python-wasi-vfs.wasm \
11-
--inherit-env true --wasm-bulk-memory true --allow-wasi --dir py \
6+
env -i PYTHONUNBUFFERED=1 \
7+
PYTHONHOME=/python \
8+
PYTHONPATH=/python:/py:/site-packages \
9+
$$(which wizer) \
10+
target/wasm32-wasi/release/python_wasi.wasm \
11+
--inherit-env true \
12+
--wasm-bulk-memory true \
13+
--allow-wasi \
14+
--dir py \
15+
--mapdir python::$$(pwd)/cpython/builddir/wasi/install/lib/python3.11 \
1216
--mapdir site-packages::$$(cd py && find $$(pipenv --venv)/lib -name site-packages | head -1) \
13-
-o target/wasm32-wasi/release/python-wasi-vfs-wizer.wasm
17+
-o target/wasm32-wasi/release/python-wasi-wizer.wasm
1418

1519
target/config.txt:
1620
mkdir -p target
1721
cp config.txt target
1822
echo "lib_dir=$$(pwd)/cpython/builddir/wasi" >> $@
19-
20-
target/lib:
21-
mkdir -p $@
22-
rsync -a --exclude='*.a' --exclude='*.pyc' --exclude='*.whl' --exclude=Makefile \
23-
--exclude=Changelog --exclude=NEWS.txt \
24-
cpython/builddir/wasi/install/lib/python3.11 $@

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# spin-python-sdk
22

3-
This is an experiment to build a Spin Python SDK using CPython, wasi-vfs, and PyO3.
3+
This is an experiment to build a Spin Python SDK using CPython, Wizer, and PyO3.
44

55
## Prerequisites
66

77
- [CPython](https://github.com/python/cpython) build prereqs (e.g. Make, Clang, etc.)
88
- [Rust](https://rustup.rs/) (including `wasm32-wasi` target)
9-
- [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) v16 or later
109
- [Wizer](https://github.com/bytecodealliance/wizer) v1.6.0 or later
1110
- [Spin](https://github.com/fermyon/spin)
12-
- [rsync](https://rsync.samba.org/) -- used at build time for copying files around; we could replace this with something more portable, if desired
1311
- [pipenv](https://pypi.org/project/pipenv/) for installing Python project dependencies
1412

1513
## Building and Running
@@ -32,13 +30,7 @@ make install
3230
cd ../../..
3331
```
3432

35-
Then, build the wasi-vfs CLI:
36-
37-
```
38-
(cd wasi-vfs && cargo build --release -p wasi-vfs-cli)
39-
```
40-
41-
Finally, build and run this app:
33+
Then, build and run this app:
4234

4335
```
4436
(cd py && pipenv install)

py/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def handle_request(request):
1919
print(f"And here's some TOML: {toml.loads(some_toml)}")
2020

2121
my_file = open("/foo.txt", "r")
22-
print(f"And here's the contents of foo.txt: {my_file.read()}")
22+
print(f"And here's the content of foo.txt: {my_file.read()}")
2323

2424
response = http_send(Request("GET", "https://some-random-api.ml/facts/dog", [], None))
2525
print(f"Got dog fact: {str(response.body, 'utf-8')}")

spin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ id = "spin-webrtc"
1010
environment = { hello = "teapot" }
1111
config = { redis_address = "redis://127.0.0.1:6379" }
1212
files = [{ source = "static-assets/", destination = "/" }]
13-
source = "target/wasm32-wasi/release/python-wasi-vfs-wizer.wasm"
13+
source = "target/wasm32-wasi/release/python-wasi-wizer.wasm"
1414
allowed_http_hosts = ["insecure:allow-all"]
1515
[component.trigger]
1616
route = "/..."

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ fn spin_redis_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
224224
module.add_function(pyo3::wrap_pyfunction!(redis_set, module)?)?;
225225
module.add_function(pyo3::wrap_pyfunction!(redis_smembers, module)?)?;
226226
module.add_function(pyo3::wrap_pyfunction!(redis_srem, module)?)
227-
228227
}
229228

230229
#[pyo3::pyfunction]
@@ -275,8 +274,6 @@ fn do_init() -> Result<()> {
275274

276275
#[export_name = "wizer.initialize"]
277276
pub extern "C" fn init() {
278-
std::hint::black_box(wasi_vfs::__internal_wasi_vfs_rt_init);
279-
280277
do_init().unwrap();
281278
}
282279

wasi-vfs

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)