From e604f2c676471bde781cbb0a0586e73a52024e94 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Mon, 24 May 2021 14:36:24 +0200 Subject: Added optional version handling. And improved error handling a bit. --- archinstall/lib/plugins.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'archinstall/lib') diff --git a/archinstall/lib/plugins.py b/archinstall/lib/plugins.py index b7b6afbb..bf4b7720 100644 --- a/archinstall/lib/plugins.py +++ b/archinstall/lib/plugins.py @@ -9,6 +9,7 @@ import urllib.request from importlib import metadata from .output import log +from .storage import storage plugins = {} @@ -56,6 +57,17 @@ def import_via_path(path :str, namespace=None): # -> module (not sure how to wri log(err, level=logging.ERROR) log(f"The above error was detected when loading the plugin: {path}", fg="red", level=logging.ERROR) + try: + del(sys.modules[namespace]) + except: + pass + +def find_nth(haystack, needle, n): + start = haystack.find(needle) + while start >= 0 and n > 1: + start = haystack.find(needle, start+len(needle)) + n -= 1 + return start def load_plugin(path :str): # -> module (not sure how to write that in type definitions) parsed_url = urllib.parse.urlparse(path) @@ -68,8 +80,20 @@ def load_plugin(path :str): # -> module (not sure how to write that in type defi elif parsed_url.scheme in ('https', 'http'): namespace = import_via_path(localize_path(path)) - try: - plugins[namespace] = sys.modules[namespace].Plugin() - except Exception as err: - log(err, level=logging.ERROR) - log(f"The above error was detected when initiating the plugin: {path}", fg="red", level=logging.ERROR) \ No newline at end of file + # Version dependency via __archinstall__version__ variable (if present) in the plugin + # Any errors in version inconsistency will be handled through normal error handling if not defined. + if namespace in sys.modules: + if hasattr(sys.modules[namespace], '__archinstall__version__'): + archinstall_major_and_minor_version = float(storage['__version__'][:find_nth(storage['__version__'], '.', 2)]) + + if sys.modules[namespace].__archinstall__version__ < archinstall_major_and_minor_version: + log(f"Plugin {sys.modules[namespace]} does not support the current Archinstall version.", fg="red", level=logging.ERROR) + + if hasattr(sys.modules[namespace], 'Plugin'): + try: + plugins[namespace] = sys.modules[namespace].Plugin() + except Exception as err: + log(err, level=logging.ERROR) + log(f"The above error was detected when initiating the plugin: {path}", fg="red", level=logging.ERROR) + else: + log(f"Plugin '{path}' is missing a valid entry-point or is corrupt.", fg="yellow", level=logging.WARNING) \ No newline at end of file -- cgit v1.2.3-54-g00ecf