Hello yarn berry

This commit is contained in:
undefined committed 2021-05-26 23:35:36 +08:00
1 parent 04a4306895
commit 782f334a27
62 files changed
+448 -239

No files matched your search

+6 -4
View File
@@ -1,18 +1,20 @@
import path from 'path';
import fs from 'fs-extra';
import { argv } from 'yargs';
import cac from 'cac';
import { Logger } from './log';
import { getConfig } from './config';
const argv = cac().parse();
const logger = new Logger('cache');
export = function main() {
const CACHE_DIR = getConfig('cache_dir');
if (argv._[1] === 'clean') {
if (argv.args[1] === 'clean') {
fs.emptyDirSync(CACHE_DIR);
logger.info('Cleaned cache.');
} else if (argv._[1] === 'prune') {
const duration = +argv.duration || 30;
} else if (argv.args[1] === 'prune') {
const duration = +argv.options.duration || 30;
const now = new Date().getTime();
const hosts = fs.readdirSync(CACHE_DIR);
let cnt = 0;
+12 -11
View File
@@ -1,11 +1,12 @@
/* eslint-disable prefer-const */
import { argv } from 'yargs';
import cac from 'cac';
import os from 'os';
import path from 'path';
import fs from 'fs-extra';
import yaml from 'js-yaml';
import log from './log';
const argv = cac().parse();
let CONFIG_FILE = path.resolve(os.homedir(), '.config', 'hydro', 'judge.yaml');
let LANGS_FILE = path.resolve(os.homedir(), '.config', 'hydro', 'langs.yaml');
@@ -43,20 +44,20 @@ if (fs.existsSync(path.resolve(process.cwd(), '.env'))) {
if (!global.Hydro) {
// standalone
if (process.env.CONFIG_FILE || argv.config) {
CONFIG_FILE = path.resolve(process.env.CONFIG_FILE || argv.config as string);
if (process.env.CONFIG_FILE || argv.options.config) {
CONFIG_FILE = path.resolve(process.env.CONFIG_FILE || argv.options.config);
}
if (process.env.LANGS_FILE || argv.langs) {
LANGS_FILE = path.resolve(process.env.LANGS_FILE || argv.langs as string);
if (process.env.LANGS_FILE || argv.options.langs) {
LANGS_FILE = path.resolve(process.env.LANGS_FILE || argv.options.langs);
}
if (process.env.TEMP_DIR || argv.tmp) {
config.tmp_dir = path.resolve(process.env.TEMP_DIR || argv.tmp as string);
if (process.env.TEMP_DIR || argv.options.tmp) {
config.tmp_dir = path.resolve(process.env.TEMP_DIR || argv.options.tmp);
}
if (process.env.CACHE_DIR || argv.cache) {
config.cache_dir = path.resolve(process.env.CACHE_DIR || argv.cache as string);
if (process.env.CACHE_DIR || argv.options.cache) {
config.cache_dir = path.resolve(process.env.CACHE_DIR || argv.options.cache);
}
if (process.env.EXECUTION_HOST || argv.sandbox) {
config.sandbox_host = path.resolve(process.env.EXECUTION_HOST || argv.execute as string);
if (process.env.EXECUTION_HOST || argv.options.sandbox) {
config.sandbox_host = path.resolve(process.env.EXECUTION_HOST || argv.options.sandbox);
}
const configFile = fs.readFileSync(CONFIG_FILE).toString();
config = { ...config, ...yaml.load(configFile) as any };
+5 -3
View File
@@ -3,9 +3,9 @@ import path from 'path';
import axios from 'axios';
import fs from 'fs-extra';
import WebSocket from 'ws';
import { argv } from 'yargs';
import cac from 'cac';
import { noop } from 'lodash';
import { ObjectID } from 'bson';
import { ObjectID } from 'mongodb';
import { LangConfig } from '@hydrooj/utils/lib/lang';
import * as tmpfs from '../tmpfs';
import log from '../log';
@@ -17,6 +17,8 @@ import readCases from '../cases';
import judge from '../judge';
import * as sysinfo from '../sysinfo';
const argv = cac().parse();
class JudgeTask {
stat: Record<string, Date>;
session: any;
@@ -86,7 +88,7 @@ class JudgeTask {
});
} else {
log.error(e);
this.next({ message: { message: e.message, params: e.params || [], ...argv.debug ? { stack: e.stack } : {} } });
this.next({ message: { message: e.message, params: e.params || [], ...argv.options.debug ? { stack: e.stack } : {} } });
this.end({
status: STATUS_SYSTEM_ERROR, score: 0, time_ms: 0, memory_kb: 0,
});
+1 -2
View File
@@ -1,7 +1,6 @@
import Queue from 'p-queue';
import path from 'path';
import fs from 'fs-extra';
import { argv } from 'yargs';
import * as STATUS from '../status';
import { SystemError } from '../error';
import { parseFilename } from '../utils';
@@ -173,7 +172,7 @@ export const judge = async (ctx) => {
if (effective) ctx.total_score += scores[sid];
}
ctx.stat.done = new Date();
if (argv.debug) ctx.next({ message: JSON.stringify(ctx.stat) });
if (process.env.DEV) ctx.next({ message: JSON.stringify(ctx.stat) });
ctx.end({
status: ctx.total_status,
score: ctx.total_score,
+1 -2
View File
@@ -1,6 +1,5 @@
import Queue from 'p-queue';
import fs from 'fs-extra';
import { argv } from 'yargs';
import * as STATUS from '../status';
import { parse } from '../testlib';
import { findFileSync, parseFilename } from '../utils';
@@ -123,7 +122,7 @@ export const judge = async (ctx) => {
}
await Promise.all(tasks);
ctx.stat.done = new Date();
if (argv.debug) ctx.next({ message: JSON.stringify(ctx.stat) });
if (process.env.DEV) ctx.next({ message: JSON.stringify(ctx.stat) });
console.log({
status: ctx.total_status,
score: ctx.total_score,
+1 -2
View File
@@ -1,6 +1,5 @@
import fs from 'fs-extra';
import path from 'path';
import { argv } from 'yargs';
import * as STATUS from '../status';
import { run } from '../sandbox';
import compile from '../compile';
@@ -100,7 +99,7 @@ export const judge = async (ctx) => {
},
});
ctx.stat.done = new Date();
if (argv.debug) ctx.next({ message: JSON.stringify(ctx.stat) });
if (process.env.DEV) ctx.next({ message: JSON.stringify(ctx.stat) });
ctx.end({
status,
score: status === STATUS.STATUS_ACCEPTED ? 100 : 0,
+4 -2
View File
@@ -1,5 +1,7 @@
import { inspect, InspectOptions, format } from 'util';
import { argv } from 'yargs';
import cac from 'cac';
const argv = cac().parse();
namespace Time {
export const second = 1000;
@@ -41,7 +43,7 @@ export class Logger {
static readonly INFO = 2;
static readonly WARN = 2;
static readonly DEBUG = 3;
static baseLevel = argv.debug ? 3 : 2;
static baseLevel = argv.options.debug ? 3 : 2;
static showDiff = false;
static levels: Record<string, number> = {};
static lastTime = 0;
+6 -5
View File
@@ -1,12 +1,13 @@
import Axios from 'axios';
import fs from 'fs-extra';
import { argv } from 'yargs';
import cac from 'cac';
import * as STATUS from './status';
import { FormatError, SystemError } from './error';
import { cmd, parseMemoryMB } from './utils';
import { getConfig } from './config';
import { Logger } from './log';
const argv = cac().parse();
const logger = new Logger('sandbox');
let callId = 0;
@@ -90,9 +91,9 @@ export async function runMultiple(execute) {
body.cmd[1].files[0] = null;
body.cmd[1].files[1] = null;
const id = callId++;
if (argv['show-sandbox-call']) logger.debug('%d %s', id, JSON.stringify(body));
if (argv.options['show-sandbox-call']) logger.debug('%d %s', id, JSON.stringify(body));
res = await Axios.create({ baseURL: getConfig('sandbox_host') }).post('/run', body);
if (argv['show-sandbox-call']) logger.debug('%d %s', id, JSON.stringify(res.data));
if (argv.options['show-sandbox-call']) logger.debug('%d %s', id, JSON.stringify(res.data));
} catch (e) {
if (e instanceof FormatError) throw e;
throw new SystemError('Sandbox Error');
@@ -113,9 +114,9 @@ export async function run(execute, params?) {
try {
const body = { cmd: [proc({ execute, ...params })] };
const id = callId++;
if (argv['show-sandbox-call']) logger.debug('%d %s', id, JSON.stringify(body));
if (argv.options['show-sandbox-call']) logger.debug('%d %s', id, JSON.stringify(body));
const res = await Axios.create({ baseURL: getConfig('sandbox_host') }).post('/run', body);
if (argv['show-sandbox-call']) logger.debug('%d %s', id, JSON.stringify(res.data));
if (argv.options['show-sandbox-call']) logger.debug('%d %s', id, JSON.stringify(res.data));
[result] = res.data;
} catch (e) {
if (e instanceof FormatError) throw e;
+3 -3
View File
@@ -2,8 +2,8 @@
/* eslint-disable no-await-in-loop */
import 'hydrooj';
import path from 'path';
import { argv } from 'yargs';
import { ObjectID } from 'bson';
import cac from 'cac';
import { ObjectID } from 'mongodb';
import fs from 'fs-extra';
import { noop } from 'lodash';
import { Logger } from 'hydrooj/dist/logger';
@@ -217,7 +217,7 @@ async function postInit() {
});
} else {
logger.error(e);
this.next({ message: { message: e.message, params: e.params, ...argv.debug ? { stack: e.stack } : {} } });
this.next({ message: { message: e.message, params: e.params, ...process.env.DEV ? { stack: e.stack } : {} } });
this.end({
status: STATUS_SYSTEM_ERROR, score: 0, time_ms: 0, memory_kb: 0,
});