-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
283 lines (247 loc) · 6.26 KB
/
main.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
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
package main
import (
"bufio"
"bytes"
_ "embed"
"errors"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
"path/filepath"
"github.com/alecthomas/kong"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)
// embedded testdata.
var (
//go:embed testdata/fcfs.csv
fcfsIn []byte
//go:embed testdata/fcfs.out
fcfsOut []byte
//go:embed testdata/sjf.csv
sjfIn []byte
//go:embed testdata/sjf.out
sjfOut []byte
//go:embed testdata/sjfp.csv
sjfpIn []byte
//go:embed testdata/sjfp.out
sjfpOut []byte
//go:embed testdata/rr.csv
rrIn []byte
//go:embed testdata/rr.out
rrOut []byte
)
type (
grammar struct {
options
PathToDir string `name:"dir" default:"." help:"Path to scheduler directory" type:"path" required:"true"`
}
options struct {
Debug bool `help:"Debug output."`
Total bool `help:"Print total only"`
}
)
func main() {
if err := kong.Parse(&grammar{},
kong.Name("gradebot"),
kong.Description("Gradebot 9000 is a tool to grade your 4600 project 1."),
kong.UsageOnError(),
).Run(); err != nil {
slog.Error("error running gradebot", slog.String("err", err.Error()))
}
pauseForInput(os.Stdout, os.Stdin)
}
func pauseForInput(w io.Writer, r io.Reader) {
_, _ = fmt.Fprintf(w, "press 'return' key to continue...")
input := bufio.NewScanner(r)
input.Scan()
}
type (
Context struct {
srcDir string
binary string
}
Check func(*Context) (Result, error)
Result struct {
label string
awarded int
possible int
message string
}
)
func (o *options) setup() {
// Set up logging.
lvl := new(slog.LevelVar)
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: lvl,
}))
if o.Debug {
lvl.Set(slog.LevelDebug)
}
if o.Total {
lvl.Set(10)
}
slog.SetDefault(logger)
}
func (cmd grammar) Run() error {
cmd.options.setup()
var (
rubric Context
results = make([]Result, 0)
)
rubric.srcDir = cmd.PathToDir
for _, check := range []Check{
CheckCompilable,
CheckScreenshotExists,
CheckREADMEExists,
CheckScheduler(Result{
label: "First-come, first-serve scheduling",
possible: 20,
}, "-fcfs", fcfsIn, fcfsOut),
CheckScheduler(Result{
label: "Shortest-job-first scheduling",
possible: 20,
}, "-sjf", sjfIn, sjfOut),
CheckScheduler(Result{
label: "Shortest-job-first with priority scheduling",
possible: 20,
}, "-sjfp", sjfpIn, sjfpOut),
CheckScheduler(Result{
label: "Round-robin scheduling",
possible: 10,
}, "-rr", rrIn, rrOut),
} {
result, err := check(&rubric)
if err != nil {
slog.Error(result.label, slog.String("err", err.Error()))
}
results = append(results, result)
}
printRubricResults(cmd.Total, results...)
// cleanup
_ = os.RemoveAll(rubric.binary)
return nil
}
func printRubricResults(onlyTotal bool, results ...Result) {
if onlyTotal {
totalPoints := 0
for i := range results {
totalPoints += results[i].awarded
}
fmt.Println(totalPoints)
return
}
t := table.NewWriter()
t.AppendHeader(table.Row{"Rubric Item", "Error?", "Possible", "Awarded"})
t.SetStyle(table.StyleRounded)
t.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AlignFooter: text.AlignRight},
})
var (
possiblePoints int
totalPoints int
)
for i := range results {
t.AppendRow([]any{results[i].label, results[i].message, results[i].possible, results[i].awarded})
possiblePoints += results[i].possible
totalPoints += results[i].awarded
}
t.AppendFooter(table.Row{"", "Total", possiblePoints, totalPoints})
fmt.Println(t.Render())
}
//region Checkers
func CheckCompilable(c *Context) (Result, error) {
result := Result{
label: "Compilable",
awarded: 0,
possible: 10,
}
if err := os.Chdir(c.srcDir); err != nil {
return result, err
}
// check for Go in path.
if _, err := exec.LookPath("go"); err != nil {
result.message = "Go executable not found in path"
return result, err
}
// compile the scheduler.
if err := exec.Command("go", "build", "-o", "scheduler.bin").Run(); err != nil {
result.message = "scheduler is not compileable"
return result, err
}
c.binary = filepath.Join(c.srcDir, "scheduler.bin")
result.awarded += 10
slog.Debug("scheduler is compileable", slog.Int("pts", 10))
return result, nil
}
func CheckScreenshotExists(c *Context) (Result, error) {
result := Result{
label: "Screenshot exists",
awarded: 0,
possible: 10,
}
if c.binary == "" {
result.message = "scheduler was not compileable"
return result, errors.New("binary not found")
}
if _, err := os.Stat(filepath.Join(c.srcDir, "screenshot.png")); err != nil {
result.message = "screenshot.png not found"
return result, err
}
result.awarded += 10
slog.Debug("screenshot.png exists", slog.Int("pts", 10))
return result, nil
}
func CheckREADMEExists(c *Context) (Result, error) {
result := Result{
label: "README.md exists",
awarded: 0,
possible: 10,
}
if c.binary == "" {
result.message = "scheduler was not compileable"
return result, errors.New("binary not found")
}
if _, err := os.Stat(filepath.Join(c.srcDir, "README.md")); err != nil {
result.message = "README.md not found"
return result, err
}
result.awarded += 10
slog.Debug("README.md exists", slog.Int("pts", 10))
return result, nil
}
func CheckScheduler(result Result, flag string, in, out []byte) func(c *Context) (Result, error) {
return func(c *Context) (Result, error) {
if c.binary == "" {
result.message = "scheduler was not compileable"
return result, errors.New("binary not found")
}
// run the scheduler
cmd := exec.Command(c.binary, flag)
// send embedded csv to stdin.
cmd.Stdin = bytes.NewReader(in)
var bb bytes.Buffer
cmd.Stdout = &bb
if err := cmd.Run(); err != nil {
result.message = "scheduler exited with error"
return result, err
}
if bb.String() == "" {
result.message = "scheduler ran with no output"
return result, nil
}
// compare output to expected output
if bytes.Compare(bb.Bytes(), out) != 0 {
result.message = "output does not match expected"
fmt.Print(flag, " expected:\n", string(out))
fmt.Print(flag, " actual:\n", bb.String())
return result, errors.New("output does not match expected")
}
result.awarded = result.possible
slog.Debug(fmt.Sprintf("%v Scheduler output matches expected", flag), slog.Int("pts", 30))
return result, nil
}
}
//endregion