From bda33bb19aac58092695ad29c6f24bd38c3943c0 Mon Sep 17 00:00:00 2001 From: daxcay Date: Sun, 2 Jun 2024 14:21:32 +0530 Subject: [PATCH] Initial Commit --- .gitignore | 156 ++++++++++++++++++++++++++++++++++++ LICENSE | 19 +++++ README.md | 0 __init__.py | 49 +++++++++++ classes/NodeInstaller.py | 80 ++++++++++++++++++ classes/NodeScriptRunner.py | 53 ++++++++++++ config.py | 26 ++++++ requirements.txt | 0 src/app.js | 12 +++ 9 files changed, 395 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 __init__.py create mode 100644 classes/NodeInstaller.py create mode 100644 classes/NodeScriptRunner.py create mode 100644 config.py create mode 100644 requirements.txt create mode 100644 src/app.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f45a09a --- /dev/null +++ b/.gitignore @@ -0,0 +1,156 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*.pyc +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +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 + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__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 maintainted 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/ +*.pyc + +.idea +/node_modules diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ece37f6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright 2024 Daxton Caylor + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..922f45b --- /dev/null +++ b/__init__.py @@ -0,0 +1,49 @@ +# Copyright 2024 Daxton Caylor +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +""" +@author: Daxton Caylor +@title: ComfyUI-NODEJS +@nickname: ComfyUI-NODEJS +@description: This node enables someone to run any nodejs application alongside comfyui. +""" + +import os +from .classes.NodeInstaller import NodeInstaller +from .classes.NodeScriptRunner import NodeScriptRunner +from .config import * + +NODE_JS_SCRIPTS_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), NODE_JS_SCRIPTS_FOLDER) + +if IS_ACTIVE: + nodeInstaller = NodeInstaller(NODE_JS_INSTALLER_URL) + if nodeInstaller.check_for_node_js() is not True: + nodeInstaller.download_nodejs() + nodeInstaller.install_nodejs() + nodeInstaller.install_all_packages(NODE_JS_PACKAGES) + + for script in NODE_RUNNABLES: + nodeScriptRunner = NodeScriptRunner(os.path.join(NODE_JS_SCRIPTS_FOLDER, script)) + nodeScriptRunner.run_js(NODE_RUNNABLES_ARGS[script]) + +NODE_CLASS_MAPPINGS = {} +NODE_DISPLAY_NAME_MAPPINGS = {} + +# WEB_DIRECTORY = "./web" diff --git a/classes/NodeInstaller.py b/classes/NodeInstaller.py new file mode 100644 index 0000000..10a0331 --- /dev/null +++ b/classes/NodeInstaller.py @@ -0,0 +1,80 @@ +# Copyright 2024 Daxton Caylor +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import os +import requests +import subprocess +import tempfile +from tqdm import tqdm +import shutil + +class NodeInstaller: + + def __init__(self, installer_url): + self.installer_url = installer_url + self.installer_path = None + + def check_for_node_js(self): + return (shutil.which('node') is not None) + + def download_nodejs(self): + temp_dir = tempfile.gettempdir() + self.installer_path = os.path.join(temp_dir, "nodejs_installer.msi") + response = requests.get(self.installer_url, stream=True) + total_size = int(response.headers.get('content-length', 0)) + block_size = 1024 + progress_bar = tqdm(total=total_size, unit='iB', unit_scale=True) + + with open(self.installer_path, 'wb') as file: + for data in response.iter_content(block_size): + progress_bar.update(len(data)) + file.write(data) + + progress_bar.close() + if total_size != 0 and progress_bar.n != total_size: + print("An error occurred during the download.") + else: + print("Node.js installer downloaded successfully.") + + def install_nodejs(self): + if self.installer_path is None: + print("Node.js installer has not been downloaded yet.") + return + process = subprocess.Popen([self.installer_path], shell=True) + process.wait() + + def install_all_packages(self, package_list): + for package_name in package_list: + self.install_npm_package(package_name) + + def install_npm_package(self, package_name): + install_command = f"npm install {package_name}" + + try: + print("") + process = subprocess.Popen(install_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) + for line in tqdm(iter(process.stdout.readline, b''), desc=f"Installing {package_name}", unit='B', unit_scale=True, leave=False): + pass + process.stdout.close() + process.wait() + print(f"\nnpm package '{package_name}' installed successfully.") + except subprocess.CalledProcessError as e: + print(f"An error occurred: {e}") + diff --git a/classes/NodeScriptRunner.py b/classes/NodeScriptRunner.py new file mode 100644 index 0000000..333e172 --- /dev/null +++ b/classes/NodeScriptRunner.py @@ -0,0 +1,53 @@ +# Copyright 2024 Daxton Caylor +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import subprocess +import shutil + +class NodeScriptRunner: + def __init__(self, js_file): + self.js_file = js_file + self.process = None + + def check_for_node_js(self): + return (shutil.which('node') is not None) + + def run_js(self, args): + try: + if self.check_for_node_js() is not True: + return + final_args = ["node", self.js_file] + final_args.extend(args) + self.process = subprocess.Popen(final_args) + print(f"JavaScript file '{self.js_file}' is running in the background.") + except FileNotFoundError: + print("Node.js is not installed or not found in the system path.") + except: + print('NodeJS failed to start script.') + + def terminate_background_js(self): + if self.process: + self.process.terminate() + print(f"Terminated the background JavaScript process.") + else: + print("No background JavaScript process is running.") + + def __del__(self): + self.terminate_background_js() diff --git a/config.py b/config.py new file mode 100644 index 0000000..761b89f --- /dev/null +++ b/config.py @@ -0,0 +1,26 @@ +# Copyright 2024 Daxton Caylor +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +IS_ACTIVE = True +NODE_JS_INSTALLER_URL = "https://nodejs.org/dist/v20.11.1/node-v20.11.1-x64.msi" +NODE_JS_PACKAGES = ["express"] +NODE_JS_SCRIPTS_FOLDER = "src" +NODE_RUNNABLES = ['app.js'] +NODE_RUNNABLES_ARGS = {'app.js': []} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/app.js b/src/app.js new file mode 100644 index 0000000..41334b3 --- /dev/null +++ b/src/app.js @@ -0,0 +1,12 @@ +console.log("NodeJS: Script is working!") +const express = require('express') +const app = express() +const port = 3000 + +app.get('/', (req, res) => { + res.send('Hello World!') +}) + +app.listen(port, () => { + console.log(`Example app listening on port ${port}`) +}) \ No newline at end of file