summaryrefslogtreecommitdiffstats
path: root/src/backends/python.rs
blob: 2776eb2a531d719b8870735ee3e4b0a3a887d0eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use serde_derive::{ Serialize, Deserialize };
use crate::backends::Backend;
use std::process::{ Command, Stdio };
use std::path::Path;
use log::trace;
use std::io::{ Error, ErrorKind };

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct PythonBackend {
    template: Option<String>,
    version: Option<String>,
}

impl Backend for PythonBackend {
    fn get_template(&self) -> Option<&str> {
        match self.template {
            Some(ref t) => Some(t),
            None => None
        }
    }

    fn run(&self, fname: &Path) -> std::io::Result<()> {
        let interpreter = format!("python{}", self.version.as_ref().unwrap_or(&String::new()));
        let stdio = match self.try_guess_test(fname) {
            Some(test_file) => Stdio::from(std::fs::File::open(test_file)?),
            None => Stdio::piped()
        };
        let timer = std::time::SystemTime::now();
        let mut child = Command::new(interpreter)
            .arg(fname.as_os_str())
            .stdin(stdio)
            .spawn()?;

        let status = child.wait()?;
        if !status.success() {
            return Err(Error::new(ErrorKind::Other,
                "Process exited with non-zero exit code"));
        }
        trace!("elapsed: {:#?}", timer.elapsed());

        Ok(())
    }
}