From b938bef8fac9e71c7cf15a32afbcbd70d570b7a9 Mon Sep 17 00:00:00 2001 From: aerinon Date: Thu, 16 Jun 2022 10:46:53 -0600 Subject: [PATCH] Check package requirements before importing them --- DungeonRandomizer.py | 8 +++--- Gui.py | 4 +++ source/meta/check_requirements.py | 41 +++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 source/meta/check_requirements.py diff --git a/DungeonRandomizer.py b/DungeonRandomizer.py index d24e81d6..cf0f73bc 100755 --- a/DungeonRandomizer.py +++ b/DungeonRandomizer.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 -import argparse -import copy +if __name__ == '__main__': + from source.meta.check_requirements import check_requirements + check_requirements(console=True) + import os import logging import RaceRandom as random -import textwrap -import shlex import sys from source.classes.BabelFish import BabelFish diff --git a/Gui.py b/Gui.py index 032cce90..3c640979 100755 --- a/Gui.py +++ b/Gui.py @@ -1,3 +1,7 @@ +if __name__ == '__main__': + from source.meta.check_requirements import check_requirements + check_requirements() + import json import os import sys diff --git a/source/meta/check_requirements.py b/source/meta/check_requirements.py new file mode 100644 index 00000000..680dfe8f --- /dev/null +++ b/source/meta/check_requirements.py @@ -0,0 +1,41 @@ +import importlib.util +import webbrowser +from tkinter import Tk, Label, Button, Frame + + +def check_requirements(console=False): + check_packages = {'aenum': 'aenum', + 'fast-enum': 'fast_enum', + 'python-bps-continued': 'bps', + 'colorama': 'colorama', + 'aioconsole' : 'aioconsole', + 'websockets' : 'websockets', + 'pyyaml': 'yaml'} + missing = [] + for package, import_name in check_packages.items(): + spec = importlib.util.find_spec(import_name) + if spec is None: + missing.append(package) + if len(missing) > 0: + packages = ','.join(missing) + if console: + import logging + logger = logging.getLogger('') + logger.error('You need to install the following python packages:') + logger.error(f'{packages}') + logger.error('See the step about "Installing Platform-specific dependencies":') + logger.error('https://github.com/aerinon/ALttPDoorRandomizer/blob/DoorDev/docs/BUILDING.md') + else: + master = Tk() + master.title('Error') + frame = Frame(master) + frame.pack(expand=True, padx =50, pady=50) + Label(frame, text='You need to install the following python packages:').pack() + Label(frame, text=f'{packages}').pack() + Label(frame, text='See the step about "Installing Platform-specific dependencies":').pack() + url = 'https://github.com/aerinon/ALttPDoorRandomizer/blob/DoorDev/docs/BUILDING.md' + link = Label(frame, fg='blue', cursor='hand2', text=url) + link.pack() + link.bind('', lambda e: webbrowser.open_new_tab(url)) + Button(master, text='Ok', command=master.destroy).pack() + master.mainloop()