-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
145 lines (115 loc) · 3.61 KB
/
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
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
package polygon
import (
"encoding/xml"
"errors"
"os"
"path/filepath"
"strings"
"github.com/mraron/njudge/pkg/language/langs/cpp"
"github.com/mraron/njudge/pkg/language/sandbox"
"github.com/mraron/njudge/pkg/problems"
"github.com/spf13/afero"
)
type Option func(*config)
func CompileBinaries(compile bool) Option {
return func(c *config) {
c.compileBinaries = compile
}
}
type config struct {
compileBinaries bool
}
func newConfig() *config {
return &config{compileBinaries: true}
}
func ParserAndIdentifier(opts ...Option) (problems.ConfigParser, problems.ConfigIdentifier) {
cfg := newConfig()
for _, opt := range opts {
opt(cfg)
}
parser := func(fs afero.Fs, path string) (problems.Problem, error) {
problemXML := filepath.Join(path, "problem.xml")
f, err := fs.Open(problemXML)
if err != nil {
return nil, err
}
defer f.Close()
p := Problem{config: cfg}
dec := xml.NewDecoder(f)
if err := dec.Decode(&p); err != nil {
return nil, err
}
p.Path = path
list, err := afero.ReadDir(fs, filepath.Join(path, "statements"))
if err == nil {
for _, dir := range list {
if !dir.IsDir() || strings.HasPrefix(dir.Name(), ".") {
continue
}
jsonStmt, err := ParseJSONStatement(fs, filepath.Join(path, "statements", dir.Name()))
if err != nil {
return nil, err
}
if jsonStmt == nil {
continue
}
// problem-properties.json might be outdated. problem.xml should take priority
jsonStmt.InputFile, jsonStmt.OutputFile = p.InputOutputFiles()
jsonStmt.TimeLimit = p.TimeLimit()
jsonStmt.MemoryLimit = p.MemoryLimit()
p.JSONStatementList = append(p.JSONStatementList, *jsonStmt)
contents, err := jsonStmt.Html()
if err != nil {
return nil, err
}
p.GeneratedStatementList = append(p.GeneratedStatementList, problems.BytesData{Loc: dir.Name(), Val: contents, Typ: "text/html"})
}
}
for _, stmt := range p.StatementList {
statementPath := filepath.Join(path, stmt.Path)
cont, err := afero.ReadFile(fs, statementPath)
if err != nil {
return nil, err
}
p.GeneratedStatementList = append(p.GeneratedStatementList, problems.BytesData{Loc: stmt.Language, Val: cont, Typ: stmt.Type})
}
if cfg.compileBinaries {
workingDirectory := p.Path
if _, err := fs.Stat(filepath.Join(p.Path, "files")); !errors.Is(err, os.ErrNotExist) {
workingDirectory = filepath.Join(p.Path, "files")
}
checkerPath := p.Assets.Checker.Source.Path
if checkerPath == "" {
checkerPath = "check.cpp"
}
if err := cpp.AutoCompile(fs, sandbox.NewDummy(), workingDirectory, filepath.Join(p.Path, checkerPath), filepath.Join(p.Path, "check")); err != nil {
return nil, err
}
if p.Assets.Interactor.Source.Path != "" {
if err := cpp.AutoCompile(fs, sandbox.NewDummy(), workingDirectory, filepath.Join(p.Path, p.Assets.Interactor.Source.Path), filepath.Join(p.Path, "files/interactor")); err != nil {
return nil, err
}
}
}
for _, val := range p.Assets.Attachments {
attachmentLocation := filepath.Join(path, val.Location)
contents, err := afero.ReadFile(fs, attachmentLocation)
if err != nil {
return nil, err
}
p.AttachmentsList = append(p.AttachmentsList, problems.BytesData{Nam: val.Name, Val: contents})
}
return p, nil
}
identifier := func(fs afero.Fs, path string) bool {
_, err := fs.Stat(filepath.Join(path, "problem.xml"))
return !os.IsNotExist(err)
}
return parser, identifier
}
func init() {
parser, identifier := ParserAndIdentifier(CompileBinaries(true))
if err := problems.RegisterConfigType("polygon", parser, identifier); err != nil {
panic(err)
}
}