From d9d2f3907210f90b371268f963fd3a898cbabf0d Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 15 Mar 2022 01:24:58 +0100 Subject: [PATCH] just run install --- InvenTree/InvenTree/settings.py | 1 + InvenTree/plugin/apps.py | 2 +- InvenTree/plugin/registry.py | 30 ++++++++++++++++++------------ 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 279225355d..9688f90c12 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -900,3 +900,4 @@ if DEBUG or TESTING: PLUGIN_TESTING = get_setting('PLUGIN_TESTING', TESTING) # are plugins beeing tested? PLUGIN_TESTING_SETUP = get_setting('PLUGIN_TESTING_SETUP', False) # load plugins from setup hooks in testing? PLUGIN_RETRY = get_setting('PLUGIN_RETRY', 5) # how often should plugin loading be tried? +PLUGIN_FILE_CHECKED = False # Was the plugin file checked? diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index e86ffffc0d..202e1570e2 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -30,7 +30,7 @@ class PluginAppConfig(AppConfig): if not registry.is_loading: # this is the first startup - registry.check_plugin_file() + registry.install_plugin_file() registry.collect_plugins() registry.load_plugins() diff --git a/InvenTree/plugin/registry.py b/InvenTree/plugin/registry.py index 98fb8038a3..3a3e6f70ad 100644 --- a/InvenTree/plugin/registry.py +++ b/InvenTree/plugin/registry.py @@ -8,9 +8,10 @@ Registry for loading and managing multiple plugins at run-time import importlib import pathlib import logging +import os +import subprocess from typing import OrderedDict from importlib import reload -import pkg_resources from django.apps import apps from django.conf import settings @@ -214,21 +215,26 @@ class PluginsRegistry: logger.info(f'Collected {len(self.plugin_modules)} plugins!') logger.info(", ".join([a.__module__ for a in self.plugin_modules])) - def check_plugin_file(self): + def install_plugin_file(self): """ - Check if all plugins are installed in the current enviroment + Make sure all plugins are installed in the current enviroment """ - # many thanks to Asclepius - # https://stackoverflow.com/questions/16294819/check-if-my-python-has-all-required-packages/45474387#45474387 + + if settings.PLUGIN_FILE_CHECKED: + logger.info('Plugin file was already checked') + return plugin_file = pathlib.Path(get_plugin_file()) - requirements = pkg_resources.parse_requirements(plugin_file.open()) - - for requirement in requirements: - try: - pkg_resources.require(str(requirement)) - except Exception as error: - handle_error(error, log_name='init', do_raise=False) + try: + output = str(subprocess.check_output(['pip', 'install', '-r', str(plugin_file.absolute())], cwd=os.path.dirname(settings.BASE_DIR)), 'utf-8') + except subprocess.CalledProcessError as error: # pragma: no cover + logger.error(f'Ran into error while trying to install plugins!\n{str(error)}') + return False + + logger.info(f'plugin requirements were run\n{output}') + + # do not run again + settings.PLUGIN_FILE_CHECKED = True # endregion