Writes a python file and call python3 on it # Arguments `python_commands` - Python commands to be written to file `output_dir` - Output directory to be created `filename_py` - Filename with extension .py
(python_exe: &str, python_commands: &String, path: &Path)
| 13 | /// * `output_dir` - Output directory to be created |
| 14 | /// * `filename_py` - Filename with extension .py |
| 15 | pub(crate) fn call_python3(python_exe: &str, python_commands: &String, path: &Path) -> Result<String, StrError> { |
| 16 | // create directory |
| 17 | if let Some(p) = path.parent() { |
| 18 | fs::create_dir_all(p).map_err(|_| "cannot create directory")?; |
| 19 | } |
| 20 | |
| 21 | // combine header with commands |
| 22 | let mut contents = String::new(); |
| 23 | contents.push_str(python_commands); |
| 24 | |
| 25 | // write file |
| 26 | let mut file = File::create(path).map_err(|_| "cannot create file")?; |
| 27 | file.write_all(contents.as_bytes()).map_err(|_| "cannot write file")?; |
| 28 | |
| 29 | // force sync |
| 30 | file.sync_all().map_err(|_| "cannot sync file")?; |
| 31 | |
| 32 | // execute file |
| 33 | let output = Command::new(python_exe) |
| 34 | .arg(path) |
| 35 | .output() |
| 36 | .map_err(|_| "cannot run python3")?; |
| 37 | |
| 38 | // results |
| 39 | let out = String::from_utf8(output.stdout).unwrap(); |
| 40 | let err = String::from_utf8(output.stderr).unwrap(); |
| 41 | let mut results = String::new(); |
| 42 | if out.len() > 0 { |
| 43 | results.push_str(&out); |
| 44 | } |
| 45 | if err.len() > 0 { |
| 46 | results.push_str(&err) |
| 47 | } |
| 48 | |
| 49 | // done |
| 50 | Ok(results) |
| 51 | } |
| 52 | |
| 53 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 54 |
no outgoing calls