From d0ede85592a7a1b25f2b284819da46db49489307 Mon Sep 17 00:00:00 2001 From: Kumi Date: Tue, 31 Oct 2023 09:33:37 +0100 Subject: [PATCH] Implement automatic module installer/updater --- src/pix360core/classes/__init__.py | 2 +- src/pix360core/classes/exceptions.py | 5 +++ src/pix360core/installer.py | 31 +++++++++++++++++++ .../management/commands/installmodules.py | 14 +++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/pix360core/installer.py create mode 100644 src/pix360core/management/commands/installmodules.py diff --git a/src/pix360core/classes/__init__.py b/src/pix360core/classes/__init__.py index 60160eb..90dab0e 100644 --- a/src/pix360core/classes/__init__.py +++ b/src/pix360core/classes/__init__.py @@ -1,4 +1,4 @@ from .modules import BaseModule, DownloaderModule -from .exceptions import DownloadError, StitchingError, ConversionError +from .exceptions import DownloadError, StitchingError, ConversionError, InstallError from .http import HTTPRequest from .stitching import BaseStitcher, PILStitcher, BlenderStitcher, DEFAULT_CUBEMAP_TO_EQUIRECTANGULAR_STITCHER, DEFAULT_STITCHER \ No newline at end of file diff --git a/src/pix360core/classes/exceptions.py b/src/pix360core/classes/exceptions.py index 1238bef..933e1bc 100644 --- a/src/pix360core/classes/exceptions.py +++ b/src/pix360core/classes/exceptions.py @@ -11,4 +11,9 @@ class DownloadError(ConversionError): class StitchingError(ConversionError): """Generic error that occurred while attempting to stitch content """ + pass + +class InstallError(Exception): + """Generic error that occurred while attempting to install a module + """ pass \ No newline at end of file diff --git a/src/pix360core/installer.py b/src/pix360core/installer.py new file mode 100644 index 0000000..45f780d --- /dev/null +++ b/src/pix360core/installer.py @@ -0,0 +1,31 @@ +from .classes import HTTPRequest, InstallError + +import json +import subprocess + +class Installer: + MODULES_JSON = "https://git.kumi/-/snippets/131/raw/main/modules.json" + + def __init__(self): + self.available_modules = self.fetch_modules() + + def fetch_modules(self): + request = HTTPRequest(self.MODULES_JSON).open() + return json.loads(request.read().decode()) + + def install_all(self): + for module in self.available_modules: + try: + self.install_module(module) + except InstallError as e: + print(f"Error while installing module {module['name']}: {e}") + + def install_module(self, module): + try: + subprocess.run(["pip", "install", "-U", module["source"]], check=True) + except Exception as e: + raise InstallError(f"Error while installing module {module['name']}: {e}") from e + +if __name__ == "__main__": + installer = Installer() + installer.install_all() \ No newline at end of file diff --git a/src/pix360core/management/commands/installmodules.py b/src/pix360core/management/commands/installmodules.py new file mode 100644 index 0000000..9d8103f --- /dev/null +++ b/src/pix360core/management/commands/installmodules.py @@ -0,0 +1,14 @@ +from django.core.management.base import BaseCommand, CommandError + +from ...installer import Installer + +import logging + +class Command(BaseCommand): + help = 'Run the worker' + + def handle(self, *args, **options): + """Handle the command + """ + installer = Installer() + installer.install_all()