diff --git a/Code Compiler/.gitignore b/Code Compiler/.gitignore new file mode 100644 index 00000000..68bc17f9 --- /dev/null +++ b/Code Compiler/.gitignore @@ -0,0 +1,160 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/Code Compiler/README.md b/Code Compiler/README.md new file mode 100644 index 00000000..c196b5ca --- /dev/null +++ b/Code Compiler/README.md @@ -0,0 +1,67 @@ + +![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99) +![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103) +[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/thecoddiwompler) + +# Code Compiler + +## 🛠️ Description + +This project is a code execution tool that interacts with an online code compiler API. It consists of the following folders and files: + +- **src:** Contains the main Python scripts. + - `list_languages.py`: Retrieves and prints a list of available programming languages from the online code compiler API based on the contents of `language_list.txt` in the `input` folder. + - `main.py`: Prompts the user for a programming language, checks its availability using the API, and then executes the code from `code.txt` in the `input` folder with corresponding input from `input.txt`. The output is stored in `output.txt` in the `bin` folder. +- **bin:** Stores the output of code execution (`output.txt`) and logs (`logfile.log`). +- **input:** Contains input files for code (`code.txt` and `input.txt`) and a list of languages (`language_list.txt`). +- **.env:** Environment configuration file. +- **requirements.txt:** Specifies the project's dependencies. Developers can install these dependencies using the `pip install -r requirements.txt` command to ensure a consistent environment. +- **README.md:** Documentation file. +- **.gitignore:** Ignored files and directories for version control. +- **LICENSE:** License information for the project. + +## ⚙️ Languages or Frameworks Used + +The script was created with Python3 and the built-in functions in it + +## 🌟 How to run + +1. Use the following command to install dependencies: + + ```bash + pip install -r requirements.txt + ``` + +2. Set the following environment variables in the `.env` file. You can get these details by subscribing to - [Online Code Compiler](https://rapidapi.com/Glavier/api/online-code-compiler/) + + ```markdown + + - `API_KEY`: Your API key for authentication. + - `HOST` : Host for the online compiler. [online-code-compiler.p.rapidapi.com] + - `LANGUAGE_LIST_URL` : API base URL to GET Languages. [https://online-code-compiler.p.rapidapi.com/v1/languages/] + - `COMPILE_CODE_URL` : API base URL to POST Request on online code compiler to execute the code. [https://online-code-compiler.p.rapidapi.com/v1/] + + ``` + +3. Execute the `list_languages.py` script to retrieve and display the list of available programming languages. The information is sourced from the `language_list.txt` file located in the `input` directory. Note that this file is pre-populated for reference, and user don't need to run this script. + + + ```bash + python src/list_languages.py + ``` + +4. Execute the `main.py` script to run user-specified code. The script will prompt the user for a programming language input. It will then perform the following steps: + + 1. Check if the specified language exists using the `get_language` API call. + 2. Verify the presence of the `code.txt` file in the `input` directory, containing the code to be executed. + 3. Check for the existence of the `input.txt` file in the `input` directory for providing input to the code. + 4. If all checks pass, the script will execute the code and return the output, which will be recorded in `output.txt` in the `bin` directory. + 5. Logs for all operations will be generated and recorded in `logfile.log` in the `bin` directory.

+ + ```bash + python src/main.py + ``` + + +## 🤖 Author +[Indrajeet Mishra](https://github.com/thecoddiwompler) \ No newline at end of file diff --git a/Code Compiler/bin/logfile.log b/Code Compiler/bin/logfile.log new file mode 100644 index 00000000..87f9501d --- /dev/null +++ b/Code Compiler/bin/logfile.log @@ -0,0 +1,3 @@ +2023-11-26 15:42:58,582 - INFO - Successfully fetched language data. +2023-11-26 15:43:06,089 - INFO - Successfully compiled the code. +2023-11-26 15:43:06,094 - INFO - Output written to bin/output.txt. diff --git a/Code Compiler/bin/output.txt b/Code Compiler/bin/output.txt new file mode 100644 index 00000000..e69de29b diff --git a/Code Compiler/input/code.txt b/Code Compiler/input/code.txt new file mode 100644 index 00000000..94c67a25 --- /dev/null +++ b/Code Compiler/input/code.txt @@ -0,0 +1,31 @@ +public class FindGreatestNumber { + public static void main(String[] args) { + // Sample array + int[] numbers = {10, 5, 8, 20, 15}; + + // Find the greatest number + int greatestNumber = findGreatestNumber(numbers); + + // Print the result + System.out.println("The greatest number is: " + greatestNumber); + } + + // Method to find the greatest number in an array + public static int findGreatestNumber(int[] arr) { + if (arr == null || arr.length == 0) { + throw new IllegalArgumentException("Array is empty or null"); + } + + // Assume the first element is the greatest + int greatest = arr[0]; + + // Iterate through the array to find the greatest number + for (int i = 1; i < arr.length; i++) { + if (arr[i] > greatest) { + greatest = arr[i]; + } + } + + return greatest; + } +} \ No newline at end of file diff --git a/Code Compiler/input/input.txt b/Code Compiler/input/input.txt new file mode 100644 index 00000000..e69de29b diff --git a/Code Compiler/input/language_list.txt b/Code Compiler/input/language_list.txt new file mode 100644 index 00000000..1ef99e1f --- /dev/null +++ b/Code Compiler/input/language_list.txt @@ -0,0 +1,77 @@ +Ada +Algol 68 +AWK +Bash Shell +BC +Befunge +Blockly +Brainf**k +C +C-99 +CLISP +Clojure +COBOL +CoffeeScript +C++ +C++ 14 +C++ 17 +C# +D +Dart +Elixir +Erlang +Factor +Falcon +Fantom +FASM +Forth +Fortran +FREE BASIC +F# +Assembler - GCC +GO Lang +Groovy +Hack +Haskell +Haxe +Icon +Intercal +Java +JBang +J Language +Kotlin +LOLCODE +Lua +OZ Mozart +Assembler - NASM +Nemerle +Nim +NodeJS +Objective C +Ocaml +Octave +Pascal +Perl +PHP +Picolisp +Pike +Prolog +Python 2 +Python 3 +R Language +Racket +Rhino JS +Ruby +RUST +Scala +Scheme +SmallTalk +SpiderMonkey +SQL +Swift +TCL +Unlambda +VB.Net +VERILOG +Whitespace +YaBasic diff --git a/Code Compiler/requirements.txt b/Code Compiler/requirements.txt new file mode 100644 index 00000000..d44fe443 --- /dev/null +++ b/Code Compiler/requirements.txt @@ -0,0 +1,2 @@ +requests +python-dotenv \ No newline at end of file diff --git a/Code Compiler/src/list_languages.py b/Code Compiler/src/list_languages.py new file mode 100644 index 00000000..87a8179e --- /dev/null +++ b/Code Compiler/src/list_languages.py @@ -0,0 +1,62 @@ +import requests +import os +import logging +from dotenv import load_dotenv + +# Configure logging + +logging.basicConfig(filename='bin/logfile.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', filemode='w') + + +load_dotenv() + + +# Get API Key from environment path + +api_key = os.getenv("API_KEY") + + +# GET language list from Online Code Compiler API using requests library + +def get_language(api_key): + + url = os.getenv("LANGUAGE_LIST_URL") + + host = os.getenv("HOST") + + headers = { + "X-RapidAPI-Key" : api_key, + "X-RapidAPI-Host" : host + } + + try: + response = requests.get(url=url, headers=headers) + response.raise_for_status() # Raise an HTTPError for bad responses + + data = response.json() + logging.info("Successfully fetched language data.") + return data + + except requests.exceptions.RequestException as e: + logging.error(f"Error making the request: {e}") + return None + + except Exception as e: + logging.error(f"An unexpected error occurred: {e}") + return None + + +# Get the language details in JSON list and define the path to dump the language list + +language_extractor = get_language(api_key) + +file_path = "input/language_list.txt" + + +# Truncate the file_path if it exists or create a new one and Write all the Language names from language_extractor + +with open(file_path, mode='w') as file: + + for language_detail in language_extractor: + file.write(language_detail['name']+'\n') + logging.info(f"Language '{language_detail['name']}' written to file.") \ No newline at end of file diff --git a/Code Compiler/src/main.py b/Code Compiler/src/main.py new file mode 100644 index 00000000..af724099 --- /dev/null +++ b/Code Compiler/src/main.py @@ -0,0 +1,138 @@ +import requests +import os +import logging +from dotenv import load_dotenv + +# Configure logging + +logging.basicConfig(filename='bin/logfile.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', filemode='w') + + +load_dotenv() + + +# Get API Key from environment path + +api_key = os.getenv("API_KEY") + + +# GET language list from Online Code Compiler API using requests library + +def get_language(api_key): + + url = os.getenv("LANGUAGE_LIST_URL") + + host = os.getenv("HOST") + + headers = { + "X-RapidAPI-Key" : api_key, + "X-RapidAPI-Host" : host + } + + try: + response = requests.get(url=url, headers=headers) + response.raise_for_status() # Raise an HTTPError for bad responses + + data = response.json() + logging.info("Successfully fetched language data.") + return data + + except requests.exceptions.RequestException as e: + logging.error(f"Error making the request: {e}") + return None + + except Exception as e: + logging.error(f"An unexpected error occurred: {e}") + return None + + +# Compile the code by sending POST request to Online Code Compiler API + +def compile_code(language_id, api_key): + + # Grab the User input from input.txt and read it. + + input_path = "input/input.txt" + + with open(input_path, mode='r') as file: + user_input = file.read() + + + # Grab the user code from code.txt file + + code_path = "input/code.txt" + + with open(code_path, mode='r') as file: + code = file.read() + + + # Grab the host and url from Environment file + + host = os.getenv("HOST") + url = os.getenv("COMPILE_CODE_URL") + + + # Define the headers for the api call + + headers = { + "content-type": "application/json", + "X-RapidAPI-Key": api_key, + "X-RapidAPI-Host": host + } + + + # Define the body for the api call + + payload = { + "language": language_id, + "version": "latest", + "code": code, + "input": user_input + } + + + try: + # Hit the API to compile the code + response = requests.post(url=url, json=payload, headers=headers) + response.raise_for_status() # Raise an HTTPError for bad responses + + data = response.json() + logging.info("Successfully compiled the code.") + return data + + except requests.exceptions.RequestException as e: + logging.error(f"Error making the compile request: {e}") + return None + + except Exception as e: + logging.error(f"An unexpected error occurred during compilation: {e}") + return None + + +# Get the language details in JSON list and get the language_id from User input of language name. Return an exception if it doesn't exist. + +language_extractor = get_language(api_key) + +language_name = input("Enter your choice of language. You can refer to language_list.txt in input folder for reference : ") + +language_id = None # Initialize language_id to None + +for language_detail in language_extractor: + if language_detail['name'].lower() == language_name.lower(): + language_id = language_detail['id'] + break + +else: + # This block is executed when the loop completes without finding a match + error_message = f"Language '{language_name}' not found in the language list. Available languages are: {[lang['name'] for lang in language_extractor]}" + logging.error(error_message) + raise ValueError(error_message) + + +# Compile the code and write the result into /bin in output.txt + +output = compile_code(language_id, api_key) + +with open("bin/output.txt", mode='w') as file: + file.write(output['output']) + logging.info("Output written to bin/output.txt.") \ No newline at end of file