forked from bytecodealliance/componentize-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.rs
More file actions
113 lines (107 loc) · 3.49 KB
/
python.rs
File metadata and controls
113 lines (107 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#![allow(
clippy::useless_conversion,
reason = "some pyo3 macros produce code that does this"
)]
use {
pyo3::{
exceptions::PyAssertionError,
pybacked::PyBackedStr,
types::{PyAnyMethods, PyModule, PyModuleMethods},
Bound, PyResult, Python,
},
std::{ffi::OsString, path::PathBuf},
tokio::runtime::Runtime,
};
#[allow(clippy::too_many_arguments)]
#[pyo3::pyfunction]
#[pyo3(name = "componentize")]
#[pyo3(signature = (wit_path, world, features, all_features, python_path, module_worlds, app_name, output_path, stub_wasi, import_interface_names, export_interface_names))]
fn python_componentize(
wit_path: Option<PathBuf>,
world: Option<&str>,
features: Vec<String>,
all_features: bool,
python_path: Vec<PyBackedStr>,
module_worlds: Vec<(PyBackedStr, PyBackedStr)>,
app_name: &str,
output_path: PathBuf,
stub_wasi: bool,
import_interface_names: Vec<(PyBackedStr, PyBackedStr)>,
export_interface_names: Vec<(PyBackedStr, PyBackedStr)>,
) -> PyResult<()> {
(|| {
Runtime::new()?.block_on(crate::componentize(
wit_path.as_deref(),
world,
&features,
all_features,
&python_path.iter().map(|s| s.as_ref()).collect::<Vec<_>>(),
&module_worlds
.iter()
.map(|(a, b)| (a.as_ref(), b.as_ref()))
.collect::<Vec<_>>(),
app_name,
&output_path,
None,
stub_wasi,
&import_interface_names
.iter()
.map(|(a, b)| (a.as_ref(), b.as_ref()))
.collect(),
&export_interface_names
.iter()
.map(|(a, b)| (a.as_ref(), b.as_ref()))
.collect(),
))
})()
.map_err(|e| PyAssertionError::new_err(format!("{e:?}")))
}
#[allow(clippy::too_many_arguments)]
#[pyo3::pyfunction]
#[pyo3(name = "generate_bindings")]
#[pyo3(signature = (wit_path, world, features, all_features, world_module, output_dir, import_interface_names, export_interface_names))]
fn python_generate_bindings(
wit_path: PathBuf,
world: Option<&str>,
features: Vec<String>,
all_features: bool,
world_module: Option<&str>,
output_dir: PathBuf,
import_interface_names: Vec<(PyBackedStr, PyBackedStr)>,
export_interface_names: Vec<(PyBackedStr, PyBackedStr)>,
) -> PyResult<()> {
crate::generate_bindings(
&wit_path,
world,
&features,
all_features,
world_module,
&output_dir,
&import_interface_names
.iter()
.map(|(a, b)| (a.as_ref(), b.as_ref()))
.collect(),
&export_interface_names
.iter()
.map(|(a, b)| (a.as_ref(), b.as_ref()))
.collect(),
)
.map_err(|e| PyAssertionError::new_err(format!("{e:?}")))
}
#[pyo3::pyfunction]
#[pyo3(name = "script")]
fn python_script(py: Python) -> PyResult<()> {
crate::command::run(
py.import_bound("sys")?
.getattr("argv")?
.extract::<Vec<OsString>>()?,
)
.map_err(|e| PyAssertionError::new_err(format!("{e:?}")))
}
#[pyo3::pymodule]
fn componentize_py(_py: Python, module: Bound<PyModule>) -> PyResult<()> {
module.add_function(pyo3::wrap_pyfunction!(python_componentize, &module)?)?;
module.add_function(pyo3::wrap_pyfunction!(python_generate_bindings, &module)?)?;
module.add_function(pyo3::wrap_pyfunction!(python_script, &module)?)?;
Ok(())
}