#!/usr/bin/env python3 import sys import os p = os.path.join pUp = os.path.dirname s = False if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): s = os.path.realpath(sys.executable) else: s = os.path.realpath(__file__) sp = pUp(s) # script start import socket import threading import queue # IMPORTANT! Obtain locks in this order, if you need multiple at once: # - clientDataLock # - clientsLock # - serverThreadsLock # - fileLock modulePath = p(sp,"modules") mainQueue = queue.Queue() clientsLock = threading.Lock() clientID = 0 clients = {} clientDataLock = threading.Lock() clientData = {} serverThreadsLock = threading.Lock() serverThreads = [] fileLock = threading.Lock() def runCode(str, lcs = False, description = "loose-code"): if lcs == False: lcs = {} code = compile(str,description,"exec") exec(code,globals(),lcs) return lcs def runScript(sf, lcs = False): if lcs == False: lcs = {} code = False with fileLock: with open(sf,"r",encoding="utf-8") as script: code = script.read() runCode(code,lcs,sf) return lcs def readModuleFile(path): with open(path,"r",encoding="utf-8") as modulesFile: for line in modulesFile: line = line.split("#",1)[0].strip(" \t\r\n") if line == "": continue modType = line.rsplit(".",1)[-1].lower() line = line.replace("\\","/") if modType == "mods": print(">> " +line+ " <<") else: print("> " +line+ " ...") line = line.replace("/",os.path.sep) if line.startswith("." +os.path.sep): line = pUp(path) + line[1:] else: line = p(modulePath,line) if modType == "py": runScript(line,locals()) if modType == "mods": readModuleFile(line) def main(): if os.path.isfile(p(modulePath,"main.mods")): print("Loading modules...") print(">> main.mods <<") readModuleFile(p(modulePath,"main.mods")) print("OK.\n") with serverThreadsLock: for server in servers: makeServer(*server) print("Serving!\n") main()