Skip to content

Commit

Permalink
chore: more wooda mods (#23)
Browse files Browse the repository at this point in the history
* chore: more wooda mods

* chore: checker + interactor + solutions + sample
  • Loading branch information
Gornak40 authored Feb 28, 2024
1 parent 6cfe91a commit 8b67630
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 20 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# algolymp
*A collection of useful CLI tools for managing Polygon and Ejudge.*
*Awesome collection of useful CLI tools for managing Polygon and Ejudge.*

## Workflow

Expand All @@ -15,6 +15,8 @@
| [wooda](#wooda) | glob problem files upload | | 🦍 | 🧑‍💻 |
| ⚙️ | move json config to ini | | | 🧑‍💻 |
| 👻 | list/commit problems | | 🦍 | 🤔 |
| 👻 | set good random group scores | | 🦍 | 🤔 |
| 👻 | algolymp config manager | | | 🤔 |
| 👻 | download/upload package | | 🦍 | 🤔 |
| 👻 | import polygon problem | 🦍 | 🦍 | 🤔 |
| 👻 | autogen static problem | 🦍 | | 🤔 |
Expand Down Expand Up @@ -281,17 +283,24 @@ valeria -i 318882 | bat -l tex

Match all files in directory with glob pattern. Upload recognized files to Polygon.

Supported modes:
#### Supported modes

- `test`
- `tags`
- `t` - test
- `tags` - tags (each tag is on a new line)
- `v` - validator
- `c` - checker
- `i` - interactor
- `ma` - main solution
- `ok` - correct solution
- `rj` - incorrect solution
- `s` - sample

### Flags
- `-i` - problem id (required)
- `-m` - uploading mode (required)
- `-g` - problem files glob (required)

You should know your shell and probably pass `-g "<glob>"`, not `-g <glob>`
You should know your shell and probably use `-g "<glob>"`, not `-g <glob>`

### Config
- `polygon.url`
Expand All @@ -302,8 +311,15 @@ You should know your shell and probably pass `-g "<glob>"`, not `-g <glob>`

```bash
wooda --help
wooda -i 337320 -m test -g "tests/*[^.a]"
wooda -i 337320 -m t -g "tests/*[^.a]" # exclude output
wooda -i 337320 -m tags -g tags
wooda -i 337320 -m v -g files/val*.cpp
wooda -i 337320 -m c -g check.cpp
wooda -i 337320 -m i -g interactor.cpp
wooda -i 337320 -m ma -g solutions/main.cpp # Main solution
wooda -i 337320 -m ok -g solutions/sol_apachee.cpp # OK solution
wooda -i 337320 -m rj -g solutions/brute.py # TL solution
wooda -i 337320 -m s -g "statements/russian/example.[0-9][0-9]"
```

![wooda logo](https://algolymp.ru/static/img/wooda.png)
7 changes: 7 additions & 0 deletions cmd/wooda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ func main() {
woodaModes := []string{
wooda.ModeTest,
wooda.ModeTags,
wooda.ModeValidator,
wooda.ModeChecker,
wooda.ModeInteractor,
wooda.ModeSolutionMain,
wooda.ModeSolutionCorrect,
wooda.ModeSolutionIncorrect,
wooda.ModeSample,
}

parser := argparse.NewParser("wooda", "Upload problem files filtered by glob to Polygon.")
Expand Down
74 changes: 67 additions & 7 deletions polygon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ const (
defaultTestset = "tests"
)

type SolutionTag string

const (
TagMain SolutionTag = "MA"
TagCorrect SolutionTag = "OK"
TagIncorrect SolutionTag = "RJ"
)

type FileType string

const (
TypeSource FileType = "source"
TypeResource FileType = "resource"
TypeAUX FileType = "aux"
)

var (
ErrBadPolygonStatus = errors.New("bad polygon status")
ErrInvalidMethod = errors.New("invalid method")
Expand Down Expand Up @@ -176,13 +192,8 @@ func (p *Polygon) EnablePoints(pID int) error {
return err
}

func (p *Polygon) SaveResource(pID int, name, content string) error {
link, params := p.buildURL("problem.saveFile", url.Values{
"problemId": []string{strconv.Itoa(pID)},
"type": []string{"resource"},
"name": []string{name},
"file": []string{content},
})
func (p *Polygon) SaveFile(fReq FileRequest) error {
link, params := p.buildURL("problem.saveFile", url.Values(fReq))
_, err := p.makeQuery(http.MethodPost, link, params)

return err
Expand All @@ -204,3 +215,52 @@ func (p *Polygon) SaveTags(pID int, tags string) error {

return err
}

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

return err
}

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

return err
}

func (p *Polygon) UpdateInfo(pr ProblemRequest) error {
link, params := p.buildURL("problem.updateInfo", url.Values(pr))
_, err := p.makeQuery(http.MethodPost, link, params)

return err
}

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

return err
}

func (p *Polygon) SaveSolution(pID int, name, data string, tag SolutionTag) error {
link, params := p.buildURL("problem.saveSolution", url.Values{
"problemId": []string{strconv.Itoa(pID)},
"name": []string{name},
"file": []string{data},
"tag": []string{string(tag)},
})
_, err := p.makeQuery(http.MethodPost, link, params)

return err
}
70 changes: 70 additions & 0 deletions polygon/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,73 @@ func (tr TestRequest) Description(description string) TestRequest {

return tr
}

func (tr TestRequest) UseInStatements(f bool) TestRequest {
tr["testUseInStatements"] = []string{strconv.FormatBool(f)}

return tr
}

type ProblemRequest url.Values

func NewProblemRequest(pID int) ProblemRequest {
return ProblemRequest{
"problemId": []string{strconv.Itoa(pID)},
}
}

func (pr ProblemRequest) InputFile(name string) ProblemRequest {
pr["inputFile"] = []string{name}

return pr
}

func (pr ProblemRequest) OutputFile(name string) ProblemRequest {
pr["outputFile"] = []string{name}

return pr
}

func (pr ProblemRequest) Interactive(f bool) ProblemRequest {
pr["interactive"] = []string{strconv.FormatBool(f)}

return pr
}

func (pr ProblemRequest) TimeLimit(tl int) ProblemRequest {
pr["timeLimit"] = []string{strconv.Itoa(tl)}

return pr
}

func (pr ProblemRequest) MemoryLimit(ml int) ProblemRequest {
pr["memoryLimit"] = []string{strconv.Itoa(ml)}

return pr
}

type FileRequest url.Values

func NewFileRequest(pID int, typ FileType, name, file string) FileRequest {
return FileRequest{
"problemId": []string{strconv.Itoa(pID)},
"type": []string{string(typ)},
"name": []string{name},
"file": []string{file},
}
}

// TODO: fix it.
func (fr FileRequest) CheckExisting(f bool) FileRequest {
fr["checkExisting"] = []string{strconv.FormatBool(f)}

return fr
}

func (fr FileRequest) SourceType(typ string) FileRequest {
fr["sourceType"] = []string{typ}

return fr
}

// TODO: add other options
3 changes: 2 additions & 1 deletion polygon/scoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func (p *Polygon) InformaticsValuer(pID int, verbose bool) error {
if verbose {
logrus.Info("valuer.cfg\n" + valuer)
}
if err := p.SaveResource(pID, "valuer.cfg", valuer); err != nil {
fr := NewFileRequest(pID, TypeResource, "valuer.cfg", valuer)
if err := p.SaveFile(fr); err != nil {
return err
}

Expand Down
75 changes: 69 additions & 6 deletions polygon/wooda/wooda.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ import (
)

const (
ModeTest = "test"
ModeTags = "tags"
ModeTest = "t"
ModeTags = "tags"
ModeValidator = "v"
ModeChecker = "c"
ModeInteractor = "i"
ModeSolutionMain = "ma"
ModeSolutionCorrect = "ok"
ModeSolutionIncorrect = "rj"
ModeSample = "s"
)

var (
Expand Down Expand Up @@ -42,20 +49,36 @@ func (w *Wooda) Resolve(path string) error {
if err != nil {
return err
}
file := string(data)
switch w.mode {
case ModeTest:
return w.resolveTest(path, string(data))
return w.resolveTest(path, file, false)
case ModeTags:
return w.resolveTags(string(data))
return w.resolveTags(file)
case ModeValidator:
return w.resolveValidator(path, file)
case ModeChecker:
return w.resolveChecker(path, file)
case ModeInteractor:
return w.resolveInteractor(path, file)
case ModeSolutionMain:
return w.resolveSolution(path, file, polygon.TagMain)
case ModeSolutionCorrect:
return w.resolveSolution(path, file, polygon.TagCorrect)
case ModeSolutionIncorrect:
return w.resolveSolution(path, file, polygon.TagIncorrect)
case ModeSample:
return w.resolveTest(path, file, true)
default:
return fmt.Errorf("%w: %s", ErrUnknownMode, w.mode)
}
}

func (w *Wooda) resolveTest(path, data string) error {
func (w *Wooda) resolveTest(path, data string, sample bool) error {
tr := polygon.NewTestRequest(w.pID, w.testIndex).
Input(data).
Description(fmt.Sprintf("File \"%s\"", filepath.Base(path)))
Description(fmt.Sprintf("File \"%s\"", filepath.Base(path))).
UseInStatements(sample)
if err := w.client.SaveTest(tr); err != nil {
return err
}
Expand All @@ -69,3 +92,43 @@ func (w *Wooda) resolveTags(data string) error {

return w.client.SaveTags(w.pID, tags)
}

func (w *Wooda) resolveValidator(path, data string) error {
name := filepath.Base(path)
fr := polygon.NewFileRequest(w.pID, polygon.TypeSource, name, data)
if err := w.client.SaveFile(fr); err != nil {
return err
}

return w.client.SetValidator(w.pID, name)
}

// TODO: support standard checkers.
func (w *Wooda) resolveChecker(path, data string) error {
name := filepath.Base(path)
fr := polygon.NewFileRequest(w.pID, polygon.TypeSource, name, data)
if err := w.client.SaveFile(fr); err != nil {
return err
}

return w.client.SetChecker(w.pID, name)
}

func (w *Wooda) resolveInteractor(path, data string) error {
pr := polygon.NewProblemRequest(w.pID).Interactive(true)
if err := w.client.UpdateInfo(pr); err != nil {
return err
}

name := filepath.Base(path)
fr := polygon.NewFileRequest(w.pID, polygon.TypeSource, name, data)
if err := w.client.SaveFile(fr); err != nil {
return err
}

return w.client.SetInteractor(w.pID, name)
}

func (w *Wooda) resolveSolution(path, data string, tag polygon.SolutionTag) error {
return w.client.SaveSolution(w.pID, filepath.Base(path), data, tag)
}

0 comments on commit 8b67630

Please sign in to comment.