Skip to content

Commit

Permalink
include user site-packages in default Python path (#81)
Browse files Browse the repository at this point in the history
* include user site-packages in default Python path

In many (most?) Python installations, this is where e.g. `pip install`'d
packages end up.

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* skip non-existent directories in Python path

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

* bump version to 0.13.2

Signed-off-by: Joel Dice <joel.dice@fermyon.com>

---------

Signed-off-by: Joel Dice <joel.dice@fermyon.com>
  • Loading branch information
dicej committed Apr 8, 2024
1 parent 35bf29e commit 65277a5
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "componentize-py"
version = "0.13.1"
version = "0.13.2"
edition = "2021"
exclude = ["cpython"]

Expand Down
4 changes: 2 additions & 2 deletions examples/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ run a Python-based component targetting the [wasi-cli] `command` world.
## Prerequisites

* `Wasmtime` 18.0.0 or later
* `componentize-py` 0.13.1
* `componentize-py` 0.13.2

Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
you don't have `cargo`, you can download and install from
https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.

```
cargo install --version 18.0.0 wasmtime-cli
pip install componentize-py==0.13.1
pip install componentize-py==0.13.2
```

## Running the demo
Expand Down
4 changes: 2 additions & 2 deletions examples/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ run a Python-based component targetting the [wasi-http] `proxy` world.
## Prerequisites

* `Wasmtime` 18.0.0 or later
* `componentize-py` 0.13.1
* `componentize-py` 0.13.2

Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
you don't have `cargo`, you can download and install from
https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.

```
cargo install --version 18.0.0 wasmtime-cli
pip install componentize-py==0.13.1
pip install componentize-py==0.13.2
```

## Running the demo
Expand Down
4 changes: 2 additions & 2 deletions examples/matrix-math/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ within a guest component.
## Prerequisites

* `wasmtime` 18.0.0 or later
* `componentize-py` 0.13.1
* `componentize-py` 0.13.2
* `NumPy`, built for WASI

Note that we use an unofficial build of NumPy since the upstream project does
Expand All @@ -23,7 +23,7 @@ https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.

```
cargo install --version 18.0.0 wasmtime-cli
pip install componentize-py==0.13.1
pip install componentize-py==0.13.2
curl -OL https://github.com/dicej/wasi-wheels/releases/download/v0.0.1/numpy-wasi.tar.gz
tar xf numpy-wasi.tar.gz
```
Expand Down
4 changes: 2 additions & 2 deletions examples/sandbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ sandboxed Python code snippets from within a Python app.
## Prerequisites

* `wasmtime-py` 18.0.0 or later
* `componentize-py` 0.13.1
* `componentize-py` 0.13.2

```
pip install componentize-py==0.13.1 wasmtime==18.0.2
pip install componentize-py==0.13.2 wasmtime==18.0.2
```

## Running the demo
Expand Down
4 changes: 2 additions & 2 deletions examples/tcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ making an outbound TCP request using `wasi-sockets`.
## Prerequisites

* `Wasmtime` 18.0.0 or later
* `componentize-py` 0.13.1
* `componentize-py` 0.13.2

Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If
you don't have `cargo`, you can download and install from
https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0.

```
cargo install --version 18.0.0 wasmtime-cli
pip install componentize-py==0.13.1
pip install componentize-py==0.13.2
```

## Running the demo
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ features = ["pyo3/extension-module"]

[project]
name = "componentize-py"
version = "0.13.1"
version = "0.13.2"
description = "Tool to package Python applications as WebAssembly components"
readme = "README.md"
license = { file = "LICENSE" }
Expand Down
37 changes: 22 additions & 15 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn generate_bindings(common: Common, bindings: Bindings) -> Result<()> {
fn componentize(common: Common, componentize: Componentize) -> Result<()> {
let mut python_path = componentize.python_path;

if let Some(site_packages) = find_site_packages()? {
for site_packages in find_site_packages()? {
python_path.push(
site_packages
.to_str()
Expand Down Expand Up @@ -168,18 +168,18 @@ fn componentize(common: Common, componentize: Componentize) -> Result<()> {
Ok(())
}

fn find_site_packages() -> Result<Option<PathBuf>> {
fn find_site_packages() -> Result<Vec<PathBuf>> {
Ok(if let Ok(env) = env::var("VIRTUAL_ENV") {
let dir = Path::new(&env).join("lib");

if let Some(site_packages) = find_dir("site-packages", &dir)? {
Some(site_packages)
vec![site_packages]
} else {
eprintln!(
"warning: site-packages directory not found under {}",
dir.display()
);
None
Vec::new()
}
} else {
let pipenv_packages = match process::Command::new("pipenv").arg("--venv").output() {
Expand All @@ -188,38 +188,45 @@ fn find_site_packages() -> Result<Option<PathBuf>> {
let dir = Path::new(str::from_utf8(&output.stdout)?.trim()).join("lib");

if let Some(site_packages) = find_dir("site-packages", &dir)? {
Some(site_packages)
vec![site_packages]
} else {
eprintln!(
"warning: site-packages directory not found under {}",
dir.display()
);
None
Vec::new()
}
} else {
// `pipenv` is in `$PATH`, but this app does not appear to be using it
None
Vec::new()
}
}
Err(_) => {
// `pipenv` is not in `$PATH -- assume this app isn't using it
None
Vec::new()
}
};

if pipenv_packages.is_some() {
if !pipenv_packages.is_empty() {
pipenv_packages
} else {
// Get site packages location using the `site` module in python
match process::Command::new("python3")
.args(["-c", "import site; print(site.getsitepackages()[0])"])
.args([
"-c",
"import site; \
list = site.getsitepackages(); \
list.insert(0, site.getusersitepackages()); \
print(';'.join(list))",
])
.output()
{
Ok(output) => {
let path = Path::new(str::from_utf8(&output.stdout)?.trim()).to_path_buf();
Some(path)
}
Err(_) => None,
Ok(output) => str::from_utf8(&output.stdout)?
.trim()
.split(';')
.map(|p| Path::new(p).to_path_buf())
.collect(),
Err(_) => Vec::new(),
}
}
})
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ pub async fn componentize(
isyswasfa: Option<&str>,
stub_wasi: bool,
) -> Result<()> {
// Remove non-existent elements from `python_path` so we don't choke on them later:
let python_path = &python_path
.iter()
.filter_map(|&s| Path::new(s).exists().then_some(s))
.collect::<Vec<_>>();

// Untar the embedded copy of the Python standard library into a temporary directory
let stdlib = tempfile::tempdir()?;

Expand Down

0 comments on commit 65277a5

Please sign in to comment.