sync: handle new sync format: flags
This commit is contained in:
parent
af55c5af9f
commit
3b7d9a2a75
4 changed files with 40 additions and 122 deletions
|
@ -53,10 +53,6 @@ func init() {
|
||||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||||
return sync.SyncExerciceKeys(sync.GlobalImporter, exercice), nil
|
return sync.SyncExerciceKeys(sync.GlobalImporter, exercice), nil
|
||||||
})))
|
})))
|
||||||
router.POST("/api/sync/exercices/:eid/quiz", apiHandler(exerciceHandler(
|
|
||||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
|
||||||
return sync.SyncExerciceMCQ(sync.GlobalImporter, exercice), nil
|
|
||||||
})))
|
|
||||||
|
|
||||||
router.POST("/api/sync/exercices/:eid/fixurlid", apiHandler(exerciceHandler(
|
router.POST("/api/sync/exercices/:eid/fixurlid", apiHandler(exerciceHandler(
|
||||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||||
|
|
|
@ -68,10 +68,6 @@ func init() {
|
||||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||||
return sync.SyncExerciceKeys(sync.GlobalImporter, exercice), nil
|
return sync.SyncExerciceKeys(sync.GlobalImporter, exercice), nil
|
||||||
})))
|
})))
|
||||||
router.POST("/api/sync/themes/:thid/exercices/:eid/quiz", apiHandler(exerciceHandler(
|
|
||||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
|
||||||
return sync.SyncExerciceMCQ(sync.GlobalImporter, exercice), nil
|
|
||||||
})))
|
|
||||||
|
|
||||||
router.POST("/api/sync/themes/:thid/fixurlid", apiHandler(themeHandler(
|
router.POST("/api/sync/themes/:thid/fixurlid", apiHandler(themeHandler(
|
||||||
func(theme fic.Theme, _ []byte) (interface{}, error) {
|
func(theme fic.Theme, _ []byte) (interface{}, error) {
|
||||||
|
|
|
@ -3,7 +3,6 @@ package sync
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
|
@ -20,131 +19,71 @@ func isFullGraphic(s string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncExerciceKeys reads the content of flags.txt and import them as Key for the given challenge.
|
// SyncExerciceKeys reads the content of challenge.txt and import "classic" flags as Key for the given challenge.
|
||||||
func SyncExerciceKeys(i Importer, exercice fic.Exercice) []string {
|
func SyncExerciceKeys(i Importer, exercice fic.Exercice) (errs []string) {
|
||||||
var errs []string
|
|
||||||
|
|
||||||
if _, err := exercice.WipeKeys(); err != nil {
|
if _, err := exercice.WipeKeys(); err != nil {
|
||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
} else if flags, err := getFileContent(i, path.Join(exercice.Path, "flags.txt")); err != nil {
|
} else if _, err := exercice.WipeMCQs(); err != nil {
|
||||||
errs = append(errs, fmt.Sprintf("%q: unable to read flags: %s", path.Base(exercice.Path), err))
|
|
||||||
} else {
|
|
||||||
for nline, flag := range strings.Split(flags, "\n") {
|
|
||||||
flag_splt := strings.SplitN(string(flag), "\t", 2)
|
|
||||||
if len(flag_splt) > 2 {
|
|
||||||
errs = append(errs, fmt.Sprintf("%q: error in flags file at line %d: invalid format", path.Base(exercice.Path), nline + 1))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(flag_splt) == 1 {
|
|
||||||
errs = append(errs, fmt.Sprintf("%q: error in flags file at line %d: no separator found", path.Base(exercice.Path), nline + 1))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var label, rawkey string
|
|
||||||
if len(flag_splt[0]) == 0 {
|
|
||||||
label = "Flag"
|
|
||||||
} else {
|
|
||||||
label = flag_splt[0]
|
|
||||||
}
|
|
||||||
rawkey = flag_splt[1]
|
|
||||||
|
|
||||||
if !isFullGraphic(rawkey) {
|
|
||||||
errs = append(errs, fmt.Sprintf("%q: WARNING in flags file at line %d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), nline + 1))
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := exercice.AddRawKey(label, rawkey); err != nil {
|
|
||||||
errs = append(errs, fmt.Sprintf("%q: error in flags file at line %d: %s", path.Base(exercice.Path), nline + 1, err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errs
|
|
||||||
}
|
|
||||||
|
|
||||||
// SyncExerciceMCQ reads the content of flags-ucq.txt and flags-mcq.txt, and import them as MCQs for the given challenge.
|
|
||||||
func SyncExerciceMCQ(i Importer, exercice fic.Exercice) (errs []string) {
|
|
||||||
if _, err := exercice.WipeMCQs(); err != nil {
|
|
||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
return errs
|
} else if params, err := parseExerciceParams(i, exercice.Path); err != nil {
|
||||||
}
|
errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", path.Base(exercice.Path), err))
|
||||||
|
} else if len(params.Flags) == 0 && len(params.FlagsUCQ) == 0 && len(params.FlagsMCQ) == 0 {
|
||||||
// Unique Choice Questions (checkbox)
|
errs = append(errs, fmt.Sprintf("%q: has no flag", path.Base(exercice.Path)))
|
||||||
if ucq, err := getFileContent(i, path.Join(exercice.Path, "flags-ucq.txt")); err != nil {
|
|
||||||
if i.exists(path.Join(exercice.Path, "flags-ucq.txt")) {
|
|
||||||
errs = append(errs, fmt.Sprintf("%q: unable to read ucq: %s", path.Base(exercice.Path), err))
|
|
||||||
}
|
|
||||||
} else if flag, err := exercice.AddMCQ(""); err != nil {
|
|
||||||
errs = append(errs, fmt.Sprintf("%q: unable to add ucq: %s", path.Base(exercice.Path), err))
|
|
||||||
} else {
|
} else {
|
||||||
for nline, quest := range strings.Split(ucq, "\n") {
|
// Import normal flags
|
||||||
if len(quest) == 0 {
|
for nline, flag := range params.Flags {
|
||||||
continue
|
if len(flag.Label) == 0 {
|
||||||
|
flag.Label = "Flag"
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(quest) < 2 {
|
if !isFullGraphic(flag.Raw) {
|
||||||
errs = append(errs, fmt.Sprintf("%q: error in ucq file at line %d: missing response", path.Base(exercice.Path), nline + 1))
|
errs = append(errs, fmt.Sprintf("%q: WARNING flag #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), nline + 1))
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expect 0 or 1
|
if _, err := exercice.AddRawKey(flag.Label, flag.Raw); err != nil {
|
||||||
if quest[0] != 48 && quest[0] != 49 {
|
errs = append(errs, fmt.Sprintf("%q: error flag #%d: %s", path.Base(exercice.Path), nline + 1, err))
|
||||||
errs = append(errs, fmt.Sprintf("%q: error in ucq file at line %d: invalid format: first character has to be either 0 or 1", path.Base(exercice.Path), nline + 1))
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := flag.AddEntry(quest[1:], quest[0] == 49); err != nil {
|
|
||||||
errs = append(errs, fmt.Sprintf("%q: error in ucq file at line %d: %s", path.Base(exercice.Path), nline + 1, err))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Multiple Choice Questions (radio)
|
// Import UCQ flags
|
||||||
if mcq, err := getFileContent(i, path.Join(exercice.Path, "flags-mcq.txt")); err != nil {
|
for nline, flag := range params.FlagsUCQ {
|
||||||
if i.exists(path.Join(exercice.Path, "flags-mcq.txt")) {
|
if len(flag.Label) == 0 {
|
||||||
errs = append(errs, fmt.Sprintf("%q: unable to read mcq: %s", path.Base(exercice.Path), err))
|
flag.Label = "Flag"
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for nline, quest := range strings.Split(mcq, "\n") {
|
|
||||||
quest_splt := strings.Split(string(quest), "\t")
|
|
||||||
if len(quest_splt) < 2 {
|
|
||||||
errs = append(errs, fmt.Sprintf("%q: error in mcq file at line %d: not enough responses", path.Base(exercice.Path), nline + 1))
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if flag, err := exercice.AddMCQ(quest_splt[0]); err != nil {
|
if !isFullGraphic(flag.Raw) {
|
||||||
errs = append(errs, fmt.Sprintf("%q: error in mcq file at line %d: %s", path.Base(exercice.Path), nline + 1, err))
|
errs = append(errs, fmt.Sprintf("%q: WARNING flag UCQ #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), nline + 1))
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := exercice.AddRawKey(flag.Label, flag.Raw); err != nil {
|
||||||
|
errs = append(errs, fmt.Sprintf("%q: error flag UCQ #%d: %s", path.Base(exercice.Path), nline + 1, err))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Import MCQ flags
|
||||||
|
for nline, quest := range params.FlagsMCQ {
|
||||||
|
if flag, err := exercice.AddMCQ(quest.Label); err != nil {
|
||||||
|
errs = append(errs, fmt.Sprintf("%q: error flag MCQ #%d: %s", path.Base(exercice.Path), nline + 1, err))
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
hasOne := false
|
hasOne := false
|
||||||
for cid, choice := range quest_splt[1:] {
|
for cid, choice := range quest.Choice {
|
||||||
// Expect 0 or 1
|
if _, err := flag.AddEntry(choice.Label, choice.Value); err != nil {
|
||||||
if choice[0] != 48 && choice[0] != 49 {
|
errs = append(errs, fmt.Sprintf("%q: error in MCQ %d choice %d: %s", path.Base(exercice.Path), nline + 1, cid, err))
|
||||||
errs = append(errs, fmt.Sprintf("%q: error in mcq file at line %d,%d: invalid format: first character has to be either 0 or 1", path.Base(exercice.Path), nline + 1, cid))
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(quest) < 2 {
|
if choice.Value {
|
||||||
errs = append(errs, fmt.Sprintf("%q: error in mcq file at line %d: missing label", path.Base(exercice.Path), nline + 1))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := flag.AddEntry(choice[1:], choice[0] == 49); err != nil {
|
|
||||||
errs = append(errs, fmt.Sprintf("%q: error in mcq file at line %d,%d: %s", path.Base(exercice.Path), nline + 1, cid, err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if choice[0] == 49 {
|
|
||||||
hasOne = true
|
hasOne = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !hasOne {
|
if !hasOne {
|
||||||
errs = append(errs, fmt.Sprintf("%q: warning in mcq file at line %d: no valid answer defined, is this really expected?", path.Base(exercice.Path), nline + 1))
|
errs = append(errs, fmt.Sprintf("%q: warning MCQ %d: no valid answer defined, is this really expected?", path.Base(exercice.Path), nline + 1))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return
|
||||||
return errs
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"time"
|
"time"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -34,19 +33,7 @@ func SyncDeep(i Importer) (errs map[string][]string) {
|
||||||
for _, exercice := range exercices {
|
for _, exercice := range exercices {
|
||||||
errs[theme.Name] = append(errs[theme.Name], SyncExerciceFiles(i, exercice)...)
|
errs[theme.Name] = append(errs[theme.Name], SyncExerciceFiles(i, exercice)...)
|
||||||
errs[theme.Name] = append(errs[theme.Name], SyncExerciceHints(i, exercice)...)
|
errs[theme.Name] = append(errs[theme.Name], SyncExerciceHints(i, exercice)...)
|
||||||
|
errs[theme.Name] = append(errs[theme.Name], SyncExerciceKeys(i, exercice)...)
|
||||||
hasFlags := false
|
|
||||||
if i.exists(path.Join(exercice.Path, "flags.txt")) {
|
|
||||||
errs[theme.Name] = append(errs[theme.Name], SyncExerciceKeys(i, exercice)...)
|
|
||||||
hasFlags = true
|
|
||||||
}
|
|
||||||
if i.exists(path.Join(exercice.Path, "flags-mcq.txt")) || i.exists(path.Join(exercice.Path, "flags-ucq.txt")) {
|
|
||||||
errs[theme.Name] = append(errs[theme.Name], SyncExerciceMCQ(i, exercice)...)
|
|
||||||
hasFlags = true
|
|
||||||
}
|
|
||||||
if !hasFlags {
|
|
||||||
errs[theme.Name] = append(errs[theme.Name], fmt.Sprintf("%q: has no flags!", path.Base(exercice.Path)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue