Skip to content

Commit

Permalink
移动了程序下载的位置(编译→评测)
Browse files Browse the repository at this point in the history
除此以外,在Config.ts中增添了Or验证器。
  • Loading branch information
qwertycxz committed Jan 22, 2025
1 parent 243223d commit c74c6f6
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 442 deletions.
48 changes: 45 additions & 3 deletions src/Config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
import * as TOML from "@iarna/toml";
import { Type, plainToClass } from "class-transformer";
import { plainToClass, Type } from "class-transformer";
import {
isBoolean,
IsBoolean,
IsInt,
IsNotEmpty,
IsNumber,
IsOptional,
IsPositive,
isString,
IsString,
Min,
ValidateBy,
ValidateNested,
validateSync,
ValidationOptions,
} from "class-validator";
import fs from "fs";
import { getLogger } from "log4js";

function Or(...constraints: ((value: unknown) => boolean)[]): PropertyDecorator;
function Or(
validationOptions?: ValidationOptions,
...constraints: ((value: unknown) => boolean)[]
): PropertyDecorator;
function Or(
validationOptions?: ValidationOptions | ((value: unknown) => boolean),
...constraints: ((value: unknown) => boolean)[]
): PropertyDecorator {
if (typeof validationOptions === "function") {
constraints = [validationOptions, ...constraints];
validationOptions = undefined;
}
return ValidateBy(
{
name: "or",
constraints,
validator: {
validate: (value, validationArguments) => {
if (validationArguments) {
for (const constraint of validationArguments.constraints) {
if (constraint(value)) {
return true;
}
}
}
return false;
},
},
},
validationOptions
);
}

const logger = getLogger("ConfigService");
const configToml = fs.readFileSync("config/config.toml").toString();
export class LanguageConfig {
Expand Down Expand Up @@ -53,15 +92,18 @@ export class LanguageConfig {
@IsString()
@IsNotEmpty()
ise!: string;
@IsString()
@Or(isBoolean, isString)
@IsNotEmpty()
shell!: string; //TODO: string or boolean
shell!: string | boolean;
@IsString()
@IsNotEmpty()
verilog!: string;
@IsString()
@IsNotEmpty()
vhdl!: string;
@IsString()
@IsNotEmpty()
impact!: string;
}
export class JailConfig {
@IsString()
Expand Down
2 changes: 1 addition & 1 deletion src/Spawn/Language/CMP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class CMP extends Language {
return [getConfig().language.ojcmp];
}

execOptionGenerator(): RunOption {
pragramOptionGenerator(): RunOption {
const binPath = getConfig().language.ojcmp;
return {
skip: false,
Expand Down
2 changes: 1 addition & 1 deletion src/Spawn/Language/PlainText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class PlainText extends Language {
return [];
}

execOptionGenerator(): RunOption {
pragramOptionGenerator(): RunOption {
const binPath = path.join(this.compileDir, this.src);
return {
skip: false,
Expand Down
6 changes: 4 additions & 2 deletions src/Spawn/Language/Verilog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ export class Verilog extends Language {
};
}

execOptionGenerator(): RunOption {
pragramOptionGenerator(): RunOption {
return {
skip: true,
skip: false,
command: getConfig().language.impact,
args: ["-batch", "main.cmd"],
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/Spawn/Language/decl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ export abstract class Language {
abstract get srcFileName(): string;
abstract get compiledFiles(): string[];
abstract compileOptionGenerator(): RunOption;
abstract execOptionGenerator(): RunOption;
abstract pragramOptionGenerator(): RunOption;
}
40 changes: 18 additions & 22 deletions src/Utilities/ExecutableAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getConfiguredLanguage } from "../Spawn/Language";
import { getLogger } from "log4js";
import { FileHandle } from "fs/promises";
import { hengSpawn, HengSpawnOption } from "../Spawn";
import { MeteredChildProcess, MeterResult } from "../Spawn/Meter";
import { MeterResult } from "../Spawn/Meter";

const compileCachedJudge = new Map<string, string>();
export const SourceCodeName = "srcCode";
Expand All @@ -29,23 +29,23 @@ export class ExecutableAgent {
protected logger = getLogger("ExecutableAgent");

constructor(
private readonly execType: ExecType,
private readonly excutable: Executable
public readonly execType: ExecType,
public readonly executable: Executable
) {
this.judgeHash = crypto
.createHash("sha256")
.update(
JSON.stringify({
execType,
excutable,
excutable: executable,
})
)
.digest("hex");
this.configuredLanguage = getConfiguredLanguage(
this.excutable.environment.language,
this.executable.environment.language,
{
execType: this.execType,
excutable: this.excutable,
excutable: this.executable,
compileDir: "",
}
);
Expand Down Expand Up @@ -105,7 +105,7 @@ export class ExecutableAgent {
await this.fileAgent.init(false);
this.fileAgent.add(
SourceCodeName,
this.excutable.source,
this.executable.source,
this.configuredLanguage.srcFileName
);
}
Expand Down Expand Up @@ -196,16 +196,16 @@ export class ExecutableAgent {
gid: getConfig().judger.gid,
timeLimit:
languageRunOption.spawnOption?.timeLimit ??
this.excutable.limit.compiler.cpuTime,
this.executable.limit.compiler.cpuTime,
memoryLimit:
languageRunOption.spawnOption?.memoryLimit ??
this.excutable.limit.compiler.memory,
this.executable.limit.compiler.memory,
pidLimit:
languageRunOption.spawnOption?.pidLimit ??
getConfig().judger.defaultPidLimit,
fileLimit:
languageRunOption.spawnOption?.fileLimit ??
this.excutable.limit.compiler.output,
this.executable.limit.compiler.output,
};

const subProc = hengSpawn(command, args, spawnOption);
Expand Down Expand Up @@ -253,15 +253,12 @@ export class ExecutableAgent {
* @param cwd
* @returns
*/
async exec(
cwd?: string,
stdio?: CompleteStdioOptions,
args?: string[]
): Promise<MeteredChildProcess> {
async program(cwd?: string, stdio?: CompleteStdioOptions, args?: string[]) {
this.checkInit();
const languageRunOption = this.configuredLanguage.execOptionGenerator();
const languageRunOption =
this.configuredLanguage.pragramOptionGenerator();
if (languageRunOption.skip) {
throw new Error("Can't skip exec");
throw new Error("Can't skip pragram");
}
if (!this.compiled && !this.compileCached) {
throw new Error("Please compile first");
Expand All @@ -285,20 +282,19 @@ export class ExecutableAgent {
gid: getConfig().judger.gid,
timeLimit:
languageRunOption.spawnOption?.timeLimit ??
this.excutable.limit.runtime.cpuTime,
this.executable.limit.runtime.cpuTime,
memoryLimit:
languageRunOption.spawnOption?.memoryLimit ??
this.excutable.limit.runtime.memory,
this.executable.limit.runtime.memory,
pidLimit:
languageRunOption.spawnOption?.pidLimit ??
getConfig().judger.defaultPidLimit,
fileLimit:
languageRunOption.spawnOption?.fileLimit ??
this.excutable.limit.runtime.output,
this.executable.limit.runtime.output,
};

const subProc = hengSpawn(command, args, spawnOption);
return subProc;
return hengSpawn(command, args, spawnOption).result;
}
}

Expand Down
Loading

0 comments on commit c74c6f6

Please sign in to comment.