From 899c604a98166269187b6fe0fc9d425fc43991b6 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 20 Jul 2022 16:57:40 +1000 Subject: [PATCH] Load custom plugin directories --- InvenTree/InvenTree/settings.py | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 447e07b09c..f3691e3412 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -938,10 +938,44 @@ PLUGIN_FILE = get_plugin_file() # Plugin Directories (local plugins will be loaded from these directories) PLUGIN_DIRS = ['plugin.builtin', ] +CUSTOM_PLUGIN_DIRS = os.getenv('INVENTREE_PLUGIN_DIR', None) + if not TESTING: # load local deploy directory in prod PLUGIN_DIRS.append('plugins') # pragma: no cover + # optionally load custom plugin directory + if CUSTOM_PLUGIN_DIRS is not None: + # Allow multiple plugin directories to be specified + for pd in CUSTOM_PLUGIN_DIRS.split(','): + pd = pd.strip() + + # Attempt to create the directory if it does not alreadyd exist + if not os.path.exists(pd): + try: + os.makedirs(pd, exist_ok=True) + except Exception: + logger.error(f"Could not create plugin directory '{pd}'") + continue + + # Ensure the directory has an __init__.py file + init_filename = os.path.join(pd, '__init__.py') + + if not os.path.exists(init_filename): + try: + with open(init_filename, 'w') as init_file: + init_file.write("# InvenTree plugin directory\n") + except Exception: + logger.error(f"Could not create file '{init_filename}'") + continue + + if os.path.exists(pd) and os.path.isdir(pd): + # By this point, we have confirmed that the directory at least exists + pd = os.path.abspath(pd) + + logger.info(f"Added plugin directory: '{pd}'") + PLUGIN_DIRS.append(pd) + if DEBUG or TESTING: # load samples in debug mode PLUGIN_DIRS.append('plugin.samples')