|
1 | 1 | use std::collections::HashMap; |
| 2 | +use std::fs::canonicalize; |
| 3 | +use std::path::PathBuf; |
2 | 4 |
|
3 | 5 | use lazy_static::lazy_static; |
| 6 | +use log::info; |
| 7 | +use path_absolutize::*; |
4 | 8 |
|
5 | | -use crate::context::CatBoxOption; |
| 9 | +use crate::context::{CatBoxBuilder, CatBoxOption}; |
6 | 10 | use crate::error::CatBoxError; |
| 11 | +use crate::preset::default::{CPP_PRESET, C_PRESET}; |
| 12 | +use crate::preset::preset::UserType; |
| 13 | +use crate::Commands; |
7 | 14 |
|
8 | 15 | mod default; |
9 | 16 | mod preset; |
@@ -42,28 +49,72 @@ fn detect_language(language: &Option<String>, submission: &String) -> Option<Str |
42 | 49 | } |
43 | 50 | } |
44 | 51 |
|
45 | | -pub fn make_compile_params( |
46 | | - language: Option<String>, |
47 | | - submission: String, |
48 | | - _output: String, |
49 | | -) -> Result<CatBoxOption, CatBoxError> { |
50 | | - let language = detect_language(&language, &submission) |
51 | | - .ok_or(CatBoxError::cli("Can not detect submission language"))?; |
| 52 | +pub(crate) fn make_compile_params( |
| 53 | + mut builder: CatBoxBuilder, |
| 54 | + command: Commands, |
| 55 | +) -> Result<CatBoxBuilder, CatBoxError> { |
| 56 | + if let Commands::Compile { |
| 57 | + language, |
| 58 | + submission, |
| 59 | + output, |
| 60 | + .. |
| 61 | + } = command |
| 62 | + { |
| 63 | + let language = detect_language(&language, &submission) |
| 64 | + .ok_or(CatBoxError::cli("Can not detect submission language"))?; |
52 | 65 |
|
53 | | - unimplemented!() |
54 | | - // match language.as_str() { |
55 | | - // "c" => { |
56 | | - // let args = vec![]; |
57 | | - // let params = CatBoxOption::new("g++", args); |
58 | | - // Ok(params) |
59 | | - // } |
60 | | - // "cpp" => { |
61 | | - // let args = vec![]; |
62 | | - // let params = CatBoxOption::new("g++", args); |
63 | | - // Ok(params) |
64 | | - // } |
65 | | - // _ => { |
66 | | - // unimplemented!() |
67 | | - // } |
68 | | - // } |
| 66 | + let preset = match language.as_str() { |
| 67 | + "c" => C_PRESET.clone(), |
| 68 | + "cpp" => CPP_PRESET.clone(), |
| 69 | + default => return Err(CatBoxError::cli("Can not find language preset")), |
| 70 | + }; |
| 71 | + |
| 72 | + info!("Compile language {}", &language); |
| 73 | + |
| 74 | + let submission = PathBuf::from(&submission); |
| 75 | + let submission = submission.absolutize().unwrap(); |
| 76 | + let submission_dir = submission.parent().unwrap(); |
| 77 | + let output = PathBuf::from(&output); |
| 78 | + let output = output.absolutize().unwrap(); |
| 79 | + let output_dir = output.parent().unwrap(); |
| 80 | + |
| 81 | + for command in preset.compile.commands.iter() { |
| 82 | + let option_builder = builder |
| 83 | + .command( |
| 84 | + command.apply_program(submission.to_str().unwrap(), output.to_str().unwrap()), |
| 85 | + command.apply_arguments(submission.to_str().unwrap(), output.to_str().unwrap()), |
| 86 | + ) |
| 87 | + .time_limit(command.time_limit) |
| 88 | + .memory_limit(command.memory_limit) |
| 89 | + .set_process(Some(command.process)) |
| 90 | + .set_chroot(command.chroot) |
| 91 | + .mount_read(submission_dir, submission_dir) |
| 92 | + .mount_write(output_dir, output_dir) |
| 93 | + .disable_ptrace(); |
| 94 | + |
| 95 | + let mut option_builder = match command.user { |
| 96 | + UserType::Nobody => option_builder, |
| 97 | + UserType::Current => option_builder.current_user(), |
| 98 | + UserType::Root => { |
| 99 | + unimplemented!() |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + for feat in command.ptrace.iter() { |
| 104 | + option_builder = option_builder.ptrace(feat.clone()) |
| 105 | + } |
| 106 | + for mount_point in command.mounts.iter() { |
| 107 | + option_builder = option_builder.mount(mount_point.clone()) |
| 108 | + } |
| 109 | + for (key, value) in command.env.iter() { |
| 110 | + option_builder = option_builder.env(key, value); |
| 111 | + } |
| 112 | + |
| 113 | + builder = option_builder.done(); |
| 114 | + } |
| 115 | + |
| 116 | + Ok(builder) |
| 117 | + } else { |
| 118 | + Err(CatBoxError::cli("unreachable")) |
| 119 | + } |
69 | 120 | } |
0 commit comments