pub(crate) fn call_python3(python_commands: &String, path: &Path) -> Result<String, &'static str> {
    // create directory
    if let Some(p) = path.parent() {
        fs::create_dir_all(p).map_err(|_| "cannot create directory")?;
    }

    // combine header with commands
    let mut contents = String::new();
    contents.push_str(PYTHON_HEADER);
    contents.push_str(python_commands);

    // write file
    let mut file = File::create(path).map_err(|_| "cannot create file")?;
    file.write_all(contents.as_bytes()).map_err(|_| "cannot write file")?;

    // force sync
    file.sync_all().map_err(|_| "cannot sync file")?;

    // execute file
    let output = Command::new("python3")
        .arg(path)
        .output()
        .map_err(|_| "cannot run python3")?;

    // results
    let out = String::from_utf8(output.stdout).unwrap();
    let err = String::from_utf8(output.stderr).unwrap();
    let mut results = String::new();
    if out.len() > 0 {
        results.push_str(&out);
    }
    if err.len() > 0 {
        results.push_str(&err)
    }

    // done
    Ok(results)
}
/// 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
///
/// # Note
///
/// The contents of PYTHON_HEADER are added at the beginning of the file.
///
Writes a python file and call python3 on it
Writes a python file and call python3 on it
pub(crate) fn call_python3(python_commands: &String, path: &Path) -> Result<String, &'static str> {
    // create directory
    if let Some(p) = path.parent() {
        fs::create_dir_all(p).map_err(|_| "cannot create directory")?;
    }
    // combine header with commands
    let mut contents = String::new();
    contents.push_str(PYTHON_HEADER);
    contents.push_str(python_commands);
    // write file
    let mut file = File::create(path).map_err(|_| "cannot create file")?;
    file.write_all(contents.as_bytes()).map_err(|_| "cannot write file")?;
    // force sync
    file.sync_all().map_err(|_| "cannot sync file")?;
    // execute file
    let output = Command::new("python3")
        .arg(path)
        .output()
        .map_err(|_| "cannot run python3")?;
    // results
    let out = String::from_utf8(output.stdout).unwrap();
    let err = String::from_utf8(output.stderr).unwrap();
    let mut results = String::new();
    if out.len() > 0 {
        results.push_str(&out);
    }
    if err.len() > 0 {
        results.push_str(&err)
    }
    // done
    Ok(results)
}
pub ( crate ) fn call_python3 ( python_commands : & String , path : & Path ) -> Result < String , & ' static str > { // create directory if let Some ( p ) = path . parent ( ) { fs :: create_dir_all ( p ) . map_err ( | _ | " " ) ? ; } // combine header with commands let mut contents = String :: new ( ) ; contents . push_str ( PYTHON_HEADER ) ; contents . push_str ( python_commands ) ; // write file let mut file = File :: create ( path ) . map_err ( | _ | " " ) ? ; file . write_all ( contents . as_bytes ( ) ) . map_err ( | _ | " " ) ? ; // force sync file . sync_all ( ) . map_err ( | _ | " " ) ? ; // execute file let output = Command :: new ( " " ) . arg ( path ) . output ( ) . map_err ( | _ | " " ) ? ; // results let out = String :: from_utf8 ( output . stdout ) . unwrap ( ) ; let err = String :: from_utf8 ( output . stderr ) . unwrap ( ) ; let mut results = String :: new ( ) ; if out . len ( ) > 0 { results . push_str ( & out ) ; } if err . len ( ) > 0 { results . push_str ( & err ) } // done Ok ( results ) }
Writes a python file and call python3 on it
Writes a python file and call python3 on it
python_commands
&String
path
&Path
python_commands
&String
path
&Path
note
The contents of PYTHON_HEADER are added at the beginning of the file.
The contents of PYTHON_HEADER are added at the beginning of the file .