Skip to content

Commit e518842

Browse files
committed
Add batch create
1 parent 8c6b724 commit e518842

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

commands/batch.go

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package commands
2+
3+
import (
4+
"os"
5+
"path"
6+
template2 "text/template"
7+
8+
"github.com/chermehdi/egor/config"
9+
"github.com/chermehdi/egor/templates"
10+
"github.com/fatih/color"
11+
"io/ioutil"
12+
13+
"github.com/urfave/cli/v2"
14+
)
15+
16+
type Batcher struct {
17+
judge Judge
18+
}
19+
20+
func (b *Batcher) Setup() error {
21+
b.judge.Setup()
22+
return nil
23+
}
24+
25+
func (b *Batcher) Run() error {
26+
return nil
27+
}
28+
29+
func (b *Batcher) Cleanup() error {
30+
b.judge.Cleanup()
31+
return nil
32+
}
33+
34+
func CreateBatch(context *cli.Context) error {
35+
configuration, err := config.LoadDefaultConfiguration()
36+
if err != nil {
37+
return err
38+
}
39+
cwd, err := os.Getwd()
40+
if err != nil {
41+
return err
42+
}
43+
egorMetaFile := path.Join(cwd, configuration.ConfigFileName)
44+
egorMeta, err := config.LoadMetaFromPath(egorMetaFile)
45+
if err != nil {
46+
return err
47+
}
48+
49+
if egorMeta.HasBatch() {
50+
color.Red("The task already has a batch file, Aborting...")
51+
return nil
52+
}
53+
54+
genPath := path.Join(cwd, "gen.cpp")
55+
egorMeta.BatchFile = genPath
56+
// Create the generator file
57+
58+
genTemp := template2.New("Solution template")
59+
compiledTemplate, err := genTemp.Parse(templates.GeneratorTemplate)
60+
if err != nil {
61+
return err
62+
}
63+
genFile, err := config.CreateFile(genPath)
64+
if err != nil {
65+
return nil
66+
}
67+
// TODO(chermehdi): Move this outside, it smells of code repetition
68+
if err = compiledTemplate.Execute(genFile, configuration); err != nil {
69+
return err
70+
}
71+
randH := path.Join(cwd, "rand.h")
72+
// Create the rand.h file
73+
if err = ioutil.WriteFile(randH, []byte(templates.RandH), 0755); err != nil {
74+
return err
75+
}
76+
// Update the metadata
77+
fileName := path.Join(cwd, configuration.ConfigFileName)
78+
if err = egorMeta.SaveToFile(fileName); err != nil {
79+
return err
80+
}
81+
color.Green("Batch creation completed successfully")
82+
return nil
83+
}
84+
85+
func RunBatch(context *cli.Context) error {
86+
configuration, err := config.LoadDefaultConfiguration()
87+
if err != nil {
88+
return err
89+
}
90+
cwd, err := os.Getwd()
91+
if err != nil {
92+
return err
93+
}
94+
egorMetaFile := path.Join(cwd, configuration.ConfigFileName)
95+
egorMeta, err := config.LoadMetaFromPath(egorMetaFile)
96+
if err != nil {
97+
return err
98+
}
99+
100+
judge, err := NewJudgeFor(egorMeta, configuration, getChecker(context.String("checker")))
101+
if err != nil {
102+
return err
103+
}
104+
if err := judge.Setup(); err != nil {
105+
return err
106+
}
107+
defer judge.Cleanup()
108+
report := newJudgeReport()
109+
110+
for i, input := range egorMeta.Inputs {
111+
output := egorMeta.Outputs[i]
112+
caseDescription := NewCaseDescription(input, output, egorMeta.TimeLimit)
113+
status := judge.RunTestCase(*caseDescription)
114+
report.Add(status, *caseDescription)
115+
}
116+
report.Display()
117+
return nil
118+
}
119+
120+
var BatchCommand = cli.Command{
121+
Name: "batch",
122+
Aliases: []string{"b"},
123+
Usage: "Create and Run bach tests",
124+
UsageText: "egor batch (create | run)",
125+
Subcommands: []*cli.Command{
126+
{
127+
Name: "create",
128+
Aliases: []string{"c"},
129+
Usage: "Create the template for the batch test",
130+
Action: CreateBatch,
131+
},
132+
{
133+
Name: "run",
134+
Aliases: []string{"r"},
135+
Usage: "Run the batch test",
136+
Action: RunBatch,
137+
},
138+
},
139+
}

config/meta.go

+15
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ type EgorMeta struct {
4646
Outputs []IoFile
4747
TaskFile string
4848
TimeLimit float64
49+
// Path to the file containing the batch generator.
50+
// Not setting this value implies that the task does not contain a Batch file.
51+
BatchFile string
4952
}
5053

5154
// Resolves the task file given the default language.
@@ -82,6 +85,7 @@ func NewEgorMeta(task Task, config Config) EgorMeta {
8285
Outputs: outputs,
8386
TaskFile: taskFile,
8487
TimeLimit: task.TimeLimit,
88+
BatchFile: "",
8589
}
8690
}
8791

@@ -91,6 +95,17 @@ func (egor *EgorMeta) CountTestCases() int {
9195
return len(egor.Inputs)
9296
}
9397

98+
func (egor *EgorMeta) HasBatch() bool {
99+
if egor.BatchFile == "" {
100+
return false
101+
}
102+
// If the path points to some deleted file, it's considered none existing.
103+
if _, err := os.Stat(egor.BatchFile); os.IsNotExist(err) {
104+
return false
105+
}
106+
return true
107+
}
108+
94109
func (egor *EgorMeta) toJson() (string, error) {
95110
var buffer bytes.Buffer
96111
encoder := json2.NewEncoder(&buffer)

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func main() {
4949
&commands.PrintCaseCommand,
5050
&commands.TestCommand,
5151
&commands.CreateTaskCommand,
52+
&commands.BatchCommand,
5253
},
5354
}
5455

templates/resolver.go

+28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@ import (
77
"io/ioutil"
88
)
99

10+
// This is embeded as a literal string for easy shipping with the binary.
11+
// We could consider using some new Go feature to embed it as a static resource.
12+
// At the time of creation of this, this is not a priority.
13+
const RandH = `
14+
class Rand {
15+
public:
16+
Rand(int argc, char** argv) {
17+
}
18+
};
19+
`
20+
21+
const GeneratorTemplate = `
22+
//
23+
// Created by egor https://github.com/chermehdi/egor
24+
// {{if .Author }}
25+
// @author {{ .Author }}
26+
{{end}}
27+
#include <iostream>
28+
#include <vector>
29+
#include "rand.h"
30+
31+
using namespace std;
32+
33+
int main(int argc, char** argv) {
34+
// Do not remove this line
35+
Rand rand(argc, argv);
36+
}
37+
`
1038
const CppTemplate = `
1139
//
1240
// Created by egor https://github.com/chermehdi/egor

0 commit comments

Comments
 (0)