Detect theme and exercice language at runtime (not stored)

This commit is contained in:
nemunaire 2023-01-17 18:26:04 +01:00
commit aa0e7406c1
17 changed files with 99 additions and 42 deletions

View file

@ -9,7 +9,7 @@ import (
"srs.epita.fr/fic-server/libfic"
)
func EPITACheckFile(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
func EPITACheckFile(file *fic.EFile, _ *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
// Enforce file format
if path.Ext(file.Name) == ".rar" || path.Ext(file.Name) == ".7z" {
errs = append(errs, fmt.Errorf("this file use a forbidden archive type."))

View file

@ -10,7 +10,7 @@ import (
"srs.epita.fr/fic-server/libfic"
)
func EPITACheckKeyFlag(flag *fic.FlagKey, raw string, exceptions *sync.CheckExceptions) (errs []error) {
func EPITACheckKeyFlag(flag *fic.FlagKey, raw string, _ *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
if (flag.Label[0] == 'Q' || flag.Label[0] == 'q') && (flag.Label[1] == 'U' || flag.Label[1] == 'u') ||
(flag.Label[0] == 'W' || flag.Label[0] == 'w') && (flag.Label[1] == 'H' || flag.Label[1] == 'h') {
errs = append(errs, fmt.Errorf("Label should not begin with %s. This seem to be a question. Reword your label as a description of the expected flag, `:` are automatically appended.", flag.Label[0:2]))
@ -47,7 +47,7 @@ func EPITACheckKeyFlag(flag *fic.FlagKey, raw string, exceptions *sync.CheckExce
return
}
func EPITACheckKeyFlagWithChoices(flag *fic.FlagKey, raw string, choices []*fic.FlagChoice, exceptions *sync.CheckExceptions) (errs []error) {
func EPITACheckKeyFlagWithChoices(flag *fic.FlagKey, raw string, choices []*fic.FlagChoice, _ *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
if !exceptions.HasException(":bruteforcable-choices") {
if len(choices) < 10 && flag.ChoicesCost == 0 {
errs = append(errs, fmt.Errorf("requires at least 10 choices to avoid brute-force"))

View file

@ -8,7 +8,7 @@ import (
"srs.epita.fr/fic-server/libfic"
)
func InspectFile(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
func InspectFile(file *fic.EFile, _ *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
if filepath.Ext(file.Name) == ".tar" || strings.HasSuffix(file.Name, ".tar.gz") || strings.HasSuffix(file.Name, ".tar.bz2") {
// Check there is more than 1 file in tarball
errs = append(errs, checkTarball(file, exceptions)...)

View file

@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"log"
"strings"
"unicode"
"srs.epita.fr/fic-server/admin/sync"
@ -11,7 +12,11 @@ import (
lib "srs.epita.fr/fic-server/repochecker/grammalecte/lib"
)
func GrammalecteCheckKeyFlag(flag *fic.FlagKey, raw string, exceptions *sync.CheckExceptions) (errs []error) {
func GrammalecteCheckKeyFlag(flag *fic.FlagKey, raw string, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
if !isRecognizedLanguage(exercice.Language) {
return
}
label, _, _, _ := flag.AnalyzeFlagLabel()
for _, err := range grammalecte("label ", label, -1, exceptions, &CommonOpts) {
if e, ok := err.(lib.GrammarError); ok && e.RuleId == "poncfin_règle1" {
@ -28,7 +33,11 @@ func GrammalecteCheckKeyFlag(flag *fic.FlagKey, raw string, exceptions *sync.Che
return
}
func GrammalecteCheckFlagChoice(choice *fic.FlagChoice, exceptions *sync.CheckExceptions) (errs []error) {
func GrammalecteCheckFlagChoice(choice *fic.FlagChoice, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
if !isRecognizedLanguage(exercice.Language) {
return
}
errs = append(errs, grammalecte("label ", choice.Label, -1, exceptions, &CommonOpts)...)
if len(errs) == 0 && !unicode.IsUpper(bytes.Runes([]byte(choice.Label))[0]) && !exceptions.HasException(":label_majuscule") {
@ -38,7 +47,11 @@ func GrammalecteCheckFlagChoice(choice *fic.FlagChoice, exceptions *sync.CheckEx
return
}
func GrammalecteCheckHint(hint *fic.EHint, exceptions *sync.CheckExceptions) (errs []error) {
func GrammalecteCheckHint(hint *fic.EHint, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
if !isRecognizedLanguage(exercice.Language) {
return
}
if len(hint.Title) > 0 {
errs = append(errs, grammalecte("title ", hint.Title, -1, exceptions, &CommonOpts)...)
if len(errs) == 0 && !unicode.IsUpper(bytes.Runes([]byte(hint.Title))[0]) && !exceptions.HasException(":title_majuscule") {
@ -52,8 +65,15 @@ func GrammalecteCheckHint(hint *fic.EHint, exceptions *sync.CheckExceptions) (er
}
func GrammalecteCheckGrammar(data interface{}, exceptions *sync.CheckExceptions) []error {
if s, ok := data.(string); ok {
return grammalecte("", s, 0, exceptions, &CommonOpts)
if s, ok := data.(struct {
Str string
Language string
}); ok {
if s.Language != "" && strings.HasPrefix(s.Language, "fr") {
return nil
}
return grammalecte("", s.Str, 0, exceptions, &CommonOpts)
} else {
log.Printf("Unknown data given to GrammalecteCheckGrammar: %T", data)
return nil

View file

@ -4,6 +4,7 @@ import (
"log"
"os"
"os/exec"
"strings"
"time"
"srs.epita.fr/fic-server/admin/sync"
@ -104,6 +105,11 @@ func runGrammalecteServer() error {
return nil
}
func isRecognizedLanguage(lang string) bool {
// Grammalecte can only check french texts
return lang == "" || strings.HasPrefix(lang, "fr")
}
func RegisterChecksHooks(h *sync.CheckHooks) {
if err := runGrammalecteServer(); err != nil {
log.Fatal("Unable to start grammalecte-server:", err)

View file

@ -17,7 +17,11 @@ import (
"github.com/yuin/goldmark/util"
)
func GrammalecteCheckMDText(str string, exceptions *sync.CheckExceptions, forbiddenStrings ...string) (errs []error) {
func GrammalecteCheckMDText(str string, lang string, exceptions *sync.CheckExceptions, forbiddenStrings ...string) (errs []error) {
if !isRecognizedLanguage(lang) {
return
}
if exceptions != nil {
for k := range *exceptions {
tmp := strings.SplitN(k, ":", 3)

View file

@ -109,7 +109,7 @@ func CheckTextFile(fd *os.File) (errs []error) {
return
}
func InspectFileForIPAddr(file *fic.EFile, exceptions *sync.CheckExceptions) (errs []error) {
func InspectFileForIPAddr(file *fic.EFile, _ *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
fd, closer, err := sync.GetFile(sync.GlobalImporter, file.GetOrigin())
if err != nil {
log.Printf("Unable to open %q: %s", file.GetOrigin(), err.Error())

View file

@ -11,9 +11,10 @@ import (
ffmpeg "github.com/u2takey/ffmpeg-go"
"srs.epita.fr/fic-server/admin/sync"
"srs.epita.fr/fic-server/libfic"
)
func CheckGrammarSubtitleTrack(path string, exceptions *sync.CheckExceptions) (errs []error) {
func CheckGrammarSubtitleTrack(path string, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
tmpfile, err := ioutil.TempFile("", "resolution-*.srt")
if err != nil {
errs = append(errs, fmt.Errorf("unable to create a temporary file: %w", err))
@ -39,7 +40,10 @@ func CheckGrammarSubtitleTrack(path string, exceptions *sync.CheckExceptions) (e
for _, item := range subtitles.Items {
lines = append(lines, item.String())
}
for _, e := range hooks.CallCustomHook("CheckGrammar", strings.Join(lines, "\n"), exceptions) {
for _, e := range hooks.CallCustomHook("CheckGrammar", struct {
Str string
Language string
}{Str: strings.Join(lines, "\n"), Language: exercice.Language}, exceptions) {
errs = append(errs, fmt.Errorf("subtitle-track: %w", e))
}

View file

@ -143,7 +143,7 @@ func checkResolutionVideo(e *fic.Exercice, exceptions *sync.CheckExceptions) (er
if !subtitles_seen && !exceptions.HasException(":subtitle:no_track") {
errs = append(errs, fmt.Errorf("no subtitles track found"))
} else if subtitles_seen {
errs = append(errs, CheckGrammarSubtitleTrack(path, exceptions)...)
errs = append(errs, CheckGrammarSubtitleTrack(path, e, exceptions)...)
}
return