Allow ModuleUpdate to use multiple requirements files, no longer need to care about naming, and use conventional requirement parsing. Also add WebHost to it.

This commit is contained in:
Fabian Dill 2021-06-06 15:11:17 +02:00
parent 7e599c51f8
commit cae1188ff8
2 changed files with 25 additions and 36 deletions

View File

@ -1,55 +1,40 @@
import os import os
import sys import sys
import subprocess import subprocess
import importlib import pkg_resources
requirements_files = ['requirements.txt']
if sys.version_info < (3, 8, 6): if sys.version_info < (3, 8, 6):
raise RuntimeError("Incompatible Python Version. 3.8.7+ is supported.") raise RuntimeError("Incompatible Python Version. 3.8.7+ is supported.")
update_ran = hasattr(sys, "frozen") and getattr(sys, "frozen") # don't run update if environment is frozen/compiled update_ran = getattr(sys, "frozen", False) # don't run update if environment is frozen/compiled
def update_command(): def update_command():
subprocess.call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt', '--upgrade']) for file in requirements_files:
subprocess.call([sys.executable, '-m', 'pip', 'install', '-r', file, '--upgrade'])
naming_specialties = {"PyYAML": "yaml", # PyYAML is imported as the name yaml
"maseya-z3pr": "maseya",
"factorio-rcon-py": "factorio_rcon"}
def update(): def update():
global update_ran global update_ran
if not update_ran: if not update_ran:
update_ran = True update_ran = True
path = os.path.join(os.path.dirname(sys.argv[0]), 'requirements.txt') for req_file in requirements_files:
if not os.path.exists(path): path = os.path.join(os.path.dirname(sys.argv[0]), req_file)
path = os.path.join(os.path.dirname(__file__), 'requirements.txt') if not os.path.exists(path):
with open(path) as requirementsfile: path = os.path.join(os.path.dirname(__file__), req_file)
for line in requirementsfile.readlines(): with open(path) as requirementsfile:
module, remote_version = line.split(">=") requirements = pkg_resources.parse_requirements(requirementsfile)
module = naming_specialties.get(module, module) for requirement in requirements:
try: requirement = str(requirement)
module = importlib.import_module(module) try:
except: pkg_resources.require(requirement)
import traceback except pkg_resources.ResolutionError:
traceback.print_exc() import traceback
input(f'Required python module {module} not found, press enter to install it') traceback.print_exc()
update_command() input(f'Requirement {requirement} is not satisfied, press enter to install it')
return update_command()
else: return
if hasattr(module, "__version__"):
module_version = module.__version__
module = module.__name__ # also unloads the module to make it writable
if type(module_version) == str:
module_version = tuple(int(part.strip()) for part in module_version.split("."))
remote_version = tuple(int(part.strip()) for part in remote_version.split("."))
if module_version < remote_version:
input(f'Required python module {module} is outdated ({module_version}<{remote_version}),'
' press enter to upgrade it')
update_command()
return
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -2,6 +2,10 @@ import os
import multiprocessing import multiprocessing
import logging import logging
import ModuleUpdate
ModuleUpdate.requirements_files.append(os.path.join("WebHostLib", "requirements.txt"))
ModuleUpdate.update()
from WebHostLib import app as raw_app from WebHostLib import app as raw_app
from waitress import serve from waitress import serve