Skip to content

Commit

Permalink
feat: judge cpp file experimentally
Browse files Browse the repository at this point in the history
  • Loading branch information
seo-rii committed Dec 29, 2021
1 parent fd19d0c commit 201bc2d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM node:16-alpine
EXPOSE 80
RUN apk add --update alpine-sdk
RUN apk add --update alpine-sdk runuser
RUN apk update
COPY dist/ /HANA/dist/
COPY res/ /HANA/res/
Expand Down
10 changes: 9 additions & 1 deletion res/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,15 @@
},
],
dataSet: {
data: [{ input: '1 2', output: '3' }],
data: [
{ input: '1 2', output: '3' },
{ input: '1 3', output: '4' },
{ input: '0 3', output: '3' },
{ input: '5 3', output: '8' },
{ input: '9 2', output: '11' },
{ input: '15 5', output: '20' },
{ input: '-2 -4', output: '-6' },
],
},
timeLimit: 0,
memoryLimit: 0,
Expand Down
23 changes: 13 additions & 10 deletions src/runner/cpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { sendMessage } from '../socket'
import { JudgeResult, WebSocketResponseType } from '../types/response'
import { execute, isSame } from './index'
import * as fs from 'fs'
import { spawn } from 'child_process'
import { spawn, execSync } from 'child_process'

export default function (
data: JudgeRequest<
Expand Down Expand Up @@ -50,7 +50,7 @@ export default function (
if (data) stderr += data.toString()
})

cp.on('exit', (code) => {
cp.on('exit', async (code) => {
if (code !== 0) {
fs.rmdirSync(tmpPath, { recursive: true })
resolve({
Expand All @@ -63,29 +63,31 @@ export default function (
})
return
} else {
execSync(
`adduser --disabled-password --no-create-home p-${data.uid}`
)
sendMessage(WebSocketResponseType.JUDGE_PROGRESS, {
uid: data.uid,
progress: 0,
reason: 'RUN',
})

for (const i in data.dataSet.data) {
if (
isSame(
execute(exePath, data.dataSet.data[i].input),
data.dataSet.data[i].output
)
) {
const result = await execute(
`p-${data.uid}`,
exePath,
data.dataSet.data[i].input
)
if (isSame(result.stdout, data.dataSet.data[i].output))
match[i] = true
}
sendMessage(WebSocketResponseType.JUDGE_PROGRESS, {
uid: data.uid,
progress:
(i as unknown as number) / data.dataSet.data.length,
reason: 'RUN',
})
}
fs.rmdirSync(tmpPath, { recursive: true })
fs.rmSync(tmpPath, { recursive: true })
resolve({
uid: data.uid,
result: match,
Expand All @@ -96,6 +98,7 @@ export default function (
time: 0,
memory: 0,
})
execSync(`deluser p-${data.uid}`)
}
})
})
Expand Down
33 changes: 31 additions & 2 deletions src/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,38 @@ import {
import judgeText from './text'
import judgeCPP from './cpp'
import { JudgeResult } from '../types/response'
import { spawn } from 'child_process'

export function execute(exePath: string, input: string) {
return ''
export function execute(userName: string, exePath: string, input: string) {
return new Promise<{ code: number; stdout: string; stderr: string }>(
(resolve, reject) => {
const child = spawn(
`runuser`,
['-l', userName, '-c', `'${exePath}'`],
{
stdio: ['pipe', 'pipe', 'pipe'],
}
)

child.stdin.write(input)
child.stdin.end()

let stdout = '',
stderr = ''

child.stdout.on('data', (data: any) => {
stdout += data
})

child.stderr.on('data', (data: any) => {
stderr += data
})

child.on('close', (code) => {
resolve({ code: code || 0, stdout, stderr })
})
}
)
}

export function isSame(in1: string, in2: string): boolean {
Expand Down

0 comments on commit 201bc2d

Please sign in to comment.