|
| 1 | +const { v4: uuid } = require('uuid'); |
| 2 | +const { exec } = require('child_process'); |
| 3 | +const fs = require('fs'); |
| 4 | +const os = require('os'); |
| 5 | +const path = require('path'); |
| 6 | +const saveFile = (file, data) => { |
| 7 | + return new Promise((resolve, reject) => { |
| 8 | + fs.writeFile(file, data, function (err) { |
| 9 | + if (err) { |
| 10 | + reject(err); |
| 11 | + } else { |
| 12 | + resolve([file]); |
| 13 | + } |
| 14 | + }); |
| 15 | + }); |
| 16 | +}; |
| 17 | + |
| 18 | +async function deleteFiles(pyPath, inputPath, exePath) { |
| 19 | + if (fs.existsSync(pyPath)) { |
| 20 | + await fs.unlinkSync(pyPath); |
| 21 | + } |
| 22 | + |
| 23 | + if (fs.existsSync(inputPath)) { |
| 24 | + await fs.unlinkSync(inputPath); |
| 25 | + } |
| 26 | + |
| 27 | + if (fs.existsSync(exePath)) { |
| 28 | + await fs.unlinkSync(exePath); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function getRunCommand(executable, input) { |
| 33 | + return `python ${executable} < ${input}`; |
| 34 | +} |
| 35 | + |
| 36 | +function getExecutablePath(fileName) { |
| 37 | + // console.log(os.platform()); |
| 38 | + if (os.platform() === 'win32') { |
| 39 | + return `${path.join(__dirname, '..', 'upload', fileName)}.exe`; |
| 40 | + } |
| 41 | + if (os.platform() === 'linux') { |
| 42 | + return `${path.join(__dirname, '..', 'upload', fileName)}`; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function getpyPath(fileName) { |
| 47 | + return `${path.join(__dirname, '..', 'upload', fileName)}.py`; |
| 48 | +} |
| 49 | + |
| 50 | +function getInputPath(fileName) { |
| 51 | + return `${path.join(__dirname, '..', 'upload', fileName)}-input.txt`; |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +function runProgram(exePath, inputPath) { |
| 57 | + return new Promise((resolve, reject) => { |
| 58 | + |
| 59 | + exec(getRunCommand(exePath, inputPath), (error, stdout, stderr) => { |
| 60 | + if (error) { |
| 61 | + reject({ error, stdout, stderr }); |
| 62 | + } else { |
| 63 | + resolve({ stdout, stderr }); |
| 64 | + } |
| 65 | + }); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +function runProgramNoIP(exePath) { |
| 71 | + return new Promise((resolve, reject) => { |
| 72 | + |
| 73 | + exec(`python ${exePath}`, (error, stdout, stderr) => { |
| 74 | + if (error) { |
| 75 | + reject({ error, stdout, stderr }); |
| 76 | + } else { |
| 77 | + resolve({ stdout, stderr }); |
| 78 | + } |
| 79 | + }); |
| 80 | + }); |
| 81 | +} |
| 82 | +const PythonCompile = async (code, input) => { |
| 83 | + let state = { |
| 84 | + stdout: null, |
| 85 | + stderr: null, |
| 86 | + statusMes: "", |
| 87 | + } |
| 88 | + if (input.length > 0) { |
| 89 | + let uniqueFileName = uuid(); |
| 90 | + let executePath = getExecutablePath(uniqueFileName) |
| 91 | + let pyPath = getpyPath(uniqueFileName) |
| 92 | + let ipPath = getInputPath(uniqueFileName) |
| 93 | + |
| 94 | + await saveFile(pyPath, code); |
| 95 | + await saveFile(ipPath, input); |
| 96 | + |
| 97 | + try { |
| 98 | + let { stdout, stderr } = await runProgram(pyPath, ipPath); |
| 99 | + state.stdout = stdout; |
| 100 | + state.stderr = stderr; |
| 101 | + } catch (err) { |
| 102 | + state.stderr = err.stderr; |
| 103 | + state.statusMes = "Compiler Error"; |
| 104 | + deleteFiles(pyPath, ipPath, executePath); |
| 105 | + return state; |
| 106 | + } |
| 107 | + if (state.stderr === '') { |
| 108 | + state.stderr = null; |
| 109 | + } |
| 110 | + state.statusMes = "Successfully Compiled"; |
| 111 | + await deleteFiles(pyPath, ipPath, executePath); |
| 112 | + return state; |
| 113 | + } else { |
| 114 | + let uniqueFileName = uuid(); |
| 115 | + let executePath = getExecutablePath(uniqueFileName) |
| 116 | + let pyPath = getpyPath(uniqueFileName) |
| 117 | + await saveFile(pyPath, code); |
| 118 | + try { |
| 119 | + let { stdout, stderr } = await runProgramNoIP(pyPath); |
| 120 | + state.stdout = stdout; |
| 121 | + state.stderr = stderr; |
| 122 | + } catch (err) { |
| 123 | + state.stderr = err.stderr; |
| 124 | + state.statusMes = "Compiler Error"; |
| 125 | + deleteFiles(pyPath, executePath); |
| 126 | + return state; |
| 127 | + } |
| 128 | + if (state.stderr === '') { |
| 129 | + state.stderr = null; |
| 130 | + } |
| 131 | + state.statusMes = "Successfully Compiled"; |
| 132 | + await deleteFiles(pyPath, executePath); |
| 133 | + return state; |
| 134 | + |
| 135 | + } |
| 136 | + return state; |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +module.exports = { PythonCompile }; |
0 commit comments