From fec3f73d29094a674c9353faf1cc800bbb4a30e9 Mon Sep 17 00:00:00 2001 From: Shweta Date: Fri, 8 Jan 2021 18:21:09 +0530 Subject: [PATCH] fixed minor bugs --- .../server/api/services/code.service.js | 106 +++++++++--------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/rce-server/server/api/services/code.service.js b/rce-server/server/api/services/code.service.js index d90512b..1671ec5 100644 --- a/rce-server/server/api/services/code.service.js +++ b/rce-server/server/api/services/code.service.js @@ -1,12 +1,12 @@ -import fs from 'fs'; -import path from 'path'; -import util from 'util'; -import { execFile, spawn, exec } from 'child_process'; -import ValidationService from './validation.service'; +import fs from "fs"; +import path from "path"; +import util from "util"; +import { execFile, spawn, exec } from "child_process"; +import ValidationService from "./validation.service"; const ROOT_DIR = `${process.cwd()}`; -const SOURCE_DIR = path.join(ROOT_DIR, 'executor'); +const SOURCE_DIR = path.join(ROOT_DIR, "executor"); const TARGET_DIR = `/app/codes`; -const IMAGE_NAME = 'executor:1.0'; +const IMAGE_NAME = "executor:1.0"; //const VOL_NAME = `my_vol`; const VOL_NAME = SOURCE_DIR; @@ -14,7 +14,7 @@ class CodeService { async execute(code, input, lang, id) { //console.log('code', code); try { - !input ? (input = '') : null; + !input ? (input = "") : null; // validating code // await this.validateCode(code, input, lang, id); @@ -26,7 +26,7 @@ class CodeService { ); if (!isValid) { throw { - message + message, }; } @@ -54,7 +54,7 @@ class CodeService { ); if (OUTPUT) { - console.log('output', OUTPUT.toString()); + console.log("output", OUTPUT.toString()); return OUTPUT.toString(); } } catch (error) { @@ -65,28 +65,28 @@ class CodeService { async writeFile(code, lang, input, id) { let fileName = `${id}code`; switch (lang) { - case 'javascript': { - fileName += '.js'; + case "javascript": { + fileName += ".js"; break; } - case 'cpp': { - fileName += '.cpp'; + case "cpp": { + fileName += ".cpp"; break; } - case 'python': { - fileName += '.py'; + case "python": { + fileName += ".py"; break; } - case 'java': { - fileName += '.java'; + case "java": { + fileName += ".java"; break; } - case 'c': { - fileName += '.c'; + case "c": { + fileName += ".c"; break; } default: { - throw { message: 'Invalid language' }; + throw { message: "Invalid language" }; } } const write = util.promisify(fs.writeFile); @@ -96,7 +96,7 @@ class CodeService { await write(path.join(SOURCE_DIR, `${id}input.txt`), input); return { file: fileName, - inputFile: `${id}input.txt` + inputFile: `${id}input.txt`, }; } catch (error) { throw { message: error }; @@ -104,33 +104,33 @@ class CodeService { } async writeCommand(lang, file, input, id, code) { - let command = ''; + let command = ""; switch (lang) { - case 'javascript': { - command = `cd ${TARGET_DIR} && node ${file} < ${input}`; + case "javascript": { + command = `cd "${TARGET_DIR}" && node ${file} < ${input}`; break; } - case 'cpp': { - command = `cd ${TARGET_DIR} && g++ -o ${id} ${file} && ./${id} < ${input}`; + case "cpp": { + command = `cd "${TARGET_DIR}" && g++ -o ${id} ${file} && ./${id} < ${input}`; break; } - case 'python': { - command = `cd ${TARGET_DIR} && python ${file} < ${input}`; + case "python": { + command = `cd "${TARGET_DIR}" && python ${file} < ${input}`; break; } - case 'java': { + case "java": { let className = await this.extractJavaClassName(code); - className = className.split(/\s/).join(''); - console.log('class ', className); - command = `cd ${TARGET_DIR} && javac ${file} && java ${className} < ${input}`; + className = className.split(/\s/).join(""); + console.log("class ", className); + command = `cd "${TARGET_DIR}" && javac ${file} && java ${className} < ${input}`; break; } - case 'c': { - command = `cd ${TARGET_DIR} && gcc -o ${id} ${file} && ./${id} < ${input}`; + case "c": { + command = `cd "${TARGET_DIR}" && gcc -o ${id} ${file} && ./${id} < ${input}`; break; } default: { - throw { message: 'Invalid language' }; + throw { message: "Invalid language" }; } } @@ -138,7 +138,7 @@ class CodeService { const runCode = `docker exec ${containerName} sh -c "${command}"`; - const runContainer = `docker run -it -d --name ${containerName} -v ${VOL_NAME}:${TARGET_DIR} ${IMAGE_NAME}`; + const runContainer = `docker run -it -d --name ${containerName} -v "${VOL_NAME}":${TARGET_DIR} ${IMAGE_NAME}`; return { runCode, runContainer }; } @@ -146,10 +146,10 @@ class CodeService { async execChild(runCode, runContainer, id, file, inputFile, lang, code) { return new Promise((resolve, reject) => { const execCont = exec(`${runContainer}`); - execCont.on('error', err => { - throw { status: '404', message: err }; + execCont.on("error", (err) => { + throw { status: "404", message: err }; }); - execCont.stdout.on('data', () => { + execCont.stdout.on("data", () => { exec(`${runCode}`, async (error, stdout, stderr) => { await this.endContainer(id); await this.deleteFiles(file, inputFile, lang, id, code); @@ -164,26 +164,26 @@ class CodeService { } async deleteFiles(fileName, inputName, lang, id, code) { - fs.unlinkSync(path.join(SOURCE_DIR, fileName), err => { + fs.unlinkSync(path.join(SOURCE_DIR, fileName), (err) => { if (err) throw { message: err }; }); if (inputName) { - fs.unlinkSync(path.join(SOURCE_DIR, inputName), err => { + fs.unlinkSync(path.join(SOURCE_DIR, inputName), (err) => { if (err) throw { message: err }; }); } - if (lang == 'cpp' || lang == 'c') { + if (lang == "cpp" || lang == "c") { if (fs.existsSync(path.join(SOURCE_DIR, id))) - fs.unlinkSync(path.join(SOURCE_DIR, id), err => { + fs.unlinkSync(path.join(SOURCE_DIR, id), (err) => { if (err) throw err; }); } - if (lang == 'java') { + if (lang == "java") { let className = await this.extractJavaClassName(code); - className = className.split(/\s/).join(''); - console.log('delete', className); + className = className.split(/\s/).join(""); + console.log("delete", className); if (fs.existsSync(path.join(SOURCE_DIR, `${className}.class`))) - fs.unlinkSync(path.join(SOURCE_DIR, `${className}.class`), err => { + fs.unlinkSync(path.join(SOURCE_DIR, `${className}.class`), (err) => { if (err) throw err; }); } @@ -195,25 +195,25 @@ class CodeService { exec(`${exit}`, (error, stdout, stderr) => { if (error) { console.log(error); - } else console.log('Container stoped and deleted'); + } else console.log("Container stoped and deleted"); }); } async extractJavaClassName(s) { - let prefix = 'class'; - let suffix = '{'; + let prefix = "class"; + let suffix = "{"; let i = s.indexOf(prefix); if (i >= 0) { s = s.substring(i + prefix.length); } else { - return ''; + return ""; } if (suffix) { i = s.indexOf(suffix); if (i >= 0) { s = s.substring(0, i); } else { - return ''; + return ""; } } return s;