vanadin_tasks/modules/tools/
python.rs

1use rquickjs::module::{Declarations, Exports, ModuleDef};
2use rquickjs::{Ctx, Function};
3
4pub struct Module;
5
6impl ModuleDef for Module {
7    #[inline(always)]
8    fn declare(declarations: &mut Declarations) -> rquickjs::Result<()> {
9        declarations.declare("py")?;
10        declarations.declare("run")?;
11        declarations.declare("launch")?;
12
13        Ok(())
14    }
15
16    #[inline(always)]
17    fn evaluate<'js>(ctx: &Ctx<'js>, exports: &mut Exports<'js>) -> rquickjs::Result<()> {
18        exports.export("py", Function::new(ctx.clone(), Self::py))?;
19        exports.export("run", Function::new(ctx.clone(), Self::run))?;
20        exports.export("launch", Function::new(ctx.clone(), Self::launch))?;
21
22        Ok(())
23    }
24}
25
26impl Module {
27    #[inline(always)]
28    fn py(cmd: String) -> Option<i32> {
29        std::process::Command::new(format!("python {}", cmd))
30            .spawn()
31            .ok()?
32            .wait()
33            .ok()?
34            .code()
35    }
36
37    #[inline(always)]
38    fn run(file: Option<String>) -> Option<i32> {
39        std::process::Command::new(format!(
40            "python {} -X dev -b",
41            file.unwrap_or("__main__.py".to_string())
42        ))
43        .spawn()
44        .ok()?
45        .wait()
46        .ok()?
47        .code()
48    }
49
50    #[inline(always)]
51    fn launch(file: Option<String>) -> Option<i32> {
52        std::process::Command::new(format!(
53            "python {} -00",
54            file.unwrap_or("__main__.py".to_string())
55        ))
56        .spawn()
57        .ok()?
58        .wait()
59        .ok()?
60        .code()
61    }
62}