summaryrefslogtreecommitdiffstats
path: root/src/backends/python.rs
blob: e3569b11ae1657076b5368e1f5acdc083c4c9b16 (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
44
45
use serde_derive::{ Serialize, Deserialize };
use crate::backends::{ Backend, RunError };
use std::process::{ Command };
use std::path::Path;
use crate::wait::{ wait_child, WaitInfo };

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


impl PythonBackend {
    fn get_interpreter(&self) -> String {
        format!(
            "python{}",
            self.version
                .as_ref()
                .unwrap_or(&String::new())
        )
    }
}


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) -> Result<WaitInfo, RunError> {
        let timer = std::time::Instant::now();

        let child = Command::new(self.get_interpreter())
            .arg(fname.as_os_str())
            .spawn()?;

        let timeout = std::time::Duration::from_secs_f32(self.timeout.unwrap_or(1.0));
        Ok(wait_child(child, timeout, timer)?)
    }
}