You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//! Small CLI entry point to introspect a Python cdylib built using PyO3 and generate [type stubs](https://typing.readthedocs.io/en/latest/source/stubs.html).
2
+
3
+
use anyhow::{anyhow,Context,Result};
4
+
use pyo3_introspection::{introspect_cdylib, module_stub_files};
5
+
use std::path::Path;
6
+
use std::{env, fs};
7
+
8
+
fnmain() -> Result<()>{
9
+
let[_, binary_path, output_path] = env::args().collect::<Vec<_>>().try_into().map_err(|_| anyhow!("pyo3-introspection takes two arguments, the path of the binary to introspect and the path of the directory to write the stub to"))?;
10
+
let module = introspect_cdylib(&binary_path,"pyo3_pytests")
11
+
.with_context(|| format!("Failed to introspect module {binary_path}"))?;
12
+
let actual_stubs = module_stub_files(&module);
13
+
for(path, module)in actual_stubs {
14
+
let file_path = Path::new(&output_path).join(path);
15
+
ifletSome(parent) = file_path.parent(){
16
+
fs::create_dir_all(parent).with_context(|| {
17
+
format!("Failed to create output directory {}", file_path.display())
18
+
})?;
19
+
}
20
+
fs::write(&file_path, module)
21
+
.with_context(|| format!("Failed to write module {}", file_path.display()))?;
0 commit comments