Skip to content

Commit

Permalink
feat: upload tests; natstream internal
Browse files Browse the repository at this point in the history
  • Loading branch information
Gornak40 committed Aug 3, 2024
1 parent 2563782 commit 51bd44e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
43 changes: 43 additions & 0 deletions internal/natstream/natstream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package natstream

import (
"errors"
"os"
"path/filepath"

"github.com/facette/natsort"
)

var (
ErrEndStream = errors.New("no more files in natstream")
)

type NatStream struct {
files []string
idx int
}

func (ns *NatStream) Init(glob string) error {
files, err := filepath.Glob(glob)
if err != nil {
return err
}
ns.files = files
natsort.Sort(ns.files)
ns.idx = 0

return nil
}

func (ns *NatStream) Next() (string, error) {
if ns.idx == len(ns.files) {
return "", ErrEndStream
}
data, err := os.ReadFile(ns.files[ns.idx])
if err != nil {
return "", err
}
ns.idx++

return string(data), nil
}
11 changes: 11 additions & 0 deletions polygon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,17 @@ func (p *Polygon) SetInteractor(pID int, interactor string) error {
return err
}

func (p *Polygon) SaveScript(pID int, testset, source string) error {
link, params := p.buildURL("problem.saveScript", url.Values{
"problemId": []string{strconv.Itoa(pID)},
"testset": []string{testset},
"source": []string{source},
})
_, err := p.makeQuery(http.MethodPost, link, params)

return err
}

func (p *Polygon) SaveSolution(sr SolutionRequest) error {
link, params := p.buildURL("problem.saveSolution", url.Values(sr))
_, err := p.makeQuery(http.MethodPost, link, params)
Expand Down
5 changes: 4 additions & 1 deletion polygon/vydra/problemxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ type Test struct {
}

type TestSet struct {
Name string `xml:"name,attr"`
TimeLimit int `xml:"time-limit"`
MemoryLimit int `xml:"memory-limit"`
TestCount int `xml:"test-count"`
InputPathPattern string `xml:"input-path-pattern"`
OutputPathPattern string `xml:"output-path-pattern"`
AnswerPathPattern string `xml:"answer-path-pattern"`
Tests []Test `xml:"tests"`
Tests struct {
Tests []Test `xml:"test"`
} `xml:"tests"`
}

type Validator struct {
Expand Down
48 changes: 48 additions & 0 deletions polygon/vydra/vydra.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"

"github.com/Gornak40/algolymp/internal/natstream"
"github.com/Gornak40/algolymp/polygon"
"github.com/sirupsen/logrus"
)
Expand All @@ -31,12 +33,14 @@ type Vydra struct {
client *polygon.Polygon
pID int
prob ProblemXML
stream *natstream.NatStream
}

func NewVydra(client *polygon.Polygon, pID int) *Vydra {
return &Vydra{
client: client,
pID: pID,
stream: new(natstream.NatStream),
}
}

Expand Down Expand Up @@ -229,6 +233,39 @@ func (v *Vydra) initProblem(judge *Judging) error {
return v.client.UpdateInfo(pr)
}

// TODO: add points, groups, etc.
func (v *Vydra) uploadTest(testset string, idx int, test *Test) error {
logrus.WithFields(logrus.Fields{
"testset": testset, "idx": idx, "method": test.Method, "sample": test.Sample,
}).Info("upload test")

tr := polygon.NewTestRequest(v.pID, idx).
Description(test.Description).
UseInStatements(test.Sample)
if test.Method == "manual" {
text, err := v.stream.Next()
if err != nil {
return err
}
tr.Input(text)
}

return v.client.SaveTest(tr)
}

func (v *Vydra) uploadScript(testset *TestSet) error {
logrus.WithField("testset", testset.Name).Info("upload script")
gens := make([]string, 0, testset.TestCount)
for idx, test := range testset.Tests.Tests { // build script
if test.Method == "generated" {
gens = append(gens, fmt.Sprintf("%s > %d", test.Cmd, idx+1))
}
}
script := strings.Join(gens, "\n")

return v.client.SaveScript(v.pID, testset.Name, script)
}

func (v *Vydra) Upload(errs chan error) error {
defer close(errs)
if err := v.readXML("problem.xml"); err != nil {
Expand All @@ -254,6 +291,17 @@ func (v *Vydra) Upload(errs chan error) error {
if chk := v.prob.Assets.Checker; chk != nil {
errs <- v.initChecker(chk)
}
for _, testset := range v.prob.Judging.TestSets {
errs <- v.uploadScript(&testset)
if err := v.stream.Init(path.Join(testset.Name, "*[^.a]")); err != nil {
errs <- err

continue
}
for idx, test := range testset.Tests.Tests {
errs <- v.uploadTest(testset.Name, idx+1, &test)
}
}

return nil
}

0 comments on commit 51bd44e

Please sign in to comment.