use std::{fs, path::Path, process::Command};
pub fn python_asm(source_code: &str, output_path: &Path) -> Result<(), String> {
let temp_py_path = output_path.with_extension("py");
fs::write(&temp_py_path, source_code).map_err(|e| format!("Failed to write temporary Python file: {}", e))?;
let compile_script = format!(
r#"
import py_compile
import sys
try:
py_compile.compile('{}', '{}', doraise=True)
print("Compilation successful")
except Exception as e:
print(f"Compilation failed: {{e}}", file=sys.stderr)
sys.exit(1)
"#,
temp_py_path.to_string_lossy().replace('\\', "\\\\"),
output_path.to_string_lossy().replace('\\', "\\\\")
);
let output = Command::new("python")
.args(["-c", &compile_script])
.output()
.map_err(|e| format!("Failed to execute Python compiler: {}", e))?;
let _ = fs::remove_file(&temp_py_path);
if !output.status.success() {
return Err(format!("Python compilation failed: {}", String::from_utf8_lossy(&output.stderr)));
}
Ok(())
}