-
Notifications
You must be signed in to change notification settings - Fork 4
/
metadata_parse.go
98 lines (89 loc) · 2.26 KB
/
metadata_parse.go
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
package problems
import (
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/google/logger"
"gopkg.in/yaml.v2"
toolspb "github.com/jsannemo/omogenjudge/problemtools/api"
"github.com/jsannemo/omogenjudge/problemtools/util"
)
type problemLimits struct {
Multiplier int32
Time float64
Memory int32
}
type problemJudging struct {
Limits problemLimits
}
type problemMetadata struct {
Author string
Source string
License string
Judging problemJudging
PublicFrom *time.Time `yaml:"public_from"`
}
func toLicense(l string, reporter util.Reporter) toolspb.License {
switch l {
case "permission":
return toolspb.License_BY_PERMISSION
case "cc by-sa 3":
return toolspb.License_CC_BY_SA_3
case "public domain":
return toolspb.License_PUBLIC_DOMAIN
case "private":
return toolspb.License_PRIVATE
}
reporter.Err("Invalid license: %v", l)
return toolspb.License_LICENSE_UNSPECIFIED
}
func parseMetadata(path string, reporter util.Reporter) (*toolspb.Metadata, error) {
metadataPath := filepath.Join(path, "problem.yaml")
logger.Infof("Looking for metadata path %s", metadataPath)
if _, err := os.Stat(metadataPath); os.IsNotExist(err) {
reporter.Err("There was no problem.yaml file")
return nil, nil
}
dat, err := ioutil.ReadFile(metadataPath)
if err != nil {
return nil, err
}
var md problemMetadata
err = yaml.Unmarshal(dat, &md)
if err != nil {
reporter.Err("Invalid problem yaml: %v", err)
return nil, nil
}
limits := md.Judging.Limits
timeLimit := limits.Time
timeMultiplier := limits.Multiplier
memLimit := limits.Memory
if memLimit == 0 {
memLimit = 1000
reporter.Warn("No explicit memory limit set: using default 1000 MB")
}
if timeMultiplier == 0 && timeLimit == 0 {
timeMultiplier = 4
}
lic := toLicense(md.License, reporter)
metadata := &toolspb.Metadata{
ProblemId: filepath.Base(path),
Limits: &toolspb.Limits{
TimeLimitMs: int32(1000 * timeLimit),
MemoryLimitKb: 1000 * memLimit,
TimeLimitMultiplier: timeMultiplier,
},
Author: md.Author,
Source: md.Source,
License: lic,
}
if md.PublicFrom != nil {
metadata.PublicFrom, err = ptypes.TimestampProto(*md.PublicFrom)
if err != nil {
return nil, err
}
}
return metadata, nil
}