├── assets ├── Readme.md ├── Rapid.png ├── rapid1.png ├── temp.png └── Simple Compiler API.png ├── .dockerignore ├── heroku.yml ├── upload ├── Main.class └── upload.txt ├── .gitignore ├── package.json ├── server.js ├── Dockerfile ├── README.md ├── api ├── pythonApi.js ├── cppApi.js └── javaApi.js ├── .github ├── ISSUE_TEMPLATE │ └── bug_report.md └── workflows │ └── codeql-analysis.yml ├── LICENSE ├── compiler ├── java.js ├── cpp.js └── python.js └── CODE_OF_CONDUCT.md /assets/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | **/node_modules 2 | .gitignore 3 | .git -------------------------------------------------------------------------------- /heroku.yml: -------------------------------------------------------------------------------- 1 | build: 2 | docker: 3 | web: Dockerfile 4 | run: 5 | web: npm run start 6 | -------------------------------------------------------------------------------- /assets/Rapid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codewithvk/Easy-Compiler-API/HEAD/assets/Rapid.png -------------------------------------------------------------------------------- /assets/rapid1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codewithvk/Easy-Compiler-API/HEAD/assets/rapid1.png -------------------------------------------------------------------------------- /assets/temp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codewithvk/Easy-Compiler-API/HEAD/assets/temp.png -------------------------------------------------------------------------------- /upload/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codewithvk/Easy-Compiler-API/HEAD/upload/Main.class -------------------------------------------------------------------------------- /upload/upload.txt: -------------------------------------------------------------------------------- 1 | Here all file create so don't remove this txt else your code not work in production mode :/ -------------------------------------------------------------------------------- /assets/Simple Compiler API.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codewithvk/Easy-Compiler-API/HEAD/assets/Simple Compiler API.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "compiler", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start" : "nodemon server.js" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "chalk": "^4.1.2", 15 | "concurrently": "^6.3.0", 16 | "cors": "^2.8.5", 17 | "express": "^4.17.1", 18 | "nodemon": "^2.0.14", 19 | "uuid": "^8.3.2" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const express = require('express'); 3 | 4 | const app = express(); 5 | const PORT = process.env.PORT || 8080; 6 | 7 | // Create router 8 | app.use(express.json()); 9 | app.use(express.urlencoded({ extended: true })); 10 | 11 | // Testing routers 12 | app.get('/', (req, res) => { 13 | res.send("API IS RUNNING") 14 | }) 15 | 16 | // C++ Router 17 | app.use('/api', require('./api/cppApi')); 18 | 19 | // Python Router 20 | app.use('/api/python', require('./api/pythonApi')); 21 | 22 | // Java Router 23 | app.use('/api/java', require('./api/javaApi')) 24 | 25 | 26 | // Serve static files 27 | app.listen(PORT, () => { 28 | console.log(`Listening at port ${PORT}`); 29 | }); 30 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu 2 | FROM node:17 3 | 4 | WORKDIR /usr/src/app 5 | 6 | ENV DEBIAN_FRONTEND noninteractive 7 | RUN apt-get update && \ 8 | apt-get -y install gcc mono-mcs && \ 9 | rm -rf /var/lib/apt/lists/* && \ 10 | apt-get -y install python3 11 | 12 | RUN apt update -y && apt-get install -y software-properties-common && \ 13 | apt-add-repository 'deb http://security.debian.org/debian-security stretch/updates main' && apt-get update -y && \ 14 | apt-get install -y openjdk-8-jdk-headless && \ 15 | apt-get clean 16 | ENV java /usr/lib/jvm/java-8-openjdk-amd64/ 17 | RUN export java 18 | 19 | COPY package*.json ./ 20 | 21 | RUN npm install 22 | 23 | 24 | COPY . . 25 | 26 | EXPOSE 8080 27 | CMD ["npm","run","start"] 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Easy-Compiler-API 2 |

3 | 4 |

🚀 Open Source Compiler API which help you by compile your code on production envierment. ✡️

5 |

6 | 7 | 8 | ## Important 9 | 10 |

11 | 12 |

13 | 14 | 15 | #### [API Reference](https://rapidapi.com/codewithvk/api/easy-compiler-api/) 16 | - If you want fast performance or you do it at a low cost, then we recommended cloning this repo and run on your own cloud. 17 | - Don't forget to give 🌟 Star 🌟 to a repository. 18 | 19 | ### Programming Language 20 | - [x] CPP 21 | - [x] Python 22 | - [x] Java
23 | 24 | 25 | Fun Fact 😉 :- You can deploy it on free cloud provider(heroku) |:) 26 | -------------------------------------------------------------------------------- /api/pythonApi.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const { PythonCompile } = require('../compiler/python'); 3 | const router = express.Router(); 4 | /* 5 | 6 | @Route: /api/python 7 | @Method: POST 8 | @Body: { 9 | code: string, 10 | input: string, 11 | } 12 | Alert : Make sure code and input are hex format. 13 | 14 | */ 15 | router.post('/', async (req, res) => { 16 | const InputCode = Buffer.from(req.body.code, 'base64').toString('binary') 17 | const DeCode = Buffer.from(req.body.input, 'base64').toString('binary') 18 | let response = await PythonCompile(InputCode, DeCode); 19 | console.log({response}) 20 | if (response.statusMes === "Compiler Error") { 21 | res.status(202).json(response) 22 | } else if (response.statusMes === "Run Time Error") { 23 | res.status(201).json(response) 24 | } else { 25 | res.status(200).json(response) 26 | } 27 | 28 | }); 29 | 30 | 31 | 32 | 33 | module.exports = router; 34 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /api/cppApi.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const { CPP, CppCompile } = require('../compiler/cpp'); 3 | const router = express.Router(); 4 | 5 | router.post('/', async (req, res) => { 6 | res.json(req.body); 7 | }) 8 | 9 | /* 10 | @Route: /api/cpp 11 | @Method: POST 12 | @Body: { 13 | code: string, 14 | input: string, 15 | } 16 | 17 | Alert : Make sure code and input are hex format. 18 | */ 19 | router.post('/cpp', async (req, res) => { 20 | const InputCode = Buffer.from(req.body.code, 'base64').toString('binary') 21 | const DeCode = Buffer.from(req.body.input, 'base64').toString('binary') 22 | let response = await CppCompile(InputCode, DeCode); 23 | if (response.statusMes === "Compiler Error") { 24 | res.status(202).json(response) 25 | } else if (response.statusMes === "Run Time Error") { 26 | res.status(201).json(response) 27 | } else { 28 | res.status(200).json(response) 29 | } 30 | 31 | }); 32 | 33 | 34 | 35 | 36 | module.exports = router; 37 | -------------------------------------------------------------------------------- /api/javaApi.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const { javaCompile} = require('../compiler/java') 3 | const router = express.Router(); 4 | 5 | 6 | /* 7 | @Route: /api/java 8 | @Method: POST 9 | @Body: { 10 | code: string, 11 | input: string, 12 | } 13 | 14 | Alert : Make sure code and input are hex format. 15 | You can pass class name in header too , if you want customized class. By default it is Main. 16 | */ 17 | 18 | 19 | router.post('/', async (req, res) => { 20 | const InputCode = Buffer.from(req.body.code, 'base64').toString('binary') 21 | const DeCode = Buffer.from(req.body.input, 'base64').toString('binary') 22 | const CentralClass = req?.headers?.class || "Main" 23 | let response = await javaCompile(InputCode, DeCode, CentralClass); 24 | if (response.statusMes === "Compiler Error") { 25 | res.status(202).json(response) 26 | } else if (response.statusMes === "Run Time Error") { 27 | res.status(201).json(response) 28 | } else { 29 | res.status(200).json(response) 30 | } 31 | 32 | }); 33 | 34 | 35 | 36 | 37 | module.exports = router; 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Vivekkumar Javiya 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ "master" ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ "master" ] 20 | schedule: 21 | - cron: '32 10 * * 0' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | permissions: 28 | actions: read 29 | contents: read 30 | security-events: write 31 | 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | language: [ 'javascript' ] 36 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] 37 | # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support 38 | 39 | steps: 40 | - name: Checkout repository 41 | uses: actions/checkout@v3 42 | 43 | # Initializes the CodeQL tools for scanning. 44 | - name: Initialize CodeQL 45 | uses: github/codeql-action/init@v2 46 | with: 47 | languages: ${{ matrix.language }} 48 | # If you wish to specify custom queries, you can do so here or in a config file. 49 | # By default, queries listed here will override any specified in a config file. 50 | # Prefix the list here with "+" to use these queries and those in the config file. 51 | 52 | # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs 53 | # queries: security-extended,security-and-quality 54 | 55 | 56 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 57 | # If this step fails, then you should remove it and run the build manually (see below) 58 | - name: Autobuild 59 | uses: github/codeql-action/autobuild@v2 60 | 61 | # ℹ️ Command-line programs to run using the OS shell. 62 | # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun 63 | 64 | # If the Autobuild fails above, remove it and uncomment the following three lines. 65 | # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. 66 | 67 | # - run: | 68 | # echo "Run, Build Application using script" 69 | # ./location_of_script_within_repo/buildscript.sh 70 | 71 | - name: Perform CodeQL Analysis 72 | uses: github/codeql-action/analyze@v2 73 | -------------------------------------------------------------------------------- /compiler/java.js: -------------------------------------------------------------------------------- 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(javaPath, inputPath, exePath) { 19 | if (fs.existsSync(javaPath)) { 20 | await fs.unlinkSync(javaPath); 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 | 33 | 34 | function getExecutablePath(fileName) { 35 | // console.log(os.platform()); 36 | if (os.platform() === 'win32') { 37 | return `${path.join(__dirname, '..', 'upload', fileName)}.exe`; 38 | } 39 | if (os.platform() === 'linux') { 40 | return `${path.join(__dirname, '..', 'upload', fileName)}`; 41 | } 42 | } 43 | 44 | function getRunCommand(input, CentralClass) { 45 | 46 | let path = getExecutablePath("") 47 | return `cd ${path} && \ java ${CentralClass} < ${input}`; 48 | 49 | } 50 | 51 | function getjavaPath(fileName) { 52 | return `${path.join(__dirname, '..', 'upload', fileName)}.java`; 53 | } 54 | 55 | function getInputPath(fileName) { 56 | return `${path.join(__dirname, '..', 'upload', fileName)}-input.txt`; 57 | } 58 | 59 | function compileProgram(javaPath) { 60 | return new Promise((resolve, reject) => { 61 | exec(`javac ${javaPath}`, (error, stdout, stderr) => { 62 | if (error) { 63 | reject({ error, stdout, stderr }); 64 | } else { 65 | resolve({ stdout, stderr }); 66 | } 67 | }); 68 | }); 69 | } 70 | 71 | function runProgram(inputPath, CentralClass) { 72 | return new Promise((resolve, reject) => { 73 | exec(getRunCommand(inputPath, CentralClass), (error, stdout, stderr) => { 74 | if (error) { 75 | reject({ error, stdout, stderr }); 76 | } else { 77 | resolve({ stdout, stderr }); 78 | } 79 | }); 80 | }); 81 | } 82 | 83 | 84 | const javaCompile = async (code, input, CentralClass) => { 85 | let state = { 86 | stdout: null, 87 | stderr: null, 88 | statusMes: "", 89 | } 90 | let uniqueFileName = uuid(); 91 | // let executePath = getExecutablePath(uniqueFileName) 92 | let javaPath = getjavaPath(uniqueFileName) 93 | let ipPath = getInputPath(uniqueFileName) 94 | 95 | await saveFile(javaPath, code); 96 | await saveFile(ipPath, input); 97 | 98 | try { 99 | let { stdout, stderr } = await compileProgram(javaPath); 100 | } catch (err) { 101 | state.stderr = err.stderr; 102 | state.statusMes = "Compiler Error"; 103 | deleteFiles(javaPath, ipPath); 104 | return state; 105 | } 106 | 107 | try { 108 | let { stdout, stderr } = await runProgram(ipPath, CentralClass); 109 | state.stdout = stdout; 110 | state.stderr = stderr; 111 | 112 | } catch (err) { 113 | state.stderr = err.stderr; 114 | state.statusMes = "Run Time Error"; 115 | deleteFiles(javaPath, ipPath); 116 | } 117 | 118 | if (state.stderr === '') { 119 | state.stderr = null; 120 | } 121 | state.statusMes = "Successfully Compiled"; 122 | await deleteFiles(javaPath, ipPath); 123 | return state; 124 | 125 | } 126 | 127 | 128 | 129 | 130 | module.exports = { javaCompile }; 131 | -------------------------------------------------------------------------------- /compiler/cpp.js: -------------------------------------------------------------------------------- 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(cppPath, inputPath, exePath) { 19 | if (fs.existsSync(cppPath)) { 20 | await fs.unlinkSync(cppPath); 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 `${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 getCPPPath(fileName) { 47 | return `${path.join(__dirname, '..', 'upload', fileName)}.cpp`; 48 | } 49 | 50 | function getInputPath(fileName) { 51 | return `${path.join(__dirname, '..', 'upload', fileName)}-input.txt`; 52 | } 53 | 54 | function compileProgram(cppPath, exePath) { 55 | return new Promise((resolve, reject) => { 56 | exec(`g++ -o ${exePath} ${cppPath}`, (error, stdout, stderr) => { 57 | if (error) { 58 | reject({ error, stdout, stderr }); 59 | } else { 60 | resolve({ stdout, stderr }); 61 | } 62 | }); 63 | }); 64 | } 65 | 66 | function runProgram(exePath, inputPath) { 67 | return new Promise((resolve, reject) => { 68 | exec(getRunCommand(exePath, inputPath), (error, stdout, stderr) => { 69 | if (error) { 70 | reject({ error, stdout, stderr }); 71 | } else { 72 | resolve({ stdout, stderr }); 73 | } 74 | }); 75 | }); 76 | } 77 | 78 | 79 | const CppCompile = async (code, input) => { 80 | // State for response and error 81 | let state = { 82 | stdout: null, 83 | stderr: null, 84 | statusMes : "", 85 | } 86 | 87 | // Path to compiler and input file code. 88 | let uniqueFileName = uuid(); 89 | let executePath = getExecutablePath(uniqueFileName) 90 | let cppPath = getCPPPath(uniqueFileName) 91 | let ipPath = getInputPath(uniqueFileName) 92 | 93 | // Save the path of input and code file. 94 | await saveFile(cppPath, code); 95 | await saveFile(ipPath, input); 96 | 97 | try { 98 | // Compile the code. 99 | await compileProgram(cppPath, executePath); 100 | } catch (err) { 101 | state.stderr = err.stderr; 102 | state.statusMes = "Compiler Error"; 103 | deleteFiles(cppPath, ipPath, executePath); 104 | return state; 105 | } 106 | 107 | try { 108 | // run the code. 109 | let { stdout, stderr } = await runProgram(executePath, ipPath); 110 | state.stdout = stdout; 111 | state.stderr = stderr; 112 | } catch (err) { 113 | state.stderr = err.stderr; 114 | state.statusMes = "Run Time Error"; 115 | deleteFiles(cppPath, ipPath, executePath); 116 | } 117 | 118 | if (state.stderr === '') { 119 | state.stderr = null; 120 | } 121 | state.statusMes = "Successfully Compiled"; 122 | // After compile delete exiting file for server. 123 | await deleteFiles(cppPath, ipPath, executePath); 124 | return state; 125 | 126 | } 127 | 128 | 129 | 130 | 131 | module.exports = { CppCompile }; 132 | -------------------------------------------------------------------------------- /compiler/python.js: -------------------------------------------------------------------------------- 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 | 34 | return `python3 ${executable} < ${input}`; 35 | 36 | 37 | } 38 | 39 | function getExecutablePath(fileName) { 40 | // console.log(os.platform()); 41 | if (os.platform() === 'win32') { 42 | return `${path.join(__dirname, '..', 'upload', fileName)}.exe`; 43 | } 44 | if (os.platform() === 'linux') { 45 | return `${path.join(__dirname, '..', 'upload', fileName)}`; 46 | } 47 | } 48 | 49 | function getpyPath(fileName) { 50 | return `${path.join(__dirname, '..', 'upload', fileName)}.py`; 51 | } 52 | 53 | function getInputPath(fileName) { 54 | return `${path.join(__dirname, '..', 'upload', fileName)}-input.txt`; 55 | } 56 | 57 | 58 | 59 | function runProgram(exePath, inputPath) { 60 | return new Promise((resolve, reject) => { 61 | 62 | exec(getRunCommand(exePath, inputPath), (error, stdout, stderr) => { 63 | if (error) { 64 | reject({ error, stdout, stderr }); 65 | } else { 66 | resolve({ stdout, stderr }); 67 | } 68 | }); 69 | }); 70 | } 71 | 72 | 73 | function runProgramNoIP(exePath) { 74 | return new Promise((resolve, reject) => { 75 | 76 | exec(`python3 ${exePath}`, (error, stdout, stderr) => { 77 | 78 | if (error) { 79 | reject({ error, stdout, stderr }); 80 | } else { 81 | resolve({ stdout, stderr }); 82 | } 83 | }); 84 | }); 85 | } 86 | const PythonCompile = async (code, input) => { 87 | let state = { 88 | stdout: null, 89 | stderr: null, 90 | statusMes: "", 91 | } 92 | if (input.length > 0) { 93 | let uniqueFileName = uuid(); 94 | let executePath = getExecutablePath(uniqueFileName) 95 | let pyPath = getpyPath(uniqueFileName) 96 | let ipPath = getInputPath(uniqueFileName) 97 | 98 | await saveFile(pyPath, code); 99 | await saveFile(ipPath, input); 100 | 101 | try { 102 | let { stdout, stderr } = await runProgram(pyPath, ipPath); 103 | state.stdout = stdout; 104 | state.stderr = stderr; 105 | } catch (err) { 106 | state.stderr = err.stderr; 107 | state.statusMes = "Compiler Error"; 108 | deleteFiles(pyPath, ipPath, executePath); 109 | return state; 110 | } 111 | if (state.stderr === '') { 112 | state.stderr = null; 113 | } 114 | state.statusMes = "Successfully Compiled"; 115 | await deleteFiles(pyPath, ipPath, executePath); 116 | return state; 117 | } else { 118 | let uniqueFileName = uuid(); 119 | let executePath = getExecutablePath(uniqueFileName) 120 | let pyPath = getpyPath(uniqueFileName) 121 | await saveFile(pyPath, code); 122 | try { 123 | let { stdout, stderr } = await runProgramNoIP(pyPath); 124 | state.stdout = stdout; 125 | state.stderr = stderr; 126 | } catch (err) { 127 | state.stderr = err.stderr; 128 | state.statusMes = "Compiler Error"; 129 | deleteFiles(pyPath, executePath); 130 | return state; 131 | } 132 | if (state.stderr === '') { 133 | state.stderr = null; 134 | } 135 | state.statusMes = "Successfully Compiled"; 136 | await deleteFiles(pyPath, executePath); 137 | return state; 138 | 139 | } 140 | return state; 141 | 142 | } 143 | 144 | 145 | 146 | 147 | module.exports = { PythonCompile }; 148 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | work.vivekjaviya@gmail.com. 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | --------------------------------------------------------------------------------