backend: treat file found in directory discovered + handle multiple keys + generate my.json
This commit is contained in:
parent
a01f463ee9
commit
70b6b30067
@ -14,6 +14,7 @@ import (
|
|||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var TeamsDir string
|
||||||
var SubmissionDir string
|
var SubmissionDir string
|
||||||
var BaseURL string
|
var BaseURL string
|
||||||
|
|
||||||
@ -32,6 +33,8 @@ func watchsubdir(watcher *inotify.Watcher, pathname string) error {
|
|||||||
if err := watchsubdir(watcher, p); err != nil {
|
if err := watchsubdir(watcher, p); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
} else if d.Mode().IsRegular() {
|
||||||
|
go treat(p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -42,6 +45,7 @@ func main() {
|
|||||||
var dbfile = flag.String("db", "fic.db", "Path to the DB")
|
var dbfile = flag.String("db", "fic.db", "Path to the DB")
|
||||||
flag.StringVar(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL")
|
flag.StringVar(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL")
|
||||||
flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions")
|
flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions")
|
||||||
|
flag.StringVar(&TeamsDir, "teams", "../TEAMS", "Base directory where save teams JSON files")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
SubmissionDir = path.Clean(SubmissionDir)
|
SubmissionDir = path.Clean(SubmissionDir)
|
||||||
@ -84,17 +88,21 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if ev.Mask&inotify.IN_CLOSE_WRITE == inotify.IN_CLOSE_WRITE {
|
} else if ev.Mask&inotify.IN_CLOSE_WRITE == inotify.IN_CLOSE_WRITE {
|
||||||
// Extract
|
go treat(ev.Name)
|
||||||
spath := strings.Split(strings.TrimPrefix(ev.Name, SubmissionDir), "/")
|
|
||||||
|
|
||||||
if len(spath) > 2 {
|
|
||||||
go treatSubmission(ev.Name, spath[1], spath[2])
|
|
||||||
} else {
|
|
||||||
log.Println("Invalid new file:", ev.Name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case err := <-watcher.Error:
|
case err := <-watcher.Error:
|
||||||
log.Println("error:", err)
|
log.Println("error:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func treat(raw_path string) {
|
||||||
|
// Extract
|
||||||
|
spath := strings.Split(strings.TrimPrefix(raw_path, SubmissionDir), "/")
|
||||||
|
|
||||||
|
if len(spath) > 2 {
|
||||||
|
treatSubmission(raw_path, spath[1], spath[2])
|
||||||
|
} else {
|
||||||
|
log.Println("Invalid new file:", raw_path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
@ -18,6 +22,8 @@ func treatSubmission(pathname string, team_id string, exercice_id string) {
|
|||||||
id := "[" + base64.StdEncoding.EncodeToString(bid) + "]"
|
id := "[" + base64.StdEncoding.EncodeToString(bid) + "]"
|
||||||
log.Println(id, "New submission receive", pathname)
|
log.Println(id, "New submission receive", pathname)
|
||||||
|
|
||||||
|
var keys map[string]string
|
||||||
|
|
||||||
if tid, err := strconv.Atoi(team_id); err != nil {
|
if tid, err := strconv.Atoi(team_id); err != nil {
|
||||||
log.Println(id, "[ERR]", err)
|
log.Println(id, "[ERR]", err)
|
||||||
} else if eid, err := strconv.Atoi(exercice_id); err != nil {
|
} else if eid, err := strconv.Atoi(exercice_id); err != nil {
|
||||||
@ -26,30 +32,47 @@ func treatSubmission(pathname string, team_id string, exercice_id string) {
|
|||||||
log.Println(id, "[ERR]", err)
|
log.Println(id, "[ERR]", err)
|
||||||
} else if exercice, err := fic.GetExercice(eid); err != nil {
|
} else if exercice, err := fic.GetExercice(eid); err != nil {
|
||||||
log.Println(id, "[ERR]", err)
|
log.Println(id, "[ERR]", err)
|
||||||
} else if file, err := os.Open(pathname); err != nil {
|
} else if theme, err := exercice.GetTheme(); err != nil {
|
||||||
|
log.Println(id, "[ERR]", err)
|
||||||
|
} else if cnt_raw, err := ioutil.ReadFile(pathname); err != nil {
|
||||||
|
log.Println(id, "[ERR]", err)
|
||||||
|
} else if err := json.Unmarshal(cnt_raw, &keys); err != nil {
|
||||||
log.Println(id, "[ERR]", err)
|
log.Println(id, "[ERR]", err)
|
||||||
} else {
|
} else {
|
||||||
defer file.Close()
|
if solved, err := exercice.CheckResponse(keys, team); err != nil {
|
||||||
|
|
||||||
cnt := ""
|
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
for scanner.Scan() {
|
|
||||||
cnt += scanner.Text()
|
|
||||||
}
|
|
||||||
|
|
||||||
if solved, err := exercice.CheckResponse(cnt, team); err != nil {
|
|
||||||
log.Println(id, "[ERR]", err)
|
log.Println(id, "[ERR]", err)
|
||||||
} else if solved {
|
} else if solved {
|
||||||
exercice.Solved(team)
|
exercice.Solved(team)
|
||||||
log.Printf("%s Team %d SOLVED exercice %d\n", id, team.Id, exercice.Id)
|
log.Printf("%s Team %d SOLVED exercice %d (%s : %s)\n", id, team.Id, exercice.Id, theme.Name, exercice.Title)
|
||||||
if err := os.Remove(pathname); err != nil {
|
if err := os.Remove(pathname); err != nil {
|
||||||
log.Println(id, "[ERR]", err)
|
log.Println(id, "[ERR]", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Printf("%s Team %d submit an invalid solution for exercice %d\n", id, team.Id, exercice.Id)
|
log.Printf("%s Team %d submit an invalid solution for exercice %d (%s : %s)\n", id, team.Id, exercice.Id, theme.Name, exercice.Title)
|
||||||
if err := os.Remove(pathname); err != nil {
|
if err := os.Remove(pathname); err != nil {
|
||||||
log.Println(id, "[ERR]", err)
|
log.Println(id, "[ERR]", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
genTeamMyFile(team)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func genTeamMyFile(team fic.Team) error {
|
||||||
|
dirPath := path.Join(TeamsDir, fmt.Sprintf("%d", team.Id))
|
||||||
|
|
||||||
|
if s, err := os.Stat(dirPath); os.IsNotExist(err) {
|
||||||
|
os.MkdirAll(dirPath, 0777)
|
||||||
|
} else if ! s.IsDir() {
|
||||||
|
return errors.New("dirPath is not a directory")
|
||||||
|
}
|
||||||
|
|
||||||
|
if my, err := fic.MyJSONTeam(&team, true); err != nil {
|
||||||
|
return err
|
||||||
|
} else if j, err := json.Marshal(my); err != nil {
|
||||||
|
return err
|
||||||
|
} else if err := ioutil.WriteFile(path.Join(dirPath, "my.json"), j, 0666); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -150,7 +150,7 @@ func (e Exercice) SolvedCount() int64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Exercice) CheckResponse(response string, t Team) (bool, error) {
|
func (e Exercice) CheckResponse(resps map[string]string, t Team) (bool, error) {
|
||||||
s, _, _ := t.HasSolved(e)
|
s, _, _ := t.HasSolved(e)
|
||||||
if s {
|
if s {
|
||||||
return true, nil
|
return true, nil
|
||||||
@ -165,12 +165,18 @@ func (e Exercice) CheckResponse(response string, t Team) (bool, error) {
|
|||||||
return true, errors.New("Exercice with no key registered")
|
return true, errors.New("Exercice with no key registered")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, k := range keys {
|
valid := true
|
||||||
if !k.Check(response) {
|
for _, key := range keys {
|
||||||
return false, nil
|
if _, ok := resps[key.Type]; !ok {
|
||||||
|
valid = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if !key.Check(resps[key.Type]) {
|
||||||
|
valid = false
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, nil
|
return valid, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user