Skip to content

Commit

Permalink
Merge pull request #562 from SidorovVladimir/feat/new_languages
Browse files Browse the repository at this point in the history
#538 Language python
  • Loading branch information
dzencot authored Oct 30, 2024
2 parents 87a5287 + 9b5de02 commit d98495b
Show file tree
Hide file tree
Showing 5 changed files with 606 additions and 985 deletions.
1 change: 1 addition & 0 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ start-prod:

build:
npm run build
docker build -t my-code-runner-python ./src/runner/dockers/python

lint:
npm run lint
Expand Down
37 changes: 37 additions & 0 deletions backend/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vm from 'node:vm';
import { Console } from 'node:console';
import { Transform } from 'node:stream';
import { execSync } from 'node:child_process';
import { Injectable } from '@nestjs/common';
import { createContext } from './console/console.config';
import { Output } from './console/interfaces/output.interface';
Expand All @@ -12,25 +13,61 @@ export class AppService {
if (language === 'html') {
return { terminal: [code], alertLogs: [] };
}

if (language === 'python') {
const alertLogs = [];
try {
const command = `docker run --memory="256m" --cpus="1" --rm -i --read-only --user 1000:1000 my-code-runner-python python -c "${code.replace(
/"/g,
'\\"',
)}"`;
const stdout = execSync(command, {
stdio: 'pipe',
maxBuffer: 1024 * 1024,
}).toString();
const terminal = stdout.split('\n');
return { terminal, alertLogs };
} catch (error) {
const lineOfError = error.stack;
const startIndex =
lineOfError.indexOf('Traceback') === -1
? lineOfError.indexOf('File')
: lineOfError.indexOf('Traceback');
const trimmedMessage = lineOfError
.slice(startIndex)
.split('\n')
.slice(0, 5)
.join('\n');
const errorMsg = `${trimmedMessage}`;
return { terminal: [errorMsg], alertLogs };
}
}

const alertLogs = [];
const stdout = new Transform({
transform(chunk, enc, cb) {
cb(null, chunk);
},
});

const logger = new Console({ stdout });

const context = createContext(logger, alertLogs);

vm.createContext(context);

try {
const script = new vm.Script(code);

// run the script
script.runInContext(context);

const terminal = (stdout.read() || '').toString().split('\n');

return { terminal, alertLogs };
} catch (err) {
// eslint-disable-next-line no-console
console.log(err.message);
const lineOfError = err.stack
.split('evalmachine.<anonymous>:')[1]
.substring(0, 1);
Expand Down
3 changes: 3 additions & 0 deletions backend/src/runner/dockers/python/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM python:3-slim
WORKDIR /app
CMD ["python", "-c"]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"start:backend": "yarn workspace backend run start:debug",
"start:frontend": "yarn workspace frontend run start",
"start": "npm-run-all --parallel start:*",
"test": "playwright test --trace on"
"test": "playwright test --trace on"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit d98495b

Please sign in to comment.