admin: synchronization of exercices, files, hints and keys

This commit is contained in:
nemunaire 2017-12-09 01:21:58 +01:00
parent a033f81f5f
commit 762d3a5222
5 changed files with 227 additions and 1 deletions

View file

@ -41,11 +41,20 @@ func init() {
// Remote // Remote
router.GET("/api/remote/themes", apiHandler(sync.ApiListRemoteThemes)) router.GET("/api/remote/themes", apiHandler(sync.ApiListRemoteThemes))
router.GET("/api/remote/themes/:thname", apiHandler(sync.ApiGetRemoteTheme)) router.GET("/api/remote/themes/:thid", apiHandler(sync.ApiGetRemoteTheme))
router.GET("/api/remote/themes/:thid/exercices", apiHandler(themeHandler(sync.ApiListRemoteExercices)))
// Synchronize // Synchronize
router.GET("/api/sync/themes", apiHandler( router.GET("/api/sync/themes", apiHandler(
func(_ httprouter.Params, _ []byte) (interface{}, error) { return sync.SyncThemes(sync.GlobalImporter), nil })) func(_ httprouter.Params, _ []byte) (interface{}, error) { return sync.SyncThemes(sync.GlobalImporter), nil }))
router.GET("/api/sync/themes/:thid/exercices", apiHandler(themeHandler(
func(theme fic.Theme, _ []byte) (interface{}, error) { return sync.SyncExercices(sync.GlobalImporter, theme), nil })))
router.GET("/api/sync/themes/:thid/exercices/:eid/files", apiHandler(exerciceHandler(
func(exercice fic.Exercice, _ []byte) (interface{}, error) { return sync.SyncExerciceFiles(sync.GlobalImporter, exercice), nil })))
router.GET("/api/sync/themes/:thid/exercices/:eid/hints", apiHandler(exerciceHandler(
func(exercice fic.Exercice, _ []byte) (interface{}, error) { return sync.SyncExerciceHints(sync.GlobalImporter, exercice), nil })))
router.GET("/api/sync/themes/:thid/exercices/:eid/keys", apiHandler(exerciceHandler(
func(exercice fic.Exercice, _ []byte) (interface{}, error) { return sync.SyncExerciceKeys(sync.GlobalImporter, exercice), nil })))
} }
func bindingFiles(_ httprouter.Params, body []byte) (interface{}, error) { func bindingFiles(_ httprouter.Params, body []byte) (interface{}, error) {

View file

@ -0,0 +1,49 @@
package sync
import (
"encoding/hex"
"fmt"
"path"
"strings"
"unicode"
"srs.epita.fr/fic-server/libfic"
)
func SyncExerciceFiles(i Importer, exercice fic.Exercice) []string {
var errs []string
if digs, err := getFileContent(i, path.Join(exercice.Path, "files", "DIGESTS.txt")); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to read DIGESTS.txt: %s", path.Base(exercice.Path), err))
} else if _, err := exercice.WipeFiles(); err != nil {
errs = append(errs, err.Error())
} else if files, err := i.listDir(path.Join(exercice.Path, "files")); err != nil {
errs = append(errs, err.Error())
} else {
// Parse DIGESTS.txt
digests := map[string][]byte{}
for nline, d := range strings.Split(digs, "\n") {
if dsplt := strings.SplitN(d, " ", 2); len(dsplt) != 2 {
errs = append(errs, fmt.Sprintf("%q: unable to parse DIGESTS.txt line %d: invalid format", path.Base(exercice.Path), nline + 1))
continue
} else if hash, err := hex.DecodeString(dsplt[0]); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to parse DIGESTS.txt line %d: %s", path.Base(exercice.Path), nline + 1, err))
continue
} else {
digests[strings.TrimLeftFunc(dsplt[1], unicode.IsSpace)] = hash
}
}
// Import files
for _, fname := range files {
if _, err := ImportFile(path.Join(exercice.Path, "files", fname),
func(filePath string, origin string) (interface{}, error) {
return exercice.ImportFile(filePath, origin, digests[fname])
}); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to import file %q: %s", path.Base(exercice.Path), fname, err))
continue
}
}
}
return errs
}

View file

@ -0,0 +1,29 @@
package sync
import (
"fmt"
"path"
"srs.epita.fr/fic-server/libfic"
)
func SyncExerciceHints(i Importer, exercice fic.Exercice) []string {
var errs []string
if _, err := exercice.WipeHints(); err != nil {
errs = append(errs, err.Error())
} else if hints, err := i.listDir(path.Join(exercice.Path, "hints")); err != nil {
errs = append(errs, err.Error())
} else {
for _, hfile := range hints {
if hint_cnt, err := getFileContent(i, path.Join(exercice.Path, "hints", hfile)); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to read hint file %q: %s", path.Base(exercice.Path), hfile, err))
continue
} else if _, err := exercice.AddHint(hfile, hint_cnt, 1); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to add hint %q: %s", path.Base(exercice.Path), hfile, err))
continue
}
}
}
return errs
}

View file

@ -0,0 +1,46 @@
package sync
import (
"fmt"
"path"
"strings"
"srs.epita.fr/fic-server/libfic"
)
func SyncExerciceKeys(i Importer, exercice fic.Exercice) []string {
var errs []string
if _, err := exercice.WipeKeys(); err != nil {
errs = append(errs, err.Error())
} else if flags, err := getFileContent(i, path.Join(exercice.Path, "flags.txt")); 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
}
var label, rawkey string
if len(flag_splt) == 1 {
rawkey = flag_splt[0]
} else {
label = flag_splt[0]
rawkey = flag_splt[1]
}
if len(label) == 0 {
label = "Flag"
}
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
}

93
admin/sync/exercices.go Normal file
View file

@ -0,0 +1,93 @@
package sync
import (
"fmt"
"path"
"strings"
"strconv"
"srs.epita.fr/fic-server/libfic"
)
func getExercices(i Importer, theme fic.Theme) ([]string, error) {
var exercices []string
if dirs, err := i.listDir(theme.Name); err != nil {
return []string{}, err
} else {
for _, dir := range dirs {
if _, err := i.listDir(path.Join(theme.Name, dir)); err == nil {
exercices = append(exercices, dir)
}
}
}
return exercices, nil
}
func SyncExercices(i Importer, theme fic.Theme) []string {
var errs []string
if exercices, err := getExercices(i, theme); err != nil {
errs = append(errs, err.Error())
} else {
emap := map[string]int{}
for _, edir := range exercices {
edir_splt := strings.SplitN(edir, "-", 2)
if len(edir_splt) != 2 {
errs = append(errs, fmt.Sprintf("%q is not a valid exercice directory: missing id prefix", edir))
continue
}
eid, err := strconv.Atoi(edir_splt[0])
if err != nil {
errs = append(errs, fmt.Sprintf("%q: invalid exercice identifier: %s", edir, err))
continue
}
ename := edir_splt[1]
emap[ename] = eid
statement, err := getFileContent(i, path.Join(theme.Name, edir, "scenario.txt"))
if err != nil {
errs = append(errs, fmt.Sprintf("%q: scenario.txt: %s", edir, err))
continue
}
// TODO: retrieve depends
var depend *fic.Exercice = nil
// TODO: calculate gain
var gain int64 = 42
// TODO: handle video
videoURI := ""
if e, err := theme.GetExerciceByTitle(ename); err != nil {
if _, err := theme.AddExercice(ename, path.Join(theme.Name, edir), statement, depend, gain, videoURI); err != nil {
errs = append(errs, fmt.Sprintf("%q: error on exercice add: %s", edir, err))
continue
}
} else if e.Title != ename || e.Statement != statement || (depend == nil && e.Depend != nil) || (depend != nil && e.Depend == nil) || (depend != nil && e.Depend != nil && *e.Depend != depend.Id) || e.Gain != gain || e.VideoURI != videoURI {
e.Title = ename
e.Statement = statement
if depend != nil {
e.Depend = &depend.Id
} else {
e.Depend = nil
}
e.Gain = gain
e.VideoURI = videoURI
if _, err := e.Update(); err != nil {
errs = append(errs, fmt.Sprintf("%q: error on exercice update: %s", edir, err))
continue
}
}
}
}
return errs
}
func ApiListRemoteExercices(theme fic.Theme, _ []byte) (interface{}, error) {
return getExercices(GlobalImporter, theme)
}