mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
core: fix icpc problem format import (#904)
This commit is contained in:
1 parent
5f60908671
commit
c90436f489
1 file changed
+20
-9
@@ -42,12 +42,16 @@ function sortable(source: string, namespaces: Record<string, string>) {
|
||||
function findOverrideContent(dir: string, base: string) {
|
||||
let files = fs.readdirSync(dir);
|
||||
if (files.includes(`${base}.md`)) return fs.readFileSync(path.join(dir, `${base}.md`), 'utf8');
|
||||
if (files.includes(`${base}.pdf`)) return `@[PDF](file://${base}.pdf)`;
|
||||
const languages = {};
|
||||
files = files.filter((i) => new RegExp(`^${base}_[a-zA-Z_]+\\.md$`).test(i));
|
||||
files = files.filter((i) => new RegExp(`^${base}(?:_|.)([a-zA-Z_]+)\\.(md|pdf)$`).test(i));
|
||||
if (!files.length) return null;
|
||||
for (const file of files) {
|
||||
const lang = file.slice(8, -3);
|
||||
languages[lang] = fs.readFileSync(path.join(dir, file), 'utf8');
|
||||
const match = file.match(`^${base}(?:_|.)([a-zA-Z_]+)\\.(md|pdf)$`);
|
||||
const lang = match[1];
|
||||
const ext = match[2];
|
||||
if (ext === 'pdf') languages[lang] = `@[PDF](file://${file})`;
|
||||
else languages[lang] = fs.readFileSync(path.join(dir, file), 'utf8');
|
||||
}
|
||||
return JSON.stringify(languages);
|
||||
}
|
||||
@@ -550,13 +554,16 @@ export class ProblemModel {
|
||||
}
|
||||
if (!await isValidPid(pid)) pid = undefined;
|
||||
}
|
||||
const overrideContent = findOverrideContent(path.join(tmpdir, i), 'problem');
|
||||
let overrideContent = findOverrideContent(path.join(tmpdir, i), 'problem');
|
||||
overrideContent ||= findOverrideContent(path.join(tmpdir, i, 'statement'), 'problem');
|
||||
overrideContent ||= findOverrideContent(path.join(tmpdir, i, 'problem_statement'), 'problem');
|
||||
if (pdoc.difficulty && !Number.isSafeInteger(pdoc.difficulty)) delete pdoc.difficulty;
|
||||
if (typeof pdoc.title !== 'string') throw new ValidationError('title', null, 'Invalid title');
|
||||
const title = pdoc.title || (pdoc as any).name;
|
||||
if (typeof title !== 'string') throw new ValidationError('title', null, 'Invalid title');
|
||||
const allFiles = await getFiles(
|
||||
'testdata', 'additional_file',
|
||||
// The following is from https://icpc.io/problem-package-format/spec/2023-07-draft.html
|
||||
'attachments', 'generators', 'include', 'data', 'statement',
|
||||
'attachments', 'generators', 'include', 'data', 'statement', 'problem_statement',
|
||||
);
|
||||
const totalSize = allFiles.map((f) => fs.statSync(f[1]).size).reduce((a, b) => a + b, 0);
|
||||
if (allFiles.length > SystemModel.get('limit.problem_files')) throw new ValidationError('files', null, 'Too many files');
|
||||
@@ -571,6 +578,10 @@ export class ProblemModel {
|
||||
// TODO: report this as a warning
|
||||
}
|
||||
}
|
||||
if ((pdoc as any).limits) {
|
||||
config.time = (pdoc as any).limits.time_limit;
|
||||
config.memory = (pdoc as any).limits.memory;
|
||||
}
|
||||
const docId = overridePid
|
||||
? (await ProblemModel.edit(domainId, overridePid, {
|
||||
title: pdoc.title.trim(),
|
||||
@@ -581,7 +592,7 @@ export class ProblemModel {
|
||||
difficulty: pdoc.difficulty,
|
||||
})).docId
|
||||
: await ProblemModel.add(
|
||||
domainId, pid, pdoc.title.trim(), overrideContent || pdoc.content.toString() || 'No content',
|
||||
domainId, pid, title.trim(), overrideContent || pdoc.content?.toString() || 'No content',
|
||||
operator || pdoc.owner, tag, { hidden: pdoc.hidden, difficulty: pdoc.difficulty },
|
||||
);
|
||||
// TODO delete unused file when updating pdoc
|
||||
@@ -600,7 +611,7 @@ export class ProblemModel {
|
||||
: null)?.(domainId, docId, file, path.join(loc, file));
|
||||
}
|
||||
}
|
||||
for (const [f, loc] of await getFiles('additional_file', 'attachments', 'statement')) {
|
||||
for (const [f, loc] of await getFiles('additional_file', 'attachments', 'statement', 'problem_statement')) {
|
||||
if (!f.isFile()) continue;
|
||||
await ProblemModel.addAdditionalFile(domainId, docId, f.name, loc);
|
||||
}
|
||||
@@ -624,7 +635,7 @@ export class ProblemModel {
|
||||
await RecordModel.add(domainId, docId, operator, f.name.split('.')[1], await fs.readFile(loc, 'utf-8'), true);
|
||||
}
|
||||
if (configChanged) await ProblemModel.addTestdata(domainId, docId, 'config.yaml', yaml.dump(config));
|
||||
const message = `${overridePid ? 'Updated' : 'Imported'} problem ${pdoc.pid} (${pdoc.title})`;
|
||||
const message = `${overridePid ? 'Updated' : 'Imported'} problem ${pdoc.pid || docId} (${title})`;
|
||||
(process.env.HYDRO_CLI ? logger.info : progress)?.(message);
|
||||
} catch (e) {
|
||||
(process.env.HYDRO_CLI ? logger.info : progress)?.(`Error importing problem ${i}: ${e.message}`);
|
||||
|
||||
Reference in new issue
Block a user