Archipelago/ModuleUpdate.py

47 lines
1.9 KiB
Python
Raw Normal View History

import os
2020-01-18 14:45:52 +00:00
import sys
import subprocess
import importlib
update_ran = hasattr(sys, "frozen") and getattr(sys, "frozen") # don't run update if environment is frozen/compiled
2020-01-18 14:45:52 +00:00
def update_command():
subprocess.call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt', '--upgrade'])
naming_specialties = {"PyYAML": "yaml"} # PyYAML is imported as the name yaml
2020-01-18 14:45:52 +00:00
def update():
global update_ran
if not update_ran:
update_ran = True
2020-03-06 22:30:18 +00:00
path = os.path.join(os.path.dirname(sys.argv[0]), 'requirements.txt')
if not os.path.exists(path):
os.path.join(os.path.dirname(__file__), 'requirements.txt')
with open(path) as requirementsfile:
2020-01-18 14:45:52 +00:00
for line in requirementsfile.readlines():
module, remoteversion = line.split(">=")
module = naming_specialties.get(module, module)
2020-01-18 14:45:52 +00:00
try:
module = importlib.import_module(module)
except:
import traceback
traceback.print_exc()
input(f'Required python module {module} not found, press enter to install it')
update_command()
else:
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("."))
remoteversion = tuple(int(part.strip()) for part in remoteversion.split("."))
if module_version < remoteversion:
input(f'Required python module {module} is outdated ({module_version}<{remoteversion}), press enter to upgrade it')
update_command()
if __name__ == "__main__":
update()