| 
									
										
										
										
											2020-01-19 12:50:04 -08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2020-01-18 15:45:52 +01:00
										 |  |  | import sys | 
					
						
							|  |  |  | import subprocess | 
					
						
							|  |  |  | import importlib | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 23:30:22 +01:00
										 |  |  | update_ran = hasattr(sys, "frozen") and getattr(sys, "frozen")  # don't run update if environment is frozen/compiled | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 15:45:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | def update_command(): | 
					
						
							|  |  |  |     subprocess.call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt', '--upgrade']) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 23:30:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-23 12:06:00 +02:00
										 |  |  | naming_specialties = {"PyYAML": "yaml",  # PyYAML is imported as the name yaml | 
					
						
							|  |  |  |                       "maseya-z3pr": "maseya"} | 
					
						
							| 
									
										
										
										
											2020-01-19 23:30:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 15:45:52 +01:00
										 |  |  | def update(): | 
					
						
							|  |  |  |     global update_ran | 
					
						
							|  |  |  |     if not update_ran: | 
					
						
							|  |  |  |         update_ran = True | 
					
						
							| 
									
										
										
										
											2020-03-06 23:30:18 +01:00
										 |  |  |         path = os.path.join(os.path.dirname(sys.argv[0]), 'requirements.txt') | 
					
						
							|  |  |  |         if not os.path.exists(path): | 
					
						
							| 
									
										
										
										
											2020-07-11 16:54:38 +02:00
										 |  |  |             path = os.path.join(os.path.dirname(__file__), 'requirements.txt') | 
					
						
							| 
									
										
										
										
											2020-03-06 23:30:18 +01:00
										 |  |  |         with open(path) as requirementsfile: | 
					
						
							| 
									
										
										
										
											2020-01-18 15:45:52 +01:00
										 |  |  |             for line in requirementsfile.readlines(): | 
					
						
							| 
									
										
										
										
											2020-03-06 23:37:57 +01:00
										 |  |  |                 module, remote_version = line.split(">=") | 
					
						
							| 
									
										
										
										
											2020-01-19 23:30:22 +01:00
										 |  |  |                 module = naming_specialties.get(module, module) | 
					
						
							| 
									
										
										
										
											2020-01-18 15:45:52 +01: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() | 
					
						
							| 
									
										
										
										
											2020-03-22 02:34:24 +01:00
										 |  |  |                     return | 
					
						
							| 
									
										
										
										
											2020-01-18 15:45:52 +01:00
										 |  |  |                 else: | 
					
						
							|  |  |  |                     if hasattr(module, "__version__"): | 
					
						
							|  |  |  |                         module_version = module.__version__ | 
					
						
							| 
									
										
										
										
											2020-03-06 23:37:57 +01:00
										 |  |  |                         module = module.__name__  # also unloads the module to make it writable | 
					
						
							| 
									
										
										
										
											2020-01-18 15:45:52 +01:00
										 |  |  |                         if type(module_version) == str: | 
					
						
							|  |  |  |                             module_version = tuple(int(part.strip()) for part in module_version.split(".")) | 
					
						
							| 
									
										
										
										
											2020-03-06 23:37:57 +01:00
										 |  |  |                         remote_version = tuple(int(part.strip()) for part in remote_version.split(".")) | 
					
						
							|  |  |  |                         if module_version < remote_version: | 
					
						
							| 
									
										
										
										
											2020-03-06 23:38:41 +01:00
										 |  |  |                             input(f'Required python module {module} is outdated ({module_version}<{remote_version}),' | 
					
						
							|  |  |  |                                   ' press enter to upgrade it') | 
					
						
							| 
									
										
										
										
											2020-01-18 15:45:52 +01:00
										 |  |  |                             update_command() | 
					
						
							| 
									
										
										
										
											2020-03-22 02:34:24 +01:00
										 |  |  |                             return | 
					
						
							| 
									
										
										
										
											2020-01-18 15:45:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 23:37:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 15:45:52 +01:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2020-01-19 12:50:04 -08:00
										 |  |  |     update() |