summaryrefslogtreecommitdiffstats
path: root/src/backends/python.rs
blob: 39827a7553669ba0bbdec06dd0cedce55d0ee2cb (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use serde_derive::{ Serialize, Deserialize };
use crate::backends::Backend;
use std::process::{ Command, Stdio };
use std::path::Path;
use std::io::{ Error, ErrorKind };
use std::fs::File;
use log::{ trace, warn, error };
use wait_timeout::ChildExt;

#[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) -> std::io::Result<()> {
        let stdio = match self.try_guess_test_file(fname) {
            Some(test_filename) => match File::open(test_filename) {
                Ok(test_content) => Stdio::from(test_content),
                Err(err) => {
                    warn!("Could not open test file. Fallback to piped: {}", err);
                    Stdio::piped()
                }
            },
            None => Stdio::piped()
        };

        let timer = std::time::SystemTime::now();

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

        let timeout = std::time::Duration::from_secs_f32(self.timeout.unwrap_or(1.0));
        match child.wait_timeout(timeout) {
            Ok(maybe_status) => match maybe_status {
                Some(status) => {
                    trace!("elapsed: {:#?}", timer.elapsed());
                    if !status.success() {
                        return Err(Error::new(ErrorKind::Other,
                            "process exited with non-zero exit code"));
                    }
                    Ok(())
                },
                None => {
                    warn!("timed out: {:#?}", timer.elapsed());
                    child.kill()?;
                    child.wait()?;
                    Err(Error::new(ErrorKind::TimedOut,
                        "process timed out"))
                }
            },
            Err(err) => {
                error!("could not wait for child: {}", err);
                child.kill()?;
                child.wait()?; // Wait defunct
                Err(err)
            }
        }
    }
}