medal/
contestreader_yaml.rs

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*  medal                                                                                                            *\
 *  Copyright (C) 2022  Bundesweite Informatikwettbewerbe, Robert Czechowski                                                            *
 *                                                                                                                   *
 *  This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero        *
 *  General Public License as published  by the Free Software Foundation, either version 3 of the License, or (at    *
 *  your option) any later version.                                                                                  *
 *                                                                                                                   *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the       *
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public      *
 *  License for more details.                                                                                        *
 *                                                                                                                   *
 *  You should have received a copy of the GNU Affero General Public License along with this program.  If not, see   *
\*  <http://www.gnu.org/licenses/>.                                                                                  */

use db_objects::{Contest, Task, Taskgroup};

use serde_yaml;
use std::path::Path;

extern crate time;

/// `ContestYaml` defines the content of a YAML file describing a contest.
///
/// Example:
///
/// ```yaml
/// name: "Funny contest"
/// duration_minutes: 0
/// public_listing: true
/// protected: false
/// requires_login: true
/// requires_contest:
///   - "Another contest"
///   - "Yet another contest"
/// participation_start: "2019-03-28T00:00:01+01:00",
/// participation_end: "2019-04-05T23:59:59+01:00",
/// review_start: "2019-03-28T00:00:01+01:00",
/// review_end: "2019-04-05T23:59:59+01:00",
/// min_grade: 1,
/// max_grade: 8,
/// team_participation: true,
/// category: "contests",
/// image: "funny.png",
/// language: blockly,
/// tags:
///   - funny
///   - contest
///   - blockly
/// message: "This is a <em>really</em> funny contest!"
/// position: 1,
/// secret: "telefonbuch",
/// tasks:
///   "Task 1": "task/for/three/stars"
///   "Task 2":
///     - "subtask/for/two/stars"
///     - "subtask/for/three/stars"
///   "Task 3":
///     "task/for/four/stars": {stars: 4}
///   "Task 4":
///     "subtask/for/four/stars": {stars: 4}
///     "subtask/for/five/stars": {}
/// ```
#[derive(Debug, Deserialize)]
struct ContestYaml {
    /// The name of the contest.
    ///
    /// This value must be set!
    name: Option<String>,

    /// The duration of the contest, in minutes. If set to 0, the contest can be
    /// accessed indefinitely. If set to something else, the contest will require a
    /// login to participate in order to enforce the time limit.
    ///
    /// This value must be set!
    duration_minutes: Option<i32>,

    /// Set to `true` to show the contest in the list of contest. Note that contests
    /// might still be hidden immediately or later after `participation_end` has
    /// passed.
    public_listing: Option<bool>,

    /// Set to `true` to protect contest participations. Protected contest
    /// participations can not be deleted by teachers – only by admins.
    protected: Option<bool>,

    /// Set to `true` to require a login for participations. Useful for contests
    /// with `duration_minutes` set to `0`.
    requires_login: Option<bool>,

    /// List of contests, of which one the users is required to have a participation
    /// in, in order to participate in the contest defined by this very config.
    requires_contest: Option<Vec<String>>,

    /// Date and time of the start of the contest. If not set, the contest can be
    /// started at any time before `participation_end`.
    participation_start: Option<String>,

    /// Date and time of the end of the contest. If not set, the contest can be
    /// started at any time after `participation_start`. Started contests can stil
    /// accessed during their duration even if `participation_end` is in the past.
    participation_end: Option<String>,

    /// Start of the review mode. Sensible values are the same value as
    /// `participation_start` to allow users to review their submissions right
    /// after participating, or the same value as `participation_end` to allow users
    /// to review their submissions after the contest has ended. One of
    /// `review_start` and `review_end` must be set to enable the review mode!
    review_start: Option<String>,

    /// End of the review mode. One of `review_start` and `review_end` must be set
    /// to enable the review mode!
    review_end: Option<String>,

    /// The minimum grade of the user for this contest to participate in.
    min_grade: Option<i32>,

    /// The maximum grade of the user for this contest to participate in.
    max_grade: Option<i32>,

    /// Set to `true` to allow for team participations.
    team_participation: Option<bool>,

    /// The category for this contest to be shown in.
    category: Option<String>,

    /// An image to be shown for this contest in the list of contests.
    image: Option<String>,

    /// The language of the contest. Can be set to `blockly` or `python`.
    language: Option<String>,

    /// A list of tags to annotate the contest with. Contests can be filtered
    /// by tag on the contest list pages.
    tags: Option<Vec<String>>,

    /// A message to be displayed at the contest (in a box or similar). Might
    /// contain HTML, which will not be escaped by the platform.
    message: Option<String>,

    /// A number to order the contests by in the contest list. Contest will be
    /// ordered by DESCENDING position number.
    position: Option<i32>,

    /// A password required to start a participation in this contest. Also allows
    /// contests to be searched by this value, so this `secret` should be reused
    /// between contests ONLY if the intended audience is exactly the same.
    secret: Option<String>,

    /// The mapping of task names to task information.
    ///
    /// A task information can be either:
    ///  - A path of a task. The task will be worth 3 stars.
    ///  - A list of paths to subtasks. The first subtask will be worth 2 stars,
    ///    each additional subtask will be worth one additional star.
    ///  - A mapping of paths to additional information. The additional
    ///    information is a mapping that currently only has one pemitted key,
    ///    `stars` with the value being the number of stars for this task/subtask.
    ///
    /// If `language` is not set, each path for an algorea-style task has to be
    /// prefixed with either `B` or `P` in order to enable the algorea task wrapper
    /// for Blockly or Python, respectively.
    tasks: Option<serde_yaml::Mapping>,
}

/// `TaskYaml` defines the content of a YAML file describing a standalone task.
///
/// The file must be named `task.yaml`.
///
/// Example:
/// ```yaml
/// name: "Funny task"
/// standalone: true
/// public_listing: false
/// image: "preview.png"
/// languages:
///   - blockly
///   - python
/// tags:
///   - easy
///   - task
///   - blockly
///   - python
/// position: 1
/// ```
#[derive(Debug, Deserialize)]
struct TaskYaml {
    /// The name of the task.
    ///
    /// This value must be set!
    name: Option<String>,

    /// Set to `true` to define this task as a standalone task.
    ///
    /// This value must be set to `true`!
    standalone: Option<bool>,

    /// Set to `true` to show the task in the list of standalone tasks.
    public_listing: Option<bool>,

    /// An image to be shown for this task in the list of standalone tasks..
    image: Option<String>,

    /// A list of the languages that the task should be available in. Possible
    /// values are `blockly` or `python`.
    languages: Option<Vec<String>>,

    /// A list of tags to annotate the contest with. Contests can be filtered
    /// by tag on the contest list pages.
    tags: Option<Vec<String>>,

    /// A number to order the task by in the list of standalone tasks. Tasks will be
    /// ordered by DESCENDING position number.
    position: Option<i32>,
}

use self::time::{strptime, Timespec};

fn parse_timespec(time: String, key: &str, directory: &str, filename: &str) -> Timespec {
    strptime(&time, &"%FT%T%z").map(|t| t.to_timespec())
                               .unwrap_or_else(|_| {
                                   panic!("Time value '{}' could not be parsed in {}{}", key, directory, filename)
                               })
}

// The task path is stored relatively to the contest.yaml for easier identificationy
// Concatenation happens in functions::show_task
fn parse_contest_yaml(content: &str, filename: &str, directory: &str) -> Option<Vec<Contest>> {
    let config: ContestYaml = match serde_yaml::from_str(&content) {
        Ok(contest) => contest,
        Err(e) => {
            eprintln!();
            eprintln!("{}", e);
            eprintln!("Error loading contest YAML: {}{}", directory, filename);
            panic!("Loading contest file")
        }
    };

    let start: Option<Timespec> =
        config.participation_start.map(|x| parse_timespec(x, "participation_start", directory, filename));
    let end: Option<Timespec> =
        config.participation_end.map(|x| parse_timespec(x, "participation_end", directory, filename));
    let review_start: Option<Timespec> =
        config.review_start.map(|x| parse_timespec(x, "review_start", directory, filename));
    let review_end: Option<Timespec> = config.review_end.map(|x| parse_timespec(x, "review_end", directory, filename));

    let review_start = if review_end.is_none() {
        review_start
    } else if let Some(end) = end {
        Some(review_start.unwrap_or(end))
    } else {
        review_start
    };

    let mut contest =
        Contest { id: None,
                  location: directory.to_string(),
                  filename: filename.to_string(),
                  name: config.name.unwrap_or_else(|| panic!("'name' missing in {}{}", directory, filename)),
                  duration:
                      config.duration_minutes
                            .unwrap_or_else(|| panic!("'duration_minutes' missing in {}{}", directory, filename)),
                  public: config.public_listing.unwrap_or(false),
                  start,
                  end,
                  review_start,
                  review_end,
                  min_grade: config.min_grade,
                  max_grade: config.max_grade,
                  max_teamsize: if config.team_participation.unwrap_or(false) { Some(2) } else { None },
                  positionalnumber: config.position,
                  protected: config.protected.unwrap_or(false),
                  requires_login: config.requires_login,
                  // Consumed by `let required_contests = contest.requires_contest.as_ref()?.split(',');` in core.rs
                  requires_contest: config.requires_contest.map(|list| list.join(",")),
                  secret: config.secret,
                  message: config.message,
                  image: config.image,
                  language: config.language.clone(),
                  category: config.category,
                  standalone_task: None,
                  tags: config.tags.unwrap_or_else(Vec::new),
                  taskgroups: Vec::new() };
    // TODO: Timeparsing should fail more pleasantly (-> Panic, thus shows message)

    for (positionalnumber, (name, info)) in config.tasks?.into_iter().enumerate() {
        if let serde_yaml::Value::String(name) = name {
            let mut taskgroup = Taskgroup::new(name, Some(positionalnumber as i32));
            match info {
                serde_yaml::Value::String(taskdir) => {
                    let task = Task::new(taskdir, config.language.clone(), 3);
                    taskgroup.tasks.push(task);
                }
                serde_yaml::Value::Sequence(taskdirs) => {
                    let mut stars = 2;
                    for taskdir in taskdirs {
                        if let serde_yaml::Value::String(taskdir) = taskdir {
                            let task = Task::new(taskdir, config.language.clone(), stars);
                            taskgroup.tasks.push(task);
                        } else {
                            panic!("Invalid contest YAML: {}{} (a)", directory, filename)
                        }

                        stars += 1;
                    }
                }
                serde_yaml::Value::Mapping(taskdirs) => {
                    let mut stars = 2;
                    for (taskdir, taskinfo) in taskdirs {
                        if let (serde_yaml::Value::String(taskdir), serde_yaml::Value::Mapping(taskinfo)) =
                            (taskdir, taskinfo)
                        {
                            if let Some(serde_yaml::Value::Number(cstars)) =
                                taskinfo.get(&serde_yaml::Value::String("stars".to_string()))
                            {
                                stars = cstars.as_u64().unwrap() as i32;
                            }
                            let task = Task::new(taskdir, config.language.clone(), stars);
                            taskgroup.tasks.push(task);
                            stars += 1;
                        } else {
                            panic!("Invalid contest YAML: {}{} (b)", directory, filename)
                        }
                    }
                }
                _ => panic!("Invalid contest YAML: {}{} (c)", directory, filename),
            }
            contest.taskgroups.push(taskgroup);
        } else {
            panic!("Invalid contest YAML: {}{} (d)", directory, filename)
        }
    }

    Some(vec![contest])
}

#[derive(Debug)]
enum ConfigError {
    #[allow(dead_code)]
    ParseError(serde_yaml::Error),
    MissingField,
}

fn parse_task_yaml(content: &str, filename: &str, directory: &str) -> Result<Vec<Contest>, ConfigError> {
    let config: TaskYaml = serde_yaml::from_str(&content).map_err(ConfigError::ParseError)?;

    // Only handle tasks with standalone = true
    if config.standalone != Some(true) {
        return Err(ConfigError::MissingField);
    }

    let languages = config.languages.ok_or(ConfigError::MissingField)?;

    if languages.len() == 0 {
        return Err(ConfigError::MissingField);
    }

    let mut contests = Vec::new();

    for language in languages {
        let name = config.name.clone().unwrap_or_else(|| panic!("'name' missing in {}{}", directory, filename));
        let mut contest = Contest { id: None,
                                    location: directory.to_string(),
                                    filename: format!("{}_{}", language, filename),
                                    name: name.clone(),
                                    // Task always are unlimited in time
                                    duration: 0,
                                    public: config.public_listing.unwrap_or(false),
                                    start: None,
                                    end: None,
                                    review_start: None,
                                    review_end: None,
                                    min_grade: None,
                                    max_grade: None,
                                    max_teamsize: None,
                                    positionalnumber: config.position,
                                    protected: false,
                                    requires_login: Some(false),
                                    requires_contest: None,
                                    secret: None,
                                    message: None,
                                    image: config.image.clone(),
                                    language: Some(language.clone()),
                                    category: Some("standalone_task".to_string()),
                                    standalone_task: Some(true),
                                    tags: config.tags.clone().unwrap_or_else(Vec::new),
                                    taskgroups: Vec::new() };

        let mut taskgroup = Taskgroup::new(name, None);
        let stars = 0;
        let taskdir = ".".to_string();
        let task = Task::new(taskdir, Some(language), stars);
        taskgroup.tasks.push(task);
        contest.taskgroups.push(taskgroup);

        contests.push(contest);
    }

    Ok(contests)
}

fn read_task_or_contest(p: &Path) -> Option<Vec<Contest>> {
    use std::fs::File;
    use std::io::Read;

    let mut file = File::open(p).unwrap();
    let mut contents = String::new();
    file.read_to_string(&mut contents).ok()?;

    let filename: &str = p.file_name().to_owned()?.to_str()?;

    if filename == "task.yaml" {
        parse_task_yaml(&contents, filename, &format!("{}/", p.parent().unwrap().to_str()?)).ok()
    } else {
        parse_contest_yaml(&contents, filename, &format!("{}/", p.parent().unwrap().to_str()?))
    }
}

use config::Config;

pub fn get_all_contest_info(task_dir: &str, config: &Config) -> Vec<Contest> {
    fn walk_me_recursively(p: &Path, contests: &mut Vec<Contest>, config: &Config) {
        if let Ok(paths) = std::fs::read_dir(p) {
            let mut paths: Vec<_> = paths.filter_map(|r| r.ok()).collect();
            paths.sort_by_key(|dir| dir.path());
            for path in paths {
                let p = path.path();
                walk_me_recursively(&p, contests, config);
            }
        }

        let filename = p.file_name().unwrap().to_string_lossy().to_string();
        if filename.ends_with(".yaml") {
            {
                use std::io::Write;
                print!(".");
                std::io::stdout().flush().unwrap();
            }

            let mut restricted = false;
            config.restricted_task_directories.as_ref().map(|restricted_task_directories| {
                let pathname = p.to_string_lossy().to_string();
                restricted_task_directories.iter().for_each(|restricted_task_directory| {
                    if pathname.starts_with(restricted_task_directory) {
                        restricted = true;
                    }
                });
            });

            if let Some(cs) = read_task_or_contest(p) {
                for c in cs {
                    if restricted {
                        if c.public {
                            println!("\nWARNING: Skipping public contest defined in '{}' due to being in a restricted directory!", p.display());
                            continue;
                        }
                        if c.secret.is_none() {
                            println!("\nWARNING: Contest defined in '{}' has no secret, can only be reached via id!",
                                     p.display());
                        }
                    }
                    contests.push(c);
                }
            }
        };
    }

    let mut contests = Vec::new();
    match std::fs::read_dir(task_dir) {
        Err(why) => eprintln!("Error opening tasks directory! {:?}", why.kind()),
        Ok(paths) => {
            for path in paths {
                walk_me_recursively(&path.unwrap().path(), &mut contests, config);
            }
        }
    };

    contests
}

#[test]
fn parse_contest_yaml_no_tasks() {
    let contest_file_contents = r#"
name: "JwInf 2020 Runde 1: Jgst. 3 – 6"
duration_minutes: 60
"#;

    let contest = parse_contest_yaml(contest_file_contents, "", "");
    assert!(contest.is_none());
}

#[test]
fn parse_contest_yaml_dates() {
    let contest_file_contents = r#"
name: "JwInf 2020 Runde 1: Jgst. 3 – 6"
participation_start: "2022-03-01T00:00:00+01:00"
participation_end: "2022-03-31T22:59:59+01:00"
duration_minutes: 60

tasks: {}
"#;

    let contest = parse_contest_yaml(contest_file_contents, "", "");
    assert!(contest.is_some());

    //let contest = contest.unwrap();

    // These tests are unfortunately dependent on the timezone the system is on. Skip them for now until we have found
    // a better solution.

    //assert_eq!(contest.start, Some(Timespec {sec: 1646089200, nsec: 0}));
    //assert_eq!(contest.end, Some(Timespec {sec: 1648763999, nsec: 0}));

    // Unix Timestamp 	1646089200
    // GMT 	Mon Feb 28 2022 23:00:00 GMT+0000
    // Your Time Zone 	Tue Mar 01 2022 00:00:00 GMT+0100 (Mitteleuropäische Normalzeit)

    // Unix Timestamp 	1648764000
    // GMT 	Thu Mar 31 2022 22:00:00 GMT+0000
    // Your Time Zone 	Fri Apr 01 2022 00:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)
}