Exemplo n.º 1
0
func exportProblem(backend storage.ProblemStore, manifest storage.ProblemManifest, dest string) error {
	if err := os.MkdirAll(dest, os.ModePerm); err != nil {
		return err
	}
	gridprefix := manifest.GetGridPrefix()
	if manifest.TesterName != "" {
		if err := os.MkdirAll(filepath.Join(dest, "Tester"), os.ModePerm); err != nil {
			return err
		}
		if _, err := backend.Copy(filepath.Join(dest, "Tester", manifest.TesterName), gridprefix+"checker", false, "", "", *authToken); err != nil {
			return err
		}
	}
	if err := ioutil.WriteFile(filepath.Join(dest, "memlimit"), []byte(fmt.Sprintf("%d", manifest.MemoryLimit)), os.ModePerm); err != nil {
		return err
	}
	if err := ioutil.WriteFile(filepath.Join(dest, "timex"), []byte(fmt.Sprintf("%f", float64(manifest.TimeLimitMicros)/1000000)), os.ModePerm); err != nil {
		return err
	}

	answers := make(map[int]struct{})
	for _, v := range manifest.Answers {
		answers[v] = struct{}{}
	}

	for i := 1; i <= manifest.TestCount; i++ {
		if i > 1 {
			fmt.Printf(" ")
		}
		fmt.Printf("%d", i)
		os.Stdout.Sync()
		test := filepath.Join(dest, fmt.Sprintf("Test.%d", i))
		if err := mkdirAndCopy(backend, filepath.Join(test, "Input"),
			"input.txt", gridprefix+fmt.Sprintf("tests/%d/input.txt", i)); err != nil {
			return err
		}
		if _, ok := answers[i]; !ok {
			continue
		}
		if err := mkdirAndCopy(backend, filepath.Join(test, "Add-ons"),
			"answer.txt", gridprefix+fmt.Sprintf("tests/%d/answer.txt", i)); err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 2
0
func importProblem(id, root string, backend storage.ProblemStore) error {
	var manifest storage.ProblemManifest
	var err error

	manifest.Id = id
	manifest.Revision, err = backend.GetNextRevision(id)
	manifest.Key = manifest.Id + "/" + strconv.FormatInt(int64(manifest.Revision), 10)

	gridprefix := manifest.GetGridPrefix()

	rootDir, err := os.Open(root)
	if err != nil {
		return err
	}

	names, err := rootDir.Readdirnames(-1)

	for _, shortName := range names {
		if !strings.HasPrefix(strings.ToLower(shortName), "test.") {
			continue
		}
		testRoot := filepath.Join(root, shortName)
		if dstat, err := os.Stat(testRoot); err != nil || !dstat.IsDir() {
			continue
		}

		ext := filepath.Ext(testRoot)
		if len(ext) < 2 {
			continue
		}

		testId, err := strconv.ParseInt(ext[1:], 10, 32)
		if err != nil {
			continue
		}

		if err = storeIfExists(backend, filepath.Join(testRoot, "Input", "input.txt"),
			gridprefix+"tests/"+strconv.FormatInt(testId, 10)+"/input.txt"); err != nil {
			continue
		}

		if err = storeIfExists(backend, filepath.Join(testRoot, "Add-ons", "answer.txt"),
			gridprefix+"tests/"+strconv.FormatInt(testId, 10)+"/answer.txt"); err == nil {
			manifest.Answers = append(manifest.Answers, int(testId))
		}

		if int(testId) > manifest.TestCount {
			manifest.TestCount = int(testId)
		}
	}

	if err = storeIfExists(backend, filepath.Join(root, "Tester", "tester.exe"), gridprefix+"checker"); err != nil {
		return err
	}

	manifest.TesterName = "tester.exe"

	memlimitString, err := readFirstLine(filepath.Join(root, "memlimit"))
	if err == nil {
		manifest.MemoryLimit, err = strconv.ParseInt(string(memlimitString), 10, 64)
		if err != nil {
			fmt.Println(err)
		}
		if manifest.MemoryLimit < 16*1024*1024 {
			manifest.MemoryLimit = 16 * 1024 * 1024
		}
	} else {
		fmt.Println(err)
	}

	timexString, err := readFirstLine(filepath.Join(root, "timex"))
	if err == nil {
		timex, err := strconv.ParseFloat(string(timexString), 64)
		if err == nil {
			manifest.TimeLimitMicros = int64(timex * 1000000)
		} else {
			fmt.Println(err)
		}
	} else {
		fmt.Println(err)
	}

	if manifest.Answers != nil {
		sort.Ints(manifest.Answers)
	}

	fmt.Println(manifest)

	return backend.SetManifest(&manifest)
}