async def start(): # Actually start the server addr = share["conf"]["server_ip"] port = share["conf"]["server_port"] if addr is None: addr = socket.gethostbyname(socket.gethostname()) server = share["server"] = await asyncio.start_server(handle_con, host=addr, port=port) share["ses"] = aiohttp.ClientSession() await pymine_api.init() try: async with server: if random.randint(0, 999) == 1: # shhhhh logger.info(f"PPMine 69.420 started on port {addr}:{port}!") else: logger.info( f'PyMine {float(share["server_version"])} started on {addr}:{port}!' ) for handler in pymine_api.server.SERVER_READY_HANDLERS: asyncio.create_task(handler()) await server.serve_forever() except ( asyncio.CancelledError, KeyboardInterrupt, ): pass
async def stop(): # Stop the server properly logger.info("Closing server...") share["server"].close() # wait for the server to be closed, stop the api, and stop the aiohttp.ClientSession await asyncio.gather(share["server"].wait_closed(), pymine_api.stop(), share["ses"].close()) logger.info("Server closed.")
def update_repo(git_dir, git_url, root, plugin_name, do_clone=False): if do_clone: try: os.rename(root, f"{root}_backup_{int(time.time())}") logger.debug(f"Renamed {root} for clone.") except FileNotFoundError: pass logger.debug(f"Cloning from {git_url}...") git_dir.clone(git_url) logger.info(f"Updated {plugin_name}!") return if not os.path.isdir(os.path.join(root, ".git")): return update_repo(git_dir, git_url, root, plugin_name, True) try: logger.debug(f"Pulling from {git_url}...") res = git.Git(root).pull() # pull latest from remote except BaseException as e: logger.debug(f"Failed to pull from {git_url}, attempting to clone...") return update_repo(git_dir, git_url, root, plugin_name, True) if res == "Already up to date.": logger.info(f"No updates found for {plugin_name}.") else: logger.info(f"Updated {plugin_name}!")
def echo(uuid: str, text: str): logger.info(f"{uuid}: {text}")
async def load_plugin(git_dir, plugin_name): root = os.path.join("plugins", plugin_name) if os.path.isfile(root): if root.endswith(".py"): # .py file (so try to import) try: plugin_path = root.rstrip(".py").replace("\\", "/").replace( "/", ".") plugin_module = importlib.import_module(plugin_path) plugins[plugin_path] = plugin_module except BaseException as e: logger.error( f"Failed to load {plugin_name} due to: {logger.f_traceback(e)}" ) return plugin_config_file = os.path.join(root, "plugin.yml") if not os.path.isfile(plugin_config_file): logger.error( f"Failed to load {plugin_name} due to missing plugin.yml.") return try: conf = load_plugin_config(root) except ValueError as e: logger.error( f"Failed to load {plugin_name} due to invalid plugin.yml. ({str(e)})" ) return except BaseException as e: logger.error( f"Failed to load {plugin_name} due to invalid plugin.yml. Error: {logger.f_traceback(e)}" ) return if conf.get("git_url"): logger.info(f"Checking for updates for {plugin_name}...") try: update_repo(git_dir, conf["git_url"], root, plugin_name) except BaseException as e: logger.error( f"Failed to update {plugin_name} due to: {logger.f_traceback(e)}" ) return plugin_path = root if conf.get("module_folder"): plugin_path = os.path.join(plugin_path, conf["module_folder"]) plugin_path = plugin_path.replace("\\", "/").replace("/", ".") try: plugin_module = importlib.import_module(plugin_path) except BaseException as e: logger.error( f"Failed to import {plugin_name} due to: {logger.f_traceback(e)}") return try: await plugin_module.setup() except BaseException as e: logger.error( f"Failed to setup {plugin_name} due to: {logger.f_traceback(e)}") return plugins[plugin_path] = plugin_module